add auth CLI support for api & metrics

This commit is contained in:
ginuerzh 2024-01-07 19:42:14 +08:00
parent f68bfdb149
commit fdd3eb61c3
3 changed files with 44 additions and 3 deletions

View File

@ -7,9 +7,11 @@ import (
"strings" "strings"
"github.com/go-gost/core/logger" "github.com/go-gost/core/logger"
mdutil "github.com/go-gost/core/metadata/util"
"github.com/go-gost/x/config" "github.com/go-gost/x/config"
"github.com/go-gost/x/config/parsing" "github.com/go-gost/x/config/parsing"
logger_parser "github.com/go-gost/x/config/parsing/logger" logger_parser "github.com/go-gost/x/config/parsing/logger"
xmd "github.com/go-gost/x/metadata"
xmetrics "github.com/go-gost/x/metrics" xmetrics "github.com/go-gost/x/metrics"
"github.com/go-gost/x/registry" "github.com/go-gost/x/registry"
"github.com/judwhite/go-svc" "github.com/judwhite/go-svc"
@ -71,6 +73,26 @@ func (p *program) Init(env svc.Environment) error {
cfg.API = &config.APIConfig{ cfg.API = &config.APIConfig{
Addr: apiAddr, Addr: apiAddr,
} }
if url, _ := normCmd(apiAddr); url != nil {
cfg.API.Addr = url.Host
if url.User != nil {
username := url.User.Username()
password, _ := url.User.Password()
cfg.API.Auth = &config.AuthConfig{
Username: username,
Password: password,
}
}
m := map[string]any{}
for k, v := range url.Query() {
if len(v) > 0 {
m[k] = v[0]
}
}
md := xmd.NewMetadata(m)
cfg.API.PathPrefix = mdutil.GetString(md, "pathPrefix")
cfg.API.AccessLog = mdutil.GetBool(md, "accesslog")
}
} }
if debug { if debug {
if cfg.Log == nil { if cfg.Log == nil {
@ -82,6 +104,25 @@ func (p *program) Init(env svc.Environment) error {
cfg.Metrics = &config.MetricsConfig{ cfg.Metrics = &config.MetricsConfig{
Addr: metricsAddr, Addr: metricsAddr,
} }
if url, _ := normCmd(metricsAddr); url != nil {
cfg.Metrics.Addr = url.Host
if url.User != nil {
username := url.User.Username()
password, _ := url.User.Password()
cfg.Metrics.Auth = &config.AuthConfig{
Username: username,
Password: password,
}
}
m := map[string]any{}
for k, v := range url.Query() {
if len(v) > 0 {
m[k] = v[0]
}
}
md := xmd.NewMetadata(m)
cfg.Metrics.Path = mdutil.GetString(md, "path")
}
} }
logCfg := cfg.Log logCfg := cfg.Log

2
go.mod
View File

@ -6,7 +6,7 @@ replace github.com/templexxx/cpu v0.0.7 => github.com/templexxx/cpu v0.0.10-0.20
require ( require (
github.com/go-gost/core v0.0.0-20240103125300-5a427b4eaf99 github.com/go-gost/core v0.0.0-20240103125300-5a427b4eaf99
github.com/go-gost/x v0.0.0-20240103125506-c959fc2f7376 github.com/go-gost/x v0.0.0-20240107114042-262ac0e9a57c
github.com/judwhite/go-svc v1.2.1 github.com/judwhite/go-svc v1.2.1
) )

4
go.sum
View File

@ -61,8 +61,8 @@ github.com/go-gost/relay v0.4.1-0.20230916134211-828f314ddfe7 h1:qAG1OyjvdA5h221
github.com/go-gost/relay v0.4.1-0.20230916134211-828f314ddfe7/go.mod h1:lcX+23LCQ3khIeASBo+tJ/WbwXFO32/N5YN6ucuYTG8= github.com/go-gost/relay v0.4.1-0.20230916134211-828f314ddfe7/go.mod h1:lcX+23LCQ3khIeASBo+tJ/WbwXFO32/N5YN6ucuYTG8=
github.com/go-gost/tls-dissector v0.0.2-0.20220408131628-aac992c27451 h1:xj8gUZGYO3nb5+6Bjw9+tsFkA9sYynrOvDvvC4uDV2I= github.com/go-gost/tls-dissector v0.0.2-0.20220408131628-aac992c27451 h1:xj8gUZGYO3nb5+6Bjw9+tsFkA9sYynrOvDvvC4uDV2I=
github.com/go-gost/tls-dissector v0.0.2-0.20220408131628-aac992c27451/go.mod h1:/9QfdewqmHdaE362Hv5nDaSWLx3pCmtD870d6GaquXs= github.com/go-gost/tls-dissector v0.0.2-0.20220408131628-aac992c27451/go.mod h1:/9QfdewqmHdaE362Hv5nDaSWLx3pCmtD870d6GaquXs=
github.com/go-gost/x v0.0.0-20240103125506-c959fc2f7376 h1:ZLaIjEbo/pGxK9Mw4RUTqsi0be9n22YnC7tsb6cHtnE= github.com/go-gost/x v0.0.0-20240107114042-262ac0e9a57c h1:f5idgyuxDMC+AQoFjRGc6G65RUMb6RsZKXL+01ORk94=
github.com/go-gost/x v0.0.0-20240103125506-c959fc2f7376/go.mod h1:aZsf9kureYawzhBhxsl/IOTYgO7Tt3TETQ5xxXmRw5E= github.com/go-gost/x v0.0.0-20240107114042-262ac0e9a57c/go.mod h1:aZsf9kureYawzhBhxsl/IOTYgO7Tt3TETQ5xxXmRw5E=
github.com/go-logr/logr v1.2.4 h1:g01GSCwiDw2xSZfjJ2/T9M+S6pFdcNtFYsp+Y43HYDQ= github.com/go-logr/logr v1.2.4 h1:g01GSCwiDw2xSZfjJ2/T9M+S6pFdcNtFYsp+Y43HYDQ=
github.com/go-logr/logr v1.2.4/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= github.com/go-logr/logr v1.2.4/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s= github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s=