check_all_proxy.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io/ioutil"
  6. "net/http"
  7. "sync"
  8. )
  9. type ProxiesResponse struct {
  10. Proxies map[string]interface{} `json:"proxies"`
  11. }
  12. func check(url string, wg *sync.WaitGroup) {
  13. defer wg.Done()
  14. for retry := 0; retry < 3; retry++ {
  15. resp, err := http.Get(url)
  16. if err != nil {
  17. fmt.Printf("Error: %s - %s\n", url, err)
  18. continue
  19. }
  20. defer resp.Body.Close()
  21. if resp.StatusCode == http.StatusOK {
  22. fmt.Printf("Success: %s\n", url)
  23. return
  24. } else {
  25. fmt.Printf("Failed: %s (Status: %d)\n", url, resp.StatusCode)
  26. }
  27. }
  28. }
  29. func prepareCheck(nodeURL string, ports []string, proxyNameList []string, wg *sync.WaitGroup) {
  30. for _, port := range ports {
  31. proxyURL := fmt.Sprintf("%s:%s", nodeURL, port)
  32. for _, proxy := range proxyNameList {
  33. url := fmt.Sprintf("%s/api/proxies/%s/delay?timeout=5000&url=http://www.gstatic.com/generate_204", proxyURL, proxy)
  34. wg.Add(1)
  35. go check(url, wg)
  36. }
  37. }
  38. }
  39. func loadNodesDetails(nodes map[string][]string) {
  40. var wg sync.WaitGroup
  41. for nodeURL, ports := range nodes {
  42. clashToolURL := fmt.Sprintf("%s:%s", nodeURL, ports[0])
  43. url := fmt.Sprintf("%s/api/proxies", clashToolURL)
  44. resp, err := http.Get(url)
  45. if err != nil {
  46. fmt.Printf("Error: %s - %s\n", url, err)
  47. continue
  48. }
  49. defer resp.Body.Close()
  50. if resp.StatusCode != http.StatusOK {
  51. fmt.Printf("Failed to load proxies from %s (Status: %d)\n", url, resp.StatusCode)
  52. continue
  53. }
  54. body, err := ioutil.ReadAll(resp.Body)
  55. if err != nil {
  56. fmt.Printf("Error reading response body: %s\n", err)
  57. continue
  58. }
  59. var proxies ProxiesResponse
  60. if err := json.Unmarshal(body, &proxies); err != nil {
  61. fmt.Printf("Error unmarshalling JSON: %s\n", err)
  62. continue
  63. }
  64. proxyNameList := make([]string, 0, len(proxies.Proxies))
  65. for key := range proxies.Proxies {
  66. proxyNameList = append(proxyNameList, key)
  67. }
  68. prepareCheck(nodeURL, ports, proxyNameList, &wg)
  69. }
  70. wg.Wait()
  71. }
  72. func main() {
  73. nodes := map[string][]string{
  74. "http://192.168.31.194": {"58001", "58002", "58003", "58004", "58005", "58006", "58007", "58008", "58009", "58010"},
  75. "http://192.168.31.201": {"32001", "32002", "32003", "32004", "32005", "32006", "32007", "32008", "32009", "32010", "32011", "32012"},
  76. }
  77. loadNodesDetails(nodes)
  78. fmt.Println("All done!")
  79. }