Multiple simultaneous requests from multiple SOCKS5 proxy servers

Viewed 28

I have 4 SOCKS5 proxies in the format ip:port and for each I created 4 clients. I'm trying to send multiple requests using these clients, but after a while I get

Only one usage of each socket address (protocol/network address/port) is normally permitted.

What could be the reason for this behavior and how can I solve the problem? Maybe I need to use a different approach?

enter image description here

func getClients(proxies []string) []*http.Client {
var clients []*http.Client
auth := proxy.Auth{User: user, Password: pass}

for _, ip := range proxies {
    dialer, err := proxy.SOCKS5("tcp", ip, &auth, proxy.Direct)
    if err != nil {
        fmt.Println("Can't connect to the proxy:", err)
    }
    dialContext := func(ctx context.Context, network, address string) (net.Conn, error) {
        return dialer.Dial(network, address)
    }
    transport := &http.Transport{
        DialContext:           dialContext,
        MaxIdleConns:          10,
        IdleConnTimeout:       60 * time.Second,
        TLSHandshakeTimeout:   10 * time.Second,
        ExpectContinueTimeout: 1 * time.Second,
        MaxIdleConnsPerHost:   10}

    client := &http.Client{
        Transport: transport,
        Timeout:   2 * time.Second,
    }
    clients = append(clients, client)
}
return clients

}

func getIp(client http.Client, wg *sync.WaitGroup) {
defer wg.Done()
req, err := http.NewRequest("GET", "http://api.ipify.org/", nil)
if err != nil {
    fmt.Println(err)
    return
}
req.Header.Set("user-agent", "okhttp/4.9.3")
resp, err := client.Do(req)
if err != nil {
    fmt.Println(err)
    return
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
    fmt.Println(err)
    return
}
fmt.Println(string(body))

}

func main() {
config := readConfig()
proxies := config.Proxy[0:4]
clients := getClients(proxies)
numClient := 0
for {
    var wg sync.WaitGroup
    start := time.Now()
    for i := 1; i < 601; i++ {
        if numClient == len(clients) {
            numClient = 0
        }
        wg.Add(1)
        go getIp(*clients[numClient], &wg)
        numClient += 1
    }
    wg.Wait()
    fmt.Printf("%.2fs \n", time.Since(start).Seconds())
}

}

0 Answers
Related