1
0

subService.go 20 KB

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