mirror of
https://github.com/Snawoot/hola-proxy.git
synced 2026-04-02 18:48:14 +00:00
adjustable pause
This commit is contained in:
@@ -12,7 +12,8 @@ const DEFAULT_LIST_LIMIT = 3
|
||||
func CredService(interval, timeout time.Duration,
|
||||
country string,
|
||||
proxytype string,
|
||||
logger *CondLogger) (auth AuthProvider,
|
||||
logger *CondLogger,
|
||||
minPause, maxPause time.Duration) (auth AuthProvider,
|
||||
tunnels *ZGetTunnelsResponse,
|
||||
err error) {
|
||||
var mux sync.Mutex
|
||||
@@ -25,7 +26,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, logger, client, country, proxytype, DEFAULT_LIST_LIMIT)
|
||||
tunnels, user_uuid, err = Tunnels(ctx, logger, client, country, proxytype, DEFAULT_LIST_LIMIT, minPause, maxPause)
|
||||
if err != nil {
|
||||
logger.Error("Configuration bootstrap error: %v. Retrying with the fallback mechanism...", err)
|
||||
return false
|
||||
@@ -55,7 +56,8 @@ 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, logger, client, country, proxytype, DEFAULT_LIST_LIMIT)
|
||||
tuns, user_uuid, err = Tunnels(ctx, logger, client, country, proxytype,
|
||||
DEFAULT_LIST_LIMIT, minPause, maxPause)
|
||||
if err != nil {
|
||||
logger.Error("Credential rotation error: %v. Retrying with the fallback mechanism...", err)
|
||||
return false
|
||||
|
||||
@@ -35,9 +35,6 @@ 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")
|
||||
|
||||
@@ -322,7 +319,8 @@ func Tunnels(ctx context.Context,
|
||||
client *http.Client,
|
||||
country string,
|
||||
proxy_type string,
|
||||
limit uint) (res *ZGetTunnelsResponse, user_uuid string, reterr error) {
|
||||
limit uint,
|
||||
minPause, maxPause time.Duration) (res *ZGetTunnelsResponse, user_uuid string, reterr error) {
|
||||
u := uuid.New()
|
||||
user_uuid = hex.EncodeToString(u[:])
|
||||
initres, err := background_init(ctx, client, user_uuid)
|
||||
@@ -330,7 +328,7 @@ func Tunnels(ctx context.Context,
|
||||
reterr = err
|
||||
return
|
||||
}
|
||||
sleepDuration := time.Duration(RandRange(float64(MinTunnelsPause), float64(MaxTunnelsPause)))
|
||||
sleepDuration := time.Duration(RandRange(int64(minPause), int64(maxPause)))
|
||||
logger.Info("Sleeping for %v...", sleepDuration)
|
||||
time.Sleep(sleepDuration)
|
||||
res, reterr = zgettunnels(ctx, client, user_uuid, initres.Key, country, proxy_type, limit)
|
||||
|
||||
9
main.go
9
main.go
@@ -54,6 +54,8 @@ type CLIArgs struct {
|
||||
showVersion bool
|
||||
proxy string
|
||||
caFile string
|
||||
minPause time.Duration
|
||||
maxPause time.Duration
|
||||
}
|
||||
|
||||
func parse_args() CLIArgs {
|
||||
@@ -68,6 +70,8 @@ func parse_args() CLIArgs {
|
||||
"(10 - debug, 20 - info, 30 - warning, 40 - error, 50 - critical)")
|
||||
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.DurationVar(&args.minPause, "min-pause", 10*time.Second, "minimum added delay between registration and tunnel request")
|
||||
flag.DurationVar(&args.maxPause, "max-pause", 25*time.Second, "maximum added delay between registration and tunnel request")
|
||||
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
|
||||
flag.StringVar(&args.resolver, "resolver", "https://cloudflare-dns.com/dns-query",
|
||||
@@ -164,7 +168,7 @@ func run() int {
|
||||
return print_countries(args.timeout)
|
||||
}
|
||||
if args.list_proxies {
|
||||
return print_proxies(mainLogger, args.country, args.proxy_type, args.limit, args.timeout)
|
||||
return print_proxies(mainLogger, args.country, args.proxy_type, args.limit, args.timeout, args.minPause, args.maxPause)
|
||||
}
|
||||
|
||||
mainLogger.Info("hola-proxy client version %s is starting...", version)
|
||||
@@ -176,7 +180,8 @@ func run() int {
|
||||
}
|
||||
|
||||
mainLogger.Info("Initializing configuration provider...")
|
||||
auth, tunnels, err := CredService(args.rotate, args.timeout, args.country, args.proxy_type, credLogger)
|
||||
auth, tunnels, err := CredService(args.rotate, args.timeout, args.country, args.proxy_type, credLogger,
|
||||
args.minPause, args.maxPause)
|
||||
if err != nil {
|
||||
mainLogger.Critical("Unable to instantiate credential service: %v", err)
|
||||
return 4
|
||||
|
||||
8
utils.go
8
utils.go
@@ -133,14 +133,14 @@ func print_countries(timeout time.Duration) int {
|
||||
return 0
|
||||
}
|
||||
|
||||
func print_proxies(logger *CondLogger, country string, proxy_type string, limit uint, timeout time.Duration) int {
|
||||
func print_proxies(logger *CondLogger, country string, proxy_type string, limit uint, timeout, minPause, maxPause 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, logger, client, country, proxy_type, limit)
|
||||
tunnels, user_uuid, err = Tunnels(ctx, logger, client, country, proxy_type, limit, minPause, maxPause)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Transaction error: %v. Retrying with the fallback mechanism...\n", err)
|
||||
return false
|
||||
@@ -294,10 +294,10 @@ func copyBody(wr io.Writer, body io.Reader) {
|
||||
}
|
||||
}
|
||||
|
||||
func RandRange(low, hi float64) float64 {
|
||||
func RandRange(low, hi int64) int64 {
|
||||
if low >= hi {
|
||||
panic("RandRange: low boundary is greater or equal to high boundary")
|
||||
}
|
||||
delta := hi - low
|
||||
return low + rand.New(RandomSource).Float64()*delta
|
||||
return low + rand.New(RandomSource).Int63n(delta+1)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user