subService.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  1. package sub
  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. "x-ui/web/service"
  11. "x-ui/xray"
  12. "github.com/goccy/go-json"
  13. )
  14. type SubService struct {
  15. address string
  16. inboundService service.InboundService
  17. settingServics service.SettingService
  18. }
  19. func (s *SubService) GetSubs(subId string, host string) ([]string, []string, error) {
  20. s.address = host
  21. var result []string
  22. var headers []string
  23. var traffic xray.ClientTraffic
  24. var clientTraffics []xray.ClientTraffic
  25. inbounds, err := s.getInboundsBySubId(subId)
  26. if err != nil {
  27. return nil, nil, err
  28. }
  29. for _, inbound := range inbounds {
  30. clients, err := s.inboundService.GetClients(inbound)
  31. if err != nil {
  32. logger.Error("SubService - GetSub: Unable to get clients from inbound")
  33. }
  34. if clients == nil {
  35. continue
  36. }
  37. if len(inbound.Listen) > 0 && inbound.Listen[0] == '@' {
  38. fallbackMaster, err := s.getFallbackMaster(inbound.Listen)
  39. if err == nil {
  40. inbound.Listen = fallbackMaster.Listen
  41. inbound.Port = fallbackMaster.Port
  42. var stream map[string]interface{}
  43. json.Unmarshal([]byte(inbound.StreamSettings), &stream)
  44. var masterStream map[string]interface{}
  45. json.Unmarshal([]byte(fallbackMaster.StreamSettings), &masterStream)
  46. stream["security"] = masterStream["security"]
  47. stream["tlsSettings"] = masterStream["tlsSettings"]
  48. modifiedStream, _ := json.MarshalIndent(stream, "", " ")
  49. inbound.StreamSettings = string(modifiedStream)
  50. }
  51. }
  52. for _, client := range clients {
  53. if client.Enable && client.SubID == subId {
  54. link := s.getLink(inbound, client.Email)
  55. result = append(result, link)
  56. clientTraffics = append(clientTraffics, s.getClientTraffics(inbound.ClientStats, client.Email))
  57. }
  58. }
  59. }
  60. for index, clientTraffic := range clientTraffics {
  61. if index == 0 {
  62. traffic.Up = clientTraffic.Up
  63. traffic.Down = clientTraffic.Down
  64. traffic.Total = clientTraffic.Total
  65. if clientTraffic.ExpiryTime > 0 {
  66. traffic.ExpiryTime = clientTraffic.ExpiryTime
  67. }
  68. } else {
  69. traffic.Up += clientTraffic.Up
  70. traffic.Down += clientTraffic.Down
  71. if traffic.Total == 0 || clientTraffic.Total == 0 {
  72. traffic.Total = 0
  73. } else {
  74. traffic.Total += clientTraffic.Total
  75. }
  76. if clientTraffic.ExpiryTime != traffic.ExpiryTime {
  77. traffic.ExpiryTime = 0
  78. }
  79. }
  80. }
  81. headers = append(headers, fmt.Sprintf("upload=%d; download=%d; total=%d; expire=%d", traffic.Up, traffic.Down, traffic.Total, traffic.ExpiryTime/1000))
  82. updateInterval, _ := s.settingServics.GetSubUpdates()
  83. headers = append(headers, fmt.Sprintf("%d", updateInterval))
  84. headers = append(headers, subId)
  85. return result, headers, nil
  86. }
  87. func (s *SubService) getInboundsBySubId(subId string) ([]*model.Inbound, error) {
  88. db := database.GetDB()
  89. var inbounds []*model.Inbound
  90. err := db.Model(model.Inbound{}).Preload("ClientStats").Where("settings like ? and enable = ?", fmt.Sprintf(`%%"subId": "%s"%%`, subId), true).Find(&inbounds).Error
  91. if err != nil {
  92. return nil, err
  93. }
  94. return inbounds, nil
  95. }
  96. func (s *SubService) getClientTraffics(traffics []xray.ClientTraffic, email string) xray.ClientTraffic {
  97. for _, traffic := range traffics {
  98. if traffic.Email == email {
  99. return traffic
  100. }
  101. }
  102. return xray.ClientTraffic{}
  103. }
  104. func (s *SubService) getFallbackMaster(dest string) (*model.Inbound, error) {
  105. db := database.GetDB()
  106. var inbound *model.Inbound
  107. err := db.Model(model.Inbound{}).
  108. Where("JSON_TYPE(settings, '$.fallbacks') = 'array'").
  109. Where("EXISTS (SELECT * FROM json_each(settings, '$.fallbacks') WHERE json_extract(value, '$.dest') = ?)", dest).
  110. Find(&inbound).Error
  111. if err != nil {
  112. return nil, err
  113. }
  114. return inbound, nil
  115. }
  116. func (s *SubService) getLink(inbound *model.Inbound, email string) string {
  117. switch inbound.Protocol {
  118. case "vmess":
  119. return s.genVmessLink(inbound, email)
  120. case "vless":
  121. return s.genVlessLink(inbound, email)
  122. case "trojan":
  123. return s.genTrojanLink(inbound, email)
  124. case "shadowsocks":
  125. return s.genShadowsocksLink(inbound, email)
  126. }
  127. return ""
  128. }
  129. func (s *SubService) genVmessLink(inbound *model.Inbound, email string) string {
  130. if inbound.Protocol != model.VMess {
  131. return ""
  132. }
  133. remark := fmt.Sprintf("%s-%s", inbound.Remark, email)
  134. obj := map[string]interface{}{
  135. "v": "2",
  136. "ps": remark,
  137. "add": s.address,
  138. "port": inbound.Port,
  139. "type": "none",
  140. }
  141. var stream map[string]interface{}
  142. json.Unmarshal([]byte(inbound.StreamSettings), &stream)
  143. network, _ := stream["network"].(string)
  144. obj["net"] = network
  145. switch network {
  146. case "tcp":
  147. tcp, _ := stream["tcpSettings"].(map[string]interface{})
  148. header, _ := tcp["header"].(map[string]interface{})
  149. typeStr, _ := header["type"].(string)
  150. obj["type"] = typeStr
  151. if typeStr == "http" {
  152. request := header["request"].(map[string]interface{})
  153. requestPath, _ := request["path"].([]interface{})
  154. obj["path"] = requestPath[0].(string)
  155. headers, _ := request["headers"].(map[string]interface{})
  156. obj["host"] = searchHost(headers)
  157. }
  158. case "kcp":
  159. kcp, _ := stream["kcpSettings"].(map[string]interface{})
  160. header, _ := kcp["header"].(map[string]interface{})
  161. obj["type"], _ = header["type"].(string)
  162. obj["path"], _ = kcp["seed"].(string)
  163. case "ws":
  164. ws, _ := stream["wsSettings"].(map[string]interface{})
  165. obj["path"] = ws["path"].(string)
  166. headers, _ := ws["headers"].(map[string]interface{})
  167. obj["host"] = searchHost(headers)
  168. case "http":
  169. obj["net"] = "h2"
  170. http, _ := stream["httpSettings"].(map[string]interface{})
  171. obj["path"], _ = http["path"].(string)
  172. obj["host"] = searchHost(http)
  173. case "quic":
  174. quic, _ := stream["quicSettings"].(map[string]interface{})
  175. header := quic["header"].(map[string]interface{})
  176. obj["type"], _ = header["type"].(string)
  177. obj["host"], _ = quic["security"].(string)
  178. obj["path"], _ = quic["key"].(string)
  179. case "grpc":
  180. grpc, _ := stream["grpcSettings"].(map[string]interface{})
  181. obj["path"] = grpc["serviceName"].(string)
  182. if grpc["multiMode"].(bool) {
  183. obj["type"] = "multi"
  184. }
  185. }
  186. security, _ := stream["security"].(string)
  187. var domains []interface{}
  188. obj["tls"] = security
  189. if security == "tls" {
  190. tlsSetting, _ := stream["tlsSettings"].(map[string]interface{})
  191. alpns, _ := tlsSetting["alpn"].([]interface{})
  192. if len(alpns) > 0 {
  193. var alpn []string
  194. for _, a := range alpns {
  195. alpn = append(alpn, a.(string))
  196. }
  197. obj["alpn"] = strings.Join(alpn, ",")
  198. }
  199. tlsSettings, _ := searchKey(tlsSetting, "settings")
  200. if tlsSetting != nil {
  201. if sniValue, ok := searchKey(tlsSettings, "serverName"); ok {
  202. obj["sni"], _ = sniValue.(string)
  203. }
  204. if fpValue, ok := searchKey(tlsSettings, "fingerprint"); ok {
  205. obj["fp"], _ = fpValue.(string)
  206. }
  207. if insecure, ok := searchKey(tlsSettings, "allowInsecure"); ok {
  208. obj["allowInsecure"], _ = insecure.(bool)
  209. }
  210. if domainSettings, ok := searchKey(tlsSettings, "domains"); ok {
  211. domains, _ = domainSettings.([]interface{})
  212. }
  213. }
  214. serverName, _ := tlsSetting["serverName"].(string)
  215. if serverName != "" {
  216. obj["add"] = serverName
  217. }
  218. }
  219. clients, _ := s.inboundService.GetClients(inbound)
  220. clientIndex := -1
  221. for i, client := range clients {
  222. if client.Email == email {
  223. clientIndex = i
  224. break
  225. }
  226. }
  227. obj["id"] = clients[clientIndex].ID
  228. if len(domains) > 0 {
  229. links := ""
  230. for index, d := range domains {
  231. domain := d.(map[string]interface{})
  232. obj["ps"] = remark + "-" + domain["remark"].(string)
  233. obj["add"] = domain["domain"].(string)
  234. if index > 0 {
  235. links += "\n"
  236. }
  237. jsonStr, _ := json.MarshalIndent(obj, "", " ")
  238. links += "vmess://" + base64.StdEncoding.EncodeToString(jsonStr)
  239. }
  240. return links
  241. }
  242. jsonStr, _ := json.MarshalIndent(obj, "", " ")
  243. return "vmess://" + base64.StdEncoding.EncodeToString(jsonStr)
  244. }
  245. func (s *SubService) genVlessLink(inbound *model.Inbound, email string) string {
  246. address := s.address
  247. if inbound.Protocol != model.VLESS {
  248. return ""
  249. }
  250. var stream map[string]interface{}
  251. json.Unmarshal([]byte(inbound.StreamSettings), &stream)
  252. clients, _ := s.inboundService.GetClients(inbound)
  253. clientIndex := -1
  254. for i, client := range clients {
  255. if client.Email == email {
  256. clientIndex = i
  257. break
  258. }
  259. }
  260. uuid := clients[clientIndex].ID
  261. port := inbound.Port
  262. streamNetwork := stream["network"].(string)
  263. params := make(map[string]string)
  264. params["type"] = streamNetwork
  265. switch streamNetwork {
  266. case "tcp":
  267. tcp, _ := stream["tcpSettings"].(map[string]interface{})
  268. header, _ := tcp["header"].(map[string]interface{})
  269. typeStr, _ := header["type"].(string)
  270. if typeStr == "http" {
  271. request := header["request"].(map[string]interface{})
  272. requestPath, _ := request["path"].([]interface{})
  273. params["path"] = requestPath[0].(string)
  274. headers, _ := request["headers"].(map[string]interface{})
  275. params["host"] = searchHost(headers)
  276. params["headerType"] = "http"
  277. }
  278. case "kcp":
  279. kcp, _ := stream["kcpSettings"].(map[string]interface{})
  280. header, _ := kcp["header"].(map[string]interface{})
  281. params["headerType"] = header["type"].(string)
  282. params["seed"] = kcp["seed"].(string)
  283. case "ws":
  284. ws, _ := stream["wsSettings"].(map[string]interface{})
  285. params["path"] = ws["path"].(string)
  286. headers, _ := ws["headers"].(map[string]interface{})
  287. params["host"] = searchHost(headers)
  288. case "http":
  289. http, _ := stream["httpSettings"].(map[string]interface{})
  290. params["path"] = http["path"].(string)
  291. params["host"] = searchHost(http)
  292. case "quic":
  293. quic, _ := stream["quicSettings"].(map[string]interface{})
  294. params["quicSecurity"] = quic["security"].(string)
  295. params["key"] = quic["key"].(string)
  296. header := quic["header"].(map[string]interface{})
  297. params["headerType"] = header["type"].(string)
  298. case "grpc":
  299. grpc, _ := stream["grpcSettings"].(map[string]interface{})
  300. params["serviceName"] = grpc["serviceName"].(string)
  301. if grpc["multiMode"].(bool) {
  302. params["mode"] = "multi"
  303. }
  304. }
  305. security, _ := stream["security"].(string)
  306. var domains []interface{}
  307. if security == "tls" {
  308. params["security"] = "tls"
  309. tlsSetting, _ := stream["tlsSettings"].(map[string]interface{})
  310. alpns, _ := tlsSetting["alpn"].([]interface{})
  311. var alpn []string
  312. for _, a := range alpns {
  313. alpn = append(alpn, a.(string))
  314. }
  315. if len(alpn) > 0 {
  316. params["alpn"] = strings.Join(alpn, ",")
  317. }
  318. tlsSettings, _ := searchKey(tlsSetting, "settings")
  319. if tlsSetting != nil {
  320. if sniValue, ok := searchKey(tlsSettings, "serverName"); ok {
  321. params["sni"], _ = sniValue.(string)
  322. }
  323. if fpValue, ok := searchKey(tlsSettings, "fingerprint"); ok {
  324. params["fp"], _ = fpValue.(string)
  325. }
  326. if insecure, ok := searchKey(tlsSettings, "allowInsecure"); ok {
  327. if insecure.(bool) {
  328. params["allowInsecure"] = "1"
  329. }
  330. }
  331. if domainSettings, ok := searchKey(tlsSettings, "domains"); ok {
  332. domains, _ = domainSettings.([]interface{})
  333. }
  334. }
  335. if streamNetwork == "tcp" && len(clients[clientIndex].Flow) > 0 {
  336. params["flow"] = clients[clientIndex].Flow
  337. }
  338. serverName, _ := tlsSetting["serverName"].(string)
  339. if serverName != "" {
  340. address = serverName
  341. }
  342. }
  343. if security == "reality" {
  344. params["security"] = "reality"
  345. realitySetting, _ := stream["realitySettings"].(map[string]interface{})
  346. realitySettings, _ := searchKey(realitySetting, "settings")
  347. if realitySetting != nil {
  348. if sniValue, ok := searchKey(realitySetting, "serverNames"); ok {
  349. sNames, _ := sniValue.([]interface{})
  350. params["sni"], _ = sNames[0].(string)
  351. }
  352. if pbkValue, ok := searchKey(realitySettings, "publicKey"); ok {
  353. params["pbk"], _ = pbkValue.(string)
  354. }
  355. if sidValue, ok := searchKey(realitySetting, "shortIds"); ok {
  356. shortIds, _ := sidValue.([]interface{})
  357. params["sid"], _ = shortIds[0].(string)
  358. }
  359. if fpValue, ok := searchKey(realitySettings, "fingerprint"); ok {
  360. if fp, ok := fpValue.(string); ok && len(fp) > 0 {
  361. params["fp"] = fp
  362. }
  363. }
  364. if spxValue, ok := searchKey(realitySettings, "spiderX"); ok {
  365. if spx, ok := spxValue.(string); ok && len(spx) > 0 {
  366. params["spx"] = spx
  367. }
  368. }
  369. if serverName, ok := searchKey(realitySettings, "serverName"); ok {
  370. if sname, ok := serverName.(string); ok && len(sname) > 0 {
  371. address = sname
  372. }
  373. }
  374. }
  375. if streamNetwork == "tcp" && len(clients[clientIndex].Flow) > 0 {
  376. params["flow"] = clients[clientIndex].Flow
  377. }
  378. }
  379. if security == "xtls" {
  380. params["security"] = "xtls"
  381. xtlsSetting, _ := stream["xtlsSettings"].(map[string]interface{})
  382. alpns, _ := xtlsSetting["alpn"].([]interface{})
  383. var alpn []string
  384. for _, a := range alpns {
  385. alpn = append(alpn, a.(string))
  386. }
  387. if len(alpn) > 0 {
  388. params["alpn"] = strings.Join(alpn, ",")
  389. }
  390. xtlsSettings, _ := searchKey(xtlsSetting, "settings")
  391. if xtlsSetting != nil {
  392. if fpValue, ok := searchKey(xtlsSettings, "fingerprint"); ok {
  393. params["fp"], _ = fpValue.(string)
  394. }
  395. if insecure, ok := searchKey(xtlsSettings, "allowInsecure"); ok {
  396. if insecure.(bool) {
  397. params["allowInsecure"] = "1"
  398. }
  399. }
  400. if sniValue, ok := searchKey(xtlsSettings, "serverName"); ok {
  401. params["sni"], _ = sniValue.(string)
  402. }
  403. }
  404. if streamNetwork == "tcp" && len(clients[clientIndex].Flow) > 0 {
  405. params["flow"] = clients[clientIndex].Flow
  406. }
  407. serverName, _ := xtlsSetting["serverName"].(string)
  408. if serverName != "" {
  409. address = serverName
  410. }
  411. }
  412. link := fmt.Sprintf("vless://%s@%s:%d", uuid, address, port)
  413. url, _ := url.Parse(link)
  414. q := url.Query()
  415. for k, v := range params {
  416. q.Add(k, v)
  417. }
  418. // Set the new query values on the URL
  419. url.RawQuery = q.Encode()
  420. remark := fmt.Sprintf("%s-%s", inbound.Remark, email)
  421. if len(domains) > 0 {
  422. links := ""
  423. for index, d := range domains {
  424. domain := d.(map[string]interface{})
  425. url.Fragment = remark + "-" + domain["remark"].(string)
  426. url.Host = fmt.Sprintf("%s:%d", domain["domain"].(string), port)
  427. if index > 0 {
  428. links += "\n"
  429. }
  430. links += url.String()
  431. }
  432. return links
  433. }
  434. url.Fragment = remark
  435. return url.String()
  436. }
  437. func (s *SubService) genTrojanLink(inbound *model.Inbound, email string) string {
  438. address := s.address
  439. if inbound.Protocol != model.Trojan {
  440. return ""
  441. }
  442. var stream map[string]interface{}
  443. json.Unmarshal([]byte(inbound.StreamSettings), &stream)
  444. clients, _ := s.inboundService.GetClients(inbound)
  445. clientIndex := -1
  446. for i, client := range clients {
  447. if client.Email == email {
  448. clientIndex = i
  449. break
  450. }
  451. }
  452. password := clients[clientIndex].Password
  453. port := inbound.Port
  454. streamNetwork := stream["network"].(string)
  455. params := make(map[string]string)
  456. params["type"] = streamNetwork
  457. switch streamNetwork {
  458. case "tcp":
  459. tcp, _ := stream["tcpSettings"].(map[string]interface{})
  460. header, _ := tcp["header"].(map[string]interface{})
  461. typeStr, _ := header["type"].(string)
  462. if typeStr == "http" {
  463. request := header["request"].(map[string]interface{})
  464. requestPath, _ := request["path"].([]interface{})
  465. params["path"] = requestPath[0].(string)
  466. headers, _ := request["headers"].(map[string]interface{})
  467. params["host"] = searchHost(headers)
  468. params["headerType"] = "http"
  469. }
  470. case "kcp":
  471. kcp, _ := stream["kcpSettings"].(map[string]interface{})
  472. header, _ := kcp["header"].(map[string]interface{})
  473. params["headerType"] = header["type"].(string)
  474. params["seed"] = kcp["seed"].(string)
  475. case "ws":
  476. ws, _ := stream["wsSettings"].(map[string]interface{})
  477. params["path"] = ws["path"].(string)
  478. headers, _ := ws["headers"].(map[string]interface{})
  479. params["host"] = searchHost(headers)
  480. case "http":
  481. http, _ := stream["httpSettings"].(map[string]interface{})
  482. params["path"] = http["path"].(string)
  483. params["host"] = searchHost(http)
  484. case "quic":
  485. quic, _ := stream["quicSettings"].(map[string]interface{})
  486. params["quicSecurity"] = quic["security"].(string)
  487. params["key"] = quic["key"].(string)
  488. header := quic["header"].(map[string]interface{})
  489. params["headerType"] = header["type"].(string)
  490. case "grpc":
  491. grpc, _ := stream["grpcSettings"].(map[string]interface{})
  492. params["serviceName"] = grpc["serviceName"].(string)
  493. if grpc["multiMode"].(bool) {
  494. params["mode"] = "multi"
  495. }
  496. }
  497. security, _ := stream["security"].(string)
  498. var domains []interface{}
  499. if security == "tls" {
  500. params["security"] = "tls"
  501. tlsSetting, _ := stream["tlsSettings"].(map[string]interface{})
  502. alpns, _ := tlsSetting["alpn"].([]interface{})
  503. var alpn []string
  504. for _, a := range alpns {
  505. alpn = append(alpn, a.(string))
  506. }
  507. if len(alpn) > 0 {
  508. params["alpn"] = strings.Join(alpn, ",")
  509. }
  510. tlsSettings, _ := searchKey(tlsSetting, "settings")
  511. if tlsSetting != nil {
  512. if sniValue, ok := searchKey(tlsSettings, "serverName"); ok {
  513. params["sni"], _ = sniValue.(string)
  514. }
  515. if fpValue, ok := searchKey(tlsSettings, "fingerprint"); ok {
  516. params["fp"], _ = fpValue.(string)
  517. }
  518. if insecure, ok := searchKey(tlsSettings, "allowInsecure"); ok {
  519. if insecure.(bool) {
  520. params["allowInsecure"] = "1"
  521. }
  522. }
  523. if domainSettings, ok := searchKey(tlsSettings, "domains"); ok {
  524. domains, _ = domainSettings.([]interface{})
  525. }
  526. }
  527. serverName, _ := tlsSetting["serverName"].(string)
  528. if serverName != "" {
  529. address = serverName
  530. }
  531. }
  532. if security == "reality" {
  533. params["security"] = "reality"
  534. realitySetting, _ := stream["realitySettings"].(map[string]interface{})
  535. realitySettings, _ := searchKey(realitySetting, "settings")
  536. if realitySetting != nil {
  537. if sniValue, ok := searchKey(realitySetting, "serverNames"); ok {
  538. sNames, _ := sniValue.([]interface{})
  539. params["sni"], _ = sNames[0].(string)
  540. }
  541. if pbkValue, ok := searchKey(realitySettings, "publicKey"); ok {
  542. params["pbk"], _ = pbkValue.(string)
  543. }
  544. if sidValue, ok := searchKey(realitySetting, "shortIds"); ok {
  545. shortIds, _ := sidValue.([]interface{})
  546. params["sid"], _ = shortIds[0].(string)
  547. }
  548. if fpValue, ok := searchKey(realitySettings, "fingerprint"); ok {
  549. if fp, ok := fpValue.(string); ok && len(fp) > 0 {
  550. params["fp"] = fp
  551. }
  552. }
  553. if spxValue, ok := searchKey(realitySettings, "spiderX"); ok {
  554. if spx, ok := spxValue.(string); ok && len(spx) > 0 {
  555. params["spx"] = spx
  556. }
  557. }
  558. if serverName, ok := searchKey(realitySettings, "serverName"); ok {
  559. if sname, ok := serverName.(string); ok && len(sname) > 0 {
  560. address = sname
  561. }
  562. }
  563. }
  564. if streamNetwork == "tcp" && len(clients[clientIndex].Flow) > 0 {
  565. params["flow"] = clients[clientIndex].Flow
  566. }
  567. }
  568. if security == "xtls" {
  569. params["security"] = "xtls"
  570. xtlsSetting, _ := stream["xtlsSettings"].(map[string]interface{})
  571. alpns, _ := xtlsSetting["alpn"].([]interface{})
  572. var alpn []string
  573. for _, a := range alpns {
  574. alpn = append(alpn, a.(string))
  575. }
  576. if len(alpn) > 0 {
  577. params["alpn"] = strings.Join(alpn, ",")
  578. }
  579. xtlsSettings, _ := searchKey(xtlsSetting, "settings")
  580. if xtlsSetting != nil {
  581. if fpValue, ok := searchKey(xtlsSettings, "fingerprint"); ok {
  582. params["fp"], _ = fpValue.(string)
  583. }
  584. if insecure, ok := searchKey(xtlsSettings, "allowInsecure"); ok {
  585. if insecure.(bool) {
  586. params["allowInsecure"] = "1"
  587. }
  588. }
  589. if sniValue, ok := searchKey(xtlsSettings, "serverName"); ok {
  590. params["sni"], _ = sniValue.(string)
  591. }
  592. }
  593. if streamNetwork == "tcp" && len(clients[clientIndex].Flow) > 0 {
  594. params["flow"] = clients[clientIndex].Flow
  595. }
  596. serverName, _ := xtlsSetting["serverName"].(string)
  597. if serverName != "" {
  598. address = serverName
  599. }
  600. }
  601. link := fmt.Sprintf("trojan://%s@%s:%d", password, address, port)
  602. url, _ := url.Parse(link)
  603. q := url.Query()
  604. for k, v := range params {
  605. q.Add(k, v)
  606. }
  607. // Set the new query values on the URL
  608. url.RawQuery = q.Encode()
  609. remark := fmt.Sprintf("%s-%s", inbound.Remark, email)
  610. if len(domains) > 0 {
  611. links := ""
  612. for index, d := range domains {
  613. domain := d.(map[string]interface{})
  614. url.Fragment = remark + "-" + domain["remark"].(string)
  615. url.Host = fmt.Sprintf("%s:%d", domain["domain"].(string), port)
  616. if index > 0 {
  617. links += "\n"
  618. }
  619. links += url.String()
  620. }
  621. return links
  622. }
  623. url.Fragment = remark
  624. return url.String()
  625. }
  626. func (s *SubService) genShadowsocksLink(inbound *model.Inbound, email string) string {
  627. address := s.address
  628. if inbound.Protocol != model.Shadowsocks {
  629. return ""
  630. }
  631. clients, _ := s.inboundService.GetClients(inbound)
  632. var settings map[string]interface{}
  633. json.Unmarshal([]byte(inbound.Settings), &settings)
  634. inboundPassword := settings["password"].(string)
  635. method := settings["method"].(string)
  636. clientIndex := -1
  637. for i, client := range clients {
  638. if client.Email == email {
  639. clientIndex = i
  640. break
  641. }
  642. }
  643. encPart := fmt.Sprintf("%s:%s:%s", method, inboundPassword, clients[clientIndex].Password)
  644. remark := fmt.Sprintf("%s-%s", inbound.Remark, clients[clientIndex].Email)
  645. return fmt.Sprintf("ss://%s@%s:%d#%s", base64.StdEncoding.EncodeToString([]byte(encPart)), address, inbound.Port, remark)
  646. }
  647. func searchKey(data interface{}, key string) (interface{}, bool) {
  648. switch val := data.(type) {
  649. case map[string]interface{}:
  650. for k, v := range val {
  651. if k == key {
  652. return v, true
  653. }
  654. if result, ok := searchKey(v, key); ok {
  655. return result, true
  656. }
  657. }
  658. case []interface{}:
  659. for _, v := range val {
  660. if result, ok := searchKey(v, key); ok {
  661. return result, true
  662. }
  663. }
  664. }
  665. return nil, false
  666. }
  667. func searchHost(headers interface{}) string {
  668. data, _ := headers.(map[string]interface{})
  669. for k, v := range data {
  670. if strings.EqualFold(k, "host") {
  671. switch v.(type) {
  672. case []interface{}:
  673. hosts, _ := v.([]interface{})
  674. if len(hosts) > 0 {
  675. return hosts[0].(string)
  676. } else {
  677. return ""
  678. }
  679. case interface{}:
  680. return v.(string)
  681. }
  682. }
  683. }
  684. return ""
  685. }