subService.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737
  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. obj["aid"] = clients[clientIndex].AlterIds
  229. if len(domains) > 0 {
  230. links := ""
  231. for index, d := range domains {
  232. domain := d.(map[string]interface{})
  233. obj["ps"] = remark + "-" + domain["remark"].(string)
  234. obj["add"] = domain["domain"].(string)
  235. if index > 0 {
  236. links += "\n"
  237. }
  238. jsonStr, _ := json.MarshalIndent(obj, "", " ")
  239. links += "vmess://" + base64.StdEncoding.EncodeToString(jsonStr)
  240. }
  241. return links
  242. }
  243. jsonStr, _ := json.MarshalIndent(obj, "", " ")
  244. return "vmess://" + base64.StdEncoding.EncodeToString(jsonStr)
  245. }
  246. func (s *SubService) genVlessLink(inbound *model.Inbound, email string) string {
  247. address := s.address
  248. if inbound.Protocol != model.VLESS {
  249. return ""
  250. }
  251. var stream map[string]interface{}
  252. json.Unmarshal([]byte(inbound.StreamSettings), &stream)
  253. clients, _ := s.inboundService.GetClients(inbound)
  254. clientIndex := -1
  255. for i, client := range clients {
  256. if client.Email == email {
  257. clientIndex = i
  258. break
  259. }
  260. }
  261. uuid := clients[clientIndex].ID
  262. port := inbound.Port
  263. streamNetwork := stream["network"].(string)
  264. params := make(map[string]string)
  265. params["type"] = streamNetwork
  266. switch streamNetwork {
  267. case "tcp":
  268. tcp, _ := stream["tcpSettings"].(map[string]interface{})
  269. header, _ := tcp["header"].(map[string]interface{})
  270. typeStr, _ := header["type"].(string)
  271. if typeStr == "http" {
  272. request := header["request"].(map[string]interface{})
  273. requestPath, _ := request["path"].([]interface{})
  274. params["path"] = requestPath[0].(string)
  275. headers, _ := request["headers"].(map[string]interface{})
  276. params["host"] = searchHost(headers)
  277. params["headerType"] = "http"
  278. }
  279. case "kcp":
  280. kcp, _ := stream["kcpSettings"].(map[string]interface{})
  281. header, _ := kcp["header"].(map[string]interface{})
  282. params["headerType"] = header["type"].(string)
  283. params["seed"] = kcp["seed"].(string)
  284. case "ws":
  285. ws, _ := stream["wsSettings"].(map[string]interface{})
  286. params["path"] = ws["path"].(string)
  287. headers, _ := ws["headers"].(map[string]interface{})
  288. params["host"] = searchHost(headers)
  289. case "http":
  290. http, _ := stream["httpSettings"].(map[string]interface{})
  291. params["path"] = http["path"].(string)
  292. params["host"] = searchHost(http)
  293. case "quic":
  294. quic, _ := stream["quicSettings"].(map[string]interface{})
  295. params["quicSecurity"] = quic["security"].(string)
  296. params["key"] = quic["key"].(string)
  297. header := quic["header"].(map[string]interface{})
  298. params["headerType"] = header["type"].(string)
  299. case "grpc":
  300. grpc, _ := stream["grpcSettings"].(map[string]interface{})
  301. params["serviceName"] = grpc["serviceName"].(string)
  302. if grpc["multiMode"].(bool) {
  303. params["mode"] = "multi"
  304. }
  305. }
  306. security, _ := stream["security"].(string)
  307. var domains []interface{}
  308. if security == "tls" {
  309. params["security"] = "tls"
  310. tlsSetting, _ := stream["tlsSettings"].(map[string]interface{})
  311. alpns, _ := tlsSetting["alpn"].([]interface{})
  312. var alpn []string
  313. for _, a := range alpns {
  314. alpn = append(alpn, a.(string))
  315. }
  316. if len(alpn) > 0 {
  317. params["alpn"] = strings.Join(alpn, ",")
  318. }
  319. tlsSettings, _ := searchKey(tlsSetting, "settings")
  320. if tlsSetting != nil {
  321. if sniValue, ok := searchKey(tlsSettings, "serverName"); ok {
  322. params["sni"], _ = sniValue.(string)
  323. }
  324. if fpValue, ok := searchKey(tlsSettings, "fingerprint"); ok {
  325. params["fp"], _ = fpValue.(string)
  326. }
  327. if insecure, ok := searchKey(tlsSettings, "allowInsecure"); ok {
  328. if insecure.(bool) {
  329. params["allowInsecure"] = "1"
  330. }
  331. }
  332. if domainSettings, ok := searchKey(tlsSettings, "domains"); ok {
  333. domains, _ = domainSettings.([]interface{})
  334. }
  335. }
  336. if streamNetwork == "tcp" && len(clients[clientIndex].Flow) > 0 {
  337. params["flow"] = clients[clientIndex].Flow
  338. }
  339. serverName, _ := tlsSetting["serverName"].(string)
  340. if serverName != "" {
  341. address = serverName
  342. }
  343. }
  344. if security == "reality" {
  345. params["security"] = "reality"
  346. realitySetting, _ := stream["realitySettings"].(map[string]interface{})
  347. realitySettings, _ := searchKey(realitySetting, "settings")
  348. if realitySetting != nil {
  349. if sniValue, ok := searchKey(realitySetting, "serverNames"); ok {
  350. sNames, _ := sniValue.([]interface{})
  351. params["sni"], _ = sNames[0].(string)
  352. }
  353. if pbkValue, ok := searchKey(realitySettings, "publicKey"); ok {
  354. params["pbk"], _ = pbkValue.(string)
  355. }
  356. if sidValue, ok := searchKey(realitySetting, "shortIds"); ok {
  357. shortIds, _ := sidValue.([]interface{})
  358. params["sid"], _ = shortIds[0].(string)
  359. }
  360. if fpValue, ok := searchKey(realitySettings, "fingerprint"); ok {
  361. if fp, ok := fpValue.(string); ok && len(fp) > 0 {
  362. params["fp"] = fp
  363. }
  364. }
  365. if spxValue, ok := searchKey(realitySettings, "spiderX"); ok {
  366. if spx, ok := spxValue.(string); ok && len(spx) > 0 {
  367. params["spx"] = spx
  368. }
  369. }
  370. if serverName, ok := searchKey(realitySettings, "serverName"); ok {
  371. if sname, ok := serverName.(string); ok && len(sname) > 0 {
  372. address = sname
  373. }
  374. }
  375. }
  376. if streamNetwork == "tcp" && len(clients[clientIndex].Flow) > 0 {
  377. params["flow"] = clients[clientIndex].Flow
  378. }
  379. }
  380. if security == "xtls" {
  381. params["security"] = "xtls"
  382. xtlsSetting, _ := stream["xtlsSettings"].(map[string]interface{})
  383. alpns, _ := xtlsSetting["alpn"].([]interface{})
  384. var alpn []string
  385. for _, a := range alpns {
  386. alpn = append(alpn, a.(string))
  387. }
  388. if len(alpn) > 0 {
  389. params["alpn"] = strings.Join(alpn, ",")
  390. }
  391. xtlsSettings, _ := searchKey(xtlsSetting, "settings")
  392. if xtlsSetting != nil {
  393. if fpValue, ok := searchKey(xtlsSettings, "fingerprint"); ok {
  394. params["fp"], _ = fpValue.(string)
  395. }
  396. if insecure, ok := searchKey(xtlsSettings, "allowInsecure"); ok {
  397. if insecure.(bool) {
  398. params["allowInsecure"] = "1"
  399. }
  400. }
  401. if sniValue, ok := searchKey(xtlsSettings, "serverName"); ok {
  402. params["sni"], _ = sniValue.(string)
  403. }
  404. }
  405. if streamNetwork == "tcp" && len(clients[clientIndex].Flow) > 0 {
  406. params["flow"] = clients[clientIndex].Flow
  407. }
  408. serverName, _ := xtlsSetting["serverName"].(string)
  409. if serverName != "" {
  410. address = serverName
  411. }
  412. }
  413. link := fmt.Sprintf("vless://%s@%s:%d", uuid, address, port)
  414. url, _ := url.Parse(link)
  415. q := url.Query()
  416. for k, v := range params {
  417. q.Add(k, v)
  418. }
  419. // Set the new query values on the URL
  420. url.RawQuery = q.Encode()
  421. remark := fmt.Sprintf("%s-%s", inbound.Remark, email)
  422. if len(domains) > 0 {
  423. links := ""
  424. for index, d := range domains {
  425. domain := d.(map[string]interface{})
  426. url.Fragment = remark + "-" + domain["remark"].(string)
  427. url.Host = fmt.Sprintf("%s:%d", domain["domain"].(string), port)
  428. if index > 0 {
  429. links += "\n"
  430. }
  431. links += url.String()
  432. }
  433. return links
  434. }
  435. url.Fragment = remark
  436. return url.String()
  437. }
  438. func (s *SubService) genTrojanLink(inbound *model.Inbound, email string) string {
  439. address := s.address
  440. if inbound.Protocol != model.Trojan {
  441. return ""
  442. }
  443. var stream map[string]interface{}
  444. json.Unmarshal([]byte(inbound.StreamSettings), &stream)
  445. clients, _ := s.inboundService.GetClients(inbound)
  446. clientIndex := -1
  447. for i, client := range clients {
  448. if client.Email == email {
  449. clientIndex = i
  450. break
  451. }
  452. }
  453. password := clients[clientIndex].Password
  454. port := inbound.Port
  455. streamNetwork := stream["network"].(string)
  456. params := make(map[string]string)
  457. params["type"] = streamNetwork
  458. switch streamNetwork {
  459. case "tcp":
  460. tcp, _ := stream["tcpSettings"].(map[string]interface{})
  461. header, _ := tcp["header"].(map[string]interface{})
  462. typeStr, _ := header["type"].(string)
  463. if typeStr == "http" {
  464. request := header["request"].(map[string]interface{})
  465. requestPath, _ := request["path"].([]interface{})
  466. params["path"] = requestPath[0].(string)
  467. headers, _ := request["headers"].(map[string]interface{})
  468. params["host"] = searchHost(headers)
  469. params["headerType"] = "http"
  470. }
  471. case "kcp":
  472. kcp, _ := stream["kcpSettings"].(map[string]interface{})
  473. header, _ := kcp["header"].(map[string]interface{})
  474. params["headerType"] = header["type"].(string)
  475. params["seed"] = kcp["seed"].(string)
  476. case "ws":
  477. ws, _ := stream["wsSettings"].(map[string]interface{})
  478. params["path"] = ws["path"].(string)
  479. headers, _ := ws["headers"].(map[string]interface{})
  480. params["host"] = searchHost(headers)
  481. case "http":
  482. http, _ := stream["httpSettings"].(map[string]interface{})
  483. params["path"] = http["path"].(string)
  484. params["host"] = searchHost(http)
  485. case "quic":
  486. quic, _ := stream["quicSettings"].(map[string]interface{})
  487. params["quicSecurity"] = quic["security"].(string)
  488. params["key"] = quic["key"].(string)
  489. header := quic["header"].(map[string]interface{})
  490. params["headerType"] = header["type"].(string)
  491. case "grpc":
  492. grpc, _ := stream["grpcSettings"].(map[string]interface{})
  493. params["serviceName"] = grpc["serviceName"].(string)
  494. if grpc["multiMode"].(bool) {
  495. params["mode"] = "multi"
  496. }
  497. }
  498. security, _ := stream["security"].(string)
  499. var domains []interface{}
  500. if security == "tls" {
  501. params["security"] = "tls"
  502. tlsSetting, _ := stream["tlsSettings"].(map[string]interface{})
  503. alpns, _ := tlsSetting["alpn"].([]interface{})
  504. var alpn []string
  505. for _, a := range alpns {
  506. alpn = append(alpn, a.(string))
  507. }
  508. if len(alpn) > 0 {
  509. params["alpn"] = strings.Join(alpn, ",")
  510. }
  511. tlsSettings, _ := searchKey(tlsSetting, "settings")
  512. if tlsSetting != nil {
  513. if sniValue, ok := searchKey(tlsSettings, "serverName"); ok {
  514. params["sni"], _ = sniValue.(string)
  515. }
  516. if fpValue, ok := searchKey(tlsSettings, "fingerprint"); ok {
  517. params["fp"], _ = fpValue.(string)
  518. }
  519. if insecure, ok := searchKey(tlsSettings, "allowInsecure"); ok {
  520. if insecure.(bool) {
  521. params["allowInsecure"] = "1"
  522. }
  523. }
  524. if domainSettings, ok := searchKey(tlsSettings, "domains"); ok {
  525. domains, _ = domainSettings.([]interface{})
  526. }
  527. }
  528. serverName, _ := tlsSetting["serverName"].(string)
  529. if serverName != "" {
  530. address = serverName
  531. }
  532. }
  533. if security == "reality" {
  534. params["security"] = "reality"
  535. realitySetting, _ := stream["realitySettings"].(map[string]interface{})
  536. realitySettings, _ := searchKey(realitySetting, "settings")
  537. if realitySetting != nil {
  538. if sniValue, ok := searchKey(realitySetting, "serverNames"); ok {
  539. sNames, _ := sniValue.([]interface{})
  540. params["sni"], _ = sNames[0].(string)
  541. }
  542. if pbkValue, ok := searchKey(realitySettings, "publicKey"); ok {
  543. params["pbk"], _ = pbkValue.(string)
  544. }
  545. if sidValue, ok := searchKey(realitySettings, "shortIds"); ok {
  546. shortIds, _ := sidValue.([]interface{})
  547. params["sid"], _ = shortIds[0].(string)
  548. }
  549. if fpValue, ok := searchKey(realitySettings, "fingerprint"); ok {
  550. if fp, ok := fpValue.(string); ok && len(fp) > 0 {
  551. params["fp"] = fp
  552. }
  553. }
  554. if spxValue, ok := searchKey(realitySettings, "spiderX"); ok {
  555. if spx, ok := spxValue.(string); ok && len(spx) > 0 {
  556. params["spx"] = spx
  557. }
  558. }
  559. if serverName, ok := searchKey(realitySettings, "serverName"); ok {
  560. if sname, ok := serverName.(string); ok && len(sname) > 0 {
  561. address = sname
  562. }
  563. }
  564. }
  565. if streamNetwork == "tcp" && len(clients[clientIndex].Flow) > 0 {
  566. params["flow"] = clients[clientIndex].Flow
  567. }
  568. }
  569. if security == "xtls" {
  570. params["security"] = "xtls"
  571. xtlsSetting, _ := stream["xtlsSettings"].(map[string]interface{})
  572. alpns, _ := xtlsSetting["alpn"].([]interface{})
  573. var alpn []string
  574. for _, a := range alpns {
  575. alpn = append(alpn, a.(string))
  576. }
  577. if len(alpn) > 0 {
  578. params["alpn"] = strings.Join(alpn, ",")
  579. }
  580. xtlsSettings, _ := searchKey(xtlsSetting, "settings")
  581. if xtlsSetting != nil {
  582. if fpValue, ok := searchKey(xtlsSettings, "fingerprint"); ok {
  583. params["fp"], _ = fpValue.(string)
  584. }
  585. if insecure, ok := searchKey(xtlsSettings, "allowInsecure"); ok {
  586. if insecure.(bool) {
  587. params["allowInsecure"] = "1"
  588. }
  589. }
  590. if sniValue, ok := searchKey(xtlsSettings, "serverName"); ok {
  591. params["sni"], _ = sniValue.(string)
  592. }
  593. }
  594. if streamNetwork == "tcp" && len(clients[clientIndex].Flow) > 0 {
  595. params["flow"] = clients[clientIndex].Flow
  596. }
  597. serverName, _ := xtlsSetting["serverName"].(string)
  598. if serverName != "" {
  599. address = serverName
  600. }
  601. }
  602. link := fmt.Sprintf("trojan://%s@%s:%d", password, address, port)
  603. url, _ := url.Parse(link)
  604. q := url.Query()
  605. for k, v := range params {
  606. q.Add(k, v)
  607. }
  608. // Set the new query values on the URL
  609. url.RawQuery = q.Encode()
  610. remark := fmt.Sprintf("%s-%s", inbound.Remark, email)
  611. if len(domains) > 0 {
  612. links := ""
  613. for index, d := range domains {
  614. domain := d.(map[string]interface{})
  615. url.Fragment = remark + "-" + domain["remark"].(string)
  616. url.Host = fmt.Sprintf("%s:%d", domain["domain"].(string), port)
  617. if index > 0 {
  618. links += "\n"
  619. }
  620. links += url.String()
  621. }
  622. return links
  623. }
  624. url.Fragment = remark
  625. return url.String()
  626. }
  627. func (s *SubService) genShadowsocksLink(inbound *model.Inbound, email string) string {
  628. address := s.address
  629. if inbound.Protocol != model.Shadowsocks {
  630. return ""
  631. }
  632. clients, _ := s.inboundService.GetClients(inbound)
  633. var settings map[string]interface{}
  634. json.Unmarshal([]byte(inbound.Settings), &settings)
  635. inboundPassword := settings["password"].(string)
  636. method := settings["method"].(string)
  637. clientIndex := -1
  638. for i, client := range clients {
  639. if client.Email == email {
  640. clientIndex = i
  641. break
  642. }
  643. }
  644. encPart := fmt.Sprintf("%s:%s:%s", method, inboundPassword, clients[clientIndex].Password)
  645. remark := fmt.Sprintf("%s-%s", inbound.Remark, clients[clientIndex].Email)
  646. return fmt.Sprintf("ss://%s@%s:%d#%s", base64.StdEncoding.EncodeToString([]byte(encPart)), address, inbound.Port, remark)
  647. }
  648. func searchKey(data interface{}, key string) (interface{}, bool) {
  649. switch val := data.(type) {
  650. case map[string]interface{}:
  651. for k, v := range val {
  652. if k == key {
  653. return v, true
  654. }
  655. if result, ok := searchKey(v, key); ok {
  656. return result, true
  657. }
  658. }
  659. case []interface{}:
  660. for _, v := range val {
  661. if result, ok := searchKey(v, key); ok {
  662. return result, true
  663. }
  664. }
  665. }
  666. return nil, false
  667. }
  668. func searchHost(headers interface{}) string {
  669. data, _ := headers.(map[string]interface{})
  670. for k, v := range data {
  671. if strings.EqualFold(k, "host") {
  672. switch v.(type) {
  673. case []interface{}:
  674. hosts, _ := v.([]interface{})
  675. if len(hosts) > 0 {
  676. return hosts[0].(string)
  677. } else {
  678. return ""
  679. }
  680. case interface{}:
  681. return v.(string)
  682. }
  683. }
  684. }
  685. return ""
  686. }