mirror of
https://github.com/yusing/godoxy.git
synced 2025-06-01 01:22:34 +02:00
refactor: remove the use of logging module in most code
This commit is contained in:
parent
20a1649275
commit
63c4bdc73d
51 changed files with 194 additions and 203 deletions
|
@ -1,11 +1,14 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"github.com/rs/zerolog"
|
||||||
|
"github.com/rs/zerolog/log"
|
||||||
"github.com/yusing/go-proxy/agent/pkg/agent"
|
"github.com/yusing/go-proxy/agent/pkg/agent"
|
||||||
"github.com/yusing/go-proxy/agent/pkg/env"
|
"github.com/yusing/go-proxy/agent/pkg/env"
|
||||||
"github.com/yusing/go-proxy/agent/pkg/server"
|
"github.com/yusing/go-proxy/agent/pkg/server"
|
||||||
"github.com/yusing/go-proxy/internal/gperr"
|
"github.com/yusing/go-proxy/internal/gperr"
|
||||||
"github.com/yusing/go-proxy/internal/logging"
|
|
||||||
"github.com/yusing/go-proxy/internal/metrics/systeminfo"
|
"github.com/yusing/go-proxy/internal/metrics/systeminfo"
|
||||||
httpServer "github.com/yusing/go-proxy/internal/net/gphttp/server"
|
httpServer "github.com/yusing/go-proxy/internal/net/gphttp/server"
|
||||||
"github.com/yusing/go-proxy/internal/task"
|
"github.com/yusing/go-proxy/internal/task"
|
||||||
|
@ -14,6 +17,12 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
writer := zerolog.ConsoleWriter{
|
||||||
|
Out: os.Stderr,
|
||||||
|
TimeFormat: "01-02 15:04",
|
||||||
|
}
|
||||||
|
zerolog.TimeFieldFormat = writer.TimeFormat
|
||||||
|
log.Logger = zerolog.New(writer).Level(zerolog.InfoLevel).With().Timestamp().Logger()
|
||||||
ca := &agent.PEMPair{}
|
ca := &agent.PEMPair{}
|
||||||
err := ca.Load(env.AgentCACert)
|
err := ca.Load(env.AgentCACert)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -34,11 +43,11 @@ func main() {
|
||||||
gperr.LogFatal("init SSL error", err)
|
gperr.LogFatal("init SSL error", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
logging.Info().Msgf("GoDoxy Agent version %s", pkg.GetVersion())
|
log.Info().Msgf("GoDoxy Agent version %s", pkg.GetVersion())
|
||||||
logging.Info().Msgf("Agent name: %s", env.AgentName)
|
log.Info().Msgf("Agent name: %s", env.AgentName)
|
||||||
logging.Info().Msgf("Agent port: %d", env.AgentPort)
|
log.Info().Msgf("Agent port: %d", env.AgentPort)
|
||||||
|
|
||||||
logging.Info().Msg(`
|
log.Info().Msg(`
|
||||||
Tips:
|
Tips:
|
||||||
1. To change the agent name, you can set the AGENT_NAME environment variable.
|
1. To change the agent name, you can set the AGENT_NAME environment variable.
|
||||||
2. To change the agent port, you can set the AGENT_PORT environment variable.
|
2. To change the agent port, you can set the AGENT_PORT environment variable.
|
||||||
|
@ -54,7 +63,7 @@ Tips:
|
||||||
server.StartAgentServer(t, opts)
|
server.StartAgentServer(t, opts)
|
||||||
|
|
||||||
if socketproxy.ListenAddr != "" {
|
if socketproxy.ListenAddr != "" {
|
||||||
logging.Info().Msgf("Docker socket listening on: %s", socketproxy.ListenAddr)
|
log.Info().Msgf("Docker socket listening on: %s", socketproxy.ListenAddr)
|
||||||
opts := httpServer.Options{
|
opts := httpServer.Options{
|
||||||
Name: "docker",
|
Name: "docker",
|
||||||
HTTPAddr: socketproxy.ListenAddr,
|
HTTPAddr: socketproxy.ListenAddr,
|
||||||
|
|
|
@ -15,8 +15,8 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/rs/zerolog"
|
"github.com/rs/zerolog"
|
||||||
|
"github.com/rs/zerolog/log"
|
||||||
"github.com/yusing/go-proxy/agent/pkg/certs"
|
"github.com/yusing/go-proxy/agent/pkg/certs"
|
||||||
"github.com/yusing/go-proxy/internal/logging"
|
|
||||||
"github.com/yusing/go-proxy/pkg"
|
"github.com/yusing/go-proxy/pkg"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -115,7 +115,7 @@ func (cfg *AgentConfig) StartWithCerts(ctx context.Context, ca, crt, key []byte)
|
||||||
|
|
||||||
cfg.name = string(name)
|
cfg.name = string(name)
|
||||||
|
|
||||||
cfg.l = logging.With().Str("agent", cfg.name).Logger()
|
cfg.l = log.With().Str("agent", cfg.name).Logger()
|
||||||
|
|
||||||
// check agent version
|
// check agent version
|
||||||
agentVersionBytes, _, err := cfg.Fetch(ctx, EndpointVersion)
|
agentVersionBytes, _, err := cfg.Fetch(ctx, EndpointVersion)
|
||||||
|
@ -127,10 +127,10 @@ func (cfg *AgentConfig) StartWithCerts(ctx context.Context, ca, crt, key []byte)
|
||||||
agentVersion := pkg.ParseVersion(cfg.version)
|
agentVersion := pkg.ParseVersion(cfg.version)
|
||||||
|
|
||||||
if serverVersion.IsNewerMajorThan(agentVersion) {
|
if serverVersion.IsNewerMajorThan(agentVersion) {
|
||||||
logging.Warn().Msgf("agent %s major version mismatch: server: %s, agent: %s", cfg.name, serverVersion, agentVersion)
|
log.Warn().Msgf("agent %s major version mismatch: server: %s, agent: %s", cfg.name, serverVersion, agentVersion)
|
||||||
}
|
}
|
||||||
|
|
||||||
logging.Info().Msgf("agent %q initialized", cfg.name)
|
log.Info().Msgf("agent %q initialized", cfg.name)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -6,9 +6,9 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/rs/zerolog/log"
|
||||||
"github.com/yusing/go-proxy/agent/pkg/env"
|
"github.com/yusing/go-proxy/agent/pkg/env"
|
||||||
"github.com/yusing/go-proxy/agent/pkg/handler"
|
"github.com/yusing/go-proxy/agent/pkg/handler"
|
||||||
"github.com/yusing/go-proxy/internal/logging"
|
|
||||||
"github.com/yusing/go-proxy/internal/net/gphttp/server"
|
"github.com/yusing/go-proxy/internal/net/gphttp/server"
|
||||||
"github.com/yusing/go-proxy/internal/task"
|
"github.com/yusing/go-proxy/internal/task"
|
||||||
)
|
)
|
||||||
|
@ -33,12 +33,11 @@ func StartAgentServer(parent task.Parent, opt Options) {
|
||||||
tlsConfig.ClientAuth = tls.NoClientCert
|
tlsConfig.ClientAuth = tls.NoClientCert
|
||||||
}
|
}
|
||||||
|
|
||||||
logger := logging.GetLogger()
|
|
||||||
agentServer := &http.Server{
|
agentServer := &http.Server{
|
||||||
Addr: fmt.Sprintf(":%d", opt.Port),
|
Addr: fmt.Sprintf(":%d", opt.Port),
|
||||||
Handler: handler.NewAgentHandler(),
|
Handler: handler.NewAgentHandler(),
|
||||||
TLSConfig: tlsConfig,
|
TLSConfig: tlsConfig,
|
||||||
}
|
}
|
||||||
|
|
||||||
server.Start(parent, agentServer, nil, logger)
|
server.Start(parent, agentServer, nil, &log.Logger)
|
||||||
}
|
}
|
||||||
|
|
11
cmd/main.go
11
cmd/main.go
|
@ -4,6 +4,7 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
|
"github.com/rs/zerolog/log"
|
||||||
"github.com/yusing/go-proxy/internal/auth"
|
"github.com/yusing/go-proxy/internal/auth"
|
||||||
"github.com/yusing/go-proxy/internal/common"
|
"github.com/yusing/go-proxy/internal/common"
|
||||||
"github.com/yusing/go-proxy/internal/config"
|
"github.com/yusing/go-proxy/internal/config"
|
||||||
|
@ -35,8 +36,8 @@ func main() {
|
||||||
initProfiling()
|
initProfiling()
|
||||||
|
|
||||||
logging.InitLogger(os.Stderr, memlogger.GetMemLogger())
|
logging.InitLogger(os.Stderr, memlogger.GetMemLogger())
|
||||||
logging.Info().Msgf("GoDoxy version %s", pkg.GetVersion())
|
log.Info().Msgf("GoDoxy version %s", pkg.GetVersion())
|
||||||
logging.Trace().Msg("trace enabled")
|
log.Trace().Msg("trace enabled")
|
||||||
parallel(
|
parallel(
|
||||||
dnsproviders.InitProviders,
|
dnsproviders.InitProviders,
|
||||||
homepage.InitIconListCache,
|
homepage.InitIconListCache,
|
||||||
|
@ -45,7 +46,7 @@ func main() {
|
||||||
)
|
)
|
||||||
|
|
||||||
if common.APIJWTSecret == nil {
|
if common.APIJWTSecret == nil {
|
||||||
logging.Warn().Msg("API_JWT_SECRET is not set, using random key")
|
log.Warn().Msg("API_JWT_SECRET is not set, using random key")
|
||||||
common.APIJWTSecret = common.RandomJWTKey()
|
common.APIJWTSecret = common.RandomJWTKey()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -62,7 +63,7 @@ func main() {
|
||||||
Proxy: true,
|
Proxy: true,
|
||||||
})
|
})
|
||||||
if err := auth.Initialize(); err != nil {
|
if err := auth.Initialize(); err != nil {
|
||||||
logging.Fatal().Err(err).Msg("failed to initialize authentication")
|
log.Fatal().Err(err).Msg("failed to initialize authentication")
|
||||||
}
|
}
|
||||||
// API Handler needs to start after auth is initialized.
|
// API Handler needs to start after auth is initialized.
|
||||||
cfg.StartServers(&config.StartServersOptions{
|
cfg.StartServers(&config.StartServersOptions{
|
||||||
|
@ -78,7 +79,7 @@ func main() {
|
||||||
func prepareDirectory(dir string) {
|
func prepareDirectory(dir string) {
|
||||||
if _, err := os.Stat(dir); os.IsNotExist(err) {
|
if _, err := os.Stat(dir); os.IsNotExist(err) {
|
||||||
if err = os.MkdirAll(dir, 0o755); err != nil {
|
if err = os.MkdirAll(dir, 0o755); err != nil {
|
||||||
logging.Fatal().Msgf("failed to create directory %s: %v", dir, err)
|
log.Fatal().Msgf("failed to create directory %s: %v", dir, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,9 +5,9 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/puzpuzpuz/xsync/v4"
|
"github.com/puzpuzpuz/xsync/v4"
|
||||||
|
"github.com/rs/zerolog/log"
|
||||||
"github.com/yusing/go-proxy/internal/common"
|
"github.com/yusing/go-proxy/internal/common"
|
||||||
"github.com/yusing/go-proxy/internal/gperr"
|
"github.com/yusing/go-proxy/internal/gperr"
|
||||||
"github.com/yusing/go-proxy/internal/logging"
|
|
||||||
"github.com/yusing/go-proxy/internal/logging/accesslog"
|
"github.com/yusing/go-proxy/internal/logging/accesslog"
|
||||||
"github.com/yusing/go-proxy/internal/maxmind"
|
"github.com/yusing/go-proxy/internal/maxmind"
|
||||||
"github.com/yusing/go-proxy/internal/task"
|
"github.com/yusing/go-proxy/internal/task"
|
||||||
|
@ -97,7 +97,7 @@ func (c *Config) Start(parent *task.Task) gperr.Error {
|
||||||
if c.valErr != nil {
|
if c.valErr != nil {
|
||||||
return c.valErr
|
return c.valErr
|
||||||
}
|
}
|
||||||
logging.Info().
|
log.Info().
|
||||||
Str("default", c.Default).
|
Str("default", c.Default).
|
||||||
Bool("allow_local", c.allowLocal).
|
Bool("allow_local", c.allowLocal).
|
||||||
Int("allow_rules", len(c.Allow)).
|
Int("allow_rules", len(c.Allow)).
|
||||||
|
|
|
@ -3,9 +3,9 @@ package certapi
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/rs/zerolog/log"
|
||||||
config "github.com/yusing/go-proxy/internal/config/types"
|
config "github.com/yusing/go-proxy/internal/config/types"
|
||||||
"github.com/yusing/go-proxy/internal/gperr"
|
"github.com/yusing/go-proxy/internal/gperr"
|
||||||
"github.com/yusing/go-proxy/internal/logging"
|
|
||||||
"github.com/yusing/go-proxy/internal/logging/memlogger"
|
"github.com/yusing/go-proxy/internal/logging/memlogger"
|
||||||
"github.com/yusing/go-proxy/internal/net/gphttp/gpwebsocket"
|
"github.com/yusing/go-proxy/internal/net/gphttp/gpwebsocket"
|
||||||
)
|
)
|
||||||
|
@ -36,7 +36,7 @@ func RenewCert(w http.ResponseWriter, r *http.Request) {
|
||||||
gperr.LogError("failed to obtain cert", err)
|
gperr.LogError("failed to obtain cert", err)
|
||||||
_ = gpwebsocket.WriteText(conn, err.Error())
|
_ = gpwebsocket.WriteText(conn, err.Error())
|
||||||
} else {
|
} else {
|
||||||
logging.Info().Msg("cert obtained successfully")
|
log.Info().Msg("cert obtained successfully")
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
for {
|
for {
|
||||||
|
|
|
@ -9,7 +9,7 @@ import (
|
||||||
"github.com/docker/docker/api/types/container"
|
"github.com/docker/docker/api/types/container"
|
||||||
"github.com/docker/docker/pkg/stdcopy"
|
"github.com/docker/docker/pkg/stdcopy"
|
||||||
"github.com/gorilla/websocket"
|
"github.com/gorilla/websocket"
|
||||||
"github.com/yusing/go-proxy/internal/logging"
|
"github.com/rs/zerolog/log"
|
||||||
"github.com/yusing/go-proxy/internal/net/gphttp"
|
"github.com/yusing/go-proxy/internal/net/gphttp"
|
||||||
"github.com/yusing/go-proxy/internal/net/gphttp/gpwebsocket"
|
"github.com/yusing/go-proxy/internal/net/gphttp/gpwebsocket"
|
||||||
"github.com/yusing/go-proxy/internal/task"
|
"github.com/yusing/go-proxy/internal/task"
|
||||||
|
@ -69,7 +69,7 @@ func Logs(w http.ResponseWriter, r *http.Request) {
|
||||||
if errors.Is(err, context.Canceled) || errors.Is(err, task.ErrProgramExiting) {
|
if errors.Is(err, context.Canceled) || errors.Is(err, task.ErrProgramExiting) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
logging.Err(err).
|
log.Err(err).
|
||||||
Str("server", server).
|
Str("server", server).
|
||||||
Str("container", containerID).
|
Str("container", containerID).
|
||||||
Msg("failed to de-multiplex logs")
|
Msg("failed to de-multiplex logs")
|
||||||
|
|
|
@ -11,9 +11,9 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/golang-jwt/jwt/v5"
|
"github.com/golang-jwt/jwt/v5"
|
||||||
|
"github.com/rs/zerolog/log"
|
||||||
"github.com/yusing/go-proxy/internal/common"
|
"github.com/yusing/go-proxy/internal/common"
|
||||||
"github.com/yusing/go-proxy/internal/jsonstore"
|
"github.com/yusing/go-proxy/internal/jsonstore"
|
||||||
"github.com/yusing/go-proxy/internal/logging"
|
|
||||||
"golang.org/x/oauth2"
|
"golang.org/x/oauth2"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -108,11 +108,11 @@ func storeOAuthRefreshToken(sessionID sessionID, username, token string) {
|
||||||
RefreshToken: token,
|
RefreshToken: token,
|
||||||
Expiry: time.Now().Add(defaultRefreshTokenExpiry),
|
Expiry: time.Now().Add(defaultRefreshTokenExpiry),
|
||||||
})
|
})
|
||||||
logging.Debug().Str("username", username).Msg("stored oauth refresh token")
|
log.Debug().Str("username", username).Msg("stored oauth refresh token")
|
||||||
}
|
}
|
||||||
|
|
||||||
func invalidateOAuthRefreshToken(sessionID sessionID) {
|
func invalidateOAuthRefreshToken(sessionID sessionID) {
|
||||||
logging.Debug().Str("session_id", string(sessionID)).Msg("invalidating oauth refresh token")
|
log.Debug().Str("session_id", string(sessionID)).Msg("invalidating oauth refresh token")
|
||||||
oauthRefreshTokens.Delete(string(sessionID))
|
oauthRefreshTokens.Delete(string(sessionID))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -127,7 +127,7 @@ func (auth *OIDCProvider) setSessionTokenCookie(w http.ResponseWriter, r *http.R
|
||||||
jwtToken := jwt.NewWithClaims(jwt.SigningMethodHS512, claims)
|
jwtToken := jwt.NewWithClaims(jwt.SigningMethodHS512, claims)
|
||||||
signed, err := jwtToken.SignedString(common.APIJWTSecret)
|
signed, err := jwtToken.SignedString(common.APIJWTSecret)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logging.Err(err).Msg("failed to sign session token")
|
log.Err(err).Msg("failed to sign session token")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
SetTokenCookie(w, r, CookieOauthSessionToken, signed, common.APIJWTTokenTTL)
|
SetTokenCookie(w, r, CookieOauthSessionToken, signed, common.APIJWTTokenTTL)
|
||||||
|
@ -205,7 +205,7 @@ func (auth *OIDCProvider) doRefreshToken(ctx context.Context, refreshToken *oaut
|
||||||
|
|
||||||
sessionID := newSessionID()
|
sessionID := newSessionID()
|
||||||
|
|
||||||
logging.Debug().Str("username", claims.Username).Time("expiry", newToken.Expiry).Msg("refreshed token")
|
log.Debug().Str("username", claims.Username).Time("expiry", newToken.Expiry).Msg("refreshed token")
|
||||||
storeOAuthRefreshToken(sessionID, claims.Username, newToken.RefreshToken)
|
storeOAuthRefreshToken(sessionID, claims.Username, newToken.RefreshToken)
|
||||||
|
|
||||||
refreshToken.result = &RefreshResult{
|
refreshToken.result = &RefreshResult{
|
||||||
|
|
|
@ -12,9 +12,9 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/coreos/go-oidc/v3/oidc"
|
"github.com/coreos/go-oidc/v3/oidc"
|
||||||
|
"github.com/rs/zerolog/log"
|
||||||
"github.com/yusing/go-proxy/internal/common"
|
"github.com/yusing/go-proxy/internal/common"
|
||||||
"github.com/yusing/go-proxy/internal/gperr"
|
"github.com/yusing/go-proxy/internal/gperr"
|
||||||
"github.com/yusing/go-proxy/internal/logging"
|
|
||||||
"github.com/yusing/go-proxy/internal/net/gphttp"
|
"github.com/yusing/go-proxy/internal/net/gphttp"
|
||||||
"github.com/yusing/go-proxy/internal/utils"
|
"github.com/yusing/go-proxy/internal/utils"
|
||||||
"golang.org/x/oauth2"
|
"golang.org/x/oauth2"
|
||||||
|
@ -79,7 +79,7 @@ func NewOIDCProvider(issuerURL, clientID, clientSecret string, allowedUsers, all
|
||||||
endSessionURL, err := url.Parse(provider.EndSessionEndpoint())
|
endSessionURL, err := url.Parse(provider.EndSessionEndpoint())
|
||||||
if err != nil && provider.EndSessionEndpoint() != "" {
|
if err != nil && provider.EndSessionEndpoint() != "" {
|
||||||
// non critical, just warn
|
// non critical, just warn
|
||||||
logging.Warn().
|
log.Warn().
|
||||||
Str("issuer", issuerURL).
|
Str("issuer", issuerURL).
|
||||||
Err(err).
|
Err(err).
|
||||||
Msg("failed to parse end session URL")
|
Msg("failed to parse end session URL")
|
||||||
|
@ -176,7 +176,7 @@ func (auth *OIDCProvider) LoginHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
// clear cookies then redirect to home
|
// clear cookies then redirect to home
|
||||||
logging.Err(err).Msg("failed to refresh token")
|
log.Err(err).Msg("failed to refresh token")
|
||||||
auth.clearCookie(w, r)
|
auth.clearCookie(w, r)
|
||||||
http.Redirect(w, r, "/", http.StatusFound)
|
http.Redirect(w, r, "/", http.StatusFound)
|
||||||
return
|
return
|
||||||
|
|
|
@ -10,8 +10,8 @@ import (
|
||||||
|
|
||||||
"github.com/go-acme/lego/v4/certcrypto"
|
"github.com/go-acme/lego/v4/certcrypto"
|
||||||
"github.com/go-acme/lego/v4/lego"
|
"github.com/go-acme/lego/v4/lego"
|
||||||
|
"github.com/rs/zerolog/log"
|
||||||
"github.com/yusing/go-proxy/internal/gperr"
|
"github.com/yusing/go-proxy/internal/gperr"
|
||||||
"github.com/yusing/go-proxy/internal/logging"
|
|
||||||
"github.com/yusing/go-proxy/internal/utils"
|
"github.com/yusing/go-proxy/internal/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -100,8 +100,8 @@ func (cfg *Config) GetLegoConfig() (*User, *lego.Config, gperr.Error) {
|
||||||
|
|
||||||
if cfg.Provider != ProviderLocal && cfg.Provider != ProviderPseudo {
|
if cfg.Provider != ProviderLocal && cfg.Provider != ProviderPseudo {
|
||||||
if privKey, err = cfg.LoadACMEKey(); err != nil {
|
if privKey, err = cfg.LoadACMEKey(); err != nil {
|
||||||
logging.Info().Err(err).Msg("load ACME private key failed")
|
log.Info().Err(err).Msg("load ACME private key failed")
|
||||||
logging.Info().Msg("generate new ACME private key")
|
log.Info().Msg("generate new ACME private key")
|
||||||
privKey, err = ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
|
privKey, err = ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, gperr.New("generate ACME private key").With(err)
|
return nil, nil, gperr.New("generate ACME private key").With(err)
|
||||||
|
|
|
@ -15,8 +15,8 @@ import (
|
||||||
"github.com/go-acme/lego/v4/lego"
|
"github.com/go-acme/lego/v4/lego"
|
||||||
"github.com/go-acme/lego/v4/registration"
|
"github.com/go-acme/lego/v4/registration"
|
||||||
"github.com/rs/zerolog"
|
"github.com/rs/zerolog"
|
||||||
|
"github.com/rs/zerolog/log"
|
||||||
"github.com/yusing/go-proxy/internal/gperr"
|
"github.com/yusing/go-proxy/internal/gperr"
|
||||||
"github.com/yusing/go-proxy/internal/logging"
|
|
||||||
"github.com/yusing/go-proxy/internal/notif"
|
"github.com/yusing/go-proxy/internal/notif"
|
||||||
"github.com/yusing/go-proxy/internal/task"
|
"github.com/yusing/go-proxy/internal/task"
|
||||||
"github.com/yusing/go-proxy/internal/utils/strutils"
|
"github.com/yusing/go-proxy/internal/utils/strutils"
|
||||||
|
@ -78,11 +78,11 @@ func (p *Provider) ObtainCert() error {
|
||||||
if p.cfg.Provider == ProviderPseudo {
|
if p.cfg.Provider == ProviderPseudo {
|
||||||
t := time.NewTicker(1000 * time.Millisecond)
|
t := time.NewTicker(1000 * time.Millisecond)
|
||||||
defer t.Stop()
|
defer t.Stop()
|
||||||
logging.Info().Msg("init client for pseudo provider")
|
log.Info().Msg("init client for pseudo provider")
|
||||||
<-t.C
|
<-t.C
|
||||||
logging.Info().Msg("registering acme for pseudo provider")
|
log.Info().Msg("registering acme for pseudo provider")
|
||||||
<-t.C
|
<-t.C
|
||||||
logging.Info().Msg("obtained cert for pseudo provider")
|
log.Info().Msg("obtained cert for pseudo provider")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -107,7 +107,7 @@ func (p *Provider) ObtainCert() error {
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
p.legoCert = nil
|
p.legoCert = nil
|
||||||
logging.Err(err).Msg("cert renew failed, fallback to obtain")
|
log.Err(err).Msg("cert renew failed, fallback to obtain")
|
||||||
} else {
|
} else {
|
||||||
p.legoCert = cert
|
p.legoCert = cert
|
||||||
}
|
}
|
||||||
|
@ -154,7 +154,7 @@ func (p *Provider) LoadCert() error {
|
||||||
p.tlsCert = &cert
|
p.tlsCert = &cert
|
||||||
p.certExpiries = expiries
|
p.certExpiries = expiries
|
||||||
|
|
||||||
logging.Info().Msgf("next renewal in %v", strutils.FormatDuration(time.Until(p.ShouldRenewOn())))
|
log.Info().Msgf("next renewal in %v", strutils.FormatDuration(time.Until(p.ShouldRenewOn())))
|
||||||
return p.renewIfNeeded()
|
return p.renewIfNeeded()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -240,7 +240,7 @@ func (p *Provider) registerACME() error {
|
||||||
}
|
}
|
||||||
if reg, err := p.client.Registration.ResolveAccountByKey(); err == nil {
|
if reg, err := p.client.Registration.ResolveAccountByKey(); err == nil {
|
||||||
p.user.Registration = reg
|
p.user.Registration = reg
|
||||||
logging.Info().Msg("reused acme registration from private key")
|
log.Info().Msg("reused acme registration from private key")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -249,7 +249,7 @@ func (p *Provider) registerACME() error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
p.user.Registration = reg
|
p.user.Registration = reg
|
||||||
logging.Info().Interface("reg", reg).Msg("acme registered")
|
log.Info().Interface("reg", reg).Msg("acme registered")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -295,7 +295,7 @@ func (p *Provider) certState() CertState {
|
||||||
sort.Strings(certDomains)
|
sort.Strings(certDomains)
|
||||||
|
|
||||||
if !reflect.DeepEqual(certDomains, wantedDomains) {
|
if !reflect.DeepEqual(certDomains, wantedDomains) {
|
||||||
logging.Info().Msgf("cert domains mismatch: %v != %v", certDomains, p.cfg.Domains)
|
log.Info().Msgf("cert domains mismatch: %v != %v", certDomains, p.cfg.Domains)
|
||||||
return CertStateMismatch
|
return CertStateMismatch
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -309,9 +309,9 @@ func (p *Provider) renewIfNeeded() error {
|
||||||
|
|
||||||
switch p.certState() {
|
switch p.certState() {
|
||||||
case CertStateExpired:
|
case CertStateExpired:
|
||||||
logging.Info().Msg("certs expired, renewing")
|
log.Info().Msg("certs expired, renewing")
|
||||||
case CertStateMismatch:
|
case CertStateMismatch:
|
||||||
logging.Info().Msg("cert domains mismatch with config, renewing")
|
log.Info().Msg("cert domains mismatch with config, renewing")
|
||||||
default:
|
default:
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,7 +4,7 @@ import (
|
||||||
"errors"
|
"errors"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"github.com/yusing/go-proxy/internal/logging"
|
"github.com/rs/zerolog/log"
|
||||||
"github.com/yusing/go-proxy/internal/utils/strutils"
|
"github.com/yusing/go-proxy/internal/utils/strutils"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -13,14 +13,14 @@ func (p *Provider) Setup() (err error) {
|
||||||
if !errors.Is(err, os.ErrNotExist) { // ignore if cert doesn't exist
|
if !errors.Is(err, os.ErrNotExist) { // ignore if cert doesn't exist
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
logging.Debug().Msg("obtaining cert due to error loading cert")
|
log.Debug().Msg("obtaining cert due to error loading cert")
|
||||||
if err = p.ObtainCert(); err != nil {
|
if err = p.ObtainCert(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, expiry := range p.GetExpiries() {
|
for _, expiry := range p.GetExpiries() {
|
||||||
logging.Info().Msg("certificate expire on " + strutils.FormatTime(expiry))
|
log.Info().Msg("certificate expire on " + strutils.FormatTime(expiry))
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -10,13 +10,13 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/rs/zerolog"
|
"github.com/rs/zerolog"
|
||||||
|
"github.com/rs/zerolog/log"
|
||||||
"github.com/yusing/go-proxy/internal/api"
|
"github.com/yusing/go-proxy/internal/api"
|
||||||
autocert "github.com/yusing/go-proxy/internal/autocert"
|
autocert "github.com/yusing/go-proxy/internal/autocert"
|
||||||
"github.com/yusing/go-proxy/internal/common"
|
"github.com/yusing/go-proxy/internal/common"
|
||||||
config "github.com/yusing/go-proxy/internal/config/types"
|
config "github.com/yusing/go-proxy/internal/config/types"
|
||||||
"github.com/yusing/go-proxy/internal/entrypoint"
|
"github.com/yusing/go-proxy/internal/entrypoint"
|
||||||
"github.com/yusing/go-proxy/internal/gperr"
|
"github.com/yusing/go-proxy/internal/gperr"
|
||||||
"github.com/yusing/go-proxy/internal/logging"
|
|
||||||
"github.com/yusing/go-proxy/internal/maxmind"
|
"github.com/yusing/go-proxy/internal/maxmind"
|
||||||
"github.com/yusing/go-proxy/internal/net/gphttp/server"
|
"github.com/yusing/go-proxy/internal/net/gphttp/server"
|
||||||
"github.com/yusing/go-proxy/internal/notif"
|
"github.com/yusing/go-proxy/internal/notif"
|
||||||
|
@ -96,10 +96,10 @@ func OnConfigChange(ev []events.Event) {
|
||||||
// just reload once and check the last event
|
// just reload once and check the last event
|
||||||
switch ev[len(ev)-1].Action {
|
switch ev[len(ev)-1].Action {
|
||||||
case events.ActionFileRenamed:
|
case events.ActionFileRenamed:
|
||||||
logging.Warn().Msg(cfgRenameWarn)
|
log.Warn().Msg(cfgRenameWarn)
|
||||||
return
|
return
|
||||||
case events.ActionFileDeleted:
|
case events.ActionFileDeleted:
|
||||||
logging.Warn().Msg(cfgDeleteWarn)
|
log.Warn().Msg(cfgDeleteWarn)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -161,7 +161,7 @@ func (cfg *Config) Start(opts ...*StartServersOptions) {
|
||||||
func (cfg *Config) StartAutoCert() {
|
func (cfg *Config) StartAutoCert() {
|
||||||
autocert := cfg.autocertProvider
|
autocert := cfg.autocertProvider
|
||||||
if autocert == nil {
|
if autocert == nil {
|
||||||
logging.Info().Msg("autocert not configured")
|
log.Info().Msg("autocert not configured")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -374,6 +374,6 @@ func (cfg *Config) loadRouteProviders(providers *config.Providers) gperr.Error {
|
||||||
}
|
}
|
||||||
results.Addf("%-"+strconv.Itoa(lenLongestName)+"s %d routes", p.String(), p.NumRoutes())
|
results.Addf("%-"+strconv.Itoa(lenLongestName)+"s %d routes", p.String(), p.NumRoutes())
|
||||||
})
|
})
|
||||||
logging.Info().Msg(results.String())
|
log.Info().Msg(results.String())
|
||||||
return errs.Error()
|
return errs.Error()
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,10 +13,10 @@ import (
|
||||||
|
|
||||||
"github.com/docker/cli/cli/connhelper"
|
"github.com/docker/cli/cli/connhelper"
|
||||||
"github.com/docker/docker/client"
|
"github.com/docker/docker/client"
|
||||||
|
"github.com/rs/zerolog/log"
|
||||||
"github.com/yusing/go-proxy/agent/pkg/agent"
|
"github.com/yusing/go-proxy/agent/pkg/agent"
|
||||||
"github.com/yusing/go-proxy/internal/common"
|
"github.com/yusing/go-proxy/internal/common"
|
||||||
config "github.com/yusing/go-proxy/internal/config/types"
|
config "github.com/yusing/go-proxy/internal/config/types"
|
||||||
"github.com/yusing/go-proxy/internal/logging"
|
|
||||||
"github.com/yusing/go-proxy/internal/task"
|
"github.com/yusing/go-proxy/internal/task"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -84,7 +84,7 @@ func closeTimedOutClients() {
|
||||||
if atomic.LoadUint32(&c.refCount) == 0 && now-atomic.LoadInt64(&c.closedOn) > clientTTLSecs {
|
if atomic.LoadUint32(&c.refCount) == 0 && now-atomic.LoadInt64(&c.closedOn) > clientTTLSecs {
|
||||||
delete(clientMap, c.Key())
|
delete(clientMap, c.Key())
|
||||||
c.Client.Close()
|
c.Client.Close()
|
||||||
logging.Debug().Str("host", c.DaemonHost()).Msg("docker client closed")
|
log.Debug().Str("host", c.DaemonHost()).Msg("docker client closed")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -149,7 +149,7 @@ func NewClient(host string) (*SharedClient, error) {
|
||||||
default:
|
default:
|
||||||
helper, err := connhelper.GetConnectionHelper(host)
|
helper, err := connhelper.GetConnectionHelper(host)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logging.Panic().Err(err).Msg("failed to get connection helper")
|
log.Panic().Err(err).Msg("failed to get connection helper")
|
||||||
}
|
}
|
||||||
if helper != nil {
|
if helper != nil {
|
||||||
httpClient := &http.Client{
|
httpClient := &http.Client{
|
||||||
|
@ -193,7 +193,7 @@ func NewClient(host string) (*SharedClient, error) {
|
||||||
c.addr = c.Client.DaemonHost()
|
c.addr = c.Client.DaemonHost()
|
||||||
}
|
}
|
||||||
|
|
||||||
defer logging.Debug().Str("host", host).Msg("docker client initialized")
|
defer log.Debug().Str("host", host).Msg("docker client initialized")
|
||||||
|
|
||||||
clientMap[c.Key()] = c
|
clientMap[c.Key()] = c
|
||||||
return c, nil
|
return c, nil
|
||||||
|
|
|
@ -9,11 +9,11 @@ import (
|
||||||
|
|
||||||
"github.com/docker/docker/api/types/container"
|
"github.com/docker/docker/api/types/container"
|
||||||
"github.com/docker/go-connections/nat"
|
"github.com/docker/go-connections/nat"
|
||||||
|
"github.com/rs/zerolog/log"
|
||||||
"github.com/yusing/go-proxy/agent/pkg/agent"
|
"github.com/yusing/go-proxy/agent/pkg/agent"
|
||||||
config "github.com/yusing/go-proxy/internal/config/types"
|
config "github.com/yusing/go-proxy/internal/config/types"
|
||||||
"github.com/yusing/go-proxy/internal/gperr"
|
"github.com/yusing/go-proxy/internal/gperr"
|
||||||
idlewatcher "github.com/yusing/go-proxy/internal/idlewatcher/types"
|
idlewatcher "github.com/yusing/go-proxy/internal/idlewatcher/types"
|
||||||
"github.com/yusing/go-proxy/internal/logging"
|
|
||||||
"github.com/yusing/go-proxy/internal/serialization"
|
"github.com/yusing/go-proxy/internal/serialization"
|
||||||
"github.com/yusing/go-proxy/internal/utils"
|
"github.com/yusing/go-proxy/internal/utils"
|
||||||
)
|
)
|
||||||
|
@ -91,7 +91,7 @@ func FromDocker(c *container.SummaryTrimmed, dockerHost string) (res *Container)
|
||||||
var ok bool
|
var ok bool
|
||||||
res.Agent, ok = config.GetInstance().GetAgent(dockerHost)
|
res.Agent, ok = config.GetInstance().GetAgent(dockerHost)
|
||||||
if !ok {
|
if !ok {
|
||||||
logging.Error().Msgf("agent %q not found", dockerHost)
|
log.Error().Msgf("agent %q not found", dockerHost)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -184,7 +184,7 @@ func (c *Container) setPublicHostname() {
|
||||||
}
|
}
|
||||||
url, err := url.Parse(c.DockerHost)
|
url, err := url.Parse(c.DockerHost)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logging.Err(err).Msgf("invalid docker host %q, falling back to 127.0.0.1", c.DockerHost)
|
log.Err(err).Msgf("invalid docker host %q, falling back to 127.0.0.1", c.DockerHost)
|
||||||
c.PublicHostname = "127.0.0.1"
|
c.PublicHostname = "127.0.0.1"
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,7 +6,7 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/yusing/go-proxy/internal/logging"
|
"github.com/rs/zerolog/log"
|
||||||
"github.com/yusing/go-proxy/internal/logging/accesslog"
|
"github.com/yusing/go-proxy/internal/logging/accesslog"
|
||||||
gphttp "github.com/yusing/go-proxy/internal/net/gphttp"
|
gphttp "github.com/yusing/go-proxy/internal/net/gphttp"
|
||||||
"github.com/yusing/go-proxy/internal/net/gphttp/middleware"
|
"github.com/yusing/go-proxy/internal/net/gphttp/middleware"
|
||||||
|
@ -50,7 +50,7 @@ func (ep *Entrypoint) SetMiddlewares(mws []map[string]any) error {
|
||||||
}
|
}
|
||||||
ep.middleware = mid
|
ep.middleware = mid
|
||||||
|
|
||||||
logging.Debug().Msg("entrypoint middleware loaded")
|
log.Debug().Msg("entrypoint middleware loaded")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -64,7 +64,7 @@ func (ep *Entrypoint) SetAccessLogger(parent task.Parent, cfg *accesslog.Request
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
logging.Debug().Msg("entrypoint access logger created")
|
log.Debug().Msg("entrypoint access logger created")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -89,7 +89,7 @@ func (ep *Entrypoint) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
// Then scraper / scanners will know the subdomain is invalid.
|
// Then scraper / scanners will know the subdomain is invalid.
|
||||||
// With StatusNotFound, they won't know whether it's the path, or the subdomain that is invalid.
|
// With StatusNotFound, they won't know whether it's the path, or the subdomain that is invalid.
|
||||||
if served := middleware.ServeStaticErrorPageFile(w, r); !served {
|
if served := middleware.ServeStaticErrorPageFile(w, r); !served {
|
||||||
logging.Err(err).
|
log.Err(err).
|
||||||
Str("method", r.Method).
|
Str("method", r.Method).
|
||||||
Str("url", r.URL.String()).
|
Str("url", r.URL.String()).
|
||||||
Str("remote", r.RemoteAddr).
|
Str("remote", r.RemoteAddr).
|
||||||
|
@ -99,7 +99,7 @@ func (ep *Entrypoint) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
w.WriteHeader(http.StatusNotFound)
|
w.WriteHeader(http.StatusNotFound)
|
||||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||||
if _, err := w.Write(errorPage); err != nil {
|
if _, err := w.Write(errorPage); err != nil {
|
||||||
logging.Err(err).Msg("failed to write error page")
|
log.Err(err).Msg("failed to write error page")
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
http.Error(w, err.Error(), http.StatusNotFound)
|
http.Error(w, err.Error(), http.StatusNotFound)
|
||||||
|
|
|
@ -4,8 +4,8 @@ import (
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"github.com/rs/zerolog"
|
"github.com/rs/zerolog"
|
||||||
|
zerologlog "github.com/rs/zerolog/log"
|
||||||
"github.com/yusing/go-proxy/internal/common"
|
"github.com/yusing/go-proxy/internal/common"
|
||||||
"github.com/yusing/go-proxy/internal/logging"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func log(msg string, err error, level zerolog.Level, logger ...*zerolog.Logger) {
|
func log(msg string, err error, level zerolog.Level, logger ...*zerolog.Logger) {
|
||||||
|
@ -13,7 +13,7 @@ func log(msg string, err error, level zerolog.Level, logger ...*zerolog.Logger)
|
||||||
if len(logger) > 0 {
|
if len(logger) > 0 {
|
||||||
l = logger[0]
|
l = logger[0]
|
||||||
} else {
|
} else {
|
||||||
l = logging.GetLogger()
|
l = &zerologlog.Logger
|
||||||
}
|
}
|
||||||
l.WithLevel(level).Msg(New(highlightANSI(msg)).With(err).Error())
|
l.WithLevel(level).Msg(New(highlightANSI(msg)).With(err).Error())
|
||||||
switch level {
|
switch level {
|
||||||
|
|
|
@ -6,9 +6,9 @@ import (
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/rs/zerolog/log"
|
||||||
"github.com/yusing/go-proxy/internal/common"
|
"github.com/yusing/go-proxy/internal/common"
|
||||||
"github.com/yusing/go-proxy/internal/jsonstore"
|
"github.com/yusing/go-proxy/internal/jsonstore"
|
||||||
"github.com/yusing/go-proxy/internal/logging"
|
|
||||||
"github.com/yusing/go-proxy/internal/task"
|
"github.com/yusing/go-proxy/internal/task"
|
||||||
"github.com/yusing/go-proxy/internal/utils"
|
"github.com/yusing/go-proxy/internal/utils"
|
||||||
"github.com/yusing/go-proxy/internal/utils/atomic"
|
"github.com/yusing/go-proxy/internal/utils/atomic"
|
||||||
|
@ -74,7 +74,7 @@ func pruneExpiredIconCache() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if nPruned > 0 {
|
if nPruned > 0 {
|
||||||
logging.Info().Int("pruned", nPruned).Msg("pruned expired icon cache")
|
log.Info().Int("pruned", nPruned).Msg("pruned expired icon cache")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -87,7 +87,7 @@ func loadIconCache(key string) *FetchResult {
|
||||||
defer iconMu.RUnlock()
|
defer iconMu.RUnlock()
|
||||||
icon, ok := iconCache.Load(key)
|
icon, ok := iconCache.Load(key)
|
||||||
if ok && len(icon.Icon) > 0 {
|
if ok && len(icon.Icon) > 0 {
|
||||||
logging.Debug().
|
log.Debug().
|
||||||
Str("key", key).
|
Str("key", key).
|
||||||
Msg("icon found in cache")
|
Msg("icon found in cache")
|
||||||
icon.LastAccess.Store(utils.TimeNow())
|
icon.LastAccess.Store(utils.TimeNow())
|
||||||
|
@ -99,7 +99,7 @@ func loadIconCache(key string) *FetchResult {
|
||||||
func storeIconCache(key string, result *FetchResult) {
|
func storeIconCache(key string, result *FetchResult) {
|
||||||
icon := result.Icon
|
icon := result.Icon
|
||||||
if len(icon) > maxIconSize {
|
if len(icon) > maxIconSize {
|
||||||
logging.Debug().Int("size", len(icon)).Msg("icon cache size exceeds max cache size")
|
log.Debug().Int("size", len(icon)).Msg("icon cache size exceeds max cache size")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -109,7 +109,7 @@ func storeIconCache(key string, result *FetchResult) {
|
||||||
entry := &cacheEntry{Icon: icon, ContentType: result.contentType}
|
entry := &cacheEntry{Icon: icon, ContentType: result.contentType}
|
||||||
entry.LastAccess.Store(time.Now())
|
entry.LastAccess.Store(time.Now())
|
||||||
iconCache.Store(key, entry)
|
iconCache.Store(key, entry)
|
||||||
logging.Debug().Str("key", key).Int("size", len(icon)).Msg("stored icon cache")
|
log.Debug().Str("key", key).Int("size", len(icon)).Msg("stored icon cache")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *cacheEntry) IsExpired() bool {
|
func (e *cacheEntry) IsExpired() bool {
|
||||||
|
|
|
@ -10,8 +10,8 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/lithammer/fuzzysearch/fuzzy"
|
"github.com/lithammer/fuzzysearch/fuzzy"
|
||||||
|
"github.com/rs/zerolog/log"
|
||||||
"github.com/yusing/go-proxy/internal/common"
|
"github.com/yusing/go-proxy/internal/common"
|
||||||
"github.com/yusing/go-proxy/internal/logging"
|
|
||||||
"github.com/yusing/go-proxy/internal/serialization"
|
"github.com/yusing/go-proxy/internal/serialization"
|
||||||
"github.com/yusing/go-proxy/internal/task"
|
"github.com/yusing/go-proxy/internal/task"
|
||||||
"github.com/yusing/go-proxy/internal/utils/strutils"
|
"github.com/yusing/go-proxy/internal/utils/strutils"
|
||||||
|
@ -101,15 +101,15 @@ func InitIconListCache() {
|
||||||
|
|
||||||
err := serialization.LoadJSONIfExist(common.IconListCachePath, iconsCache)
|
err := serialization.LoadJSONIfExist(common.IconListCachePath, iconsCache)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logging.Error().Err(err).Msg("failed to load icons")
|
log.Error().Err(err).Msg("failed to load icons")
|
||||||
} else if len(iconsCache.Icons) > 0 {
|
} else if len(iconsCache.Icons) > 0 {
|
||||||
logging.Info().
|
log.Info().
|
||||||
Int("icons", len(iconsCache.Icons)).
|
Int("icons", len(iconsCache.Icons)).
|
||||||
Msg("icons loaded")
|
Msg("icons loaded")
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = updateIcons(); err != nil {
|
if err = updateIcons(); err != nil {
|
||||||
logging.Error().Err(err).Msg("failed to update icons")
|
log.Error().Err(err).Msg("failed to update icons")
|
||||||
}
|
}
|
||||||
|
|
||||||
task.OnProgramExit("save_icons_cache", func() {
|
task.OnProgramExit("save_icons_cache", func() {
|
||||||
|
@ -134,17 +134,17 @@ func ListAvailableIcons() (*Cache, error) {
|
||||||
iconsCache.Lock()
|
iconsCache.Lock()
|
||||||
defer iconsCache.Unlock()
|
defer iconsCache.Unlock()
|
||||||
|
|
||||||
logging.Info().Msg("updating icon data")
|
log.Info().Msg("updating icon data")
|
||||||
if err := updateIcons(); err != nil {
|
if err := updateIcons(); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
logging.Info().Int("icons", len(iconsCache.Icons)).Msg("icons list updated")
|
log.Info().Int("icons", len(iconsCache.Icons)).Msg("icons list updated")
|
||||||
|
|
||||||
iconsCache.LastUpdate = time.Now()
|
iconsCache.LastUpdate = time.Now()
|
||||||
|
|
||||||
err := serialization.SaveJSON(common.IconListCachePath, iconsCache, 0o644)
|
err := serialization.SaveJSON(common.IconListCachePath, iconsCache, 0o644)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logging.Warn().Err(err).Msg("failed to save icons")
|
log.Warn().Err(err).Msg("failed to save icons")
|
||||||
}
|
}
|
||||||
return iconsCache, nil
|
return iconsCache, nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,10 +7,10 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/rs/zerolog"
|
"github.com/rs/zerolog"
|
||||||
|
"github.com/rs/zerolog/log"
|
||||||
"github.com/yusing/go-proxy/internal/gperr"
|
"github.com/yusing/go-proxy/internal/gperr"
|
||||||
"github.com/yusing/go-proxy/internal/idlewatcher/provider"
|
"github.com/yusing/go-proxy/internal/idlewatcher/provider"
|
||||||
idlewatcher "github.com/yusing/go-proxy/internal/idlewatcher/types"
|
idlewatcher "github.com/yusing/go-proxy/internal/idlewatcher/types"
|
||||||
"github.com/yusing/go-proxy/internal/logging"
|
|
||||||
"github.com/yusing/go-proxy/internal/net/gphttp/reverseproxy"
|
"github.com/yusing/go-proxy/internal/net/gphttp/reverseproxy"
|
||||||
net "github.com/yusing/go-proxy/internal/net/types"
|
net "github.com/yusing/go-proxy/internal/net/types"
|
||||||
"github.com/yusing/go-proxy/internal/route/routes"
|
"github.com/yusing/go-proxy/internal/route/routes"
|
||||||
|
@ -120,7 +120,7 @@ func NewWatcher(parent task.Parent, r routes.Route) (*Watcher, error) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
w.provider = p
|
w.provider = p
|
||||||
w.l = logging.With().
|
w.l = log.With().
|
||||||
Str("provider", providerType).
|
Str("provider", providerType).
|
||||||
Str("container", cfg.ContainerName()).
|
Str("container", cfg.ContainerName()).
|
||||||
Logger()
|
Logger()
|
||||||
|
|
|
@ -9,9 +9,9 @@ import (
|
||||||
"maps"
|
"maps"
|
||||||
|
|
||||||
"github.com/puzpuzpuz/xsync/v4"
|
"github.com/puzpuzpuz/xsync/v4"
|
||||||
|
"github.com/rs/zerolog/log"
|
||||||
"github.com/yusing/go-proxy/internal/common"
|
"github.com/yusing/go-proxy/internal/common"
|
||||||
"github.com/yusing/go-proxy/internal/gperr"
|
"github.com/yusing/go-proxy/internal/gperr"
|
||||||
"github.com/yusing/go-proxy/internal/logging"
|
|
||||||
"github.com/yusing/go-proxy/internal/serialization"
|
"github.com/yusing/go-proxy/internal/serialization"
|
||||||
"github.com/yusing/go-proxy/internal/task"
|
"github.com/yusing/go-proxy/internal/task"
|
||||||
)
|
)
|
||||||
|
@ -42,7 +42,7 @@ var storesPath = common.DataDir
|
||||||
func init() {
|
func init() {
|
||||||
task.OnProgramExit("save_stores", func() {
|
task.OnProgramExit("save_stores", func() {
|
||||||
if err := save(); err != nil {
|
if err := save(); err != nil {
|
||||||
logging.Error().Err(err).Msg("failed to save stores")
|
log.Error().Err(err).Msg("failed to save stores")
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -54,20 +54,20 @@ func loadNS[T store](ns namespace) T {
|
||||||
file, err := os.Open(path)
|
file, err := os.Open(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if !os.IsNotExist(err) {
|
if !os.IsNotExist(err) {
|
||||||
logging.Err(err).
|
log.Err(err).
|
||||||
Str("path", path).
|
Str("path", path).
|
||||||
Msg("failed to load store")
|
Msg("failed to load store")
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
defer file.Close()
|
defer file.Close()
|
||||||
if err := json.NewDecoder(file).Decode(&store); err != nil {
|
if err := json.NewDecoder(file).Decode(&store); err != nil {
|
||||||
logging.Err(err).
|
log.Err(err).
|
||||||
Str("path", path).
|
Str("path", path).
|
||||||
Msg("failed to load store")
|
Msg("failed to load store")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
stores[ns] = store
|
stores[ns] = store
|
||||||
logging.Debug().
|
log.Debug().
|
||||||
Str("namespace", string(ns)).
|
Str("namespace", string(ns)).
|
||||||
Str("path", path).
|
Str("path", path).
|
||||||
Msg("loaded store")
|
Msg("loaded store")
|
||||||
|
@ -86,7 +86,7 @@ func save() error {
|
||||||
|
|
||||||
func Store[VT any](namespace namespace) MapStore[VT] {
|
func Store[VT any](namespace namespace) MapStore[VT] {
|
||||||
if _, ok := stores[namespace]; ok {
|
if _, ok := stores[namespace]; ok {
|
||||||
logging.Fatal().Str("namespace", string(namespace)).Msg("namespace already exists")
|
log.Fatal().Str("namespace", string(namespace)).Msg("namespace already exists")
|
||||||
}
|
}
|
||||||
store := loadNS[*MapStore[VT]](namespace)
|
store := loadNS[*MapStore[VT]](namespace)
|
||||||
stores[namespace] = store
|
stores[namespace] = store
|
||||||
|
@ -95,7 +95,7 @@ func Store[VT any](namespace namespace) MapStore[VT] {
|
||||||
|
|
||||||
func Object[Ptr Initializer](namespace namespace) Ptr {
|
func Object[Ptr Initializer](namespace namespace) Ptr {
|
||||||
if _, ok := stores[namespace]; ok {
|
if _, ok := stores[namespace]; ok {
|
||||||
logging.Fatal().Str("namespace", string(namespace)).Msg("namespace already exists")
|
log.Fatal().Str("namespace", string(namespace)).Msg("namespace already exists")
|
||||||
}
|
}
|
||||||
obj := loadNS[*ObjectStore[Ptr]](namespace)
|
obj := loadNS[*ObjectStore[Ptr]](namespace)
|
||||||
stores[namespace] = obj
|
stores[namespace] = obj
|
||||||
|
|
|
@ -8,8 +8,8 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/rs/zerolog"
|
"github.com/rs/zerolog"
|
||||||
|
"github.com/rs/zerolog/log"
|
||||||
"github.com/yusing/go-proxy/internal/gperr"
|
"github.com/yusing/go-proxy/internal/gperr"
|
||||||
"github.com/yusing/go-proxy/internal/logging"
|
|
||||||
maxmind "github.com/yusing/go-proxy/internal/maxmind/types"
|
maxmind "github.com/yusing/go-proxy/internal/maxmind/types"
|
||||||
"github.com/yusing/go-proxy/internal/task"
|
"github.com/yusing/go-proxy/internal/task"
|
||||||
"github.com/yusing/go-proxy/internal/utils"
|
"github.com/yusing/go-proxy/internal/utils"
|
||||||
|
@ -120,7 +120,7 @@ func NewAccessLoggerWithIO(parent task.Parent, writer WriterWithName, anyCfg Any
|
||||||
bufSize: MinBufferSize,
|
bufSize: MinBufferSize,
|
||||||
lineBufPool: synk.NewBytesPool(),
|
lineBufPool: synk.NewBytesPool(),
|
||||||
errRateLimiter: rate.NewLimiter(rate.Every(errRateLimit), errBurst),
|
errRateLimiter: rate.NewLimiter(rate.Every(errRateLimit), errBurst),
|
||||||
logger: logging.With().Str("file", writer.Name()).Logger(),
|
logger: log.With().Str("file", writer.Name()).Logger(),
|
||||||
}
|
}
|
||||||
|
|
||||||
l.supportRotate = unwrap[supportRotate](writer)
|
l.supportRotate = unwrap[supportRotate](writer)
|
||||||
|
|
|
@ -7,7 +7,7 @@ import (
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"github.com/yusing/go-proxy/internal/logging"
|
"github.com/rs/zerolog/log"
|
||||||
"github.com/yusing/go-proxy/internal/utils"
|
"github.com/yusing/go-proxy/internal/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -90,7 +90,7 @@ func (f *File) Close() error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *File) closeOnZero() {
|
func (f *File) closeOnZero() {
|
||||||
defer logging.Debug().
|
defer log.Debug().
|
||||||
Str("path", f.path).
|
Str("path", f.path).
|
||||||
Msg("access log closed")
|
Msg("access log closed")
|
||||||
|
|
||||||
|
|
|
@ -66,21 +66,3 @@ func InitLogger(out ...io.Writer) {
|
||||||
zerolog.TimeFieldFormat = timeFmt
|
zerolog.TimeFieldFormat = timeFmt
|
||||||
zerologlog.Logger = logger
|
zerologlog.Logger = logger
|
||||||
}
|
}
|
||||||
|
|
||||||
func DiscardLogger() { zerolog.SetGlobalLevel(zerolog.Disabled) }
|
|
||||||
|
|
||||||
func AddHook(h zerolog.Hook) { logger = logger.Hook(h) }
|
|
||||||
|
|
||||||
func GetLogger() *zerolog.Logger { return &logger }
|
|
||||||
func With() zerolog.Context { return logger.With() }
|
|
||||||
|
|
||||||
func WithLevel(level zerolog.Level) *zerolog.Event { return logger.WithLevel(level) }
|
|
||||||
|
|
||||||
func Info() *zerolog.Event { return logger.Info() }
|
|
||||||
func Warn() *zerolog.Event { return logger.Warn() }
|
|
||||||
func Error() *zerolog.Event { return logger.Error() }
|
|
||||||
func Err(err error) *zerolog.Event { return logger.Err(err) }
|
|
||||||
func Debug() *zerolog.Event { return logger.Debug() }
|
|
||||||
func Fatal() *zerolog.Event { return logger.Fatal() }
|
|
||||||
func Panic() *zerolog.Event { return logger.Panic() }
|
|
||||||
func Trace() *zerolog.Event { return logger.Trace() }
|
|
||||||
|
|
|
@ -2,8 +2,8 @@ package maxmind
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/rs/zerolog"
|
"github.com/rs/zerolog"
|
||||||
|
"github.com/rs/zerolog/log"
|
||||||
"github.com/yusing/go-proxy/internal/gperr"
|
"github.com/yusing/go-proxy/internal/gperr"
|
||||||
"github.com/yusing/go-proxy/internal/logging"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type (
|
type (
|
||||||
|
@ -28,6 +28,6 @@ func (cfg *Config) Validate() gperr.Error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cfg *Config) Logger() *zerolog.Logger {
|
func (cfg *Config) Logger() *zerolog.Logger {
|
||||||
l := logging.With().Str("database", string(cfg.Database)).Logger()
|
l := log.With().Str("database", string(cfg.Database)).Logger()
|
||||||
return &l
|
return &l
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,8 +10,8 @@ import (
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/rs/zerolog/log"
|
||||||
"github.com/yusing/go-proxy/internal/gperr"
|
"github.com/yusing/go-proxy/internal/gperr"
|
||||||
"github.com/yusing/go-proxy/internal/logging"
|
|
||||||
"github.com/yusing/go-proxy/internal/task"
|
"github.com/yusing/go-proxy/internal/task"
|
||||||
"github.com/yusing/go-proxy/internal/utils/atomic"
|
"github.com/yusing/go-proxy/internal/utils/atomic"
|
||||||
)
|
)
|
||||||
|
@ -47,7 +47,7 @@ var initDataDirOnce sync.Once
|
||||||
|
|
||||||
func initDataDir() {
|
func initDataDir() {
|
||||||
if err := os.MkdirAll(saveBaseDir, 0o755); err != nil {
|
if err := os.MkdirAll(saveBaseDir, 0o755); err != nil {
|
||||||
logging.Error().Err(err).Msg("failed to create metrics data directory")
|
log.Error().Err(err).Msg("failed to create metrics data directory")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -138,10 +138,10 @@ func (p *Poller[T, AggregateT]) Start() {
|
||||||
err := p.load()
|
err := p.load()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if !os.IsNotExist(err) {
|
if !os.IsNotExist(err) {
|
||||||
logging.Error().Err(err).Msgf("failed to load last metrics data for %s", p.name)
|
log.Error().Err(err).Msgf("failed to load last metrics data for %s", p.name)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
logging.Debug().Msgf("Loaded last metrics data for %s, %d entries", p.name, p.period.Total())
|
log.Debug().Msgf("Loaded last metrics data for %s, %d entries", p.name, p.period.Total())
|
||||||
}
|
}
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
|
@ -158,7 +158,7 @@ func (p *Poller[T, AggregateT]) Start() {
|
||||||
t.Finish(nil)
|
t.Finish(nil)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
logging.Debug().Msgf("Starting poller %s with interval %s", p.name, pollInterval)
|
log.Debug().Msgf("Starting poller %s with interval %s", p.name, pollInterval)
|
||||||
|
|
||||||
p.pollWithTimeout(t.Context())
|
p.pollWithTimeout(t.Context())
|
||||||
|
|
||||||
|
@ -176,7 +176,7 @@ func (p *Poller[T, AggregateT]) Start() {
|
||||||
case <-gatherErrsTicker.C:
|
case <-gatherErrsTicker.C:
|
||||||
errs, ok := p.gatherErrs()
|
errs, ok := p.gatherErrs()
|
||||||
if ok {
|
if ok {
|
||||||
logging.Error().Msg(errs)
|
log.Error().Msg(errs)
|
||||||
}
|
}
|
||||||
p.clearErrs()
|
p.clearErrs()
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,6 +8,7 @@ import (
|
||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/rs/zerolog/log"
|
||||||
"github.com/shirou/gopsutil/v4/cpu"
|
"github.com/shirou/gopsutil/v4/cpu"
|
||||||
"github.com/shirou/gopsutil/v4/disk"
|
"github.com/shirou/gopsutil/v4/disk"
|
||||||
"github.com/shirou/gopsutil/v4/mem"
|
"github.com/shirou/gopsutil/v4/mem"
|
||||||
|
@ -16,7 +17,6 @@ import (
|
||||||
"github.com/shirou/gopsutil/v4/warning"
|
"github.com/shirou/gopsutil/v4/warning"
|
||||||
"github.com/yusing/go-proxy/internal/common"
|
"github.com/yusing/go-proxy/internal/common"
|
||||||
"github.com/yusing/go-proxy/internal/gperr"
|
"github.com/yusing/go-proxy/internal/gperr"
|
||||||
"github.com/yusing/go-proxy/internal/logging"
|
|
||||||
"github.com/yusing/go-proxy/internal/metrics/period"
|
"github.com/yusing/go-proxy/internal/metrics/period"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -130,7 +130,7 @@ func getSystemInfo(ctx context.Context, lastResult *SystemInfo) (*SystemInfo, er
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
if allWarnings.HasError() {
|
if allWarnings.HasError() {
|
||||||
logging.Warn().Msg(allWarnings.String())
|
log.Warn().Msg(allWarnings.String())
|
||||||
}
|
}
|
||||||
if allErrors.HasError() {
|
if allErrors.HasError() {
|
||||||
return nil, allErrors.Error()
|
return nil, allErrors.Error()
|
||||||
|
@ -195,7 +195,7 @@ func (s *SystemInfo) collectDisksInfo(ctx context.Context, lastResult *SystemInf
|
||||||
if len(s.Disks) == 0 {
|
if len(s.Disks) == 0 {
|
||||||
return errs.Error()
|
return errs.Error()
|
||||||
}
|
}
|
||||||
logging.Warn().Msg(errs.String())
|
log.Warn().Msg(errs.String())
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,7 +6,7 @@ import (
|
||||||
"errors"
|
"errors"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/yusing/go-proxy/internal/logging"
|
"github.com/rs/zerolog/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
func WriteBody(w http.ResponseWriter, body []byte) {
|
func WriteBody(w http.ResponseWriter, body []byte) {
|
||||||
|
@ -14,9 +14,9 @@ func WriteBody(w http.ResponseWriter, body []byte) {
|
||||||
switch {
|
switch {
|
||||||
case errors.Is(err, http.ErrHandlerTimeout),
|
case errors.Is(err, http.ErrHandlerTimeout),
|
||||||
errors.Is(err, context.DeadlineExceeded):
|
errors.Is(err, context.DeadlineExceeded):
|
||||||
logging.Err(err).Msg("timeout writing body")
|
log.Err(err).Msg("timeout writing body")
|
||||||
default:
|
default:
|
||||||
logging.Err(err).Msg("failed to write body")
|
log.Err(err).Msg("failed to write body")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,11 +9,11 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/gorilla/websocket"
|
"github.com/gorilla/websocket"
|
||||||
"github.com/yusing/go-proxy/internal/logging"
|
"github.com/rs/zerolog/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
func warnNoMatchDomains() {
|
func warnNoMatchDomains() {
|
||||||
logging.Warn().Msg("no match domains configured, accepting websocket API request from all origins")
|
log.Warn().Msg("no match domains configured, accepting websocket API request from all origins")
|
||||||
}
|
}
|
||||||
|
|
||||||
var warnNoMatchDomainOnce sync.Once
|
var warnNoMatchDomainOnce sync.Once
|
||||||
|
|
|
@ -7,8 +7,8 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/rs/zerolog"
|
"github.com/rs/zerolog"
|
||||||
|
"github.com/rs/zerolog/log"
|
||||||
"github.com/yusing/go-proxy/internal/gperr"
|
"github.com/yusing/go-proxy/internal/gperr"
|
||||||
"github.com/yusing/go-proxy/internal/logging"
|
|
||||||
"github.com/yusing/go-proxy/internal/net/gphttp/httpheaders"
|
"github.com/yusing/go-proxy/internal/net/gphttp/httpheaders"
|
||||||
"github.com/yusing/go-proxy/internal/net/gphttp/loadbalancer/types"
|
"github.com/yusing/go-proxy/internal/net/gphttp/loadbalancer/types"
|
||||||
"github.com/yusing/go-proxy/internal/task"
|
"github.com/yusing/go-proxy/internal/task"
|
||||||
|
@ -47,7 +47,7 @@ func New(cfg *Config) *LoadBalancer {
|
||||||
lb := &LoadBalancer{
|
lb := &LoadBalancer{
|
||||||
Config: new(Config),
|
Config: new(Config),
|
||||||
pool: pool.New[Server]("loadbalancer." + cfg.Link),
|
pool: pool.New[Server]("loadbalancer." + cfg.Link),
|
||||||
l: logging.With().Str("name", cfg.Link).Logger(),
|
l: log.With().Str("name", cfg.Link).Logger(),
|
||||||
}
|
}
|
||||||
lb.UpdateConfigIfNeeded(cfg)
|
lb.UpdateConfigIfNeeded(cfg)
|
||||||
return lb
|
return lb
|
||||||
|
|
|
@ -4,11 +4,11 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/rs/zerolog"
|
"github.com/rs/zerolog"
|
||||||
"github.com/yusing/go-proxy/internal/logging"
|
"github.com/rs/zerolog/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
func reqLogger(r *http.Request, level zerolog.Level) *zerolog.Event {
|
func reqLogger(r *http.Request, level zerolog.Level) *zerolog.Event {
|
||||||
return logging.WithLevel(level).
|
return log.WithLevel(level).
|
||||||
Str("remote", r.RemoteAddr).
|
Str("remote", r.RemoteAddr).
|
||||||
Str("host", r.Host).
|
Str("host", r.Host).
|
||||||
Str("uri", r.Method+" "+r.RequestURI)
|
Str("uri", r.Method+" "+r.RequestURI)
|
||||||
|
|
|
@ -4,8 +4,8 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"text/template"
|
"text/template"
|
||||||
|
|
||||||
|
"github.com/rs/zerolog/log"
|
||||||
"github.com/yusing/go-proxy/internal/auth"
|
"github.com/yusing/go-proxy/internal/auth"
|
||||||
"github.com/yusing/go-proxy/internal/logging"
|
|
||||||
"github.com/yusing/go-proxy/internal/net/gphttp"
|
"github.com/yusing/go-proxy/internal/net/gphttp"
|
||||||
|
|
||||||
_ "embed"
|
_ "embed"
|
||||||
|
@ -55,7 +55,7 @@ func PreRequest(p Provider, w http.ResponseWriter, r *http.Request) (proceed boo
|
||||||
"FormHTML": p.FormHTML(),
|
"FormHTML": p.FormHTML(),
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logging.Error().Err(err).Msg("failed to execute captcha page")
|
log.Error().Err(err).Msg("failed to execute captcha page")
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,8 +9,8 @@ import (
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/rs/zerolog/log"
|
||||||
"github.com/yusing/go-proxy/internal/common"
|
"github.com/yusing/go-proxy/internal/common"
|
||||||
"github.com/yusing/go-proxy/internal/logging"
|
|
||||||
"github.com/yusing/go-proxy/internal/net/types"
|
"github.com/yusing/go-proxy/internal/net/types"
|
||||||
"github.com/yusing/go-proxy/internal/utils/atomic"
|
"github.com/yusing/go-proxy/internal/utils/atomic"
|
||||||
"github.com/yusing/go-proxy/internal/utils/strutils"
|
"github.com/yusing/go-proxy/internal/utils/strutils"
|
||||||
|
@ -89,16 +89,16 @@ func tryFetchCFCIDR() (cfCIDRs []*types.CIDR) {
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
cfCIDRsLastUpdate.Store(time.Now().Add(-cfCIDRsUpdateRetryInterval - cfCIDRsUpdateInterval))
|
cfCIDRsLastUpdate.Store(time.Now().Add(-cfCIDRsUpdateRetryInterval - cfCIDRsUpdateInterval))
|
||||||
logging.Err(err).Msg("failed to update cloudflare range, retry in " + strutils.FormatDuration(cfCIDRsUpdateRetryInterval))
|
log.Err(err).Msg("failed to update cloudflare range, retry in " + strutils.FormatDuration(cfCIDRsUpdateRetryInterval))
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
if len(cfCIDRs) == 0 {
|
if len(cfCIDRs) == 0 {
|
||||||
logging.Warn().Msg("cloudflare CIDR range is empty")
|
log.Warn().Msg("cloudflare CIDR range is empty")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
cfCIDRsLastUpdate.Store(time.Now())
|
cfCIDRsLastUpdate.Store(time.Now())
|
||||||
logging.Info().Msg("cloudflare CIDR range updated")
|
log.Info().Msg("cloudflare CIDR range updated")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -8,7 +8,7 @@ import (
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/yusing/go-proxy/internal/logging"
|
"github.com/rs/zerolog/log"
|
||||||
gphttp "github.com/yusing/go-proxy/internal/net/gphttp"
|
gphttp "github.com/yusing/go-proxy/internal/net/gphttp"
|
||||||
"github.com/yusing/go-proxy/internal/net/gphttp/httpheaders"
|
"github.com/yusing/go-proxy/internal/net/gphttp/httpheaders"
|
||||||
"github.com/yusing/go-proxy/internal/net/gphttp/middleware/errorpage"
|
"github.com/yusing/go-proxy/internal/net/gphttp/middleware/errorpage"
|
||||||
|
@ -32,7 +32,7 @@ func (customErrorPage) modifyResponse(resp *http.Response) error {
|
||||||
if !gphttp.IsSuccess(resp.StatusCode) && (contentType.IsHTML() || contentType.IsPlainText()) {
|
if !gphttp.IsSuccess(resp.StatusCode) && (contentType.IsHTML() || contentType.IsPlainText()) {
|
||||||
errorPage, ok := errorpage.GetErrorPageByStatus(resp.StatusCode)
|
errorPage, ok := errorpage.GetErrorPageByStatus(resp.StatusCode)
|
||||||
if ok {
|
if ok {
|
||||||
logging.Debug().Msgf("error page for status %d loaded", resp.StatusCode)
|
log.Debug().Msgf("error page for status %d loaded", resp.StatusCode)
|
||||||
_, _ = io.Copy(io.Discard, resp.Body) // drain the original body
|
_, _ = io.Copy(io.Discard, resp.Body) // drain the original body
|
||||||
resp.Body.Close()
|
resp.Body.Close()
|
||||||
resp.Body = io.NopCloser(bytes.NewReader(errorPage))
|
resp.Body = io.NopCloser(bytes.NewReader(errorPage))
|
||||||
|
@ -40,7 +40,7 @@ func (customErrorPage) modifyResponse(resp *http.Response) error {
|
||||||
resp.Header.Set(httpheaders.HeaderContentLength, strconv.Itoa(len(errorPage)))
|
resp.Header.Set(httpheaders.HeaderContentLength, strconv.Itoa(len(errorPage)))
|
||||||
resp.Header.Set(httpheaders.HeaderContentType, "text/html; charset=utf-8")
|
resp.Header.Set(httpheaders.HeaderContentType, "text/html; charset=utf-8")
|
||||||
} else {
|
} else {
|
||||||
logging.Error().Msgf("unable to load error page for status %d", resp.StatusCode)
|
log.Error().Msgf("unable to load error page for status %d", resp.StatusCode)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -56,7 +56,7 @@ func ServeStaticErrorPageFile(w http.ResponseWriter, r *http.Request) (served bo
|
||||||
filename := path[len(StaticFilePathPrefix):]
|
filename := path[len(StaticFilePathPrefix):]
|
||||||
file, ok := errorpage.GetStaticFile(filename)
|
file, ok := errorpage.GetStaticFile(filename)
|
||||||
if !ok {
|
if !ok {
|
||||||
logging.Error().Msg("unable to load resource " + filename)
|
log.Error().Msg("unable to load resource " + filename)
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
ext := filepath.Ext(filename)
|
ext := filepath.Ext(filename)
|
||||||
|
@ -68,10 +68,10 @@ func ServeStaticErrorPageFile(w http.ResponseWriter, r *http.Request) (served bo
|
||||||
case ".css":
|
case ".css":
|
||||||
w.Header().Set(httpheaders.HeaderContentType, "text/css; charset=utf-8")
|
w.Header().Set(httpheaders.HeaderContentType, "text/css; charset=utf-8")
|
||||||
default:
|
default:
|
||||||
logging.Error().Msgf("unexpected file type %q for %s", ext, filename)
|
log.Error().Msgf("unexpected file type %q for %s", ext, filename)
|
||||||
}
|
}
|
||||||
if _, err := w.Write(file); err != nil {
|
if _, err := w.Write(file); err != nil {
|
||||||
logging.Err(err).Msg("unable to write resource " + filename)
|
log.Err(err).Msg("unable to write resource " + filename)
|
||||||
http.Error(w, "Error page failure", http.StatusInternalServerError)
|
http.Error(w, "Error page failure", http.StatusInternalServerError)
|
||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
|
|
|
@ -6,9 +6,9 @@ import (
|
||||||
"path"
|
"path"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
|
"github.com/rs/zerolog/log"
|
||||||
"github.com/yusing/go-proxy/internal/common"
|
"github.com/yusing/go-proxy/internal/common"
|
||||||
"github.com/yusing/go-proxy/internal/gperr"
|
"github.com/yusing/go-proxy/internal/gperr"
|
||||||
"github.com/yusing/go-proxy/internal/logging"
|
|
||||||
"github.com/yusing/go-proxy/internal/task"
|
"github.com/yusing/go-proxy/internal/task"
|
||||||
U "github.com/yusing/go-proxy/internal/utils"
|
U "github.com/yusing/go-proxy/internal/utils"
|
||||||
F "github.com/yusing/go-proxy/internal/utils/functional"
|
F "github.com/yusing/go-proxy/internal/utils/functional"
|
||||||
|
@ -48,7 +48,7 @@ func GetErrorPageByStatus(statusCode int) (content []byte, ok bool) {
|
||||||
func loadContent() {
|
func loadContent() {
|
||||||
files, err := U.ListFiles(errPagesBasePath, 0)
|
files, err := U.ListFiles(errPagesBasePath, 0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logging.Err(err).Msg("failed to list error page resources")
|
log.Err(err).Msg("failed to list error page resources")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
for _, file := range files {
|
for _, file := range files {
|
||||||
|
@ -57,11 +57,11 @@ func loadContent() {
|
||||||
}
|
}
|
||||||
content, err := os.ReadFile(file)
|
content, err := os.ReadFile(file)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logging.Warn().Err(err).Msgf("failed to read error page resource %s", file)
|
log.Warn().Err(err).Msgf("failed to read error page resource %s", file)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
file = path.Base(file)
|
file = path.Base(file)
|
||||||
logging.Info().Msgf("error page resource %s loaded", file)
|
log.Info().Msgf("error page resource %s loaded", file)
|
||||||
fileContentMap.Store(file, content)
|
fileContentMap.Store(file, content)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -83,9 +83,9 @@ func watchDir() {
|
||||||
loadContent()
|
loadContent()
|
||||||
case events.ActionFileDeleted:
|
case events.ActionFileDeleted:
|
||||||
fileContentMap.Delete(filename)
|
fileContentMap.Delete(filename)
|
||||||
logging.Warn().Msgf("error page resource %s deleted", filename)
|
log.Warn().Msgf("error page resource %s deleted", filename)
|
||||||
case events.ActionFileRenamed:
|
case events.ActionFileRenamed:
|
||||||
logging.Warn().Msgf("error page resource %s deleted", filename)
|
log.Warn().Msgf("error page resource %s deleted", filename)
|
||||||
fileContentMap.Delete(filename)
|
fileContentMap.Delete(filename)
|
||||||
loadContent()
|
loadContent()
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,8 +8,8 @@ import (
|
||||||
"sort"
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/rs/zerolog/log"
|
||||||
"github.com/yusing/go-proxy/internal/gperr"
|
"github.com/yusing/go-proxy/internal/gperr"
|
||||||
"github.com/yusing/go-proxy/internal/logging"
|
|
||||||
gphttp "github.com/yusing/go-proxy/internal/net/gphttp"
|
gphttp "github.com/yusing/go-proxy/internal/net/gphttp"
|
||||||
"github.com/yusing/go-proxy/internal/net/gphttp/reverseproxy"
|
"github.com/yusing/go-proxy/internal/net/gphttp/reverseproxy"
|
||||||
"github.com/yusing/go-proxy/internal/serialization"
|
"github.com/yusing/go-proxy/internal/serialization"
|
||||||
|
@ -87,7 +87,7 @@ func NewMiddleware[ImplType any]() *Middleware {
|
||||||
func (m *Middleware) enableTrace() {
|
func (m *Middleware) enableTrace() {
|
||||||
if tracer, ok := m.impl.(MiddlewareWithTracer); ok {
|
if tracer, ok := m.impl.(MiddlewareWithTracer); ok {
|
||||||
tracer.enableTrace()
|
tracer.enableTrace()
|
||||||
logging.Trace().Msgf("middleware %s enabled trace", m.name)
|
log.Trace().Msgf("middleware %s enabled trace", m.name)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -3,9 +3,9 @@ package middleware
|
||||||
import (
|
import (
|
||||||
"path"
|
"path"
|
||||||
|
|
||||||
|
"github.com/rs/zerolog/log"
|
||||||
"github.com/yusing/go-proxy/internal/common"
|
"github.com/yusing/go-proxy/internal/common"
|
||||||
"github.com/yusing/go-proxy/internal/gperr"
|
"github.com/yusing/go-proxy/internal/gperr"
|
||||||
"github.com/yusing/go-proxy/internal/logging"
|
|
||||||
"github.com/yusing/go-proxy/internal/utils"
|
"github.com/yusing/go-proxy/internal/utils"
|
||||||
"github.com/yusing/go-proxy/internal/utils/strutils"
|
"github.com/yusing/go-proxy/internal/utils/strutils"
|
||||||
)
|
)
|
||||||
|
@ -59,7 +59,7 @@ func LoadComposeFiles() {
|
||||||
errs := gperr.NewBuilder("middleware compile errors")
|
errs := gperr.NewBuilder("middleware compile errors")
|
||||||
middlewareDefs, err := utils.ListFiles(common.MiddlewareComposeBasePath, 0)
|
middlewareDefs, err := utils.ListFiles(common.MiddlewareComposeBasePath, 0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logging.Err(err).Msg("failed to list middleware definitions")
|
log.Err(err).Msg("failed to list middleware definitions")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
for _, defFile := range middlewareDefs {
|
for _, defFile := range middlewareDefs {
|
||||||
|
@ -75,7 +75,7 @@ func LoadComposeFiles() {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
allMiddlewares[name] = m
|
allMiddlewares[name] = m
|
||||||
logging.Info().
|
log.Info().
|
||||||
Str("src", path.Base(defFile)).
|
Str("src", path.Base(defFile)).
|
||||||
Str("name", name).
|
Str("name", name).
|
||||||
Msg("middleware loaded")
|
Msg("middleware loaded")
|
||||||
|
@ -94,7 +94,7 @@ func LoadComposeFiles() {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
allMiddlewares[name] = m
|
allMiddlewares[name] = m
|
||||||
logging.Info().
|
log.Info().
|
||||||
Str("src", path.Base(defFile)).
|
Str("src", path.Base(defFile)).
|
||||||
Str("name", name).
|
Str("name", name).
|
||||||
Msg("middleware loaded")
|
Msg("middleware loaded")
|
||||||
|
|
|
@ -25,7 +25,7 @@ import (
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"github.com/rs/zerolog"
|
"github.com/rs/zerolog"
|
||||||
"github.com/yusing/go-proxy/internal/logging"
|
"github.com/rs/zerolog/log"
|
||||||
"github.com/yusing/go-proxy/internal/logging/accesslog"
|
"github.com/yusing/go-proxy/internal/logging/accesslog"
|
||||||
"github.com/yusing/go-proxy/internal/net/gphttp/httpheaders"
|
"github.com/yusing/go-proxy/internal/net/gphttp/httpheaders"
|
||||||
"github.com/yusing/go-proxy/internal/net/types"
|
"github.com/yusing/go-proxy/internal/net/types"
|
||||||
|
@ -138,7 +138,7 @@ func NewReverseProxy(name string, target *types.URL, transport http.RoundTripper
|
||||||
panic("nil transport")
|
panic("nil transport")
|
||||||
}
|
}
|
||||||
rp := &ReverseProxy{
|
rp := &ReverseProxy{
|
||||||
Logger: logging.With().Str("name", name).Logger(),
|
Logger: log.With().Str("name", name).Logger(),
|
||||||
Transport: transport,
|
Transport: transport,
|
||||||
TargetName: name,
|
TargetName: name,
|
||||||
TargetURL: target,
|
TargetURL: target,
|
||||||
|
@ -173,17 +173,17 @@ func (p *ReverseProxy) errorHandler(rw http.ResponseWriter, r *http.Request, err
|
||||||
case errors.Is(err, context.Canceled),
|
case errors.Is(err, context.Canceled),
|
||||||
errors.Is(err, io.EOF),
|
errors.Is(err, io.EOF),
|
||||||
errors.Is(err, context.DeadlineExceeded):
|
errors.Is(err, context.DeadlineExceeded):
|
||||||
logging.Debug().Err(err).Str("url", reqURL).Msg("http proxy error")
|
log.Debug().Err(err).Str("url", reqURL).Msg("http proxy error")
|
||||||
default:
|
default:
|
||||||
var recordErr tls.RecordHeaderError
|
var recordErr tls.RecordHeaderError
|
||||||
if errors.As(err, &recordErr) {
|
if errors.As(err, &recordErr) {
|
||||||
logging.Error().
|
log.Error().
|
||||||
Str("url", reqURL).
|
Str("url", reqURL).
|
||||||
Msgf(`scheme was likely misconfigured as https,
|
Msgf(`scheme was likely misconfigured as https,
|
||||||
try setting "proxy.%s.scheme" back to "http"`, p.TargetName)
|
try setting "proxy.%s.scheme" back to "http"`, p.TargetName)
|
||||||
logging.Err(err).Msg("underlying error")
|
log.Err(err).Msg("underlying error")
|
||||||
} else {
|
} else {
|
||||||
logging.Err(err).Str("url", reqURL).Msg("http proxy error")
|
log.Err(err).Str("url", reqURL).Msg("http proxy error")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -9,9 +9,9 @@ import (
|
||||||
|
|
||||||
"github.com/quic-go/quic-go/http3"
|
"github.com/quic-go/quic-go/http3"
|
||||||
"github.com/rs/zerolog"
|
"github.com/rs/zerolog"
|
||||||
|
"github.com/rs/zerolog/log"
|
||||||
"github.com/yusing/go-proxy/internal/acl"
|
"github.com/yusing/go-proxy/internal/acl"
|
||||||
"github.com/yusing/go-proxy/internal/common"
|
"github.com/yusing/go-proxy/internal/common"
|
||||||
"github.com/yusing/go-proxy/internal/logging"
|
|
||||||
"github.com/yusing/go-proxy/internal/task"
|
"github.com/yusing/go-proxy/internal/task"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -53,7 +53,7 @@ func StartServer(parent task.Parent, opt Options) (s *Server) {
|
||||||
func NewServer(opt Options) (s *Server) {
|
func NewServer(opt Options) (s *Server) {
|
||||||
var httpSer, httpsSer *http.Server
|
var httpSer, httpsSer *http.Server
|
||||||
|
|
||||||
logger := logging.With().Str("server", opt.Name).Logger()
|
logger := log.With().Str("server", opt.Name).Logger()
|
||||||
|
|
||||||
certAvailable := false
|
certAvailable := false
|
||||||
if opt.CertProvider != nil {
|
if opt.CertProvider != nil {
|
||||||
|
|
|
@ -8,8 +8,8 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/rs/zerolog/log"
|
||||||
"github.com/yusing/go-proxy/internal/gperr"
|
"github.com/yusing/go-proxy/internal/gperr"
|
||||||
"github.com/yusing/go-proxy/internal/logging"
|
|
||||||
"github.com/yusing/go-proxy/internal/serialization"
|
"github.com/yusing/go-proxy/internal/serialization"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -73,7 +73,7 @@ func (msg *LogMessage) notify(ctx context.Context, provider Provider) error {
|
||||||
switch resp.StatusCode {
|
switch resp.StatusCode {
|
||||||
case http.StatusOK, http.StatusCreated, http.StatusAccepted, http.StatusNoContent:
|
case http.StatusOK, http.StatusCreated, http.StatusAccepted, http.StatusNoContent:
|
||||||
body, _ := io.ReadAll(resp.Body)
|
body, _ := io.ReadAll(resp.Body)
|
||||||
logging.Debug().
|
log.Debug().
|
||||||
Str("provider", provider.GetName()).
|
Str("provider", provider.GetName()).
|
||||||
Str("url", provider.GetURL()).
|
Str("url", provider.GetURL()).
|
||||||
Str("status", resp.Status).
|
Str("status", resp.Status).
|
||||||
|
|
|
@ -7,10 +7,10 @@ import (
|
||||||
"github.com/docker/docker/client"
|
"github.com/docker/docker/client"
|
||||||
"github.com/goccy/go-yaml"
|
"github.com/goccy/go-yaml"
|
||||||
"github.com/rs/zerolog"
|
"github.com/rs/zerolog"
|
||||||
|
"github.com/rs/zerolog/log"
|
||||||
"github.com/yusing/go-proxy/internal/common"
|
"github.com/yusing/go-proxy/internal/common"
|
||||||
"github.com/yusing/go-proxy/internal/docker"
|
"github.com/yusing/go-proxy/internal/docker"
|
||||||
"github.com/yusing/go-proxy/internal/gperr"
|
"github.com/yusing/go-proxy/internal/gperr"
|
||||||
"github.com/yusing/go-proxy/internal/logging"
|
|
||||||
"github.com/yusing/go-proxy/internal/route"
|
"github.com/yusing/go-proxy/internal/route"
|
||||||
"github.com/yusing/go-proxy/internal/serialization"
|
"github.com/yusing/go-proxy/internal/serialization"
|
||||||
"github.com/yusing/go-proxy/internal/utils/strutils"
|
"github.com/yusing/go-proxy/internal/utils/strutils"
|
||||||
|
@ -36,7 +36,7 @@ func DockerProviderImpl(name, dockerHost string) ProviderImpl {
|
||||||
return &DockerProvider{
|
return &DockerProvider{
|
||||||
name,
|
name,
|
||||||
dockerHost,
|
dockerHost,
|
||||||
logging.With().Str("type", "docker").Str("name", name).Logger(),
|
log.With().Str("type", "docker").Str("name", name).Logger(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -6,9 +6,9 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/rs/zerolog"
|
"github.com/rs/zerolog"
|
||||||
|
"github.com/rs/zerolog/log"
|
||||||
"github.com/yusing/go-proxy/internal/common"
|
"github.com/yusing/go-proxy/internal/common"
|
||||||
"github.com/yusing/go-proxy/internal/gperr"
|
"github.com/yusing/go-proxy/internal/gperr"
|
||||||
"github.com/yusing/go-proxy/internal/logging"
|
|
||||||
"github.com/yusing/go-proxy/internal/route"
|
"github.com/yusing/go-proxy/internal/route"
|
||||||
"github.com/yusing/go-proxy/internal/serialization"
|
"github.com/yusing/go-proxy/internal/serialization"
|
||||||
W "github.com/yusing/go-proxy/internal/watcher"
|
W "github.com/yusing/go-proxy/internal/watcher"
|
||||||
|
@ -24,7 +24,7 @@ func FileProviderImpl(filename string) (ProviderImpl, error) {
|
||||||
impl := &FileProvider{
|
impl := &FileProvider{
|
||||||
fileName: filename,
|
fileName: filename,
|
||||||
path: path.Join(common.ConfigBasePath, filename),
|
path: path.Join(common.ConfigBasePath, filename),
|
||||||
l: logging.With().Str("type", "file").Str("name", filename).Logger(),
|
l: log.With().Str("type", "file").Str("name", filename).Logger(),
|
||||||
}
|
}
|
||||||
_, err := os.Stat(impl.path)
|
_, err := os.Stat(impl.path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -7,12 +7,12 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/docker/docker/api/types/container"
|
"github.com/docker/docker/api/types/container"
|
||||||
|
"github.com/rs/zerolog/log"
|
||||||
"github.com/yusing/go-proxy/agent/pkg/agent"
|
"github.com/yusing/go-proxy/agent/pkg/agent"
|
||||||
"github.com/yusing/go-proxy/internal/docker"
|
"github.com/yusing/go-proxy/internal/docker"
|
||||||
"github.com/yusing/go-proxy/internal/gperr"
|
"github.com/yusing/go-proxy/internal/gperr"
|
||||||
"github.com/yusing/go-proxy/internal/homepage"
|
"github.com/yusing/go-proxy/internal/homepage"
|
||||||
idlewatcher "github.com/yusing/go-proxy/internal/idlewatcher/types"
|
idlewatcher "github.com/yusing/go-proxy/internal/idlewatcher/types"
|
||||||
"github.com/yusing/go-proxy/internal/logging"
|
|
||||||
netutils "github.com/yusing/go-proxy/internal/net"
|
netutils "github.com/yusing/go-proxy/internal/net"
|
||||||
net "github.com/yusing/go-proxy/internal/net/types"
|
net "github.com/yusing/go-proxy/internal/net/types"
|
||||||
"github.com/yusing/go-proxy/internal/proxmox"
|
"github.com/yusing/go-proxy/internal/proxmox"
|
||||||
|
@ -116,7 +116,7 @@ func (r *Route) Validate() gperr.Error {
|
||||||
Subject(containerName)
|
Subject(containerName)
|
||||||
}
|
}
|
||||||
|
|
||||||
l := logging.With().Str("container", containerName).Logger()
|
l := log.With().Str("container", containerName).Logger()
|
||||||
|
|
||||||
l.Info().Msg("checking if container is running")
|
l.Info().Msg("checking if container is running")
|
||||||
running, err := node.LXCIsRunning(ctx, vmid)
|
running, err := node.LXCIsRunning(ctx, vmid)
|
||||||
|
|
|
@ -5,9 +5,9 @@ import (
|
||||||
"errors"
|
"errors"
|
||||||
|
|
||||||
"github.com/rs/zerolog"
|
"github.com/rs/zerolog"
|
||||||
|
"github.com/rs/zerolog/log"
|
||||||
"github.com/yusing/go-proxy/internal/gperr"
|
"github.com/yusing/go-proxy/internal/gperr"
|
||||||
"github.com/yusing/go-proxy/internal/idlewatcher"
|
"github.com/yusing/go-proxy/internal/idlewatcher"
|
||||||
"github.com/yusing/go-proxy/internal/logging"
|
|
||||||
net "github.com/yusing/go-proxy/internal/net/types"
|
net "github.com/yusing/go-proxy/internal/net/types"
|
||||||
"github.com/yusing/go-proxy/internal/route/routes"
|
"github.com/yusing/go-proxy/internal/route/routes"
|
||||||
"github.com/yusing/go-proxy/internal/task"
|
"github.com/yusing/go-proxy/internal/task"
|
||||||
|
@ -32,7 +32,7 @@ func NewStreamRoute(base *Route) (routes.Route, gperr.Error) {
|
||||||
// TODO: support non-coherent scheme
|
// TODO: support non-coherent scheme
|
||||||
return &StreamRoute{
|
return &StreamRoute{
|
||||||
Route: base,
|
Route: base,
|
||||||
l: logging.With().
|
l: log.With().
|
||||||
Str("type", string(base.Scheme)).
|
Str("type", string(base.Scheme)).
|
||||||
Str("name", base.Name()).
|
Str("name", base.Name()).
|
||||||
Logger(),
|
Logger(),
|
||||||
|
|
|
@ -6,8 +6,8 @@ import (
|
||||||
"net"
|
"net"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
|
"github.com/rs/zerolog/log"
|
||||||
"github.com/yusing/go-proxy/internal/gperr"
|
"github.com/yusing/go-proxy/internal/gperr"
|
||||||
"github.com/yusing/go-proxy/internal/logging"
|
|
||||||
"github.com/yusing/go-proxy/internal/net/types"
|
"github.com/yusing/go-proxy/internal/net/types"
|
||||||
F "github.com/yusing/go-proxy/internal/utils/functional"
|
F "github.com/yusing/go-proxy/internal/utils/functional"
|
||||||
)
|
)
|
||||||
|
@ -88,7 +88,7 @@ func (w *UDPForwarder) dialDst() (dstConn net.Conn, err error) {
|
||||||
func (w *UDPForwarder) readFromListener(buf *UDPBuf) (srcAddr *net.UDPAddr, err error) {
|
func (w *UDPForwarder) readFromListener(buf *UDPBuf) (srcAddr *net.UDPAddr, err error) {
|
||||||
buf.n, buf.oobn, _, srcAddr, err = w.forwarder.ReadMsgUDP(buf.data, buf.oob)
|
buf.n, buf.oobn, _, srcAddr, err = w.forwarder.ReadMsgUDP(buf.data, buf.oob)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
logging.Debug().Msgf("read from listener udp://%s success (n: %d, oobn: %d)", w.Addr().String(), buf.n, buf.oobn)
|
log.Debug().Msgf("read from listener udp://%s success (n: %d, oobn: %d)", w.Addr().String(), buf.n, buf.oobn)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -102,7 +102,7 @@ func (conn *UDPConn) read() (err error) {
|
||||||
conn.buf.oobn = 0
|
conn.buf.oobn = 0
|
||||||
}
|
}
|
||||||
if err == nil {
|
if err == nil {
|
||||||
logging.Debug().Msgf("read from dst %s success (n: %d, oobn: %d)", conn.DstAddrString(), conn.buf.n, conn.buf.oobn)
|
log.Debug().Msgf("read from dst %s success (n: %d, oobn: %d)", conn.DstAddrString(), conn.buf.n, conn.buf.oobn)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -110,7 +110,7 @@ func (conn *UDPConn) read() (err error) {
|
||||||
func (w *UDPForwarder) writeToSrc(srcAddr *net.UDPAddr, buf *UDPBuf) (err error) {
|
func (w *UDPForwarder) writeToSrc(srcAddr *net.UDPAddr, buf *UDPBuf) (err error) {
|
||||||
buf.n, buf.oobn, err = w.forwarder.WriteMsgUDP(buf.data[:buf.n], buf.oob[:buf.oobn], srcAddr)
|
buf.n, buf.oobn, err = w.forwarder.WriteMsgUDP(buf.data[:buf.n], buf.oob[:buf.oobn], srcAddr)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
logging.Debug().Msgf("write to src %s://%s success (n: %d, oobn: %d)", srcAddr.Network(), srcAddr.String(), buf.n, buf.oobn)
|
log.Debug().Msgf("write to src %s://%s success (n: %d, oobn: %d)", srcAddr.Network(), srcAddr.String(), buf.n, buf.oobn)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -120,12 +120,12 @@ func (conn *UDPConn) write() (err error) {
|
||||||
case *net.UDPConn:
|
case *net.UDPConn:
|
||||||
conn.buf.n, conn.buf.oobn, err = dstConn.WriteMsgUDP(conn.buf.data[:conn.buf.n], conn.buf.oob[:conn.buf.oobn], nil)
|
conn.buf.n, conn.buf.oobn, err = dstConn.WriteMsgUDP(conn.buf.data[:conn.buf.n], conn.buf.oob[:conn.buf.oobn], nil)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
logging.Debug().Msgf("write to dst %s success (n: %d, oobn: %d)", conn.DstAddrString(), conn.buf.n, conn.buf.oobn)
|
log.Debug().Msgf("write to dst %s success (n: %d, oobn: %d)", conn.DstAddrString(), conn.buf.n, conn.buf.oobn)
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
_, err = dstConn.Write(conn.buf.data[:conn.buf.n])
|
_, err = dstConn.Write(conn.buf.data[:conn.buf.n])
|
||||||
if err == nil {
|
if err == nil {
|
||||||
logging.Debug().Msgf("write to dst %s success (n: %d)", conn.DstAddrString(), conn.buf.n)
|
log.Debug().Msgf("write to dst %s success (n: %d)", conn.DstAddrString(), conn.buf.n)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -7,9 +7,9 @@ import (
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/rs/zerolog/log"
|
||||||
"github.com/yusing/go-proxy/internal/common"
|
"github.com/yusing/go-proxy/internal/common"
|
||||||
"github.com/yusing/go-proxy/internal/gperr"
|
"github.com/yusing/go-proxy/internal/gperr"
|
||||||
"github.com/yusing/go-proxy/internal/logging"
|
|
||||||
"github.com/yusing/go-proxy/internal/utils/strutils"
|
"github.com/yusing/go-proxy/internal/utils/strutils"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -116,14 +116,14 @@ func (t *Task) Finish(reason any) {
|
||||||
func (t *Task) finish(reason any) {
|
func (t *Task) finish(reason any) {
|
||||||
t.cancel(fmtCause(reason))
|
t.cancel(fmtCause(reason))
|
||||||
if !waitWithTimeout(t.childrenDone) {
|
if !waitWithTimeout(t.childrenDone) {
|
||||||
logging.Debug().
|
log.Debug().
|
||||||
Str("task", t.name).
|
Str("task", t.name).
|
||||||
Strs("subtasks", t.listChildren()).
|
Strs("subtasks", t.listChildren()).
|
||||||
Msg("Timeout waiting for subtasks to finish")
|
Msg("Timeout waiting for subtasks to finish")
|
||||||
}
|
}
|
||||||
go t.runCallbacks()
|
go t.runCallbacks()
|
||||||
if !waitWithTimeout(t.callbacksDone) {
|
if !waitWithTimeout(t.callbacksDone) {
|
||||||
logging.Debug().
|
log.Debug().
|
||||||
Str("task", t.name).
|
Str("task", t.name).
|
||||||
Strs("callbacks", t.listCallbacks()).
|
Strs("callbacks", t.listCallbacks()).
|
||||||
Msg("Timeout waiting for callbacks to finish")
|
Msg("Timeout waiting for callbacks to finish")
|
||||||
|
@ -134,7 +134,7 @@ func (t *Task) finish(reason any) {
|
||||||
}
|
}
|
||||||
t.parent.subChildCount()
|
t.parent.subChildCount()
|
||||||
allTasks.Remove(t)
|
allTasks.Remove(t)
|
||||||
logging.Trace().Msg("task " + t.name + " finished")
|
log.Trace().Msg("task " + t.name + " finished")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Subtask returns a new subtask with the given name, derived from the parent's context.
|
// Subtask returns a new subtask with the given name, derived from the parent's context.
|
||||||
|
@ -166,7 +166,7 @@ func (t *Task) Subtask(name string, needFinish ...bool) *Task {
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
|
||||||
logging.Trace().Msg("task " + child.name + " started")
|
log.Trace().Msg("task " + child.name + " started")
|
||||||
return child
|
return child
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -189,7 +189,7 @@ func (t *Task) MarshalText() ([]byte, error) {
|
||||||
func (t *Task) invokeWithRecover(fn func(), caller string) {
|
func (t *Task) invokeWithRecover(fn func(), caller string) {
|
||||||
defer func() {
|
defer func() {
|
||||||
if err := recover(); err != nil {
|
if err := recover(); err != nil {
|
||||||
logging.Error().
|
log.Error().
|
||||||
Interface("err", err).
|
Interface("err", err).
|
||||||
Msg("panic in task " + t.name + "." + caller)
|
Msg("panic in task " + t.name + "." + caller)
|
||||||
if common.IsDebug {
|
if common.IsDebug {
|
||||||
|
|
|
@ -9,7 +9,7 @@ import (
|
||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/yusing/go-proxy/internal/logging"
|
"github.com/rs/zerolog/log"
|
||||||
F "github.com/yusing/go-proxy/internal/utils/functional"
|
F "github.com/yusing/go-proxy/internal/utils/functional"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -68,10 +68,10 @@ func GracefulShutdown(timeout time.Duration) (err error) {
|
||||||
case <-after:
|
case <-after:
|
||||||
b, err := json.Marshal(DebugTaskList())
|
b, err := json.Marshal(DebugTaskList())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logging.Warn().Err(err).Msg("failed to marshal tasks")
|
log.Warn().Err(err).Msg("failed to marshal tasks")
|
||||||
return context.DeadlineExceeded
|
return context.DeadlineExceeded
|
||||||
}
|
}
|
||||||
logging.Warn().RawJSON("tasks", b).Msgf("Timeout waiting for these %d tasks to finish", allTasks.Size())
|
log.Warn().RawJSON("tasks", b).Msgf("Timeout waiting for these %d tasks to finish", allTasks.Size())
|
||||||
return context.DeadlineExceeded
|
return context.DeadlineExceeded
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -87,6 +87,6 @@ func WaitExit(shutdownTimeout int) {
|
||||||
<-sig
|
<-sig
|
||||||
|
|
||||||
// gracefully shutdown
|
// gracefully shutdown
|
||||||
logging.Info().Msg("shutting down")
|
log.Info().Msg("shutting down")
|
||||||
_ = GracefulShutdown(time.Second * time.Duration(shutdownTimeout))
|
_ = GracefulShutdown(time.Second * time.Duration(shutdownTimeout))
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,8 +8,8 @@ import (
|
||||||
|
|
||||||
"github.com/fsnotify/fsnotify"
|
"github.com/fsnotify/fsnotify"
|
||||||
"github.com/rs/zerolog"
|
"github.com/rs/zerolog"
|
||||||
|
"github.com/rs/zerolog/log"
|
||||||
"github.com/yusing/go-proxy/internal/gperr"
|
"github.com/yusing/go-proxy/internal/gperr"
|
||||||
"github.com/yusing/go-proxy/internal/logging"
|
|
||||||
"github.com/yusing/go-proxy/internal/task"
|
"github.com/yusing/go-proxy/internal/task"
|
||||||
"github.com/yusing/go-proxy/internal/watcher/events"
|
"github.com/yusing/go-proxy/internal/watcher/events"
|
||||||
)
|
)
|
||||||
|
@ -41,13 +41,13 @@ func NewDirectoryWatcher(parent task.Parent, dirPath string) *DirWatcher {
|
||||||
//! subdirectories are not watched
|
//! subdirectories are not watched
|
||||||
w, err := fsnotify.NewWatcher()
|
w, err := fsnotify.NewWatcher()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logging.Panic().Err(err).Msg("unable to create fs watcher")
|
log.Panic().Err(err).Msg("unable to create fs watcher")
|
||||||
}
|
}
|
||||||
if err = w.Add(dirPath); err != nil {
|
if err = w.Add(dirPath); err != nil {
|
||||||
logging.Panic().Err(err).Msg("unable to create fs watcher")
|
log.Panic().Err(err).Msg("unable to create fs watcher")
|
||||||
}
|
}
|
||||||
helper := &DirWatcher{
|
helper := &DirWatcher{
|
||||||
Logger: logging.With().
|
Logger: log.With().
|
||||||
Str("type", "dir").
|
Str("type", "dir").
|
||||||
Str("path", dirPath).
|
Str("path", dirPath).
|
||||||
Logger(),
|
Logger(),
|
||||||
|
|
|
@ -8,9 +8,9 @@ import (
|
||||||
docker_events "github.com/docker/docker/api/types/events"
|
docker_events "github.com/docker/docker/api/types/events"
|
||||||
"github.com/docker/docker/api/types/filters"
|
"github.com/docker/docker/api/types/filters"
|
||||||
"github.com/docker/docker/client"
|
"github.com/docker/docker/client"
|
||||||
|
"github.com/rs/zerolog/log"
|
||||||
"github.com/yusing/go-proxy/internal/docker"
|
"github.com/yusing/go-proxy/internal/docker"
|
||||||
"github.com/yusing/go-proxy/internal/gperr"
|
"github.com/yusing/go-proxy/internal/gperr"
|
||||||
"github.com/yusing/go-proxy/internal/logging"
|
|
||||||
"github.com/yusing/go-proxy/internal/watcher/events"
|
"github.com/yusing/go-proxy/internal/watcher/events"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -81,7 +81,7 @@ func (w DockerWatcher) EventsWithOptions(ctx context.Context, options DockerList
|
||||||
}()
|
}()
|
||||||
|
|
||||||
cEventCh, cErrCh := client.Events(ctx, options)
|
cEventCh, cErrCh := client.Events(ctx, options)
|
||||||
defer logging.Debug().Str("host", client.Address()).Msg("docker watcher closed")
|
defer log.Debug().Str("host", client.Address()).Msg("docker watcher closed")
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
|
@ -153,7 +153,7 @@ func checkConnection(ctx context.Context, client *docker.SharedClient) bool {
|
||||||
defer cancel()
|
defer cancel()
|
||||||
err := client.CheckConnection(ctx)
|
err := client.CheckConnection(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logging.Debug().Err(err).Msg("docker watcher: connection failed")
|
log.Debug().Err(err).Msg("docker watcher: connection failed")
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
|
|
|
@ -7,9 +7,9 @@ import (
|
||||||
"net/url"
|
"net/url"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/rs/zerolog/log"
|
||||||
"github.com/yusing/go-proxy/internal/docker"
|
"github.com/yusing/go-proxy/internal/docker"
|
||||||
"github.com/yusing/go-proxy/internal/gperr"
|
"github.com/yusing/go-proxy/internal/gperr"
|
||||||
"github.com/yusing/go-proxy/internal/logging"
|
|
||||||
"github.com/yusing/go-proxy/internal/notif"
|
"github.com/yusing/go-proxy/internal/notif"
|
||||||
"github.com/yusing/go-proxy/internal/route/routes"
|
"github.com/yusing/go-proxy/internal/route/routes"
|
||||||
"github.com/yusing/go-proxy/internal/task"
|
"github.com/yusing/go-proxy/internal/task"
|
||||||
|
@ -48,7 +48,7 @@ func NewMonitor(r routes.Route) health.HealthMonCheck {
|
||||||
case routes.StreamRoute:
|
case routes.StreamRoute:
|
||||||
mon = NewRawHealthMonitor(&r.TargetURL().URL, r.HealthCheckConfig())
|
mon = NewRawHealthMonitor(&r.TargetURL().URL, r.HealthCheckConfig())
|
||||||
default:
|
default:
|
||||||
logging.Panic().Msgf("unexpected route type: %T", r)
|
log.Panic().Msgf("unexpected route type: %T", r)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if r.IsDocker() {
|
if r.IsDocker() {
|
||||||
|
@ -91,7 +91,7 @@ func (mon *monitor) Start(parent task.Parent) gperr.Error {
|
||||||
mon.task = parent.Subtask("health_monitor", true)
|
mon.task = parent.Subtask("health_monitor", true)
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
logger := logging.With().Str("name", mon.service).Logger()
|
logger := log.With().Str("name", mon.service).Logger()
|
||||||
|
|
||||||
defer func() {
|
defer func() {
|
||||||
if mon.status.Load() != health.StatusError {
|
if mon.status.Load() != health.StatusError {
|
||||||
|
@ -221,7 +221,7 @@ func (mon *monitor) MarshalJSON() ([]byte, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (mon *monitor) checkUpdateHealth() error {
|
func (mon *monitor) checkUpdateHealth() error {
|
||||||
logger := logging.With().Str("name", mon.Name()).Logger()
|
logger := log.With().Str("name", mon.Name()).Logger()
|
||||||
result, err := mon.checkHealth()
|
result, err := mon.checkHealth()
|
||||||
|
|
||||||
var lastStatus health.Status
|
var lastStatus health.Status
|
||||||
|
|
|
@ -34,16 +34,16 @@ func init() {
|
||||||
// lastVersion = ParseVersion(lastVersionStr)
|
// lastVersion = ParseVersion(lastVersionStr)
|
||||||
// }
|
// }
|
||||||
// if err != nil && !os.IsNotExist(err) {
|
// if err != nil && !os.IsNotExist(err) {
|
||||||
// logging.Warn().Err(err).Msg("failed to read version file")
|
// log.Warn().Err(err).Msg("failed to read version file")
|
||||||
// return
|
// return
|
||||||
// }
|
// }
|
||||||
// if err := f.Truncate(0); err != nil {
|
// if err := f.Truncate(0); err != nil {
|
||||||
// logging.Warn().Err(err).Msg("failed to truncate version file")
|
// log.Warn().Err(err).Msg("failed to truncate version file")
|
||||||
// return
|
// return
|
||||||
// }
|
// }
|
||||||
// _, err = f.WriteString(version)
|
// _, err = f.WriteString(version)
|
||||||
// if err != nil {
|
// if err != nil {
|
||||||
// logging.Warn().Err(err).Msg("failed to save version file")
|
// log.Warn().Err(err).Msg("failed to save version file")
|
||||||
// return
|
// return
|
||||||
// }
|
// }
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue