()
| 10 | ) |
| 11 | |
| 12 | func InitProxy() { |
| 13 | var proxies []string |
| 14 | proxyUrl := os.Getenv("PROXY_URL") |
| 15 | if proxyUrl != "" { |
| 16 | proxies = append(proxies, proxyUrl) |
| 17 | } |
| 18 | |
| 19 | if _, err := os.Stat("proxies.txt"); err == nil { |
| 20 | file, _ := os.Open("proxies.txt") |
| 21 | defer file.Close() |
| 22 | scanner := bufio.NewScanner(file) |
| 23 | for scanner.Scan() { |
| 24 | proxy := scanner.Text() |
| 25 | parsedURL, err := url.Parse(proxy) |
| 26 | if err != nil { |
| 27 | slog.Warn("proxy url is invalid", "url", proxy, "err", err) |
| 28 | continue |
| 29 | } |
| 30 | // 如果缺少端口信息,不是完整的代理链接 |
| 31 | if parsedURL.Port() != "" { |
| 32 | proxies = append(proxies, proxy) |
| 33 | } else { |
| 34 | continue |
| 35 | } |
| 36 | } |
| 37 | } |
| 38 | if len(proxies) == 0 { |
| 39 | proxy := os.Getenv("http_proxy") |
| 40 | if proxy != "" { |
| 41 | proxies = append(proxies, proxy) |
| 42 | } |
| 43 | } |
| 44 | global.ProxyPool = proxypool.NewIProxyIP(proxies) |
| 45 | } |
no test coverage detected