fix tunnels API client

This commit is contained in:
Vladislav Yarmak
2023-02-01 23:35:57 +02:00
parent c413ef95b1
commit 4b8cb56ff5
4 changed files with 22 additions and 6 deletions

View File

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

View File

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

View File

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

View File

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