sub.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639
  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 serverName, ok := searchKey(realitySettings, "serverName"); ok {
  314. if sname, ok := serverName.(string); ok && len(sname) > 0 {
  315. address = sname
  316. }
  317. }
  318. }
  319. if streamNetwork == "tcp" && len(clients[clientIndex].Flow) > 0 {
  320. params["flow"] = clients[clientIndex].Flow
  321. }
  322. }
  323. if security == "xtls" {
  324. params["security"] = "xtls"
  325. xtlsSetting, _ := stream["xtlsSettings"].(map[string]interface{})
  326. alpns, _ := xtlsSetting["alpn"].([]interface{})
  327. var alpn []string
  328. for _, a := range alpns {
  329. alpn = append(alpn, a.(string))
  330. }
  331. if len(alpn) > 0 {
  332. params["alpn"] = strings.Join(alpn, ",")
  333. }
  334. xtlsSettings, _ := searchKey(xtlsSetting, "settings")
  335. if xtlsSetting != nil {
  336. if fpValue, ok := searchKey(xtlsSettings, "fingerprint"); ok {
  337. params["fp"], _ = fpValue.(string)
  338. }
  339. if insecure, ok := searchKey(xtlsSettings, "allowInsecure"); ok {
  340. if insecure.(bool) {
  341. params["allowInsecure"] = "1"
  342. }
  343. }
  344. if sniValue, ok := searchKey(xtlsSettings, "serverName"); ok {
  345. params["sni"], _ = sniValue.(string)
  346. }
  347. }
  348. if streamNetwork == "tcp" && len(clients[clientIndex].Flow) > 0 {
  349. params["flow"] = clients[clientIndex].Flow
  350. }
  351. serverName, _ := xtlsSetting["serverName"].(string)
  352. if serverName != "" {
  353. address = serverName
  354. }
  355. }
  356. link := fmt.Sprintf("vless://%s@%s:%d", uuid, address, port)
  357. url, _ := url.Parse(link)
  358. q := url.Query()
  359. for k, v := range params {
  360. q.Add(k, v)
  361. }
  362. // Set the new query values on the URL
  363. url.RawQuery = q.Encode()
  364. remark := fmt.Sprintf("%s-%s", inbound.Remark, email)
  365. url.Fragment = remark
  366. return url.String()
  367. }
  368. func (s *SubService) genTrojanLink(inbound *model.Inbound, email string) string {
  369. address := s.address
  370. if inbound.Protocol != model.Trojan {
  371. return ""
  372. }
  373. var stream map[string]interface{}
  374. json.Unmarshal([]byte(inbound.StreamSettings), &stream)
  375. clients, _ := s.inboundService.getClients(inbound)
  376. clientIndex := -1
  377. for i, client := range clients {
  378. if client.Email == email {
  379. clientIndex = i
  380. break
  381. }
  382. }
  383. password := clients[clientIndex].Password
  384. port := inbound.Port
  385. streamNetwork := stream["network"].(string)
  386. params := make(map[string]string)
  387. params["type"] = streamNetwork
  388. switch streamNetwork {
  389. case "tcp":
  390. tcp, _ := stream["tcpSettings"].(map[string]interface{})
  391. header, _ := tcp["header"].(map[string]interface{})
  392. typeStr, _ := header["type"].(string)
  393. if typeStr == "http" {
  394. request := header["request"].(map[string]interface{})
  395. requestPath, _ := request["path"].([]interface{})
  396. params["path"] = requestPath[0].(string)
  397. headers, _ := request["headers"].(map[string]interface{})
  398. params["host"] = searchHost(headers)
  399. params["headerType"] = "http"
  400. }
  401. case "kcp":
  402. kcp, _ := stream["kcpSettings"].(map[string]interface{})
  403. header, _ := kcp["header"].(map[string]interface{})
  404. params["headerType"] = header["type"].(string)
  405. params["seed"] = kcp["seed"].(string)
  406. case "ws":
  407. ws, _ := stream["wsSettings"].(map[string]interface{})
  408. params["path"] = ws["path"].(string)
  409. headers, _ := ws["headers"].(map[string]interface{})
  410. params["host"] = searchHost(headers)
  411. case "http":
  412. http, _ := stream["httpSettings"].(map[string]interface{})
  413. params["path"] = http["path"].(string)
  414. params["host"] = searchHost(http)
  415. case "quic":
  416. quic, _ := stream["quicSettings"].(map[string]interface{})
  417. params["quicSecurity"] = quic["security"].(string)
  418. params["key"] = quic["key"].(string)
  419. header := quic["header"].(map[string]interface{})
  420. params["headerType"] = header["type"].(string)
  421. case "grpc":
  422. grpc, _ := stream["grpcSettings"].(map[string]interface{})
  423. params["serviceName"] = grpc["serviceName"].(string)
  424. if grpc["multiMode"].(bool) {
  425. params["mode"] = "multi"
  426. }
  427. }
  428. security, _ := stream["security"].(string)
  429. if security == "tls" {
  430. params["security"] = "tls"
  431. tlsSetting, _ := stream["tlsSettings"].(map[string]interface{})
  432. alpns, _ := tlsSetting["alpn"].([]interface{})
  433. var alpn []string
  434. for _, a := range alpns {
  435. alpn = append(alpn, a.(string))
  436. }
  437. if len(alpn) > 0 {
  438. params["alpn"] = strings.Join(alpn, ",")
  439. }
  440. tlsSettings, _ := searchKey(tlsSetting, "settings")
  441. if tlsSetting != nil {
  442. if sniValue, ok := searchKey(tlsSettings, "serverName"); ok {
  443. params["sni"], _ = sniValue.(string)
  444. }
  445. if fpValue, ok := searchKey(tlsSettings, "fingerprint"); ok {
  446. params["fp"], _ = fpValue.(string)
  447. }
  448. if insecure, ok := searchKey(tlsSettings, "allowInsecure"); ok {
  449. if insecure.(bool) {
  450. params["allowInsecure"] = "1"
  451. }
  452. }
  453. }
  454. serverName, _ := tlsSetting["serverName"].(string)
  455. if serverName != "" {
  456. address = serverName
  457. }
  458. }
  459. if security == "reality" {
  460. params["security"] = "reality"
  461. realitySetting, _ := stream["realitySettings"].(map[string]interface{})
  462. realitySettings, _ := searchKey(realitySetting, "settings")
  463. if realitySetting != nil {
  464. if sniValue, ok := searchKey(realitySetting, "serverNames"); ok {
  465. sNames, _ := sniValue.([]interface{})
  466. params["sni"], _ = sNames[0].(string)
  467. }
  468. if pbkValue, ok := searchKey(realitySettings, "publicKey"); ok {
  469. params["pbk"], _ = pbkValue.(string)
  470. }
  471. if sidValue, ok := searchKey(realitySettings, "shortIds"); ok {
  472. shortIds, _ := sidValue.([]interface{})
  473. params["sid"], _ = shortIds[0].(string)
  474. }
  475. if fpValue, ok := searchKey(realitySettings, "fingerprint"); ok {
  476. if fp, ok := fpValue.(string); ok && len(fp) > 0 {
  477. params["fp"] = fp
  478. }
  479. }
  480. if serverName, ok := searchKey(realitySettings, "serverName"); ok {
  481. if sname, ok := serverName.(string); ok && len(sname) > 0 {
  482. address = sname
  483. }
  484. }
  485. }
  486. if streamNetwork == "tcp" && len(clients[clientIndex].Flow) > 0 {
  487. params["flow"] = clients[clientIndex].Flow
  488. }
  489. }
  490. if security == "xtls" {
  491. params["security"] = "xtls"
  492. xtlsSetting, _ := stream["xtlsSettings"].(map[string]interface{})
  493. alpns, _ := xtlsSetting["alpn"].([]interface{})
  494. var alpn []string
  495. for _, a := range alpns {
  496. alpn = append(alpn, a.(string))
  497. }
  498. if len(alpn) > 0 {
  499. params["alpn"] = strings.Join(alpn, ",")
  500. }
  501. xtlsSettings, _ := searchKey(xtlsSetting, "settings")
  502. if xtlsSetting != nil {
  503. if fpValue, ok := searchKey(xtlsSettings, "fingerprint"); ok {
  504. params["fp"], _ = fpValue.(string)
  505. }
  506. if insecure, ok := searchKey(xtlsSettings, "allowInsecure"); ok {
  507. if insecure.(bool) {
  508. params["allowInsecure"] = "1"
  509. }
  510. }
  511. if sniValue, ok := searchKey(xtlsSettings, "serverName"); ok {
  512. params["sni"], _ = sniValue.(string)
  513. }
  514. }
  515. if streamNetwork == "tcp" && len(clients[clientIndex].Flow) > 0 {
  516. params["flow"] = clients[clientIndex].Flow
  517. }
  518. serverName, _ := xtlsSetting["serverName"].(string)
  519. if serverName != "" {
  520. address = serverName
  521. }
  522. }
  523. link := fmt.Sprintf("trojan://%s@%s:%d", password, address, port)
  524. url, _ := url.Parse(link)
  525. q := url.Query()
  526. for k, v := range params {
  527. q.Add(k, v)
  528. }
  529. // Set the new query values on the URL
  530. url.RawQuery = q.Encode()
  531. remark := fmt.Sprintf("%s-%s", inbound.Remark, email)
  532. url.Fragment = remark
  533. return url.String()
  534. }
  535. func (s *SubService) genShadowsocksLink(inbound *model.Inbound, email string) string {
  536. address := s.address
  537. if inbound.Protocol != model.Shadowsocks {
  538. return ""
  539. }
  540. clients, _ := s.inboundService.getClients(inbound)
  541. var settings map[string]interface{}
  542. json.Unmarshal([]byte(inbound.Settings), &settings)
  543. inboundPassword := settings["password"].(string)
  544. method := settings["method"].(string)
  545. clientIndex := -1
  546. for i, client := range clients {
  547. if client.Email == email {
  548. clientIndex = i
  549. break
  550. }
  551. }
  552. encPart := fmt.Sprintf("%s:%s:%s", method, inboundPassword, clients[clientIndex].Password)
  553. return fmt.Sprintf("ss://%s@%s:%d#%s", base64.StdEncoding.EncodeToString([]byte(encPart)), address, inbound.Port, clients[clientIndex].Email)
  554. }
  555. func searchKey(data interface{}, key string) (interface{}, bool) {
  556. switch val := data.(type) {
  557. case map[string]interface{}:
  558. for k, v := range val {
  559. if k == key {
  560. return v, true
  561. }
  562. if result, ok := searchKey(v, key); ok {
  563. return result, true
  564. }
  565. }
  566. case []interface{}:
  567. for _, v := range val {
  568. if result, ok := searchKey(v, key); ok {
  569. return result, true
  570. }
  571. }
  572. }
  573. return nil, false
  574. }
  575. func searchHost(headers interface{}) string {
  576. data, _ := headers.(map[string]interface{})
  577. for k, v := range data {
  578. if strings.EqualFold(k, "host") {
  579. switch v.(type) {
  580. case []interface{}:
  581. hosts, _ := v.([]interface{})
  582. if len(hosts) > 0 {
  583. return hosts[0].(string)
  584. } else {
  585. return ""
  586. }
  587. case interface{}:
  588. return v.(string)
  589. }
  590. }
  591. }
  592. return ""
  593. }