sub.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  1. package service
  2. import (
  3. "encoding/base64"
  4. "fmt"
  5. "net/url"
  6. "strings"
  7. "x-ui/database"
  8. "x-ui/database/model"
  9. "x-ui/logger"
  10. "github.com/goccy/go-json"
  11. "gorm.io/gorm"
  12. )
  13. type SubService struct {
  14. address string
  15. inboundService InboundService
  16. }
  17. func (s *SubService) GetSubs(subId string, host string) ([]string, error) {
  18. s.address = host
  19. var result []string
  20. inbounds, err := s.getInboundsBySubId(subId)
  21. if err != nil {
  22. return nil, err
  23. }
  24. for _, inbound := range inbounds {
  25. clients, err := s.inboundService.getClients(inbound)
  26. if err != nil {
  27. logger.Error("SubService - GetSub: Unable to get clients from inbound")
  28. }
  29. if clients == nil {
  30. continue
  31. }
  32. for _, client := range clients {
  33. if client.SubID == subId {
  34. link := s.getLink(inbound, client.Email)
  35. result = append(result, link)
  36. }
  37. }
  38. }
  39. return result, nil
  40. }
  41. func (s *SubService) getInboundsBySubId(subId string) ([]*model.Inbound, error) {
  42. db := database.GetDB()
  43. var inbounds []*model.Inbound
  44. err := db.Model(model.Inbound{}).Where("settings like ?", fmt.Sprintf(`%%"subId": "%s"%%`, subId)).Find(&inbounds).Error
  45. if err != nil && err != gorm.ErrRecordNotFound {
  46. return nil, err
  47. }
  48. return inbounds, nil
  49. }
  50. func (s *SubService) getLink(inbound *model.Inbound, email string) string {
  51. switch inbound.Protocol {
  52. case "vmess":
  53. return s.genVmessLink(inbound, email)
  54. case "vless":
  55. return s.genVlessLink(inbound, email)
  56. case "trojan":
  57. return s.genTrojanLink(inbound, email)
  58. }
  59. return ""
  60. }
  61. func (s *SubService) genVmessLink(inbound *model.Inbound, email string) string {
  62. address := s.address
  63. if inbound.Protocol != model.VMess {
  64. return ""
  65. }
  66. var stream map[string]interface{}
  67. json.Unmarshal([]byte(inbound.StreamSettings), &stream)
  68. network, _ := stream["network"].(string)
  69. typeStr := "none"
  70. host := ""
  71. path := ""
  72. sni := ""
  73. fp := ""
  74. var alpn []string
  75. allowInsecure := false
  76. switch network {
  77. case "tcp":
  78. tcp, _ := stream["tcpSettings"].(map[string]interface{})
  79. header, _ := tcp["header"].(map[string]interface{})
  80. typeStr, _ = header["type"].(string)
  81. if typeStr == "http" {
  82. request := header["request"].(map[string]interface{})
  83. requestPath, _ := request["path"].([]interface{})
  84. path = requestPath[0].(string)
  85. headers, _ := request["headers"].(map[string]interface{})
  86. host = searchHost(headers)
  87. }
  88. case "kcp":
  89. kcp, _ := stream["kcpSettings"].(map[string]interface{})
  90. header, _ := kcp["header"].(map[string]interface{})
  91. typeStr, _ = header["type"].(string)
  92. path, _ = kcp["seed"].(string)
  93. case "ws":
  94. ws, _ := stream["wsSettings"].(map[string]interface{})
  95. path = ws["path"].(string)
  96. headers, _ := ws["headers"].(map[string]interface{})
  97. host = searchHost(headers)
  98. case "http":
  99. network = "h2"
  100. http, _ := stream["httpSettings"].(map[string]interface{})
  101. path, _ = http["path"].(string)
  102. host = searchHost(http)
  103. case "quic":
  104. quic, _ := stream["quicSettings"].(map[string]interface{})
  105. header := quic["header"].(map[string]interface{})
  106. typeStr, _ = header["type"].(string)
  107. host, _ = quic["security"].(string)
  108. path, _ = quic["key"].(string)
  109. case "grpc":
  110. grpc, _ := stream["grpcSettings"].(map[string]interface{})
  111. path = grpc["serviceName"].(string)
  112. }
  113. security, _ := stream["security"].(string)
  114. if security == "tls" {
  115. tlsSetting, _ := stream["tlsSettings"].(map[string]interface{})
  116. alpns, _ := tlsSetting["alpn"].([]interface{})
  117. for _, a := range alpns {
  118. alpn = append(alpn, a.(string))
  119. }
  120. tlsSettings, _ := searchKey(tlsSetting, "settings")
  121. if tlsSetting != nil {
  122. if sniValue, ok := searchKey(tlsSettings, "serverName"); ok {
  123. sni, _ = sniValue.(string)
  124. }
  125. if fpValue, ok := searchKey(tlsSettings, "fingerprint"); ok {
  126. fp, _ = fpValue.(string)
  127. }
  128. if insecure, ok := searchKey(tlsSettings, "allowInsecure"); ok {
  129. allowInsecure, _ = insecure.(bool)
  130. }
  131. }
  132. serverName, _ := tlsSetting["serverName"].(string)
  133. if serverName != "" {
  134. address = serverName
  135. }
  136. }
  137. clients, _ := s.inboundService.getClients(inbound)
  138. clientIndex := -1
  139. for i, client := range clients {
  140. if client.Email == email {
  141. clientIndex = i
  142. break
  143. }
  144. }
  145. obj := map[string]interface{}{
  146. "v": "2",
  147. "ps": email,
  148. "add": address,
  149. "port": inbound.Port,
  150. "id": clients[clientIndex].ID,
  151. "aid": clients[clientIndex].AlterIds,
  152. "net": network,
  153. "type": typeStr,
  154. "host": host,
  155. "path": path,
  156. "tls": security,
  157. "sni": sni,
  158. "fp": fp,
  159. "alpn": strings.Join(alpn, ","),
  160. "allowInsecure": allowInsecure,
  161. }
  162. jsonStr, _ := json.MarshalIndent(obj, "", " ")
  163. return "vmess://" + base64.StdEncoding.EncodeToString(jsonStr)
  164. }
  165. func (s *SubService) genVlessLink(inbound *model.Inbound, email string) string {
  166. address := s.address
  167. if inbound.Protocol != model.VLESS {
  168. return ""
  169. }
  170. var stream map[string]interface{}
  171. json.Unmarshal([]byte(inbound.StreamSettings), &stream)
  172. clients, _ := s.inboundService.getClients(inbound)
  173. clientIndex := -1
  174. for i, client := range clients {
  175. if client.Email == email {
  176. clientIndex = i
  177. break
  178. }
  179. }
  180. uuid := clients[clientIndex].ID
  181. port := inbound.Port
  182. streamNetwork := stream["network"].(string)
  183. params := make(map[string]string)
  184. params["type"] = streamNetwork
  185. switch streamNetwork {
  186. case "tcp":
  187. tcp, _ := stream["tcpSettings"].(map[string]interface{})
  188. header, _ := tcp["header"].(map[string]interface{})
  189. typeStr, _ := header["type"].(string)
  190. if typeStr == "http" {
  191. request := header["request"].(map[string]interface{})
  192. requestPath, _ := request["path"].([]interface{})
  193. params["path"] = requestPath[0].(string)
  194. headers, _ := request["headers"].(map[string]interface{})
  195. params["host"] = searchHost(headers)
  196. params["headerType"] = "http"
  197. }
  198. case "kcp":
  199. kcp, _ := stream["kcpSettings"].(map[string]interface{})
  200. header, _ := kcp["header"].(map[string]interface{})
  201. params["headerType"] = header["type"].(string)
  202. params["seed"] = kcp["seed"].(string)
  203. case "ws":
  204. ws, _ := stream["wsSettings"].(map[string]interface{})
  205. params["path"] = ws["path"].(string)
  206. headers, _ := ws["headers"].(map[string]interface{})
  207. params["host"] = searchHost(headers)
  208. case "http":
  209. http, _ := stream["httpSettings"].(map[string]interface{})
  210. params["path"] = http["path"].(string)
  211. params["host"] = searchHost(http)
  212. case "quic":
  213. quic, _ := stream["quicSettings"].(map[string]interface{})
  214. params["quicSecurity"] = quic["security"].(string)
  215. params["key"] = quic["key"].(string)
  216. header := quic["header"].(map[string]interface{})
  217. params["headerType"] = header["type"].(string)
  218. case "grpc":
  219. grpc, _ := stream["grpcSettings"].(map[string]interface{})
  220. params["serviceName"] = grpc["serviceName"].(string)
  221. }
  222. security, _ := stream["security"].(string)
  223. if security == "tls" {
  224. params["security"] = "tls"
  225. tlsSetting, _ := stream["tlsSettings"].(map[string]interface{})
  226. alpns, _ := tlsSetting["alpn"].([]interface{})
  227. var alpn []string
  228. for _, a := range alpns {
  229. alpn = append(alpn, a.(string))
  230. }
  231. if len(alpn) > 0 {
  232. params["alpn"] = strings.Join(alpn, ",")
  233. }
  234. tlsSettings, _ := searchKey(tlsSetting, "settings")
  235. if tlsSetting != nil {
  236. if sniValue, ok := searchKey(tlsSettings, "serverName"); ok {
  237. params["sni"], _ = sniValue.(string)
  238. }
  239. if fpValue, ok := searchKey(tlsSettings, "fingerprint"); ok {
  240. params["fp"], _ = fpValue.(string)
  241. }
  242. if insecure, ok := searchKey(tlsSettings, "allowInsecure"); ok {
  243. if insecure.(bool) {
  244. params["allowInsecure"] = "1"
  245. }
  246. }
  247. }
  248. if streamNetwork == "tcp" && len(clients[clientIndex].Flow) > 0 {
  249. params["flow"] = clients[clientIndex].Flow
  250. }
  251. serverName, _ := tlsSetting["serverName"].(string)
  252. if serverName != "" {
  253. address = serverName
  254. }
  255. }
  256. if security == "reality" {
  257. params["security"] = "reality"
  258. realitySettings, _ := stream["realitySettings"].(map[string]interface{})
  259. if realitySettings != nil {
  260. if sniValue, ok := searchKey(realitySettings, "serverNames"); ok {
  261. sNames, _ := sniValue.([]interface{})
  262. params["sni"], _ = sNames[0].(string)
  263. }
  264. if pbkValue, ok := searchKey(realitySettings, "publicKey"); ok {
  265. params["pbk"], _ = pbkValue.(string)
  266. }
  267. if sidValue, ok := searchKey(realitySettings, "shortIds"); ok {
  268. shortIds, _ := sidValue.([]interface{})
  269. params["sid"], _ = shortIds[0].(string)
  270. }
  271. if fpValue, ok := searchKey(realitySettings, "fingerprint"); ok {
  272. params["fp"], _ = fpValue.(string)
  273. }
  274. }
  275. if streamNetwork == "tcp" && len(clients[clientIndex].Flow) > 0 {
  276. params["flow"] = clients[clientIndex].Flow
  277. }
  278. }
  279. if security == "xtls" {
  280. params["security"] = "xtls"
  281. xtlsSetting, _ := stream["XTLSSettings"].(map[string]interface{})
  282. alpns, _ := xtlsSetting["alpn"].([]interface{})
  283. var alpn []string
  284. for _, a := range alpns {
  285. alpn = append(alpn, a.(string))
  286. }
  287. if len(alpn) > 0 {
  288. params["alpn"] = strings.Join(alpn, ",")
  289. }
  290. XTLSSettings, _ := searchKey(xtlsSetting, "settings")
  291. if xtlsSetting != nil {
  292. if sniValue, ok := searchKey(XTLSSettings, "serverName"); ok {
  293. params["sni"], _ = sniValue.(string)
  294. }
  295. if fpValue, ok := searchKey(XTLSSettings, "fingerprint"); ok {
  296. params["fp"], _ = fpValue.(string)
  297. }
  298. if insecure, ok := searchKey(XTLSSettings, "allowInsecure"); ok {
  299. if insecure.(bool) {
  300. params["allowInsecure"] = "1"
  301. }
  302. }
  303. }
  304. if streamNetwork == "tcp" && len(clients[clientIndex].Flow) > 0 {
  305. params["flow"] = clients[clientIndex].Flow
  306. }
  307. serverName, _ := xtlsSetting["serverName"].(string)
  308. if serverName != "" {
  309. address = serverName
  310. }
  311. }
  312. link := fmt.Sprintf("vless://%s@%s:%d", uuid, address, port)
  313. url, _ := url.Parse(link)
  314. q := url.Query()
  315. for k, v := range params {
  316. q.Add(k, v)
  317. }
  318. // Set the new query values on the URL
  319. url.RawQuery = q.Encode()
  320. url.Fragment = email
  321. return url.String()
  322. }
  323. func (s *SubService) genTrojanLink(inbound *model.Inbound, email string) string {
  324. address := s.address
  325. if inbound.Protocol != model.Trojan {
  326. return ""
  327. }
  328. var stream map[string]interface{}
  329. json.Unmarshal([]byte(inbound.StreamSettings), &stream)
  330. clients, _ := s.inboundService.getClients(inbound)
  331. clientIndex := -1
  332. for i, client := range clients {
  333. if client.Email == email {
  334. clientIndex = i
  335. break
  336. }
  337. }
  338. password := clients[clientIndex].Password
  339. port := inbound.Port
  340. streamNetwork := stream["network"].(string)
  341. params := make(map[string]string)
  342. params["type"] = streamNetwork
  343. switch streamNetwork {
  344. case "tcp":
  345. tcp, _ := stream["tcpSettings"].(map[string]interface{})
  346. header, _ := tcp["header"].(map[string]interface{})
  347. typeStr, _ := header["type"].(string)
  348. if typeStr == "http" {
  349. request := header["request"].(map[string]interface{})
  350. requestPath, _ := request["path"].([]interface{})
  351. params["path"] = requestPath[0].(string)
  352. headers, _ := request["headers"].(map[string]interface{})
  353. params["host"] = searchHost(headers)
  354. params["headerType"] = "http"
  355. }
  356. case "kcp":
  357. kcp, _ := stream["kcpSettings"].(map[string]interface{})
  358. header, _ := kcp["header"].(map[string]interface{})
  359. params["headerType"] = header["type"].(string)
  360. params["seed"] = kcp["seed"].(string)
  361. case "ws":
  362. ws, _ := stream["wsSettings"].(map[string]interface{})
  363. params["path"] = ws["path"].(string)
  364. headers, _ := ws["headers"].(map[string]interface{})
  365. params["host"] = searchHost(headers)
  366. case "http":
  367. http, _ := stream["httpSettings"].(map[string]interface{})
  368. params["path"] = http["path"].(string)
  369. params["host"] = searchHost(http)
  370. case "quic":
  371. quic, _ := stream["quicSettings"].(map[string]interface{})
  372. params["quicSecurity"] = quic["security"].(string)
  373. params["key"] = quic["key"].(string)
  374. header := quic["header"].(map[string]interface{})
  375. params["headerType"] = header["type"].(string)
  376. case "grpc":
  377. grpc, _ := stream["grpcSettings"].(map[string]interface{})
  378. params["serviceName"] = grpc["serviceName"].(string)
  379. }
  380. security, _ := stream["security"].(string)
  381. if security == "tls" {
  382. params["security"] = "tls"
  383. tlsSetting, _ := stream["tlsSettings"].(map[string]interface{})
  384. alpns, _ := tlsSetting["alpn"].([]interface{})
  385. var alpn []string
  386. for _, a := range alpns {
  387. alpn = append(alpn, a.(string))
  388. }
  389. if len(alpn) > 0 {
  390. params["alpn"] = strings.Join(alpn, ",")
  391. }
  392. tlsSettings, _ := searchKey(tlsSetting, "settings")
  393. if tlsSetting != nil {
  394. if sniValue, ok := searchKey(tlsSettings, "serverName"); ok {
  395. params["sni"], _ = sniValue.(string)
  396. }
  397. if fpValue, ok := searchKey(tlsSettings, "fingerprint"); ok {
  398. params["fp"], _ = fpValue.(string)
  399. }
  400. if insecure, ok := searchKey(tlsSettings, "allowInsecure"); ok {
  401. if insecure.(bool) {
  402. params["allowInsecure"] = "1"
  403. }
  404. }
  405. }
  406. serverName, _ := tlsSetting["serverName"].(string)
  407. if serverName != "" {
  408. address = serverName
  409. }
  410. }
  411. if security == "reality" {
  412. params["security"] = "reality"
  413. realitySettings, _ := stream["realitySettings"].(map[string]interface{})
  414. if realitySettings != nil {
  415. if sniValue, ok := searchKey(realitySettings, "serverNames"); ok {
  416. sNames, _ := sniValue.([]interface{})
  417. params["sni"], _ = sNames[0].(string)
  418. }
  419. if pbkValue, ok := searchKey(realitySettings, "publicKey"); ok {
  420. params["pbk"], _ = pbkValue.(string)
  421. }
  422. if sidValue, ok := searchKey(realitySettings, "shortIds"); ok {
  423. shortIds, _ := sidValue.([]interface{})
  424. params["sid"], _ = shortIds[0].(string)
  425. }
  426. if fpValue, ok := searchKey(realitySettings, "fingerprint"); ok {
  427. params["fp"], _ = fpValue.(string)
  428. }
  429. }
  430. if streamNetwork == "tcp" && len(clients[clientIndex].Flow) > 0 {
  431. params["flow"] = clients[clientIndex].Flow
  432. }
  433. }
  434. if security == "xtls" {
  435. params["security"] = "xtls"
  436. xtlsSetting, _ := stream["XTLSSettings"].(map[string]interface{})
  437. alpns, _ := xtlsSetting["alpn"].([]interface{})
  438. var alpn []string
  439. for _, a := range alpns {
  440. alpn = append(alpn, a.(string))
  441. }
  442. if len(alpn) > 0 {
  443. params["alpn"] = strings.Join(alpn, ",")
  444. }
  445. XTLSSettings, _ := searchKey(xtlsSetting, "settings")
  446. if xtlsSetting != nil {
  447. if sniValue, ok := searchKey(XTLSSettings, "serverName"); ok {
  448. params["sni"], _ = sniValue.(string)
  449. }
  450. if fpValue, ok := searchKey(XTLSSettings, "fingerprint"); ok {
  451. params["fp"], _ = fpValue.(string)
  452. }
  453. if insecure, ok := searchKey(XTLSSettings, "allowInsecure"); ok {
  454. if insecure.(bool) {
  455. params["allowInsecure"] = "1"
  456. }
  457. }
  458. }
  459. if streamNetwork == "tcp" && len(clients[clientIndex].Flow) > 0 {
  460. params["flow"] = clients[clientIndex].Flow
  461. }
  462. serverName, _ := xtlsSetting["serverName"].(string)
  463. if serverName != "" {
  464. address = serverName
  465. }
  466. }
  467. link := fmt.Sprintf("trojan://%s@%s:%d", password, address, port)
  468. url, _ := url.Parse(link)
  469. q := url.Query()
  470. for k, v := range params {
  471. q.Add(k, v)
  472. }
  473. // Set the new query values on the URL
  474. url.RawQuery = q.Encode()
  475. url.Fragment = email
  476. return url.String()
  477. }
  478. func searchKey(data interface{}, key string) (interface{}, bool) {
  479. switch val := data.(type) {
  480. case map[string]interface{}:
  481. for k, v := range val {
  482. if k == key {
  483. return v, true
  484. }
  485. if result, ok := searchKey(v, key); ok {
  486. return result, true
  487. }
  488. }
  489. case []interface{}:
  490. for _, v := range val {
  491. if result, ok := searchKey(v, key); ok {
  492. return result, true
  493. }
  494. }
  495. }
  496. return nil, false
  497. }
  498. func searchHost(headers interface{}) string {
  499. data, _ := headers.(map[string]interface{})
  500. for k, v := range data {
  501. if strings.EqualFold(k, "host") {
  502. switch v.(type) {
  503. case []interface{}:
  504. hosts, _ := v.([]interface{})
  505. return hosts[0].(string)
  506. case interface{}:
  507. return v.(string)
  508. }
  509. }
  510. }
  511. return ""
  512. }