mirror of
https://github.com/Snawoot/opera-proxy.git
synced 2025-09-02 10:42:07 +00:00
retry init
This commit is contained in:
parent
12211805bd
commit
13b805635c
1 changed files with 59 additions and 21 deletions
80
main.go
80
main.go
|
@ -98,6 +98,8 @@ type CLIArgs struct {
|
||||||
bootstrapDNS *CSVArg
|
bootstrapDNS *CSVArg
|
||||||
refresh time.Duration
|
refresh time.Duration
|
||||||
refreshRetry time.Duration
|
refreshRetry time.Duration
|
||||||
|
initRetries int
|
||||||
|
initRetryInterval time.Duration
|
||||||
certChainWorkaround bool
|
certChainWorkaround bool
|
||||||
caFile string
|
caFile string
|
||||||
}
|
}
|
||||||
|
@ -140,6 +142,8 @@ func parse_args() *CLIArgs {
|
||||||
"Examples: https://1.1.1.1/dns-query,quic://dns.adguard.com")
|
"Examples: https://1.1.1.1/dns-query,quic://dns.adguard.com")
|
||||||
flag.DurationVar(&args.refresh, "refresh", 4*time.Hour, "login refresh interval")
|
flag.DurationVar(&args.refresh, "refresh", 4*time.Hour, "login refresh interval")
|
||||||
flag.DurationVar(&args.refreshRetry, "refresh-retry", 5*time.Second, "login refresh retry interval")
|
flag.DurationVar(&args.refreshRetry, "refresh-retry", 5*time.Second, "login refresh retry interval")
|
||||||
|
flag.IntVar(&args.initRetries, "init-retries", 0, "initialization steps attempts, zero for unlimited retry")
|
||||||
|
flag.DurationVar(&args.initRetryInterval, "init-retry-interval", 5 * time.Second, "delay between initialization retries")
|
||||||
flag.BoolVar(&args.certChainWorkaround, "certchain-workaround", true,
|
flag.BoolVar(&args.certChainWorkaround, "certchain-workaround", true,
|
||||||
"add bundled cross-signed intermediate cert to certchain to make it check out on old systems")
|
"add bundled cross-signed intermediate cert to certchain to make it check out on old systems")
|
||||||
flag.StringVar(&args.caFile, "cafile", "", "use custom CA certificate bundle file")
|
flag.StringVar(&args.caFile, "cafile", "", "use custom CA certificate bundle file")
|
||||||
|
@ -237,39 +241,48 @@ func run() int {
|
||||||
TLSHandshakeTimeout: 10 * time.Second,
|
TLSHandshakeTimeout: 10 * time.Second,
|
||||||
ExpectContinueTimeout: 1 * time.Second,
|
ExpectContinueTimeout: 1 * time.Second,
|
||||||
})
|
})
|
||||||
seclient.Settings.ClientType = args.apiClientType
|
|
||||||
seclient.Settings.ClientVersion = args.apiClientVersion
|
|
||||||
seclient.Settings.UserAgent = args.apiUserAgent
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
mainLogger.Critical("Unable to construct SEClient: %v", err)
|
mainLogger.Critical("Unable to construct SEClient: %v", err)
|
||||||
return 8
|
return 8
|
||||||
}
|
}
|
||||||
|
seclient.Settings.ClientType = args.apiClientType
|
||||||
|
seclient.Settings.ClientVersion = args.apiClientVersion
|
||||||
|
seclient.Settings.UserAgent = args.apiUserAgent
|
||||||
|
|
||||||
ctx, cl := context.WithTimeout(context.Background(), args.timeout)
|
try := retryPolicy(args.initRetries, args.initRetryInterval, mainLogger)
|
||||||
err = seclient.AnonRegister(ctx)
|
|
||||||
|
err = try("anonymous registration", func() error {
|
||||||
|
ctx, cl := context.WithTimeout(context.Background(), args.timeout)
|
||||||
|
defer cl()
|
||||||
|
return seclient.AnonRegister(ctx)
|
||||||
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
mainLogger.Critical("Unable to perform anonymous registration: %v", err)
|
|
||||||
return 9
|
return 9
|
||||||
}
|
}
|
||||||
cl()
|
|
||||||
|
|
||||||
ctx, cl = context.WithTimeout(context.Background(), args.timeout)
|
err = try("device registration", func() error {
|
||||||
err = seclient.RegisterDevice(ctx)
|
ctx, cl := context.WithTimeout(context.Background(), args.timeout)
|
||||||
|
defer cl()
|
||||||
|
return seclient.RegisterDevice(ctx)
|
||||||
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
mainLogger.Critical("Unable to perform device registration: %v", err)
|
|
||||||
return 10
|
return 10
|
||||||
}
|
}
|
||||||
cl()
|
|
||||||
|
|
||||||
if args.listCountries {
|
if args.listCountries {
|
||||||
return printCountries(mainLogger, args.timeout, seclient)
|
return printCountries(try, mainLogger, args.timeout, seclient)
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx, cl = context.WithTimeout(context.Background(), args.timeout)
|
var ips []se.SEIPEntry
|
||||||
// TODO: learn about requested_geo value format
|
err = try("discover", func() error {
|
||||||
ips, err := seclient.Discover(ctx, fmt.Sprintf("\"%s\",,", args.country))
|
ctx, cl := context.WithTimeout(context.Background(), args.timeout)
|
||||||
|
defer cl()
|
||||||
|
// TODO: learn about requested_geo value format
|
||||||
|
res, err := seclient.Discover(ctx, fmt.Sprintf("\"%s\",,", args.country))
|
||||||
|
ips = res
|
||||||
|
return err
|
||||||
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
mainLogger.Critical("Endpoint discovery failed: %v", err)
|
|
||||||
return 12
|
return 12
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -340,12 +353,16 @@ func run() int {
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
func printCountries(logger *clog.CondLogger, timeout time.Duration, seclient *se.SEClient) int {
|
func printCountries(try func(string, func() error) error, logger *clog.CondLogger, timeout time.Duration, seclient *se.SEClient) int {
|
||||||
ctx, cl := context.WithTimeout(context.Background(), timeout)
|
var list []se.SEGeoEntry
|
||||||
defer cl()
|
err := try("geolist", func() error {
|
||||||
list, err := seclient.GeoList(ctx)
|
ctx, cl := context.WithTimeout(context.Background(), timeout)
|
||||||
|
defer cl()
|
||||||
|
l, err := seclient.GeoList(ctx)
|
||||||
|
list = l
|
||||||
|
return err
|
||||||
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Critical("GeoList error: %v", err)
|
|
||||||
return 11
|
return 11
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -382,3 +399,24 @@ func printProxies(ips []se.SEIPEntry, seclient *se.SEClient) int {
|
||||||
func main() {
|
func main() {
|
||||||
os.Exit(run())
|
os.Exit(run())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func retryPolicy(retries int, retryInterval time.Duration, logger *clog.CondLogger) func(string, func() error) error {
|
||||||
|
return func(name string, f func() error) error {
|
||||||
|
var err error
|
||||||
|
for i:=1; retries <= 0 || i<=retries; i++ {
|
||||||
|
if i > 1 {
|
||||||
|
logger.Warning("Retrying action %q in %v...", name, retryInterval)
|
||||||
|
time.Sleep(retryInterval)
|
||||||
|
}
|
||||||
|
logger.Info("Attempting action %q, attempt #%d...", name, i)
|
||||||
|
err = f()
|
||||||
|
if err == nil {
|
||||||
|
logger.Info("Action %q succeeded on attempt #%d", name, i)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
logger.Warning("Action %q failed: %v", name, err)
|
||||||
|
}
|
||||||
|
logger.Critical("All attempts for action %q have failed. Last error: %v", name, err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue