1
0

sub.go 19 KB

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