sub.go 14 KB

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