1
0

sub.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  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. realitySetting, _ := stream["realitySettings"].(map[string]interface{})
  259. realitySettings, _ := searchKey(realitySetting, "settings")
  260. if realitySetting != nil {
  261. if sniValue, ok := searchKey(realitySettings, "serverName"); ok {
  262. params["sni"], _ = sniValue.(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. params["sid"], _ = sidValue.(string)
  269. }
  270. if fpValue, ok := searchKey(realitySettings, "fingerprint"); ok {
  271. params["fp"], _ = fpValue.(string)
  272. }
  273. }
  274. if streamNetwork == "tcp" && len(clients[clientIndex].Flow) > 0 {
  275. params["flow"] = clients[clientIndex].Flow
  276. }
  277. serverName, _ := realitySetting["serverName"].(string)
  278. if serverName != "" {
  279. address = serverName
  280. }
  281. }
  282. if security == "xtls" {
  283. params["security"] = "xtls"
  284. xtlsSetting, _ := stream["XTLSSettings"].(map[string]interface{})
  285. alpns, _ := xtlsSetting["alpn"].([]interface{})
  286. var alpn []string
  287. for _, a := range alpns {
  288. alpn = append(alpn, a.(string))
  289. }
  290. if len(alpn) > 0 {
  291. params["alpn"] = strings.Join(alpn, ",")
  292. }
  293. XTLSSettings, _ := searchKey(xtlsSetting, "settings")
  294. if xtlsSetting != nil {
  295. if sniValue, ok := searchKey(XTLSSettings, "serverName"); ok {
  296. params["sni"], _ = sniValue.(string)
  297. }
  298. if fpValue, ok := searchKey(XTLSSettings, "fingerprint"); ok {
  299. params["fp"], _ = fpValue.(string)
  300. }
  301. if insecure, ok := searchKey(XTLSSettings, "allowInsecure"); ok {
  302. if insecure.(bool) {
  303. params["allowInsecure"] = "1"
  304. }
  305. }
  306. }
  307. if streamNetwork == "tcp" && len(clients[clientIndex].Flow) > 0 {
  308. params["flow"] = clients[clientIndex].Flow
  309. }
  310. serverName, _ := xtlsSetting["serverName"].(string)
  311. if serverName != "" {
  312. address = serverName
  313. }
  314. }
  315. link := fmt.Sprintf("vless://%s@%s:%d", uuid, address, port)
  316. url, _ := url.Parse(link)
  317. q := url.Query()
  318. for k, v := range params {
  319. q.Add(k, v)
  320. }
  321. // Set the new query values on the URL
  322. url.RawQuery = q.Encode()
  323. url.Fragment = email
  324. return url.String()
  325. }
  326. func (s *SubService) genTrojanLink(inbound *model.Inbound, email string) string {
  327. address := s.address
  328. if inbound.Protocol != model.Trojan {
  329. return ""
  330. }
  331. var stream map[string]interface{}
  332. json.Unmarshal([]byte(inbound.StreamSettings), &stream)
  333. clients, _ := s.inboundService.getClients(inbound)
  334. clientIndex := -1
  335. for i, client := range clients {
  336. if client.Email == email {
  337. clientIndex = i
  338. break
  339. }
  340. }
  341. password := clients[clientIndex].Password
  342. port := inbound.Port
  343. streamNetwork := stream["network"].(string)
  344. params := make(map[string]string)
  345. params["type"] = streamNetwork
  346. switch streamNetwork {
  347. case "tcp":
  348. tcp, _ := stream["tcpSettings"].(map[string]interface{})
  349. header, _ := tcp["header"].(map[string]interface{})
  350. typeStr, _ := header["type"].(string)
  351. if typeStr == "http" {
  352. request := header["request"].(map[string]interface{})
  353. requestPath, _ := request["path"].([]interface{})
  354. params["path"] = requestPath[0].(string)
  355. headers, _ := request["headers"].(map[string]interface{})
  356. params["host"] = searchHost(headers)
  357. params["headerType"] = "http"
  358. }
  359. case "kcp":
  360. kcp, _ := stream["kcpSettings"].(map[string]interface{})
  361. header, _ := kcp["header"].(map[string]interface{})
  362. params["headerType"] = header["type"].(string)
  363. params["seed"] = kcp["seed"].(string)
  364. case "ws":
  365. ws, _ := stream["wsSettings"].(map[string]interface{})
  366. params["path"] = ws["path"].(string)
  367. headers, _ := ws["headers"].(map[string]interface{})
  368. params["host"] = searchHost(headers)
  369. case "http":
  370. http, _ := stream["httpSettings"].(map[string]interface{})
  371. params["path"] = http["path"].(string)
  372. params["host"] = searchHost(http)
  373. case "quic":
  374. quic, _ := stream["quicSettings"].(map[string]interface{})
  375. params["quicSecurity"] = quic["security"].(string)
  376. params["key"] = quic["key"].(string)
  377. header := quic["header"].(map[string]interface{})
  378. params["headerType"] = header["type"].(string)
  379. case "grpc":
  380. grpc, _ := stream["grpcSettings"].(map[string]interface{})
  381. params["serviceName"] = grpc["serviceName"].(string)
  382. }
  383. security, _ := stream["security"].(string)
  384. if security == "tls" {
  385. params["security"] = "tls"
  386. tlsSetting, _ := stream["tlsSettings"].(map[string]interface{})
  387. alpns, _ := tlsSetting["alpn"].([]interface{})
  388. var alpn []string
  389. for _, a := range alpns {
  390. alpn = append(alpn, a.(string))
  391. }
  392. if len(alpn) > 0 {
  393. params["alpn"] = strings.Join(alpn, ",")
  394. }
  395. tlsSettings, _ := searchKey(tlsSetting, "settings")
  396. if tlsSetting != nil {
  397. if sniValue, ok := searchKey(tlsSettings, "serverName"); ok {
  398. params["sni"], _ = sniValue.(string)
  399. }
  400. if fpValue, ok := searchKey(tlsSettings, "fingerprint"); ok {
  401. params["fp"], _ = fpValue.(string)
  402. }
  403. if insecure, ok := searchKey(tlsSettings, "allowInsecure"); ok {
  404. if insecure.(bool) {
  405. params["allowInsecure"] = "1"
  406. }
  407. }
  408. }
  409. serverName, _ := tlsSetting["serverName"].(string)
  410. if serverName != "" {
  411. address = serverName
  412. }
  413. }
  414. if security == "reality" {
  415. params["security"] = "reality"
  416. realitySetting, _ := stream["realitySettings"].(map[string]interface{})
  417. realitySettings, _ := searchKey(realitySetting, "settings")
  418. if realitySetting != nil {
  419. if sniValue, ok := searchKey(realitySettings, "serverName"); ok {
  420. params["sni"], _ = sniValue.(string)
  421. }
  422. if pbkValue, ok := searchKey(realitySettings, "publicKey"); ok {
  423. params["pbk"], _ = pbkValue.(string)
  424. }
  425. if sidValue, ok := searchKey(realitySettings, "shortIds"); ok {
  426. params["sid"], _ = sidValue.(string)
  427. }
  428. if fpValue, ok := searchKey(realitySettings, "fingerprint"); ok {
  429. params["fp"], _ = fpValue.(string)
  430. }
  431. }
  432. serverName, _ := realitySetting["serverName"].(string)
  433. if serverName != "" {
  434. address = serverName
  435. }
  436. }
  437. if security == "xtls" {
  438. params["security"] = "xtls"
  439. xtlsSetting, _ := stream["XTLSSettings"].(map[string]interface{})
  440. alpns, _ := xtlsSetting["alpn"].([]interface{})
  441. var alpn []string
  442. for _, a := range alpns {
  443. alpn = append(alpn, a.(string))
  444. }
  445. if len(alpn) > 0 {
  446. params["alpn"] = strings.Join(alpn, ",")
  447. }
  448. XTLSSettings, _ := searchKey(xtlsSetting, "settings")
  449. if xtlsSetting != nil {
  450. if sniValue, ok := searchKey(XTLSSettings, "serverName"); ok {
  451. params["sni"], _ = sniValue.(string)
  452. }
  453. if fpValue, ok := searchKey(XTLSSettings, "fingerprint"); ok {
  454. params["fp"], _ = fpValue.(string)
  455. }
  456. if insecure, ok := searchKey(XTLSSettings, "allowInsecure"); ok {
  457. if insecure.(bool) {
  458. params["allowInsecure"] = "1"
  459. }
  460. }
  461. }
  462. if streamNetwork == "tcp" && len(clients[clientIndex].Flow) > 0 {
  463. params["flow"] = clients[clientIndex].Flow
  464. }
  465. serverName, _ := xtlsSetting["serverName"].(string)
  466. if serverName != "" {
  467. address = serverName
  468. }
  469. }
  470. link := fmt.Sprintf("trojan://%s@%s:%d", password, address, port)
  471. url, _ := url.Parse(link)
  472. q := url.Query()
  473. for k, v := range params {
  474. q.Add(k, v)
  475. }
  476. // Set the new query values on the URL
  477. url.RawQuery = q.Encode()
  478. url.Fragment = email
  479. return url.String()
  480. }
  481. func searchKey(data interface{}, key string) (interface{}, bool) {
  482. switch val := data.(type) {
  483. case map[string]interface{}:
  484. for k, v := range val {
  485. if k == key {
  486. return v, true
  487. }
  488. if result, ok := searchKey(v, key); ok {
  489. return result, true
  490. }
  491. }
  492. case []interface{}:
  493. for _, v := range val {
  494. if result, ok := searchKey(v, key); ok {
  495. return result, true
  496. }
  497. }
  498. }
  499. return nil, false
  500. }
  501. func searchHost(headers interface{}) string {
  502. data, _ := headers.(map[string]interface{})
  503. for k, v := range data {
  504. if strings.EqualFold(k, "host") {
  505. switch v.(type) {
  506. case []interface{}:
  507. hosts, _ := v.([]interface{})
  508. return hosts[0].(string)
  509. case interface{}:
  510. return v.(string)
  511. }
  512. }
  513. }
  514. return ""
  515. }