From 4b8cb56ff5844e8c1392e283aad94db84b712691 Mon Sep 17 00:00:00 2001 From: Vladislav Yarmak Date: Wed, 1 Feb 2023 23:35:57 +0200 Subject: [PATCH] fix tunnels API client --- credservice.go | 4 ++-- holaapi.go | 7 +++++++ main.go | 4 ++-- utils.go | 13 +++++++++++-- 4 files changed, 22 insertions(+), 6 deletions(-) diff --git a/credservice.go b/credservice.go index 8ce8f19..b783faa 100644 --- a/credservice.go +++ b/credservice.go @@ -25,7 +25,7 @@ func CredService(interval, timeout time.Duration, } tx_res, tx_err := EnsureTransaction(context.Background(), timeout, func(ctx context.Context, client *http.Client) bool { - tunnels, user_uuid, err = Tunnels(ctx, client, country, proxytype, DEFAULT_LIST_LIMIT) + tunnels, user_uuid, err = Tunnels(ctx, logger, client, country, proxytype, DEFAULT_LIST_LIMIT) if err != nil { logger.Error("Configuration bootstrap error: %v. Retrying with the fallback mechanism...", err) return false @@ -55,7 +55,7 @@ func CredService(interval, timeout time.Duration, <-ticker.C logger.Info("Rotating credentials...") tx_res, tx_err := EnsureTransaction(context.Background(), timeout, func(ctx context.Context, client *http.Client) bool { - tuns, user_uuid, err = Tunnels(ctx, client, country, proxytype, DEFAULT_LIST_LIMIT) + tuns, user_uuid, err = Tunnels(ctx, logger, client, country, proxytype, DEFAULT_LIST_LIMIT) if err != nil { logger.Error("Credential rotation error: %v. Retrying with the fallback mechanism...", err) return false diff --git a/holaapi.go b/holaapi.go index 9f507fa..2b4c4b6 100644 --- a/holaapi.go +++ b/holaapi.go @@ -35,6 +35,9 @@ const LOGIN_PREFIX = "user-uuid-" const FALLBACK_CONF_URL = "https://www.dropbox.com/s/jemizcvpmf2qb9v/cloud_failover.conf?dl=1" const AGENT_SUFFIX = ".hola.org" +const MinTunnelsPause = 10 * time.Second +const MaxTunnelsPause = 25 * time.Second + var TemporaryBanError = errors.New("temporary ban detected") var PermanentBanError = errors.New("permanent ban detected") @@ -315,6 +318,7 @@ func GetFallbackProxies(ctx context.Context) (*FallbackConfig, error) { } func Tunnels(ctx context.Context, + logger *CondLogger, client *http.Client, country string, proxy_type string, @@ -326,6 +330,9 @@ func Tunnels(ctx context.Context, reterr = err return } + sleepDuration := time.Duration(RandRange(float64(MinTunnelsPause), float64(MaxTunnelsPause))) + logger.Info("Sleeping for %v...", sleepDuration) + time.Sleep(sleepDuration) res, reterr = zgettunnels(ctx, client, user_uuid, initres.Key, country, proxy_type, limit) return } diff --git a/main.go b/main.go index a2c654c..b57b178 100644 --- a/main.go +++ b/main.go @@ -66,7 +66,7 @@ func parse_args() CLIArgs { flag.StringVar(&args.bind_address, "bind-address", "127.0.0.1:8080", "HTTP proxy listen address") flag.IntVar(&args.verbosity, "verbosity", 20, "logging verbosity "+ "(10 - debug, 20 - info, 30 - warning, 40 - error, 50 - critical)") - flag.DurationVar(&args.timeout, "timeout", 10*time.Second, "timeout for network operations") + flag.DurationVar(&args.timeout, "timeout", 35*time.Second, "timeout for network operations") flag.DurationVar(&args.rotate, "rotate", 1*time.Hour, "rotate user ID once per given period") flag.StringVar(&args.proxy_type, "proxy-type", "direct", "proxy type: direct or lum") // or skip but not mentioned // skip would be used something like this: `./bin/hola-proxy -proxy-type skip -force-port-field 24232 -country ua.peer` for debugging @@ -164,7 +164,7 @@ func run() int { return print_countries(args.timeout) } if args.list_proxies { - return print_proxies(args.country, args.proxy_type, args.limit, args.timeout) + return print_proxies(mainLogger, args.country, args.proxy_type, args.limit, args.timeout) } mainLogger.Info("hola-proxy client version %s is starting...", version) diff --git a/utils.go b/utils.go index 2820a38..b2a2313 100644 --- a/utils.go +++ b/utils.go @@ -8,6 +8,7 @@ import ( "errors" "fmt" "io" + "math/rand" "net" "net/http" "net/url" @@ -132,14 +133,14 @@ func print_countries(timeout time.Duration) int { return 0 } -func print_proxies(country string, proxy_type string, limit uint, timeout time.Duration) int { +func print_proxies(logger *CondLogger, country string, proxy_type string, limit uint, timeout time.Duration) int { var ( tunnels *ZGetTunnelsResponse user_uuid string err error ) tx_res, tx_err := EnsureTransaction(context.Background(), timeout, func(ctx context.Context, client *http.Client) bool { - tunnels, user_uuid, err = Tunnels(ctx, client, country, proxy_type, limit) + tunnels, user_uuid, err = Tunnels(ctx, logger, client, country, proxy_type, limit) if err != nil { fmt.Fprintf(os.Stderr, "Transaction error: %v. Retrying with the fallback mechanism...\n", err) return false @@ -292,3 +293,11 @@ func copyBody(wr io.Writer, body io.Reader) { } } } + +func RandRange(low, hi float64) float64 { + if low >= hi { + panic("RandRange: low boundary is greater or equal to high boundary") + } + delta := hi - low + return low + rand.New(RandomSource).Float64()*delta +}