mirror of
https://github.com/GopeedLab/gopeed.git
synced 2026-11-18 22:33:42 +00:00
fix: web auth config (#1094)
This commit is contained in:
3 files changed
+775
-163
No files matched your search
+38
-42
@@ -34,17 +34,17 @@ func parse() *args {
|
|||||||
loadEnvVars(cfg)
|
loadEnvVars(cfg)
|
||||||
// override with non-default command line arguments
|
// override with non-default command line arguments
|
||||||
overrideWithCliArgs(cfg, cliConfig)
|
overrideWithCliArgs(cfg, cliConfig)
|
||||||
// set default values for any unset fields
|
// set default values
|
||||||
setDefaults(cfg)
|
setDefaults(cfg, cliConfig)
|
||||||
return cfg
|
return cfg
|
||||||
}
|
}
|
||||||
|
|
||||||
// loadCliArgs parses command line arguments and returns initial config
|
// loadCliArgs parses command line arguments and returns initial config
|
||||||
func loadCliArgs() *args {
|
func loadCliArgs() *args {
|
||||||
cfg := &args{}
|
cfg := &args{}
|
||||||
cfg.Address = flag.String("A", "", "Bind Address")
|
cfg.Address = flag.String("A", "0.0.0.0", "Bind Address")
|
||||||
cfg.Port = flag.Int("P", 0, "Bind Port")
|
cfg.Port = flag.Int("P", 9999, "Bind Port")
|
||||||
cfg.Username = flag.String("u", "", "Web Authentication Username")
|
cfg.Username = flag.String("u", "gopeed", "Web Authentication Username")
|
||||||
cfg.Password = flag.String("p", "", "Web Authentication Password, if no password is set, web authentication will not be enabled")
|
cfg.Password = flag.String("p", "", "Web Authentication Password, if no password is set, web authentication will not be enabled")
|
||||||
cfg.ApiToken = flag.String("T", "", "API token, it must be configured when using HTTP API in the case of enabling web authentication")
|
cfg.ApiToken = flag.String("T", "", "API token, it must be configured when using HTTP API in the case of enabling web authentication")
|
||||||
cfg.StorageDir = flag.String("d", "", "Storage directory")
|
cfg.StorageDir = flag.String("d", "", "Storage directory")
|
||||||
@@ -66,51 +66,47 @@ func loadCliArgs() *args {
|
|||||||
|
|
||||||
// overrideWithCliArgs overrides config with non-empty command line arguments
|
// overrideWithCliArgs overrides config with non-empty command line arguments
|
||||||
func overrideWithCliArgs(cfg *args, cliConfig *args) {
|
func overrideWithCliArgs(cfg *args, cliConfig *args) {
|
||||||
// Only override if the cli value is not empty/zero
|
flag.Visit(func(f *flag.Flag) {
|
||||||
if cliConfig.Address != nil && *cliConfig.Address != "" {
|
switch f.Name {
|
||||||
cfg.Address = cliConfig.Address
|
case "A":
|
||||||
}
|
cfg.Address = cliConfig.Address
|
||||||
|
case "P":
|
||||||
if cliConfig.Port != nil && *cliConfig.Port != 0 {
|
cfg.Port = cliConfig.Port
|
||||||
cfg.Port = cliConfig.Port
|
case "u":
|
||||||
}
|
cfg.Username = cliConfig.Username
|
||||||
|
case "p":
|
||||||
if cliConfig.Username != nil && *cliConfig.Username != "" {
|
cfg.Password = cliConfig.Password
|
||||||
cfg.Username = cliConfig.Username
|
case "T":
|
||||||
}
|
cfg.ApiToken = cliConfig.ApiToken
|
||||||
|
case "d":
|
||||||
if cliConfig.Password != nil && *cliConfig.Password != "" {
|
cfg.StorageDir = cliConfig.StorageDir
|
||||||
cfg.Password = cliConfig.Password
|
case "w":
|
||||||
}
|
cfg.WhiteDownloadDirs = cliConfig.WhiteDownloadDirs
|
||||||
|
case "c":
|
||||||
if cliConfig.ApiToken != nil && *cliConfig.ApiToken != "" {
|
cfg.configPath = cliConfig.configPath
|
||||||
cfg.ApiToken = cliConfig.ApiToken
|
}
|
||||||
}
|
})
|
||||||
|
|
||||||
if cliConfig.StorageDir != nil && *cliConfig.StorageDir != "" {
|
|
||||||
cfg.StorageDir = cliConfig.StorageDir
|
|
||||||
}
|
|
||||||
|
|
||||||
if cliConfig.WhiteDownloadDirs != nil {
|
|
||||||
cfg.WhiteDownloadDirs = cliConfig.WhiteDownloadDirs
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// setDefaults sets default values for any unset configuration fields
|
// setDefaults sets default values for any unset configuration fields
|
||||||
func setDefaults(cfg *args) {
|
func setDefaults(cfg *args, cliConfig *args) {
|
||||||
if cfg.Address == nil {
|
if cfg.Address == nil {
|
||||||
address := "0.0.0.0"
|
cfg.Address = cliConfig.Address
|
||||||
cfg.Address = &address
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if cfg.Port == nil {
|
if cfg.Port == nil {
|
||||||
port := 9999
|
cfg.Port = cliConfig.Port
|
||||||
cfg.Port = &port
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if cfg.Username == nil {
|
if cfg.Username == nil {
|
||||||
username := "gopeed"
|
cfg.Username = cliConfig.Username
|
||||||
cfg.Username = &username
|
}
|
||||||
|
if cfg.Password == nil {
|
||||||
|
cfg.Password = cliConfig.Password
|
||||||
|
}
|
||||||
|
if cfg.ApiToken == nil {
|
||||||
|
cfg.ApiToken = cliConfig.ApiToken
|
||||||
|
}
|
||||||
|
if cfg.StorageDir == nil {
|
||||||
|
cfg.StorageDir = cliConfig.StorageDir
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+736
-120
File diff suppressed because it is too large.
Load diff
+1
-1
@@ -191,7 +191,7 @@ func BuildServer(startCfg *model.StartConfig) (*http.Server, net.Listener, error
|
|||||||
}
|
}
|
||||||
|
|
||||||
if enableBasicAuth {
|
if enableBasicAuth {
|
||||||
if r.URL.Path == "/api/web/login" {
|
if !strings.HasPrefix(r.URL.Path, "/api/") || r.URL.Path == "/api/web/login" {
|
||||||
h.ServeHTTP(w, r)
|
h.ServeHTTP(w, r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user