mirror of
https://github.com/Snawoot/hola-proxy.git
synced 2026-04-02 16:08:14 +00:00
Merge pull request #72 from Snawoot/fix_tunnels_api
fix tunnels API client
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -10,6 +10,7 @@ type secureRandomSource struct{}
|
||||
var RandomSource secureRandomSource
|
||||
|
||||
var int63Limit = big.NewInt(0).Lsh(big.NewInt(1), 63)
|
||||
var int64Limit = big.NewInt(0).Lsh(big.NewInt(1), 64)
|
||||
|
||||
func (_ secureRandomSource) Seed(_ int64) {
|
||||
}
|
||||
@@ -21,3 +22,11 @@ func (_ secureRandomSource) Int63() int64 {
|
||||
}
|
||||
return randNum.Int64()
|
||||
}
|
||||
|
||||
func (_ secureRandomSource) Uint64() uint64 {
|
||||
randNum, err := crand.Int(crand.Reader, int64Limit)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return randNum.Uint64()
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
4
main.go
4
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)
|
||||
|
||||
13
utils.go
13
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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user