发布时间:2024-12-22 23:26:27
下面是一个示例代码:
```go package main import ( "fmt" "io/ioutil" "net/http" "strings" ) func getIPList(url string) ([]string, error) { resp, err := http.Get(url) if err != nil { return nil, err } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) if err != nil { return nil, err } ipList := strings.Split(string(body), "\n") return ipList, nil } func main() { url := "https://example.com/ip-list.txt" ipList, err := getIPList(url) if err != nil { fmt.Printf("Failed to get IP list: %v\n", err) return } fmt.Println("IP List:") for _, ip := range ipList { fmt.Println(ip) } } ```以下是一个简单的示例代码:
```go package main import ( "fmt" "net/http" "time" ) func checkIP(ip string) bool { targetURL := "https://www.example.com" client := &http.Client{ Timeout: 10 * time.Second, } req, err := http.NewRequest("GET", targetURL, nil) if err != nil { return false } req.Header.Set("X-Forwarded-For", ip) resp, err := client.Do(req) if err != nil { return false } defer resp.Body.Close() if resp.StatusCode == http.StatusOK { return true } return false } func main() { ip := "1.2.3.4" if checkIP(ip) { fmt.Printf("IP %s is available\n", ip) } else { fmt.Printf("IP %s is not available\n", ip) } } ```以下是一个简单的示例代码:
```go package main import ( "fmt" "net/http" "sync" "time" ) var ( ipList []string currentIP int currentIPLock sync.Mutex ) func getValidIP() string { currentIPLock.Lock() defer currentIPLock.Unlock() if currentIP >= len(ipList) { currentIP = 0 } ip := ipList[currentIP] currentIP++ return ip } func doRequest(url string) { client := &http.Client{ Timeout: 10 * time.Second, } req, err := http.NewRequest("GET", url, nil) if err != nil { fmt.Printf("Failed to create request: %v\n", err) return } ip := getValidIP() req.Header.Set("X-Forwarded-For", ip) resp, err := client.Do(req) if err != nil { fmt.Printf("Failed to send request: %v\n", err) return } defer resp.Body.Close() // 处理响应 } func main() { url := "https://www.example.com" for i := 0; i < 10; i++ { doRequest(url) } } ```