| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- package main
- import (
- "encoding/json"
- "fmt"
- "net/http"
- "net/url"
- "strings"
- "sync"
- "time"
- )
- type ProxiesResponse struct {
- Proxies map[string]interface{} `json:"proxies"`
- }
- var nodes = [][][]string{
- {
- {"192.168.31.194", "58001", "58002", "58003", "58004", "58005", "58006", "58007", "58008", "58009", "58010"},
- },
- {
- {"192.168.31.201", "32001", "32002", "32003", "32004", "32005", "32006", "32007", "32008", "32009", "32010", "32011", "32012"},
- },
- {
- {"127.0.0.1", "17888"},
- },
- }
- var selectedNodes = nodes[0] // 默认选择第一个节点
- func checkProxy(proxyURL, chooseProxy string) string {
- // 确保 proxyURL 包含协议
- if !strings.HasPrefix(proxyURL, "http://") && !strings.HasPrefix(proxyURL, "https://") {
- proxyURL = "http://" + proxyURL // 默认使用 http 协议
- }
- // 对 chooseProxy 进行 URL 编码
- encodeProxyName := url.QueryEscape(chooseProxy)
- // 构建完整的请求 URL
- requestURL := fmt.Sprintf("%s/api/proxies/%s/delay?timeout=5000&url=http://www.gstatic.com/generate_204", proxyURL, encodeProxyName)
- // 创建 HTTP 客户端
- client := &http.Client{
- Timeout: 6 * time.Second, // 设置超时时间
- }
- // 发送 HTTP GET 请求
- resp, err := client.Get(requestURL)
- if err != nil {
- return fmt.Sprintf("Error: %v", err)
- }
- defer resp.Body.Close()
- // 检查 HTTP 状态码
- if resp.StatusCode != http.StatusOK {
- return fmt.Sprintf("HTTP Error: %s", resp.Status)
- }
- // 解析 JSON 响应
- var result map[string]interface{}
- if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
- return fmt.Sprintf("Error parsing JSON: %v", err)
- }
- // 检查 meanDelay 字段是否存在
- meanDelay, ok := result["meanDelay"]
- if !ok {
- return "meanDelay field not found in JSON"
- }
- // 返回 meanDelay 的值
- return fmt.Sprintf("meanDelay: %v", meanDelay)
- }
- func checkNowProxy(ip, port string, wg *sync.WaitGroup) {
- defer wg.Done()
- urlAndPort := fmt.Sprintf("%s:%s", ip, port)
- url := fmt.Sprintf("http://%s/api/proxies", urlAndPort)
- headers := map[string]string{
- "Accept": "application/json, text/plain, */*",
- "Accept-Encoding": "gzip, deflate, br, zstd",
- "Accept-Language": "zh-CN,zh;q=0.8",
- "Connection": "keep-alive",
- "Host": urlAndPort,
- "Referer": fmt.Sprintf("http://%s/", urlAndPort),
- "Sec-CH-UA": `"Chromium";v="134", "Not:A-Brand";v="24", "Brave";v="134"`,
- "Sec-CH-UA-Mobile": "?0",
- "Sec-CH-UA-Platform": `"macOS"`,
- "Sec-Fetch-Dest": "empty",
- "Sec-Fetch-Mode": "cors",
- "Sec-Fetch-Site": "same-origin",
- "Sec-GPC": "1",
- "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36",
- }
- req, err := http.NewRequest("GET", url, nil)
- if err != nil {
- fmt.Printf("Error creating request: %v\n", err)
- return
- }
- for key, value := range headers {
- req.Header.Set(key, value)
- }
- client := &http.Client{}
- resp, err := client.Do(req)
- if err != nil {
- fmt.Printf("Request failed: %v\n", err)
- return
- }
- defer resp.Body.Close()
- if resp.StatusCode != http.StatusOK {
- fmt.Printf("Failed to load proxies from %s (Status: %d)\n", url, resp.StatusCode)
- return
- }
- var jsonData ProxiesResponse
- if err := json.NewDecoder(resp.Body).Decode(&jsonData); err != nil {
- fmt.Printf("Error decoding JSON: %v\n", err)
- return
- }
- proxies := jsonData.Proxies
- proxyGlobal, ok := proxies["GLOBAL"].(map[string]interface{})
- if !ok {
- fmt.Println("Error: GLOBAL proxy not found")
- return
- }
- nowProxy := proxyGlobal["now"].(string)
- checkResult := checkProxy(urlAndPort, nowProxy)
- message := fmt.Sprintf("%s --- %s --- %s", urlAndPort, nowProxy, checkResult)
- fmt.Println(message)
- }
- func main() {
- var wg sync.WaitGroup
- ip := selectedNodes[0][0]
- for _, port := range selectedNodes[0][1:] {
- wg.Add(1)
- go checkNowProxy(ip, port, &wg)
- }
- wg.Wait()
- }
|