From 419108e00769f280a7f04bc336e1e45a214c04a8 Mon Sep 17 00:00:00 2001 From: tsosunchia <59512455+tsosunchia@users.noreply.github.com> Date: Thu, 1 Jun 2023 23:54:14 +0800 Subject: [PATCH] =?UTF-8?q?fix=20pow=20jwt=5Ftoken=20=E4=B8=8D=E8=83=BD?= =?UTF-8?q?=E5=A4=8D=E7=94=A8=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/leomoeapi/leomoeapi.go | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/pkg/leomoeapi/leomoeapi.go b/pkg/leomoeapi/leomoeapi.go index 68a92bdd..bdded992 100644 --- a/pkg/leomoeapi/leomoeapi.go +++ b/pkg/leomoeapi/leomoeapi.go @@ -50,28 +50,32 @@ func FetchIPInfo(ip string, token string) (*IPGeoData, string, error) { } //host, port, fastIp = "103.120.18.35", "api.leo.moe", "443" envToken := util.EnvToken - jwtToken := "Bearer " + token - ua := []string{"Privileged Client"} + jwtToken := token + ua := []string{pow.UserAgent} + if envToken != "" { + ua = []string{"Privileged Client"} + } + if token == "" { + // 如果没有传入 token,尝试从环境变量中获取 jwtToken = envToken err := error(nil) if envToken == "" { + // 如果环境变量中没有 token,尝试从 pow 获取 jwtToken, err = pow.GetToken(fastIp, host, port) if err != nil { log.Println(err) os.Exit(1) } - ua = []string{pow.UserAgent} + } - jwtToken = "Bearer " + jwtToken } requestHeader := http.Header{ "Host": []string{host}, "User-Agent": ua, - "Authorization": []string{jwtToken}, + "Authorization": []string{"Bearer " + jwtToken}, } - jwtToken = strings.TrimPrefix(jwtToken, "Bearer ") dialer := websocket.DefaultDialer dialer.TLSClientConfig = &tls.Config{ ServerName: host, @@ -90,6 +94,7 @@ func FetchIPInfo(ip string, token string) (*IPGeoData, string, error) { conn = nil // 将全局的 conn 设为 nil return nil }) + // ws留给下次复用 conn = c } else { c = conn