sub.go 18 KB

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