Skip to content

Commit

Permalink
choose different proxy on each error
Browse files Browse the repository at this point in the history
  • Loading branch information
arriven committed Apr 5, 2022
1 parent ee21dbd commit 36af0e5
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions src/utils/proxy.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package utils

import (
"fmt"
"math/rand"
"net"
"net/url"
Expand All @@ -20,15 +21,18 @@ func GetProxyFunc(proxyURLs string, timeout time.Duration) ProxyFunc {

proxies := strings.Split(proxyURLs, ",")

u, err := url.Parse(proxies[rand.Intn(len(proxies))]) //nolint:gosec // Cryptographically secure random not required
if err != nil {
return proxy.FromEnvironmentUsing(direct).Dial
}
// We need to dial new proxy on each call
return func(network, addr string) (net.Conn, error) {
u, err := url.Parse(proxies[rand.Intn(len(proxies))]) //nolint:gosec // Cryptographically secure random not required
if err != nil {
return nil, fmt.Errorf("error building proxy %v: %w", u.String(), err)
}

client, err := proxy.FromURL(u, direct)
if err != nil {
return proxy.FromEnvironmentUsing(direct).Dial
}
client, err := proxy.FromURL(u, direct)
if err != nil {
return nil, fmt.Errorf("error building proxy %v: %w", u.String(), err)
}

return client.Dial
return client.Dial(network, addr)
}
}

0 comments on commit 36af0e5

Please sign in to comment.