IPv6 hosts check
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"math"
|
||||
"math/rand"
|
||||
"net"
|
||||
"net/http"
|
||||
"strings"
|
||||
@@ -22,14 +23,15 @@ const (
|
||||
)
|
||||
|
||||
type HTTPClient struct {
|
||||
client *http.Client
|
||||
maxRetries int
|
||||
backoff func(attempt int) time.Duration
|
||||
getRetryIncr func(resp *http.Response, hasRangeHeader bool, err error) int
|
||||
bearerToken string
|
||||
cfg config.ConfigInterface
|
||||
ipv6 cmap.ConcurrentMap[string, string]
|
||||
log *logutil.Logger
|
||||
client *http.Client
|
||||
maxRetries int
|
||||
backoff func(attempt int) time.Duration
|
||||
getRetryIncr func(resp *http.Response, hasRangeHeader bool, err error) int
|
||||
bearerToken string
|
||||
restrictToHosts []string
|
||||
cfg config.ConfigInterface
|
||||
ipv6 cmap.ConcurrentMap[string, string]
|
||||
log *logutil.Logger
|
||||
}
|
||||
|
||||
// {
|
||||
@@ -46,7 +48,7 @@ func (e *ErrorResponse) Error() string {
|
||||
return fmt.Sprintf("api response error: %s (code: %d)", e.Message, e.Code)
|
||||
}
|
||||
|
||||
func NewHTTPClient(token string, maxRetries int, timeoutSecs int, cfg config.ConfigInterface, log *logutil.Logger) *HTTPClient {
|
||||
func NewHTTPClient(token string, maxRetries int, timeoutSecs int, restrictToHosts []string, cfg config.ConfigInterface, log *logutil.Logger) *HTTPClient {
|
||||
client := HTTPClient{
|
||||
bearerToken: token,
|
||||
client: &http.Client{
|
||||
@@ -100,9 +102,10 @@ func NewHTTPClient(token string, maxRetries int, timeoutSecs int, cfg config.Con
|
||||
}
|
||||
return RATE_LIMIT_FACTOR
|
||||
},
|
||||
cfg: cfg,
|
||||
ipv6: cmap.New[string](),
|
||||
log: log,
|
||||
restrictToHosts: restrictToHosts,
|
||||
cfg: cfg,
|
||||
ipv6: cmap.New[string](),
|
||||
log: log,
|
||||
}
|
||||
|
||||
if cfg.ShouldForceIPv6() {
|
||||
@@ -115,6 +118,22 @@ func NewHTTPClient(token string, maxRetries int, timeoutSecs int, cfg config.Con
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(restrictToHosts) > 0 {
|
||||
found := false
|
||||
for _, h := range restrictToHosts {
|
||||
if h == host {
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
log.Warnf("Host %s is not an IPv6 host, replacing with a random host (ensure you have preferred_hosts properly set in your config.yml)", host)
|
||||
// replace with a random ipv6 host
|
||||
restrictToHostsLen := len(restrictToHosts)
|
||||
randomHost := restrictToHosts[rand.Intn(restrictToHostsLen)]
|
||||
host = randomHost
|
||||
}
|
||||
}
|
||||
ips, err := net.DefaultResolver.LookupIPAddr(ctx, host)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
Reference in New Issue
Block a user