IPv6 hosts check

This commit is contained in:
Ben Sarmiento
2024-01-11 02:28:38 +01:00
parent 72dda4f9fd
commit 9e3d12b008
5 changed files with 156 additions and 92 deletions

View File

@@ -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