From ad1f51dd01ba884b4b830299acd5692a25f5cce5 Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 8 May 2026 02:42:32 +0000 Subject: [PATCH 1/3] Make HTTP timeout configurable, raise default to 30s The previous 2-second hardcoded timeout in libauth was too short for slow auth-server responses (e.g. ~16s observed at FIT building, see issue #26). Expose libauth.HttpTimeout and add a --timeout / -t flag and "timeout" config-file field so users can tune it for their network. https://claude.ai/code/session_01Az7YBQcoUBUSZCDQZ23kRc --- cli/main.go | 10 ++++++++++ libauth/requests.go | 10 +++++++--- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/cli/main.go b/cli/main.go index 9127e04..d1f4c0a 100644 --- a/cli/main.go +++ b/cli/main.go @@ -37,6 +37,7 @@ type Settings struct { Debug bool `json:"debug"` AcID string `json:"acId"` Campus bool `json:"campusOnly"` + Timeout int `json:"timeout"` } var logger = loggo.GetLogger("auth-thu") @@ -99,7 +100,14 @@ func mergeCliSettings(c *cli.Command) { merged.AcID = settings.AcID } merged.Campus = settings.Campus || c.Bool("campus-only") + merged.Timeout = c.Int("timeout") + if !c.IsSet("timeout") && settings.Timeout != 0 { + merged.Timeout = settings.Timeout + } settings = merged + if settings.Timeout > 0 { + libauth.HttpTimeout = time.Duration(settings.Timeout) * time.Second + } logger.Debugf("Settings Username: \"%s\"\n", settings.Username) logger.Debugf("Settings Ip: \"%s\"\n", settings.Ip) logger.Debugf("Settings Host: \"%s\"\n", settings.Host) @@ -114,6 +122,7 @@ func mergeCliSettings(c *cli.Command) { logger.Debugf("Settings Debug: %t\n", settings.Debug) logger.Debugf("Settings AcID: \"%s\"\n", settings.AcID) logger.Debugf("Settings Campus: %t\n", settings.Campus) + logger.Debugf("Settings Timeout: %d\n", settings.Timeout) } func requestUser() (err error) { @@ -424,6 +433,7 @@ func main() { &cli.StringFlag{Name: "config-file", Aliases: []string{"c"}, Usage: "`path` to your config file, default ~/.auth-thu"}, &cli.StringFlag{Name: "hook-success", Usage: "command line to be executed in shell after successful login/out"}, &cli.IntFlag{Name: "online-interval", Aliases: []string{"I"}, Usage: "the interval between each keepAlive request (s)", Value: 3}, + &cli.IntFlag{Name: "timeout", Aliases: []string{"t"}, Usage: "HTTP request timeout in seconds for the auth server", Value: 30}, &cli.BoolFlag{Name: "daemonize", Aliases: []string{"D"}, Usage: "run without reading username/password from standard input; less log"}, &cli.BoolFlag{Name: "debug", Usage: "print debug messages"}, &cli.BoolFlag{Name: "help, h", Usage: "print the help"}, diff --git a/libauth/requests.go b/libauth/requests.go index 2286213..d9d3ce2 100644 --- a/libauth/requests.go +++ b/libauth/requests.go @@ -16,6 +16,10 @@ import ( var logger = loggo.GetLogger("libauth") +// HttpTimeout is the timeout used for all HTTP requests issued by libauth. +// It can be overridden by the caller (e.g. the CLI) before making requests. +var HttpTimeout = 30 * time.Second + func extractJSONFromJSONP(jsonp, callbackName string) (string, error) { l := len(callbackName) if len(jsonp) < l+2 { @@ -90,7 +94,7 @@ func GetJSON(baseUrl string, params url.Values) (string, error) { const CB = "C_a_l_l_b_a_c_k" params.Set("callback", CB) var netClient = &http.Client{ - Timeout: time.Second * 2, + Timeout: HttpTimeout, } url := baseUrl + "?" + params.Encode() logger.Debugf("GET \"%s\"\n", url) @@ -109,7 +113,7 @@ func GetJSON(baseUrl string, params url.Values) (string, error) { func IsOnline(host *UrlProvider, acID string) (online bool, err error, username string) { logger.Debugf("Check if online\n") var netClient = &http.Client{ - Timeout: time.Second * 2, + Timeout: HttpTimeout, } online = false params := url.Values{ @@ -174,7 +178,7 @@ func GetNasID(IP, user, password string) (nasID string, err error) { func GetAcID(V6 bool) (acID string, err error) { logger.Debugf("Get AC ID\n") var netClient = &http.Client{ - Timeout: time.Second * 2, + Timeout: HttpTimeout, CheckRedirect: func(req *http.Request, via []*http.Request) error { logger.Debugf("REDIRECT \"%v\"\n", req.URL) return errors.New("should not redirect") From 5a7cd2e8253010b0dc49bf53c4c40e6b455c34c5 Mon Sep 17 00:00:00 2001 From: Yihang Wang <16917636+WangYihang@users.noreply.github.com> Date: Fri, 8 May 2026 11:59:01 +0800 Subject: [PATCH 2/3] Revert the default HTTP timeout to 2 seconds Reduce the default HTTP timeout from 30 seconds to 2 seconds. --- libauth/requests.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libauth/requests.go b/libauth/requests.go index d9d3ce2..a20d6e5 100644 --- a/libauth/requests.go +++ b/libauth/requests.go @@ -18,7 +18,7 @@ var logger = loggo.GetLogger("libauth") // HttpTimeout is the timeout used for all HTTP requests issued by libauth. // It can be overridden by the caller (e.g. the CLI) before making requests. -var HttpTimeout = 30 * time.Second +var HttpTimeout = 2 * time.Second func extractJSONFromJSONP(jsonp, callbackName string) (string, error) { l := len(callbackName) From 960882b37fe2308846888f927300b00c56bd811f Mon Sep 17 00:00:00 2001 From: Yihang Wang <16917636+WangYihang@users.noreply.github.com> Date: Fri, 8 May 2026 15:03:59 +0800 Subject: [PATCH 3/3] Change auth server HTTP request timeout to 2 seconds Reduced the HTTP request timeout for the auth server from 30 seconds to 2 seconds. --- cli/main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cli/main.go b/cli/main.go index d1f4c0a..f7a8ec7 100644 --- a/cli/main.go +++ b/cli/main.go @@ -433,7 +433,7 @@ func main() { &cli.StringFlag{Name: "config-file", Aliases: []string{"c"}, Usage: "`path` to your config file, default ~/.auth-thu"}, &cli.StringFlag{Name: "hook-success", Usage: "command line to be executed in shell after successful login/out"}, &cli.IntFlag{Name: "online-interval", Aliases: []string{"I"}, Usage: "the interval between each keepAlive request (s)", Value: 3}, - &cli.IntFlag{Name: "timeout", Aliases: []string{"t"}, Usage: "HTTP request timeout in seconds for the auth server", Value: 30}, + &cli.IntFlag{Name: "timeout", Aliases: []string{"t"}, Usage: "HTTP request timeout in seconds for the auth server", Value: 2}, &cli.BoolFlag{Name: "daemonize", Aliases: []string{"D"}, Usage: "run without reading username/password from standard input; less log"}, &cli.BoolFlag{Name: "debug", Usage: "print debug messages"}, &cli.BoolFlag{Name: "help, h", Usage: "print the help"},