lib/http: convert options to new style

There are still users of the old style options which haven't been
converted yet.
This commit is contained in:
Nick Craig-Wood 2024-07-08 16:22:48 +01:00
parent dce8317042
commit cf25ae69ad
3 changed files with 104 additions and 19 deletions

View File

@ -5,6 +5,7 @@ import (
"html/template" "html/template"
"log" "log"
"github.com/rclone/rclone/fs"
"github.com/rclone/rclone/fs/config/flags" "github.com/rclone/rclone/fs/config/flags"
"github.com/spf13/pflag" "github.com/spf13/pflag"
) )
@ -63,14 +64,37 @@ Use ` + "`--{{ .Prefix }}salt`" + ` to change the password hashing salt from the
// If a non nil value is returned then it is added to the context under the key // If a non nil value is returned then it is added to the context under the key
type CustomAuthFn func(user, pass string) (value interface{}, err error) type CustomAuthFn func(user, pass string) (value interface{}, err error)
// AuthConfigInfo descripts the Options in use
var AuthConfigInfo = fs.Options{{
Name: "htpasswd",
Default: "",
Help: "A htpasswd file - if not provided no authentication is done",
}, {
Name: "realm",
Default: "",
Help: "Realm for authentication",
}, {
Name: "user",
Default: "",
Help: "User name for authentication",
}, {
Name: "pass",
Default: "",
Help: "Password for authentication",
}, {
Name: "salt",
Default: "dlPL2MqE",
Help: "Password hashing salt",
}}
// AuthConfig contains options for the http authentication // AuthConfig contains options for the http authentication
type AuthConfig struct { type AuthConfig struct {
HtPasswd string // htpasswd file - if not provided no authentication is done HtPasswd string `config:"htpasswd"` // htpasswd file - if not provided no authentication is done
Realm string // realm for authentication Realm string `config:"realm"` // realm for authentication
BasicUser string // single username for basic auth if not using Htpasswd BasicUser string `config:"user"` // single username for basic auth if not using Htpasswd
BasicPass string // password for BasicUser BasicPass string `config:"pass"` // password for BasicUser
Salt string // password hashing salt Salt string `config:"salt"` // password hashing salt
CustomAuthFn CustomAuthFn `json:"-"` // custom Auth (not set by command line flags) CustomAuthFn CustomAuthFn `json:"-" config:"-"` // custom Auth (not set by command line flags)
} }
// AddFlagsPrefix adds flags to the flag set for AuthConfig // AddFlagsPrefix adds flags to the flag set for AuthConfig
@ -88,6 +112,9 @@ func AddAuthFlagsPrefix(flagSet *pflag.FlagSet, prefix string, cfg *AuthConfig)
} }
// DefaultAuthCfg returns a new config which can be customized by command line flags // DefaultAuthCfg returns a new config which can be customized by command line flags
//
// Note that this needs to be kept in sync with AuthConfigInfo above and
// can be removed when all callers have been converted.
func DefaultAuthCfg() AuthConfig { func DefaultAuthCfg() AuthConfig {
return AuthConfig{ return AuthConfig{
Salt: "dlPL2MqE", Salt: "dlPL2MqE",

View File

@ -19,6 +19,7 @@ import (
"time" "time"
"github.com/go-chi/chi/v5" "github.com/go-chi/chi/v5"
"github.com/rclone/rclone/fs"
"github.com/rclone/rclone/fs/config/flags" "github.com/rclone/rclone/fs/config/flags"
"github.com/rclone/rclone/lib/atexit" "github.com/rclone/rclone/lib/atexit"
"github.com/spf13/pflag" "github.com/spf13/pflag"
@ -96,20 +97,63 @@ certificate authority certificate.
// Middleware function signature required by chi.Router.Use() // Middleware function signature required by chi.Router.Use()
type Middleware func(http.Handler) http.Handler type Middleware func(http.Handler) http.Handler
// ConfigInfo descripts the Options in use
var ConfigInfo = fs.Options{{
Name: "addr",
Default: []string{"127.0.0.1:8080"},
Help: "IPaddress:Port or :Port to bind server to",
}, {
Name: "server_read_timeout",
Default: 1 * time.Hour,
Help: "Timeout for server reading data",
}, {
Name: "server_write_timeout",
Default: 1 * time.Hour,
Help: "Timeout for server writing data",
}, {
Name: "max_header_bytes",
Default: 4096,
Help: "Maximum size of request header",
}, {
Name: "cert",
Default: "",
Help: "TLS PEM key (concatenation of certificate and CA certificate)",
}, {
Name: "key",
Default: "",
Help: "TLS PEM Private key",
}, {
Name: "client_ca",
Default: "",
Help: "Client certificate authority to verify clients with",
}, {
Name: "baseurl",
Default: "",
Help: "Prefix for URLs - leave blank for root",
}, {
Name: "min_tls_version",
Default: "tls1.0",
Help: "Minimum TLS version that is acceptable",
}, {
Name: "allow_origin",
Default: "",
Help: "Origin which cross-domain request (CORS) can be executed from",
}}
// Config contains options for the http Server // Config contains options for the http Server
type Config struct { type Config struct {
ListenAddr []string // Port to listen on ListenAddr []string `config:"addr"` // Port to listen on
BaseURL string // prefix to strip from URLs BaseURL string `config:"baseurl"` // prefix to strip from URLs
ServerReadTimeout time.Duration // Timeout for server reading data ServerReadTimeout time.Duration `config:"server_read_timeout"` // Timeout for server reading data
ServerWriteTimeout time.Duration // Timeout for server writing data ServerWriteTimeout time.Duration `config:"server_write_timeout"` // Timeout for server writing data
MaxHeaderBytes int // Maximum size of request header MaxHeaderBytes int `config:"max_header_bytes"` // Maximum size of request header
TLSCert string // Path to TLS PEM key (concatenation of certificate and CA certificate) TLSCert string `config:"cert"` // Path to TLS PEM key (concatenation of certificate and CA certificate)
TLSKey string // Path to TLS PEM Private key TLSKey string `config:"key"` // Path to TLS PEM Private key
TLSCertBody []byte // TLS PEM key (concatenation of certificate and CA certificate) body, ignores TLSCert TLSCertBody []byte `config:"-"` // TLS PEM key (concatenation of certificate and CA certificate) body, ignores TLSCert
TLSKeyBody []byte // TLS PEM Private key body, ignores TLSKey TLSKeyBody []byte `config:"-"` // TLS PEM Private key body, ignores TLSKey
ClientCA string // Client certificate authority to verify clients with ClientCA string `config:"client_ca"` // Client certificate authority to verify clients with
MinTLSVersion string // MinTLSVersion contains the minimum TLS version that is acceptable. MinTLSVersion string `config:"min_tls_version"` // MinTLSVersion contains the minimum TLS version that is acceptable.
AllowOrigin string // AllowOrigin sets the Access-Control-Allow-Origin header AllowOrigin string `config:"allow_origin"` // AllowOrigin sets the Access-Control-Allow-Origin header
} }
// AddFlagsPrefix adds flags for the httplib // AddFlagsPrefix adds flags for the httplib
@ -132,6 +176,9 @@ func AddHTTPFlagsPrefix(flagSet *pflag.FlagSet, prefix string, cfg *Config) {
} }
// DefaultCfg is the default values used for Config // DefaultCfg is the default values used for Config
//
// Note that this needs to be kept in sync with ConfigInfo above and
// can be removed when all callers have been converted.
func DefaultCfg() Config { func DefaultCfg() Config {
return Config{ return Config{
ListenAddr: []string{"127.0.0.1:8080"}, ListenAddr: []string{"127.0.0.1:8080"},

View File

@ -11,6 +11,7 @@ import (
"github.com/spf13/pflag" "github.com/spf13/pflag"
"github.com/rclone/rclone/fs"
"github.com/rclone/rclone/fs/config/flags" "github.com/rclone/rclone/fs/config/flags"
) )
@ -72,9 +73,16 @@ be used to render HTML based on specific conditions.
return buf.String() return buf.String()
} }
// TemplateConfigInfo descripts the Options in use
var TemplateConfigInfo = fs.Options{{
Name: "template",
Default: "",
Help: "User-specified template",
}}
// TemplateConfig for the templating functionality // TemplateConfig for the templating functionality
type TemplateConfig struct { type TemplateConfig struct {
Path string Path string `config:"template"`
} }
// AddFlagsPrefix for the templating functionality // AddFlagsPrefix for the templating functionality
@ -88,6 +96,9 @@ func AddTemplateFlagsPrefix(flagSet *pflag.FlagSet, prefix string, cfg *Template
} }
// DefaultTemplateCfg returns a new config which can be customized by command line flags // DefaultTemplateCfg returns a new config which can be customized by command line flags
//
// Note that this needs to be kept in sync with TemplateConfigInfo above and
// can be removed when all callers have been converted.
func DefaultTemplateCfg() TemplateConfig { func DefaultTemplateCfg() TemplateConfig {
return TemplateConfig{} return TemplateConfig{}
} }