diff --git a/agent/go.mod b/agent/go.mod index b5c90283..ab72daf2 100644 --- a/agent/go.mod +++ b/agent/go.mod @@ -89,6 +89,7 @@ require ( github.com/ugorji/go/codec v1.3.0 // indirect github.com/vincent-petithory/dataurl v1.0.0 // indirect github.com/yusing/ds v0.1.0 // indirect + github.com/yusing/goutils v0.0.0-20250922091446-1c6a11717d72 // indirect github.com/yusufpapurcu/wmi v1.2.4 // indirect go.opentelemetry.io/auto/sdk v1.2.0 // indirect go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.63.0 // indirect diff --git a/agent/go.sum b/agent/go.sum index f79c7ff7..7f1ebeb9 100644 --- a/agent/go.sum +++ b/agent/go.sum @@ -208,6 +208,8 @@ github.com/vincent-petithory/dataurl v1.0.0/go.mod h1:FHafX5vmDzyP+1CQATJn7WFKc9 github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= github.com/yusing/ds v0.1.0 h1:aiZs7jPMN3MEChUsddMYjpZFHhhAmkxrwRyIUnGy5AU= github.com/yusing/ds v0.1.0/go.mod h1:KC785+mtt+Bau0LLR+slExDaUjeiqLT1k9Or6Rpryh4= +github.com/yusing/goutils v0.0.0-20250922091446-1c6a11717d72 h1:NHYq8ZqoLSCJYzZaIOn8mJ28/Ac7GEFgeZgcfBoamWE= +github.com/yusing/goutils v0.0.0-20250922091446-1c6a11717d72/go.mod h1:XROGErdAT8UxsbLQbpejGhrTpknWEEgy+9HVjq7ULBI= github.com/yusufpapurcu/wmi v1.2.4 h1:zFUKzehAFReQwLys1b/iSMl+JQGSCSjtVqQn9bBrPo0= github.com/yusufpapurcu/wmi v1.2.4/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQmPyzfmi0= go.opentelemetry.io/auto/sdk v1.2.0 h1:YpRtUFjvhSymycLS2T81lT6IGhcUP+LUPtv0iv1N8bM= diff --git a/go.mod b/go.mod index 0696de85..ea41ab46 100644 --- a/go.mod +++ b/go.mod @@ -215,6 +215,7 @@ require ( github.com/gin-gonic/gin v1.10.1 github.com/pires/go-proxyproto v0.8.1 github.com/yusing/ds v0.1.0 + github.com/yusing/goutils v0.0.0-20250922091446-1c6a11717d72 ) require ( diff --git a/go.sum b/go.sum index b3880793..c1842db2 100644 --- a/go.sum +++ b/go.sum @@ -1650,6 +1650,8 @@ github.com/yuin/goldmark v1.4.1/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1 github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= github.com/yusing/ds v0.1.0 h1:aiZs7jPMN3MEChUsddMYjpZFHhhAmkxrwRyIUnGy5AU= github.com/yusing/ds v0.1.0/go.mod h1:KC785+mtt+Bau0LLR+slExDaUjeiqLT1k9Or6Rpryh4= +github.com/yusing/goutils v0.0.0-20250922091446-1c6a11717d72 h1:NHYq8ZqoLSCJYzZaIOn8mJ28/Ac7GEFgeZgcfBoamWE= +github.com/yusing/goutils v0.0.0-20250922091446-1c6a11717d72/go.mod h1:XROGErdAT8UxsbLQbpejGhrTpknWEEgy+9HVjq7ULBI= github.com/yusufpapurcu/wmi v1.2.4 h1:zFUKzehAFReQwLys1b/iSMl+JQGSCSjtVqQn9bBrPo0= github.com/yusufpapurcu/wmi v1.2.4/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQmPyzfmi0= github.com/zeebo/assert v1.3.0/go.mod h1:Pq9JiuJQpG8JLJdtkwrJESF0Foym2/D9XMU5ciN/wJ0= diff --git a/internal/common/env.go b/internal/common/env.go index 4a3f0bce..4463c1af 100644 --- a/internal/common/env.go +++ b/internal/common/env.go @@ -1,129 +1,63 @@ package common import ( - "fmt" - "log" - "net" "os" - "strconv" "strings" "time" - "github.com/yusing/godoxy/internal/utils/strutils" + "github.com/yusing/goutils/env" ) +func init() { + env.SetPrefixes("GODOXY_", "GOPROXY_", "") +} + var ( - prefixes = []string{"GODOXY_", "GOPROXY_", ""} + IsTest = env.GetEnvBool("TEST", false) || strings.HasSuffix(os.Args[0], ".test") + IsDebug = env.GetEnvBool("DEBUG", IsTest) + IsTrace = env.GetEnvBool("TRACE", false) && IsDebug - IsTest = GetEnvBool("TEST", false) || strings.HasSuffix(os.Args[0], ".test") - IsDebug = GetEnvBool("DEBUG", IsTest) - IsTrace = GetEnvBool("TRACE", false) && IsDebug - - HTTP3Enabled = GetEnvBool("HTTP3_ENABLED", true) + HTTP3Enabled = env.GetEnvBool("HTTP3_ENABLED", true) ProxyHTTPAddr, ProxyHTTPHost, ProxyHTTPPort, - ProxyHTTPURL = GetAddrEnv("HTTP_ADDR", ":80", "http") + ProxyHTTPURL = env.GetAddrEnv("HTTP_ADDR", ":80", "http") ProxyHTTPSAddr, ProxyHTTPSHost, ProxyHTTPSPort, - ProxyHTTPSURL = GetAddrEnv("HTTPS_ADDR", ":443", "https") + ProxyHTTPSURL = env.GetAddrEnv("HTTPS_ADDR", ":443", "https") APIHTTPAddr, APIHTTPHost, APIHTTPPort, - APIHTTPURL = GetAddrEnv("API_ADDR", "127.0.0.1:8888", "http") + APIHTTPURL = env.GetAddrEnv("API_ADDR", "127.0.0.1:8888", "http") - APIJWTSecure = GetEnvBool("API_JWT_SECURE", true) - APIJWTSecret = decodeJWTKey(GetEnvString("API_JWT_SECRET", "")) - APIJWTTokenTTL = GetDurationEnv("API_JWT_TOKEN_TTL", 24*time.Hour) - APIUser = GetEnvString("API_USER", "admin") - APIPassword = GetEnvString("API_PASSWORD", "password") + APIJWTSecure = env.GetEnvBool("API_JWT_SECURE", true) + APIJWTSecret = decodeJWTKey(env.GetEnvString("API_JWT_SECRET", "")) + APIJWTTokenTTL = env.GetEnvDuation("API_JWT_TOKEN_TTL", 24*time.Hour) + APIUser = env.GetEnvString("API_USER", "admin") + APIPassword = env.GetEnvString("API_PASSWORD", "password") - APISkipOriginCheck = GetEnvBool("API_SKIP_ORIGIN_CHECK", false) // skip this in UI Demo + APISkipOriginCheck = env.GetEnvBool("API_SKIP_ORIGIN_CHECK", false) // skip this in UI Demo - DebugDisableAuth = GetEnvBool("DEBUG_DISABLE_AUTH", false) + DebugDisableAuth = env.GetEnvBool("DEBUG_DISABLE_AUTH", false) // OIDC Configuration. - OIDCIssuerURL = GetEnvString("OIDC_ISSUER_URL", "") - OIDCClientID = GetEnvString("OIDC_CLIENT_ID", "") - OIDCClientSecret = GetEnvString("OIDC_CLIENT_SECRET", "") - OIDCScopes = GetCommaSepEnv("OIDC_SCOPES", "openid, profile, email, groups") - OIDCAllowedUsers = GetCommaSepEnv("OIDC_ALLOWED_USERS", "") - OIDCAllowedGroups = GetCommaSepEnv("OIDC_ALLOWED_GROUPS", "") + OIDCIssuerURL = env.GetEnvString("OIDC_ISSUER_URL", "") + OIDCClientID = env.GetEnvString("OIDC_CLIENT_ID", "") + OIDCClientSecret = env.GetEnvString("OIDC_CLIENT_SECRET", "") + OIDCScopes = env.GetEnvCommaSep("OIDC_SCOPES", "openid, profile, email, groups") + OIDCAllowedUsers = env.GetEnvCommaSep("OIDC_ALLOWED_USERS", "") + OIDCAllowedGroups = env.GetEnvCommaSep("OIDC_ALLOWED_GROUPS", "") // metrics configuration - MetricsDisableCPU = GetEnvBool("METRICS_DISABLE_CPU", false) - MetricsDisableMemory = GetEnvBool("METRICS_DISABLE_MEMORY", false) - MetricsDisableDisk = GetEnvBool("METRICS_DISABLE_DISK", false) - MetricsDisableNetwork = GetEnvBool("METRICS_DISABLE_NETWORK", false) - MetricsDisableSensors = GetEnvBool("METRICS_DISABLE_SENSORS", false) + MetricsDisableCPU = env.GetEnvBool("METRICS_DISABLE_CPU", false) + MetricsDisableMemory = env.GetEnvBool("METRICS_DISABLE_MEMORY", false) + MetricsDisableDisk = env.GetEnvBool("METRICS_DISABLE_DISK", false) + MetricsDisableNetwork = env.GetEnvBool("METRICS_DISABLE_NETWORK", false) + MetricsDisableSensors = env.GetEnvBool("METRICS_DISABLE_SENSORS", false) - ForceResolveCountry = GetEnvBool("FORCE_RESOLVE_COUNTRY", false) + ForceResolveCountry = env.GetEnvBool("FORCE_RESOLVE_COUNTRY", false) ) - -func GetEnv[T any](key string, defaultValue T, parser func(string) (T, error)) T { - var value string - var ok bool - for _, prefix := range prefixes { - value, ok = os.LookupEnv(prefix + key) - if ok && value != "" { - break - } - } - if !ok || value == "" { - return defaultValue - } - parsed, err := parser(value) - if err == nil { - return parsed - } - log.Fatalf("env %s: invalid %T value: %s", key, parsed, value) - return defaultValue -} - -func stringstring(s string) (string, error) { - return s, nil -} - -func GetEnvString(key string, defaultValue string) string { - return GetEnv(key, defaultValue, stringstring) -} - -func GetEnvBool(key string, defaultValue bool) bool { - return GetEnv(key, defaultValue, strconv.ParseBool) -} - -func GetEnvInt(key string, defaultValue int) int { - return GetEnv(key, defaultValue, strconv.Atoi) -} - -func GetAddrEnv(key, defaultValue, scheme string) (addr, host string, portInt int, fullURL string) { - addr = GetEnvString(key, defaultValue) - if addr == "" { - return - } - host, port, err := net.SplitHostPort(addr) - if err != nil { - log.Fatalf("env %s: invalid address: %s", key, addr) - } - if host == "" { - host = "localhost" - } - fullURL = fmt.Sprintf("%s://%s:%s", scheme, host, port) - portInt, err = strconv.Atoi(port) - if err != nil { - log.Fatalf("env %s: invalid port: %s", key, port) - } - return -} - -func GetDurationEnv(key string, defaultValue time.Duration) time.Duration { - return GetEnv(key, defaultValue, time.ParseDuration) -} - -func GetCommaSepEnv(key string, defaultValue string) []string { - return strutils.CommaSeperatedList(GetEnvString(key, defaultValue)) -} diff --git a/internal/dnsproviders/go.mod b/internal/dnsproviders/go.mod index 811427c9..f31391c2 100644 --- a/internal/dnsproviders/go.mod +++ b/internal/dnsproviders/go.mod @@ -149,6 +149,7 @@ require ( github.com/vultr/govultr/v3 v3.23.0 // indirect github.com/youmark/pkcs8 v0.0.0-20240726163527-a2c0da244d78 // indirect github.com/yusing/godoxy/internal/utils v0.0.0 // indirect + github.com/yusing/goutils v0.0.0-20250922091446-1c6a11717d72 // indirect go.opentelemetry.io/auto/sdk v1.2.0 // indirect go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.63.0 // indirect go.opentelemetry.io/otel v1.38.0 // indirect diff --git a/internal/dnsproviders/go.sum b/internal/dnsproviders/go.sum index 92b238e2..91f289a1 100644 --- a/internal/dnsproviders/go.sum +++ b/internal/dnsproviders/go.sum @@ -1515,6 +1515,8 @@ github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9dec github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= github.com/yuin/goldmark v1.4.1/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= +github.com/yusing/goutils v0.0.0-20250922091446-1c6a11717d72 h1:NHYq8ZqoLSCJYzZaIOn8mJ28/Ac7GEFgeZgcfBoamWE= +github.com/yusing/goutils v0.0.0-20250922091446-1c6a11717d72/go.mod h1:XROGErdAT8UxsbLQbpejGhrTpknWEEgy+9HVjq7ULBI= github.com/zeebo/assert v1.3.0/go.mod h1:Pq9JiuJQpG8JLJdtkwrJESF0Foym2/D9XMU5ciN/wJ0= github.com/zeebo/xxh3 v1.0.2/go.mod h1:5NWz9Sef7zIDm2JHfFlcQvNekmcEl9ekUZQQKCYaDcA= go.etcd.io/bbolt v1.3.2/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU= diff --git a/internal/route/provider/provider.go b/internal/route/provider/provider.go index 6a8a32e5..05099f4a 100644 --- a/internal/route/provider/provider.go +++ b/internal/route/provider/provider.go @@ -20,6 +20,7 @@ import ( "github.com/yusing/godoxy/internal/types" W "github.com/yusing/godoxy/internal/watcher" "github.com/yusing/godoxy/internal/watcher/events" + "github.com/yusing/goutils/env" ) type ( @@ -70,7 +71,7 @@ func NewFileProvider(filename string) (p *Provider, err error) { func NewDockerProvider(name string, dockerHost string) *Provider { if dockerHost == common.DockerHostFromEnv { - dockerHost = common.GetEnvString("DOCKER_HOST", client.DefaultDockerHost) + dockerHost = env.GetEnvString("DOCKER_HOST", client.DefaultDockerHost) } p := newProvider(provider.ProviderTypeDocker)