diff --git a/main.go b/main.go index 45cd778..610f37d 100644 --- a/main.go +++ b/main.go @@ -4,6 +4,7 @@ import ( "anonymousoverflow/env" "anonymousoverflow/src/middleware" "anonymousoverflow/src/routes" + "anonymousoverflow/src/utils" "fmt" "os" @@ -14,8 +15,8 @@ import ( ) func main() { - env.RunChecks() + utils.ReqSetup() host := os.Getenv("HOST") if host == "" { @@ -29,7 +30,6 @@ func main() { if os.Getenv("DEV") != "true" { gin.SetMode(gin.ReleaseMode) - fmt.Printf("Listening on %s:%s\n", host, port) } r := gin.Default() @@ -92,5 +92,6 @@ func main() { sePingCheck := checks.NewPingCheck("https://stackexchange.com", "GET", 5000, nil, nil) healthcheck.New(r, config.DefaultConfig(), []checks.Check{soPingCheck, sePingCheck}) + fmt.Printf("Starting the web server on %s:%s\n", host, port) r.Run(fmt.Sprintf("%s:%s", host, port)) } diff --git a/src/utils/request.go b/src/utils/request.go index f8518de..37e8e77 100644 --- a/src/utils/request.go +++ b/src/utils/request.go @@ -6,6 +6,7 @@ import ( "net/http" "net/url" "os" + "strconv" "time" "github.com/go-resty/resty/v2" @@ -79,17 +80,20 @@ func (s *Solution) HttpCookies() []*http.Cookie { } const USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.3" +const DEFAULT_TIMEOUT = 120 -var solution *Solution = nil +var ( + solution *Solution = nil + fs_url string = "" + fs_timeout int = DEFAULT_TIMEOUT +) func Solve(target string) error { - fsurl := os.Getenv("FLARESOLVER") - - if fsurl == "" { + if fs_url == "" { return fmt.Errorf("flaresolver is not configured") } - fsurl, _ = url.JoinPath(fsurl, "/v1") + url, _ := url.JoinPath(fs_url, "/v1") response := Response{} client := resty.New() @@ -97,10 +101,10 @@ func Solve(target string) error { SetBody(Request{ Cmd: "request.get", Url: target, - MaxTimeout: 40_000, + MaxTimeout: fs_timeout * 100, OnlyCookies: true, }). - Post(fsurl) + Post(url) if err != nil { return fmt.Errorf("request failed: %s", err.Error()) @@ -148,3 +152,23 @@ func GET(target string, no_retry ...bool) (*resty.Response, error) { return nil, fmt.Errorf("solution did not work") } + +func ReqSetup() { + fs_url = os.Getenv("FLARESOLVER") + timeout := os.Getenv("TIMEOUT") + + if timeout == "" { + return + } + + if to, err := strconv.Atoi(timeout); err != nil { + fmt.Printf("failed to parse timeout: %s\n", err.Error()) + } else if to <= 0 { + fmt.Println("invalid timeout, timeout should be greater than zero") + } else { + return + } + + fmt.Printf("using the default timeout instead (%d secs)\n", DEFAULT_TIMEOUT) + fs_timeout = DEFAULT_TIMEOUT +}