diff --git a/config/viper.go b/config/viper.go index 1c3b945..4d5036f 100644 --- a/config/viper.go +++ b/config/viper.go @@ -59,6 +59,7 @@ type alistConfig struct { URL string `toml:"url" mapstructure:"url"` Username string `toml:"username" mapstructure:"username"` Password string `toml:"password" mapstructure:"password"` + Token string `toml:"token" mapstructure:"token"` BasePath string `toml:"base_path" mapstructure:"base_path"` TokenExp int64 `toml:"token_exp" mapstructure:"token_exp"` } diff --git a/storage/alist/alist.go b/storage/alist/alist.go index 75bb3e2..1c461b2 100644 --- a/storage/alist/alist.go +++ b/storage/alist/alist.go @@ -40,6 +40,15 @@ type loginResponse struct { } `json:"data"` } +type meResponse struct { + Code int `json:"code"` + Message string `json:"message"` + Data struct { + ID int `json:"id"` + Username string `json:"username"` + } `json:"data"` +} + type putResponse struct { Code int `json:"code"` Message string `json:"message"` @@ -110,6 +119,44 @@ func (a *Alist) Init() { TLSHandshakeTimeout: 10 * time.Second, }, } + if config.Cfg.Storage.Alist.Token != "" { + a.token = config.Cfg.Storage.Alist.Token + ctx, cancel := context.WithTimeout(context.Background(), 1*time.Minute) + defer cancel() + req, err := http.NewRequestWithContext(ctx, http.MethodGet, a.baseURL+"/api/me", nil) + if err != nil { + logger.L.Fatalf("Failed to create request: %v", err) + os.Exit(1) + } + req.Header.Set("Authorization", a.token) + + resp, err := a.client.Do(req) + if err != nil { + logger.L.Fatalf("Failed to send request: %v", err) + os.Exit(1) + } + defer resp.Body.Close() + if resp.StatusCode != http.StatusOK { + logger.L.Fatalf("Failed to get alist user info: %s", resp.Status) + os.Exit(1) + } + body, err := io.ReadAll(resp.Body) + if err != nil { + logger.L.Fatalf("Failed to read response body: %v", err) + os.Exit(1) + } + var meResp meResponse + if err := json.Unmarshal(body, &meResp); err != nil { + logger.L.Fatalf("Failed to unmarshal me response: %v", err) + os.Exit(1) + } + if meResp.Code != http.StatusOK { + logger.L.Fatalf("Failed to get alist user info: %s", meResp.Message) + os.Exit(1) + } + logger.L.Debugf("Logged in Alist as %s", meResp.Data.Username) + return + } a.loginInfo = &loginRequest{ Username: config.Cfg.Storage.Alist.Username, Password: config.Cfg.Storage.Alist.Password,