1
0

subService.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744
  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. if security != "tls" && security != "reality" && security != "xtls" {
  413. params["security"] = "none"
  414. }
  415. link := fmt.Sprintf("vless://%s@%s:%d", uuid, address, port)
  416. url, _ := url.Parse(link)
  417. q := url.Query()
  418. for k, v := range params {
  419. q.Add(k, v)
  420. }
  421. // Set the new query values on the URL
  422. url.RawQuery = q.Encode()
  423. remark := fmt.Sprintf("%s-%s", inbound.Remark, email)
  424. if len(domains) > 0 {
  425. links := ""
  426. for index, d := range domains {
  427. domain := d.(map[string]interface{})
  428. url.Fragment = remark + "-" + domain["remark"].(string)
  429. url.Host = fmt.Sprintf("%s:%d", domain["domain"].(string), port)
  430. if index > 0 {
  431. links += "\n"
  432. }
  433. links += url.String()
  434. }
  435. return links
  436. }
  437. url.Fragment = remark
  438. return url.String()
  439. }
  440. func (s *SubService) genTrojanLink(inbound *model.Inbound, email string) string {
  441. address := s.address
  442. if inbound.Protocol != model.Trojan {
  443. return ""
  444. }
  445. var stream map[string]interface{}
  446. json.Unmarshal([]byte(inbound.StreamSettings), &stream)
  447. clients, _ := s.inboundService.GetClients(inbound)
  448. clientIndex := -1
  449. for i, client := range clients {
  450. if client.Email == email {
  451. clientIndex = i
  452. break
  453. }
  454. }
  455. password := clients[clientIndex].Password
  456. port := inbound.Port
  457. streamNetwork := stream["network"].(string)
  458. params := make(map[string]string)
  459. params["type"] = streamNetwork
  460. switch streamNetwork {
  461. case "tcp":
  462. tcp, _ := stream["tcpSettings"].(map[string]interface{})
  463. header, _ := tcp["header"].(map[string]interface{})
  464. typeStr, _ := header["type"].(string)
  465. if typeStr == "http" {
  466. request := header["request"].(map[string]interface{})
  467. requestPath, _ := request["path"].([]interface{})
  468. params["path"] = requestPath[0].(string)
  469. headers, _ := request["headers"].(map[string]interface{})
  470. params["host"] = searchHost(headers)
  471. params["headerType"] = "http"
  472. }
  473. case "kcp":
  474. kcp, _ := stream["kcpSettings"].(map[string]interface{})
  475. header, _ := kcp["header"].(map[string]interface{})
  476. params["headerType"] = header["type"].(string)
  477. params["seed"] = kcp["seed"].(string)
  478. case "ws":
  479. ws, _ := stream["wsSettings"].(map[string]interface{})
  480. params["path"] = ws["path"].(string)
  481. headers, _ := ws["headers"].(map[string]interface{})
  482. params["host"] = searchHost(headers)
  483. case "http":
  484. http, _ := stream["httpSettings"].(map[string]interface{})
  485. params["path"] = http["path"].(string)
  486. params["host"] = searchHost(http)
  487. case "quic":
  488. quic, _ := stream["quicSettings"].(map[string]interface{})
  489. params["quicSecurity"] = quic["security"].(string)
  490. params["key"] = quic["key"].(string)
  491. header := quic["header"].(map[string]interface{})
  492. params["headerType"] = header["type"].(string)
  493. case "grpc":
  494. grpc, _ := stream["grpcSettings"].(map[string]interface{})
  495. params["serviceName"] = grpc["serviceName"].(string)
  496. if grpc["multiMode"].(bool) {
  497. params["mode"] = "multi"
  498. }
  499. }
  500. security, _ := stream["security"].(string)
  501. var domains []interface{}
  502. if security == "tls" {
  503. params["security"] = "tls"
  504. tlsSetting, _ := stream["tlsSettings"].(map[string]interface{})
  505. alpns, _ := tlsSetting["alpn"].([]interface{})
  506. var alpn []string
  507. for _, a := range alpns {
  508. alpn = append(alpn, a.(string))
  509. }
  510. if len(alpn) > 0 {
  511. params["alpn"] = strings.Join(alpn, ",")
  512. }
  513. tlsSettings, _ := searchKey(tlsSetting, "settings")
  514. if tlsSetting != nil {
  515. if sniValue, ok := searchKey(tlsSettings, "serverName"); ok {
  516. params["sni"], _ = sniValue.(string)
  517. }
  518. if fpValue, ok := searchKey(tlsSettings, "fingerprint"); ok {
  519. params["fp"], _ = fpValue.(string)
  520. }
  521. if insecure, ok := searchKey(tlsSettings, "allowInsecure"); ok {
  522. if insecure.(bool) {
  523. params["allowInsecure"] = "1"
  524. }
  525. }
  526. if domainSettings, ok := searchKey(tlsSettings, "domains"); ok {
  527. domains, _ = domainSettings.([]interface{})
  528. }
  529. }
  530. serverName, _ := tlsSetting["serverName"].(string)
  531. if serverName != "" {
  532. address = serverName
  533. }
  534. }
  535. if security == "reality" {
  536. params["security"] = "reality"
  537. realitySetting, _ := stream["realitySettings"].(map[string]interface{})
  538. realitySettings, _ := searchKey(realitySetting, "settings")
  539. if realitySetting != nil {
  540. if sniValue, ok := searchKey(realitySetting, "serverNames"); ok {
  541. sNames, _ := sniValue.([]interface{})
  542. params["sni"], _ = sNames[0].(string)
  543. }
  544. if pbkValue, ok := searchKey(realitySettings, "publicKey"); ok {
  545. params["pbk"], _ = pbkValue.(string)
  546. }
  547. if sidValue, ok := searchKey(realitySetting, "shortIds"); ok {
  548. shortIds, _ := sidValue.([]interface{})
  549. params["sid"], _ = shortIds[0].(string)
  550. }
  551. if fpValue, ok := searchKey(realitySettings, "fingerprint"); ok {
  552. if fp, ok := fpValue.(string); ok && len(fp) > 0 {
  553. params["fp"] = fp
  554. }
  555. }
  556. if spxValue, ok := searchKey(realitySettings, "spiderX"); ok {
  557. if spx, ok := spxValue.(string); ok && len(spx) > 0 {
  558. params["spx"] = spx
  559. }
  560. }
  561. if serverName, ok := searchKey(realitySettings, "serverName"); ok {
  562. if sname, ok := serverName.(string); ok && len(sname) > 0 {
  563. address = sname
  564. }
  565. }
  566. }
  567. if streamNetwork == "tcp" && len(clients[clientIndex].Flow) > 0 {
  568. params["flow"] = clients[clientIndex].Flow
  569. }
  570. }
  571. if security == "xtls" {
  572. params["security"] = "xtls"
  573. xtlsSetting, _ := stream["xtlsSettings"].(map[string]interface{})
  574. alpns, _ := xtlsSetting["alpn"].([]interface{})
  575. var alpn []string
  576. for _, a := range alpns {
  577. alpn = append(alpn, a.(string))
  578. }
  579. if len(alpn) > 0 {
  580. params["alpn"] = strings.Join(alpn, ",")
  581. }
  582. xtlsSettings, _ := searchKey(xtlsSetting, "settings")
  583. if xtlsSetting != nil {
  584. if fpValue, ok := searchKey(xtlsSettings, "fingerprint"); ok {
  585. params["fp"], _ = fpValue.(string)
  586. }
  587. if insecure, ok := searchKey(xtlsSettings, "allowInsecure"); ok {
  588. if insecure.(bool) {
  589. params["allowInsecure"] = "1"
  590. }
  591. }
  592. if sniValue, ok := searchKey(xtlsSettings, "serverName"); ok {
  593. params["sni"], _ = sniValue.(string)
  594. }
  595. }
  596. if streamNetwork == "tcp" && len(clients[clientIndex].Flow) > 0 {
  597. params["flow"] = clients[clientIndex].Flow
  598. }
  599. serverName, _ := xtlsSetting["serverName"].(string)
  600. if serverName != "" {
  601. address = serverName
  602. }
  603. }
  604. if security != "tls" && security != "reality" && security != "xtls" {
  605. params["security"] = "none"
  606. }
  607. link := fmt.Sprintf("trojan://%s@%s:%d", password, address, port)
  608. url, _ := url.Parse(link)
  609. q := url.Query()
  610. for k, v := range params {
  611. q.Add(k, v)
  612. }
  613. // Set the new query values on the URL
  614. url.RawQuery = q.Encode()
  615. remark := fmt.Sprintf("%s-%s", inbound.Remark, email)
  616. if len(domains) > 0 {
  617. links := ""
  618. for index, d := range domains {
  619. domain := d.(map[string]interface{})
  620. url.Fragment = remark + "-" + domain["remark"].(string)
  621. url.Host = fmt.Sprintf("%s:%d", domain["domain"].(string), port)
  622. if index > 0 {
  623. links += "\n"
  624. }
  625. links += url.String()
  626. }
  627. return links
  628. }
  629. url.Fragment = remark
  630. return url.String()
  631. }
  632. func (s *SubService) genShadowsocksLink(inbound *model.Inbound, email string) string {
  633. address := s.address
  634. if inbound.Protocol != model.Shadowsocks {
  635. return ""
  636. }
  637. clients, _ := s.inboundService.GetClients(inbound)
  638. var settings map[string]interface{}
  639. json.Unmarshal([]byte(inbound.Settings), &settings)
  640. inboundPassword := settings["password"].(string)
  641. method := settings["method"].(string)
  642. clientIndex := -1
  643. for i, client := range clients {
  644. if client.Email == email {
  645. clientIndex = i
  646. break
  647. }
  648. }
  649. encPart := fmt.Sprintf("%s:%s:%s", method, inboundPassword, clients[clientIndex].Password)
  650. remark := fmt.Sprintf("%s-%s", inbound.Remark, clients[clientIndex].Email)
  651. return fmt.Sprintf("ss://%s@%s:%d#%s", base64.StdEncoding.EncodeToString([]byte(encPart)), address, inbound.Port, remark)
  652. }
  653. func searchKey(data interface{}, key string) (interface{}, bool) {
  654. switch val := data.(type) {
  655. case map[string]interface{}:
  656. for k, v := range val {
  657. if k == key {
  658. return v, true
  659. }
  660. if result, ok := searchKey(v, key); ok {
  661. return result, true
  662. }
  663. }
  664. case []interface{}:
  665. for _, v := range val {
  666. if result, ok := searchKey(v, key); ok {
  667. return result, true
  668. }
  669. }
  670. }
  671. return nil, false
  672. }
  673. func searchHost(headers interface{}) string {
  674. data, _ := headers.(map[string]interface{})
  675. for k, v := range data {
  676. if strings.EqualFold(k, "host") {
  677. switch v.(type) {
  678. case []interface{}:
  679. hosts, _ := v.([]interface{})
  680. if len(hosts) > 0 {
  681. return hosts[0].(string)
  682. } else {
  683. return ""
  684. }
  685. case interface{}:
  686. return v.(string)
  687. }
  688. }
  689. }
  690. return ""
  691. }