mirror of
https://github.com/yusing/godoxy.git
synced 2025-07-16 10:14:04 +02:00
fix logs not printing correctly, removed unneccessary loggers
This commit is contained in:
parent
64e85c3076
commit
dd0bbdc7b4
38 changed files with 130 additions and 150 deletions
|
@ -16,6 +16,7 @@ import (
|
||||||
"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"
|
||||||
E "github.com/yusing/go-proxy/internal/error"
|
E "github.com/yusing/go-proxy/internal/error"
|
||||||
|
"github.com/yusing/go-proxy/internal/homepage"
|
||||||
"github.com/yusing/go-proxy/internal/logging"
|
"github.com/yusing/go-proxy/internal/logging"
|
||||||
"github.com/yusing/go-proxy/internal/net/http/middleware"
|
"github.com/yusing/go-proxy/internal/net/http/middleware"
|
||||||
"github.com/yusing/go-proxy/internal/route/routes/routequery"
|
"github.com/yusing/go-proxy/internal/route/routes/routequery"
|
||||||
|
@ -25,13 +26,15 @@ import (
|
||||||
|
|
||||||
var rawLogger = log.New(os.Stdout, "", 0)
|
var rawLogger = log.New(os.Stdout, "", 0)
|
||||||
|
|
||||||
func main() {
|
func init() {
|
||||||
var out io.Writer = os.Stdout
|
var out io.Writer = os.Stdout
|
||||||
if common.EnableLogStreaming {
|
if common.EnableLogStreaming {
|
||||||
out = io.MultiWriter(out, v1.MemLogger())
|
out = io.MultiWriter(out, v1.MemLogger())
|
||||||
}
|
}
|
||||||
logging.InitLogger(out)
|
logging.InitLogger(out)
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
args := common.GetArgs()
|
args := common.GetArgs()
|
||||||
|
|
||||||
switch args.Command {
|
switch args.Command {
|
||||||
|
@ -94,6 +97,8 @@ func main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
middleware.LoadComposeFiles()
|
middleware.LoadComposeFiles()
|
||||||
|
internal.InitIconListCache()
|
||||||
|
homepage.InitOverridesConfig()
|
||||||
|
|
||||||
var cfg *config.Config
|
var cfg *config.Config
|
||||||
var err E.Error
|
var err E.Error
|
||||||
|
|
|
@ -1,5 +0,0 @@
|
||||||
package autocert
|
|
||||||
|
|
||||||
import "github.com/yusing/go-proxy/internal/logging"
|
|
||||||
|
|
||||||
var logger = logging.With().Str("module", "autocert").Logger()
|
|
|
@ -17,6 +17,7 @@ import (
|
||||||
"github.com/go-acme/lego/v4/registration"
|
"github.com/go-acme/lego/v4/registration"
|
||||||
"github.com/yusing/go-proxy/internal/config/types"
|
"github.com/yusing/go-proxy/internal/config/types"
|
||||||
E "github.com/yusing/go-proxy/internal/error"
|
E "github.com/yusing/go-proxy/internal/error"
|
||||||
|
"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"
|
||||||
"github.com/yusing/go-proxy/internal/utils/strutils"
|
"github.com/yusing/go-proxy/internal/utils/strutils"
|
||||||
|
@ -89,7 +90,7 @@ func (p *Provider) ObtainCert() E.Error {
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
p.legoCert = nil
|
p.legoCert = nil
|
||||||
logger.Err(err).Msg("cert renew failed, fallback to obtain")
|
logging.Err(err).Msg("cert renew failed, fallback to obtain")
|
||||||
} else {
|
} else {
|
||||||
p.legoCert = cert
|
p.legoCert = cert
|
||||||
}
|
}
|
||||||
|
@ -136,7 +137,7 @@ func (p *Provider) LoadCert() E.Error {
|
||||||
p.tlsCert = &cert
|
p.tlsCert = &cert
|
||||||
p.certExpiries = expiries
|
p.certExpiries = expiries
|
||||||
|
|
||||||
logger.Info().Msgf("next renewal in %v", strutils.FormatDuration(time.Until(p.ShouldRenewOn())))
|
logging.Info().Msgf("next renewal in %v", strutils.FormatDuration(time.Until(p.ShouldRenewOn())))
|
||||||
return p.renewIfNeeded()
|
return p.renewIfNeeded()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -172,7 +173,7 @@ func (p *Provider) ScheduleRenewal(parent task.Parent) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if err := p.renewIfNeeded(); err != nil {
|
if err := p.renewIfNeeded(); err != nil {
|
||||||
E.LogWarn("cert renew failed", err, &logger)
|
E.LogWarn("cert renew failed", err)
|
||||||
lastErrOn = time.Now()
|
lastErrOn = time.Now()
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
@ -215,7 +216,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
|
||||||
logger.Info().Msg("reused acme registration from private key")
|
logging.Info().Msg("reused acme registration from private key")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -224,7 +225,7 @@ func (p *Provider) registerACME() error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
p.user.Registration = reg
|
p.user.Registration = reg
|
||||||
logger.Info().Interface("reg", reg).Msg("acme registered")
|
logging.Info().Interface("reg", reg).Msg("acme registered")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -270,7 +271,7 @@ func (p *Provider) certState() CertState {
|
||||||
sort.Strings(certDomains)
|
sort.Strings(certDomains)
|
||||||
|
|
||||||
if !reflect.DeepEqual(certDomains, wantedDomains) {
|
if !reflect.DeepEqual(certDomains, wantedDomains) {
|
||||||
logger.Info().Msgf("cert domains mismatch: %v != %v", certDomains, p.cfg.Domains)
|
logging.Info().Msgf("cert domains mismatch: %v != %v", certDomains, p.cfg.Domains)
|
||||||
return CertStateMismatch
|
return CertStateMismatch
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -284,9 +285,9 @@ func (p *Provider) renewIfNeeded() E.Error {
|
||||||
|
|
||||||
switch p.certState() {
|
switch p.certState() {
|
||||||
case CertStateExpired:
|
case CertStateExpired:
|
||||||
logger.Info().Msg("certs expired, renewing")
|
logging.Info().Msg("certs expired, renewing")
|
||||||
case CertStateMismatch:
|
case CertStateMismatch:
|
||||||
logger.Info().Msg("cert domains mismatch with config, renewing")
|
logging.Info().Msg("cert domains mismatch with config, renewing")
|
||||||
default:
|
default:
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
E "github.com/yusing/go-proxy/internal/error"
|
E "github.com/yusing/go-proxy/internal/error"
|
||||||
|
"github.com/yusing/go-proxy/internal/logging"
|
||||||
"github.com/yusing/go-proxy/internal/utils/strutils"
|
"github.com/yusing/go-proxy/internal/utils/strutils"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -12,14 +13,14 @@ func (p *Provider) Setup() (err E.Error) {
|
||||||
if !err.Is(os.ErrNotExist) { // ignore if cert doesn't exist
|
if !err.Is(os.ErrNotExist) { // ignore if cert doesn't exist
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
logger.Debug().Msg("obtaining cert due to error loading cert")
|
logging.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() {
|
||||||
logger.Info().Msg("certificate expire on " + strutils.FormatTime(expiry))
|
logging.Info().Msg("certificate expire on " + strutils.FormatTime(expiry))
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -38,7 +38,6 @@ type Config struct {
|
||||||
var (
|
var (
|
||||||
instance *Config
|
instance *Config
|
||||||
cfgWatcher watcher.Watcher
|
cfgWatcher watcher.Watcher
|
||||||
logger = logging.With().Str("module", "config").Logger()
|
|
||||||
reloadMu sync.Mutex
|
reloadMu sync.Mutex
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -86,7 +85,7 @@ func WatchChanges() {
|
||||||
configEventFlushInterval,
|
configEventFlushInterval,
|
||||||
OnConfigChange,
|
OnConfigChange,
|
||||||
func(err E.Error) {
|
func(err E.Error) {
|
||||||
E.LogError("config reload error", err, &logger)
|
E.LogError("config reload error", err)
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
eventQueue.Start(cfgWatcher.Events(t.Context()))
|
eventQueue.Start(cfgWatcher.Events(t.Context()))
|
||||||
|
@ -97,15 +96,15 @@ 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:
|
||||||
logger.Warn().Msg(cfgRenameWarn)
|
logging.Warn().Msg(cfgRenameWarn)
|
||||||
return
|
return
|
||||||
case events.ActionFileDeleted:
|
case events.ActionFileDeleted:
|
||||||
logger.Warn().Msg(cfgDeleteWarn)
|
logging.Warn().Msg(cfgDeleteWarn)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := Reload(); err != nil {
|
if err := Reload(); err != nil {
|
||||||
logger.Warn().Msg("using last config")
|
logging.Warn().Msg("using last config")
|
||||||
// recovered in event queue
|
// recovered in event queue
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
@ -178,11 +177,20 @@ func (cfg *Config) StartProxyProviders() {
|
||||||
})
|
})
|
||||||
|
|
||||||
if err := E.Join(errs...); err != nil {
|
if err := E.Join(errs...); err != nil {
|
||||||
E.LogError("route provider errors", err, &logger)
|
E.LogError("route provider errors", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cfg *Config) StartServers() {
|
type StartServersOptions struct {
|
||||||
|
Proxy, API, Metrics bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cfg *Config) StartServers(opts ...*StartServersOptions) {
|
||||||
|
if len(opts) == 0 {
|
||||||
|
opts = append(opts, &StartServersOptions{Proxy: true, API: true, Metrics: true})
|
||||||
|
}
|
||||||
|
opt := opts[0]
|
||||||
|
if opt.Proxy {
|
||||||
server.StartServer(cfg.task, server.Options{
|
server.StartServer(cfg.task, server.Options{
|
||||||
Name: "proxy",
|
Name: "proxy",
|
||||||
CertProvider: cfg.AutoCertProvider(),
|
CertProvider: cfg.AutoCertProvider(),
|
||||||
|
@ -190,13 +198,16 @@ func (cfg *Config) StartServers() {
|
||||||
HTTPSAddr: common.ProxyHTTPSAddr,
|
HTTPSAddr: common.ProxyHTTPSAddr,
|
||||||
Handler: cfg.entrypoint,
|
Handler: cfg.entrypoint,
|
||||||
})
|
})
|
||||||
|
}
|
||||||
|
if opt.API {
|
||||||
server.StartServer(cfg.task, server.Options{
|
server.StartServer(cfg.task, server.Options{
|
||||||
Name: "api",
|
Name: "api",
|
||||||
CertProvider: cfg.AutoCertProvider(),
|
CertProvider: cfg.AutoCertProvider(),
|
||||||
HTTPAddr: common.APIHTTPAddr,
|
HTTPAddr: common.APIHTTPAddr,
|
||||||
Handler: api.NewHandler(cfg),
|
Handler: api.NewHandler(cfg),
|
||||||
})
|
})
|
||||||
if common.PrometheusEnabled {
|
}
|
||||||
|
if opt.Metrics && common.PrometheusEnabled {
|
||||||
server.StartServer(cfg.task, server.Options{
|
server.StartServer(cfg.task, server.Options{
|
||||||
Name: "metrics",
|
Name: "metrics",
|
||||||
CertProvider: cfg.AutoCertProvider(),
|
CertProvider: cfg.AutoCertProvider(),
|
||||||
|
@ -211,12 +222,12 @@ func (cfg *Config) load() E.Error {
|
||||||
|
|
||||||
data, err := os.ReadFile(common.ConfigPath)
|
data, err := os.ReadFile(common.ConfigPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
E.LogFatal(errMsg, err, &logger)
|
E.LogFatal(errMsg, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
model := types.DefaultConfig()
|
model := types.DefaultConfig()
|
||||||
if err := utils.DeserializeYAML(data, model); err != nil {
|
if err := utils.DeserializeYAML(data, model); err != nil {
|
||||||
E.LogFatal(errMsg, err, &logger)
|
E.LogFatal(errMsg, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// errors are non fatal below
|
// errors are non fatal below
|
||||||
|
@ -296,6 +307,6 @@ func (cfg *Config) loadRouteProviders(providers *types.Providers) E.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())
|
||||||
})
|
})
|
||||||
logger.Info().Msg(results.String())
|
logging.Info().Msg(results.String())
|
||||||
return errs.Error()
|
return errs.Error()
|
||||||
}
|
}
|
||||||
|
|
|
@ -119,7 +119,7 @@ func ConnectClient(host string) (*SharedClient, error) {
|
||||||
Client: client,
|
Client: client,
|
||||||
key: host,
|
key: host,
|
||||||
refCount: U.NewRefCounter(),
|
refCount: U.NewRefCounter(),
|
||||||
l: logger.With().Str("address", client.DaemonHost()).Logger(),
|
l: logging.With().Str("address", client.DaemonHost()).Logger(),
|
||||||
}
|
}
|
||||||
c.l.Trace().Msg("client connected")
|
c.l.Trace().Msg("client connected")
|
||||||
|
|
||||||
|
|
|
@ -6,6 +6,7 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/docker/docker/api/types"
|
"github.com/docker/docker/api/types"
|
||||||
|
"github.com/yusing/go-proxy/internal/logging"
|
||||||
U "github.com/yusing/go-proxy/internal/utils"
|
U "github.com/yusing/go-proxy/internal/utils"
|
||||||
"github.com/yusing/go-proxy/internal/utils/strutils"
|
"github.com/yusing/go-proxy/internal/utils/strutils"
|
||||||
)
|
)
|
||||||
|
@ -128,7 +129,7 @@ func (c *Container) setPublicIP() {
|
||||||
}
|
}
|
||||||
url, err := url.Parse(c.DockerHost)
|
url, err := url.Parse(c.DockerHost)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Err(err).Msgf("invalid docker host %q, falling back to 127.0.0.1", c.DockerHost)
|
logging.Err(err).Msgf("invalid docker host %q, falling back to 127.0.0.1", c.DockerHost)
|
||||||
c.PublicIP = "127.0.0.1"
|
c.PublicIP = "127.0.0.1"
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
@ -46,8 +46,6 @@ var (
|
||||||
watcherMapMu sync.Mutex
|
watcherMapMu sync.Mutex
|
||||||
|
|
||||||
errShouldNotReachHere = errors.New("should not reach here")
|
errShouldNotReachHere = errors.New("should not reach here")
|
||||||
|
|
||||||
logger = logging.With().Str("module", "idle_watcher").Logger()
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const dockerReqTimeout = 3 * time.Second
|
const dockerReqTimeout = 3 * time.Second
|
||||||
|
@ -78,7 +76,7 @@ func registerWatcher(watcherTask *task.Task, entry route.Entry, waker *waker) (*
|
||||||
}
|
}
|
||||||
|
|
||||||
w := &Watcher{
|
w := &Watcher{
|
||||||
Logger: logger.With().Str("name", cfg.ContainerName).Logger(),
|
Logger: logging.With().Str("name", cfg.ContainerName).Logger(),
|
||||||
Config: cfg,
|
Config: cfg,
|
||||||
waker: waker,
|
waker: waker,
|
||||||
client: client,
|
client: client,
|
||||||
|
|
|
@ -1,7 +0,0 @@
|
||||||
package docker
|
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/yusing/go-proxy/internal/logging"
|
|
||||||
)
|
|
||||||
|
|
||||||
var logger = logging.With().Str("module", "docker").Logger()
|
|
|
@ -6,6 +6,7 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/yusing/go-proxy/internal/logging"
|
||||||
gphttp "github.com/yusing/go-proxy/internal/net/http"
|
gphttp "github.com/yusing/go-proxy/internal/net/http"
|
||||||
"github.com/yusing/go-proxy/internal/net/http/accesslog"
|
"github.com/yusing/go-proxy/internal/net/http/accesslog"
|
||||||
"github.com/yusing/go-proxy/internal/net/http/middleware"
|
"github.com/yusing/go-proxy/internal/net/http/middleware"
|
||||||
|
@ -50,7 +51,7 @@ func (ep *Entrypoint) SetMiddlewares(mws []map[string]any) error {
|
||||||
}
|
}
|
||||||
ep.middleware = mid
|
ep.middleware = mid
|
||||||
|
|
||||||
logger.Debug().Msg("entrypoint middleware loaded")
|
logging.Debug().Msg("entrypoint middleware loaded")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -64,7 +65,7 @@ func (ep *Entrypoint) SetAccessLogger(parent task.Parent, cfg *accesslog.Config)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
logger.Debug().Msg("entrypoint access logger created")
|
logging.Debug().Msg("entrypoint access logger created")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -89,7 +90,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 {
|
||||||
logger.Err(err).
|
logging.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 +100,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 {
|
||||||
logger.Err(err).Msg("failed to write error page")
|
logging.Err(err).Msg("failed to write error page")
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
http.Error(w, err.Error(), http.StatusNotFound)
|
http.Error(w, err.Error(), http.StatusNotFound)
|
||||||
|
|
|
@ -1,7 +0,0 @@
|
||||||
package entrypoint
|
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/yusing/go-proxy/internal/logging"
|
|
||||||
)
|
|
||||||
|
|
||||||
var logger = logging.With().Str("module", "entrypoint").Logger()
|
|
|
@ -27,7 +27,6 @@ func InitLogger(out io.Writer) {
|
||||||
default:
|
default:
|
||||||
timeFmt = "01-02 15:04"
|
timeFmt = "01-02 15:04"
|
||||||
level = zerolog.InfoLevel
|
level = zerolog.InfoLevel
|
||||||
exclude = []string{"module"}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
prefixLength := len(timeFmt) + 5 // level takes 3 + 2 spaces
|
prefixLength := len(timeFmt) + 5 // level takes 3 + 2 spaces
|
||||||
|
|
|
@ -43,8 +43,6 @@ type (
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
var logger = logging.With().Str("module", "accesslog").Logger()
|
|
||||||
|
|
||||||
func NewAccessLogger(parent task.Parent, io AccessLogIO, cfg *Config) *AccessLogger {
|
func NewAccessLogger(parent task.Parent, io AccessLogIO, cfg *Config) *AccessLogger {
|
||||||
l := &AccessLogger{
|
l := &AccessLogger{
|
||||||
task: parent.Subtask("accesslog"),
|
task: parent.Subtask("accesslog"),
|
||||||
|
@ -133,7 +131,7 @@ func (l *AccessLogger) Flush(force bool) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *AccessLogger) handleErr(err error) {
|
func (l *AccessLogger) handleErr(err error) {
|
||||||
E.LogError("failed to write access log", err, &logger)
|
E.LogError("failed to write access log", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *AccessLogger) start() {
|
func (l *AccessLogger) start() {
|
||||||
|
@ -170,6 +168,6 @@ func (l *AccessLogger) write(data []byte) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
l.handleErr(err)
|
l.handleErr(err)
|
||||||
} else {
|
} else {
|
||||||
logger.Debug().Msg("access log flushed to " + l.io.Name())
|
logging.Debug().Msg("access log flushed to " + l.io.Name())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,6 +6,7 @@ import (
|
||||||
"path"
|
"path"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
|
"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"
|
||||||
)
|
)
|
||||||
|
@ -55,7 +56,7 @@ func (f *File) Close() error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *File) closeOnZero() {
|
func (f *File) closeOnZero() {
|
||||||
defer logger.Debug().
|
defer logging.Debug().
|
||||||
Str("path", f.path).
|
Str("path", f.path).
|
||||||
Msg("access log closed")
|
Msg("access log closed")
|
||||||
|
|
||||||
|
|
|
@ -8,6 +8,8 @@ import (
|
||||||
"net/url"
|
"net/url"
|
||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/yusing/go-proxy/internal/logging"
|
||||||
)
|
)
|
||||||
|
|
||||||
type (
|
type (
|
||||||
|
@ -137,6 +139,6 @@ func (f *JSONFormatter) Format(line *bytes.Buffer, req *http.Request, res *http.
|
||||||
marshaller := json.NewEncoder(line)
|
marshaller := json.NewEncoder(line)
|
||||||
err := marshaller.Encode(entry)
|
err := marshaller.Encode(entry)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Err(err).Msg("failed to marshal json log")
|
logging.Err(err).Msg("failed to marshal json log")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,6 +8,7 @@ import (
|
||||||
"github.com/rs/zerolog"
|
"github.com/rs/zerolog"
|
||||||
"github.com/yusing/go-proxy/internal/common"
|
"github.com/yusing/go-proxy/internal/common"
|
||||||
E "github.com/yusing/go-proxy/internal/error"
|
E "github.com/yusing/go-proxy/internal/error"
|
||||||
|
"github.com/yusing/go-proxy/internal/logging"
|
||||||
"github.com/yusing/go-proxy/internal/net/http/loadbalancer/types"
|
"github.com/yusing/go-proxy/internal/net/http/loadbalancer/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"
|
||||||
|
@ -46,7 +47,7 @@ func New(cfg *Config) *LoadBalancer {
|
||||||
lb := &LoadBalancer{
|
lb := &LoadBalancer{
|
||||||
Config: new(Config),
|
Config: new(Config),
|
||||||
pool: types.NewServerPool(),
|
pool: types.NewServerPool(),
|
||||||
l: logger.With().Str("name", cfg.Link).Logger(),
|
l: logging.With().Str("name", cfg.Link).Logger(),
|
||||||
}
|
}
|
||||||
lb.UpdateConfigIfNeeded(cfg)
|
lb.UpdateConfigIfNeeded(cfg)
|
||||||
return lb
|
return lb
|
||||||
|
|
|
@ -1,5 +0,0 @@
|
||||||
package loadbalancer
|
|
||||||
|
|
||||||
import "github.com/yusing/go-proxy/internal/logging"
|
|
||||||
|
|
||||||
var logger = logging.With().Str("module", "load_balancer").Logger()
|
|
|
@ -30,7 +30,6 @@ const (
|
||||||
var (
|
var (
|
||||||
cfCIDRsLastUpdate time.Time
|
cfCIDRsLastUpdate time.Time
|
||||||
cfCIDRsMu sync.Mutex
|
cfCIDRsMu sync.Mutex
|
||||||
cfCIDRsLogger = logger.With().Str("name", "CloudflareRealIP").Logger()
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var CloudflareRealIP = NewMiddleware[cloudflareRealIP]()
|
var CloudflareRealIP = NewMiddleware[cloudflareRealIP]()
|
||||||
|
@ -87,7 +86,7 @@ func tryFetchCFCIDR() (cfCIDRs []*types.CIDR) {
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
cfCIDRsLastUpdate = time.Now().Add(-cfCIDRsUpdateRetryInterval - cfCIDRsUpdateInterval)
|
cfCIDRsLastUpdate = time.Now().Add(-cfCIDRsUpdateRetryInterval - cfCIDRsUpdateInterval)
|
||||||
cfCIDRsLogger.Err(err).Msg("failed to update cloudflare range, retry in " + strutils.FormatDuration(cfCIDRsUpdateRetryInterval))
|
logging.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 {
|
||||||
|
@ -96,7 +95,7 @@ func tryFetchCFCIDR() (cfCIDRs []*types.CIDR) {
|
||||||
}
|
}
|
||||||
|
|
||||||
cfCIDRsLastUpdate = time.Now()
|
cfCIDRsLastUpdate = time.Now()
|
||||||
cfCIDRsLogger.Info().Msg("cloudflare CIDR range updated")
|
logging.Info().Msg("cloudflare CIDR range updated")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -8,6 +8,7 @@ import (
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/yusing/go-proxy/internal/logging"
|
||||||
gphttp "github.com/yusing/go-proxy/internal/net/http"
|
gphttp "github.com/yusing/go-proxy/internal/net/http"
|
||||||
"github.com/yusing/go-proxy/internal/net/http/middleware/errorpage"
|
"github.com/yusing/go-proxy/internal/net/http/middleware/errorpage"
|
||||||
)
|
)
|
||||||
|
@ -28,7 +29,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 {
|
||||||
logger.Debug().Msgf("error page for status %d loaded", resp.StatusCode)
|
logging.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))
|
||||||
|
@ -36,7 +37,7 @@ func (customErrorPage) modifyResponse(resp *http.Response) error {
|
||||||
resp.Header.Set(gphttp.HeaderContentLength, strconv.Itoa(len(errorPage)))
|
resp.Header.Set(gphttp.HeaderContentLength, strconv.Itoa(len(errorPage)))
|
||||||
resp.Header.Set(gphttp.HeaderContentType, "text/html; charset=utf-8")
|
resp.Header.Set(gphttp.HeaderContentType, "text/html; charset=utf-8")
|
||||||
} else {
|
} else {
|
||||||
logger.Error().Msgf("unable to load error page for status %d", resp.StatusCode)
|
logging.Error().Msgf("unable to load error page for status %d", resp.StatusCode)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -52,7 +53,7 @@ func ServeStaticErrorPageFile(w http.ResponseWriter, r *http.Request) (served bo
|
||||||
filename := path[len(gphttp.StaticFilePathPrefix):]
|
filename := path[len(gphttp.StaticFilePathPrefix):]
|
||||||
file, ok := errorpage.GetStaticFile(filename)
|
file, ok := errorpage.GetStaticFile(filename)
|
||||||
if !ok {
|
if !ok {
|
||||||
logger.Error().Msg("unable to load resource " + filename)
|
logging.Error().Msg("unable to load resource " + filename)
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
ext := filepath.Ext(filename)
|
ext := filepath.Ext(filename)
|
||||||
|
@ -64,10 +65,10 @@ func ServeStaticErrorPageFile(w http.ResponseWriter, r *http.Request) (served bo
|
||||||
case ".css":
|
case ".css":
|
||||||
w.Header().Set(gphttp.HeaderContentType, "text/css; charset=utf-8")
|
w.Header().Set(gphttp.HeaderContentType, "text/css; charset=utf-8")
|
||||||
default:
|
default:
|
||||||
logger.Error().Msgf("unexpected file type %q for %s", ext, filename)
|
logging.Error().Msgf("unexpected file type %q for %s", ext, filename)
|
||||||
}
|
}
|
||||||
if _, err := w.Write(file); err != nil {
|
if _, err := w.Write(file); err != nil {
|
||||||
logger.Err(err).Msg("unable to write resource " + filename)
|
logging.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
|
||||||
|
|
|
@ -55,7 +55,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 {
|
||||||
logger.Err(err).Msg("failed to list error page resources")
|
logging.Err(err).Msg("failed to list error page resources")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
for _, file := range files {
|
for _, file := range files {
|
||||||
|
@ -64,7 +64,7 @@ func loadContent() {
|
||||||
}
|
}
|
||||||
content, err := os.ReadFile(file)
|
content, err := os.ReadFile(file)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Warn().Err(err).Msgf("failed to read error page resource %s", file)
|
logging.Warn().Err(err).Msgf("failed to read error page resource %s", file)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
file = path.Base(file)
|
file = path.Base(file)
|
||||||
|
@ -90,14 +90,14 @@ func watchDir() {
|
||||||
loadContent()
|
loadContent()
|
||||||
case events.ActionFileDeleted:
|
case events.ActionFileDeleted:
|
||||||
fileContentMap.Delete(filename)
|
fileContentMap.Delete(filename)
|
||||||
logger.Warn().Msgf("error page resource %s deleted", filename)
|
logging.Warn().Msgf("error page resource %s deleted", filename)
|
||||||
case events.ActionFileRenamed:
|
case events.ActionFileRenamed:
|
||||||
logger.Warn().Msgf("error page resource %s deleted", filename)
|
logging.Warn().Msgf("error page resource %s deleted", filename)
|
||||||
fileContentMap.Delete(filename)
|
fileContentMap.Delete(filename)
|
||||||
loadContent()
|
loadContent()
|
||||||
}
|
}
|
||||||
case err := <-errCh:
|
case err := <-errCh:
|
||||||
E.LogError("error watching error page directory", err, &logger)
|
E.LogError("error watching error page directory", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +0,0 @@
|
||||||
package errorpage
|
|
||||||
|
|
||||||
import "github.com/yusing/go-proxy/internal/logging"
|
|
||||||
|
|
||||||
var logger = logging.With().Str("module", "errorpage").Logger()
|
|
|
@ -1,5 +0,0 @@
|
||||||
package middleware
|
|
||||||
|
|
||||||
import "github.com/yusing/go-proxy/internal/logging"
|
|
||||||
|
|
||||||
var logger = logging.With().Str("module", "middleware").Logger()
|
|
|
@ -5,6 +5,7 @@ import (
|
||||||
|
|
||||||
"github.com/yusing/go-proxy/internal/common"
|
"github.com/yusing/go-proxy/internal/common"
|
||||||
E "github.com/yusing/go-proxy/internal/error"
|
E "github.com/yusing/go-proxy/internal/error"
|
||||||
|
"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"
|
||||||
)
|
)
|
||||||
|
@ -60,7 +61,7 @@ func LoadComposeFiles() {
|
||||||
errs := E.NewBuilder("middleware compile errors")
|
errs := E.NewBuilder("middleware compile errors")
|
||||||
middlewareDefs, err := utils.ListFiles(common.MiddlewareComposeBasePath, 0)
|
middlewareDefs, err := utils.ListFiles(common.MiddlewareComposeBasePath, 0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Err(err).Msg("failed to list middleware definitions")
|
logging.Err(err).Msg("failed to list middleware definitions")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
for _, defFile := range middlewareDefs {
|
for _, defFile := range middlewareDefs {
|
||||||
|
@ -76,7 +77,7 @@ func LoadComposeFiles() {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
allMiddlewares[name] = m
|
allMiddlewares[name] = m
|
||||||
logger.Info().
|
logging.Info().
|
||||||
Str("src", path.Base(defFile)).
|
Str("src", path.Base(defFile)).
|
||||||
Str("name", name).
|
Str("name", name).
|
||||||
Msg("middleware loaded")
|
Msg("middleware loaded")
|
||||||
|
@ -95,13 +96,13 @@ func LoadComposeFiles() {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
allMiddlewares[name] = m
|
allMiddlewares[name] = m
|
||||||
logger.Info().
|
logging.Info().
|
||||||
Str("src", path.Base(defFile)).
|
Str("src", path.Base(defFile)).
|
||||||
Str("name", name).
|
Str("name", name).
|
||||||
Msg("middleware loaded")
|
Msg("middleware loaded")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if errs.HasError() {
|
if errs.HasError() {
|
||||||
E.LogError(errs.About(), errs.Error(), &logger)
|
E.LogError(errs.About(), errs.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,6 +5,7 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/yusing/go-proxy/internal/common"
|
"github.com/yusing/go-proxy/internal/common"
|
||||||
|
"github.com/yusing/go-proxy/internal/logging"
|
||||||
)
|
)
|
||||||
|
|
||||||
type redirectHTTP struct{}
|
type redirectHTTP struct{}
|
||||||
|
@ -22,7 +23,7 @@ func (redirectHTTP) before(w http.ResponseWriter, r *http.Request) (proceed bool
|
||||||
host = host[:i] // strip port number if present
|
host = host[:i] // strip port number if present
|
||||||
}
|
}
|
||||||
r.URL.Host = host + ":" + common.ProxyHTTPSPort
|
r.URL.Host = host + ":" + common.ProxyHTTPSPort
|
||||||
logger.Debug().Str("url", r.URL.String()).Msg("redirect to https")
|
logging.Debug().Str("url", r.URL.String()).Msg("redirect to https")
|
||||||
http.Redirect(w, r, r.URL.String(), http.StatusTemporaryRedirect)
|
http.Redirect(w, r, r.URL.String(), http.StatusTemporaryRedirect)
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
|
@ -105,8 +105,6 @@ type httpMetricLogger struct {
|
||||||
labels *metrics.HTTPRouteMetricLabels
|
labels *metrics.HTTPRouteMetricLabels
|
||||||
}
|
}
|
||||||
|
|
||||||
var logger = logging.With().Str("module", "reverse_proxy").Logger()
|
|
||||||
|
|
||||||
// WriteHeader implements http.ResponseWriter.
|
// WriteHeader implements http.ResponseWriter.
|
||||||
func (l *httpMetricLogger) WriteHeader(status int) {
|
func (l *httpMetricLogger) WriteHeader(status int) {
|
||||||
l.ResponseWriter.WriteHeader(status)
|
l.ResponseWriter.WriteHeader(status)
|
||||||
|
@ -174,7 +172,7 @@ func NewReverseProxy(name string, target types.URL, transport http.RoundTripper)
|
||||||
panic("nil transport")
|
panic("nil transport")
|
||||||
}
|
}
|
||||||
rp := &ReverseProxy{
|
rp := &ReverseProxy{
|
||||||
Logger: logger.With().Str("name", name).Logger(),
|
Logger: logging.With().Str("name", name).Logger(),
|
||||||
Transport: transport,
|
Transport: transport,
|
||||||
TargetName: name,
|
TargetName: name,
|
||||||
TargetURL: target,
|
TargetURL: target,
|
||||||
|
@ -213,17 +211,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):
|
||||||
logger.Debug().Err(err).Str("url", reqURL).Msg("http proxy error")
|
logging.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) {
|
||||||
logger.Error().
|
logging.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")
|
logging.Err(err).Msg("underlying error")
|
||||||
} else {
|
} else {
|
||||||
logger.Err(err).Str("url", reqURL).Msg("http proxy error")
|
logging.Err(err).Str("url", reqURL).Msg("http proxy error")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -9,6 +9,7 @@ import (
|
||||||
"github.com/yusing/go-proxy/internal/docker"
|
"github.com/yusing/go-proxy/internal/docker"
|
||||||
"github.com/yusing/go-proxy/internal/docker/idlewatcher"
|
"github.com/yusing/go-proxy/internal/docker/idlewatcher"
|
||||||
E "github.com/yusing/go-proxy/internal/error"
|
E "github.com/yusing/go-proxy/internal/error"
|
||||||
|
"github.com/yusing/go-proxy/internal/logging"
|
||||||
gphttp "github.com/yusing/go-proxy/internal/net/http"
|
gphttp "github.com/yusing/go-proxy/internal/net/http"
|
||||||
"github.com/yusing/go-proxy/internal/net/http/accesslog"
|
"github.com/yusing/go-proxy/internal/net/http/accesslog"
|
||||||
"github.com/yusing/go-proxy/internal/net/http/loadbalancer"
|
"github.com/yusing/go-proxy/internal/net/http/loadbalancer"
|
||||||
|
@ -63,7 +64,7 @@ func NewHTTPRoute(entry *entry.ReverseProxyEntry) (impl, E.Error) {
|
||||||
r := &HTTPRoute{
|
r := &HTTPRoute{
|
||||||
ReverseProxyEntry: entry,
|
ReverseProxyEntry: entry,
|
||||||
rp: rp,
|
rp: rp,
|
||||||
l: logger.With().
|
l: logging.With().
|
||||||
Str("type", entry.URL.Scheme).
|
Str("type", entry.URL.Scheme).
|
||||||
Str("name", service).
|
Str("name", service).
|
||||||
Logger(),
|
Logger(),
|
||||||
|
@ -123,7 +124,7 @@ func (r *HTTPRoute) Start(parent task.Parent) E.Error {
|
||||||
case len(pathPatterns) == 1 && pathPatterns[0] == "/":
|
case len(pathPatterns) == 1 && pathPatterns[0] == "/":
|
||||||
r.handler = r.rp
|
r.handler = r.rp
|
||||||
default:
|
default:
|
||||||
logger.Warn().
|
logging.Warn().
|
||||||
Str("route", r.TargetName()).
|
Str("route", r.TargetName()).
|
||||||
Msg("`path_patterns` is deprecated. Use `rules` instead.")
|
Msg("`path_patterns` is deprecated. Use `rules` instead.")
|
||||||
mux := gphttp.NewServeMux()
|
mux := gphttp.NewServeMux()
|
||||||
|
|
|
@ -1,5 +0,0 @@
|
||||||
package route
|
|
||||||
|
|
||||||
import "github.com/yusing/go-proxy/internal/logging"
|
|
||||||
|
|
||||||
var logger = logging.With().Str("module", "route").Logger()
|
|
|
@ -10,6 +10,7 @@ import (
|
||||||
"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"
|
||||||
E "github.com/yusing/go-proxy/internal/error"
|
E "github.com/yusing/go-proxy/internal/error"
|
||||||
|
"github.com/yusing/go-proxy/internal/logging"
|
||||||
"github.com/yusing/go-proxy/internal/route"
|
"github.com/yusing/go-proxy/internal/route"
|
||||||
U "github.com/yusing/go-proxy/internal/utils"
|
U "github.com/yusing/go-proxy/internal/utils"
|
||||||
"github.com/yusing/go-proxy/internal/utils/strutils"
|
"github.com/yusing/go-proxy/internal/utils/strutils"
|
||||||
|
@ -36,7 +37,7 @@ func DockerProviderImpl(name, dockerHost string) (ProviderImpl, error) {
|
||||||
return &DockerProvider{
|
return &DockerProvider{
|
||||||
name,
|
name,
|
||||||
dockerHost,
|
dockerHost,
|
||||||
logger.With().Str("type", "docker").Str("name", name).Logger(),
|
logging.With().Str("type", "docker").Str("name", name).Logger(),
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -8,6 +8,7 @@ import (
|
||||||
"github.com/rs/zerolog"
|
"github.com/rs/zerolog"
|
||||||
"github.com/yusing/go-proxy/internal/common"
|
"github.com/yusing/go-proxy/internal/common"
|
||||||
E "github.com/yusing/go-proxy/internal/error"
|
E "github.com/yusing/go-proxy/internal/error"
|
||||||
|
"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/utils"
|
"github.com/yusing/go-proxy/internal/utils"
|
||||||
W "github.com/yusing/go-proxy/internal/watcher"
|
W "github.com/yusing/go-proxy/internal/watcher"
|
||||||
|
@ -23,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: logger.With().Str("type", "file").Str("name", filename).Logger(),
|
l: logging.With().Str("type", "file").Str("name", filename).Logger(),
|
||||||
}
|
}
|
||||||
_, err := os.Stat(impl.path)
|
_, err := os.Stat(impl.path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -1,5 +0,0 @@
|
||||||
package provider
|
|
||||||
|
|
||||||
import "github.com/yusing/go-proxy/internal/logging"
|
|
||||||
|
|
||||||
var logger = logging.With().Str("module", "provider").Logger()
|
|
|
@ -8,6 +8,7 @@ import (
|
||||||
"github.com/yusing/go-proxy/internal/docker"
|
"github.com/yusing/go-proxy/internal/docker"
|
||||||
"github.com/yusing/go-proxy/internal/docker/idlewatcher"
|
"github.com/yusing/go-proxy/internal/docker/idlewatcher"
|
||||||
E "github.com/yusing/go-proxy/internal/error"
|
E "github.com/yusing/go-proxy/internal/error"
|
||||||
|
"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/entry"
|
"github.com/yusing/go-proxy/internal/route/entry"
|
||||||
"github.com/yusing/go-proxy/internal/route/routes"
|
"github.com/yusing/go-proxy/internal/route/routes"
|
||||||
|
@ -36,7 +37,7 @@ func NewStreamRoute(entry *entry.StreamEntry) (impl, E.Error) {
|
||||||
}
|
}
|
||||||
return &StreamRoute{
|
return &StreamRoute{
|
||||||
StreamEntry: entry,
|
StreamEntry: entry,
|
||||||
l: logger.With().
|
l: logging.With().
|
||||||
Str("type", string(entry.Scheme.ListeningScheme)).
|
Str("type", string(entry.Scheme.ListeningScheme)).
|
||||||
Str("name", entry.TargetName()).
|
Str("name", entry.TargetName()).
|
||||||
Logger(),
|
Logger(),
|
||||||
|
|
|
@ -7,6 +7,7 @@ import (
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
E "github.com/yusing/go-proxy/internal/error"
|
E "github.com/yusing/go-proxy/internal/error"
|
||||||
|
"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"
|
||||||
)
|
)
|
||||||
|
@ -87,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 {
|
||||||
logger.Debug().Msgf("read from listener udp://%s success (n: %d, oobn: %d)", w.Addr().String(), buf.n, buf.oobn)
|
logging.Debug().Msgf("read from listener udp://%s success (n: %d, oobn: %d)", w.Addr().String(), buf.n, buf.oobn)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -101,7 +102,7 @@ func (conn *UDPConn) read() (err error) {
|
||||||
conn.buf.oobn = 0
|
conn.buf.oobn = 0
|
||||||
}
|
}
|
||||||
if err == nil {
|
if err == nil {
|
||||||
logger.Debug().Msgf("read from dst %s success (n: %d, oobn: %d)", conn.DstAddrString(), conn.buf.n, conn.buf.oobn)
|
logging.Debug().Msgf("read from dst %s success (n: %d, oobn: %d)", conn.DstAddrString(), conn.buf.n, conn.buf.oobn)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -109,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 {
|
||||||
logger.Debug().Msgf("write to src %s://%s success (n: %d, oobn: %d)", srcAddr.Network(), srcAddr.String(), buf.n, buf.oobn)
|
logging.Debug().Msgf("write to src %s://%s success (n: %d, oobn: %d)", srcAddr.Network(), srcAddr.String(), buf.n, buf.oobn)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -119,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 {
|
||||||
logger.Debug().Msgf("write to dst %s success (n: %d, oobn: %d)", conn.DstAddrString(), conn.buf.n, conn.buf.oobn)
|
logging.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 {
|
||||||
logger.Debug().Msgf("write to dst %s success (n: %d)", conn.DstAddrString(), conn.buf.n)
|
logging.Debug().Msgf("write to dst %s success (n: %d)", conn.DstAddrString(), conn.buf.n)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -8,6 +8,7 @@ import (
|
||||||
|
|
||||||
"github.com/yusing/go-proxy/internal/common"
|
"github.com/yusing/go-proxy/internal/common"
|
||||||
E "github.com/yusing/go-proxy/internal/error"
|
E "github.com/yusing/go-proxy/internal/error"
|
||||||
|
"github.com/yusing/go-proxy/internal/logging"
|
||||||
"github.com/yusing/go-proxy/internal/utils/strutils"
|
"github.com/yusing/go-proxy/internal/utils/strutils"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -105,14 +106,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) {
|
||||||
logger.Debug().
|
logging.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) {
|
||||||
logger.Debug().
|
logging.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")
|
||||||
|
@ -123,7 +124,7 @@ func (t *Task) finish(reason any) {
|
||||||
}
|
}
|
||||||
t.parent.subChildCount()
|
t.parent.subChildCount()
|
||||||
allTasks.Remove(t)
|
allTasks.Remove(t)
|
||||||
logger.Trace().Msg("task " + t.name + " finished")
|
logging.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.
|
||||||
|
@ -155,7 +156,7 @@ func (t *Task) Subtask(name string, needFinish ...bool) *Task {
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.Trace().Msg("task " + child.name + " started")
|
logging.Trace().Msg("task " + child.name + " started")
|
||||||
return child
|
return child
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -178,7 +179,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 {
|
||||||
logger.Error().
|
logging.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 {
|
||||||
|
|
|
@ -12,8 +12,6 @@ import (
|
||||||
|
|
||||||
var ErrProgramExiting = errors.New("program exiting")
|
var ErrProgramExiting = errors.New("program exiting")
|
||||||
|
|
||||||
var logger = logging.With().Str("module", "task").Logger()
|
|
||||||
|
|
||||||
var (
|
var (
|
||||||
root = newRoot()
|
root = newRoot()
|
||||||
allTasks = F.NewSet[*Task]()
|
allTasks = F.NewSet[*Task]()
|
||||||
|
@ -67,10 +65,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 {
|
||||||
logger.Warn().Err(err).Msg("failed to marshal tasks")
|
logging.Warn().Err(err).Msg("failed to marshal tasks")
|
||||||
return context.DeadlineExceeded
|
return context.DeadlineExceeded
|
||||||
}
|
}
|
||||||
logger.Warn().RawJSON("tasks", b).Msgf("Timeout waiting for these %d tasks to finish", allTasks.Size())
|
logging.Warn().RawJSON("tasks", b).Msgf("Timeout waiting for these %d tasks to finish", allTasks.Size())
|
||||||
return context.DeadlineExceeded
|
return context.DeadlineExceeded
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,13 +8,17 @@ import (
|
||||||
|
|
||||||
// Recursively lists all files in a directory until `maxDepth` is reached
|
// Recursively lists all files in a directory until `maxDepth` is reached
|
||||||
// Returns a slice of file paths relative to `dir`.
|
// Returns a slice of file paths relative to `dir`.
|
||||||
func ListFiles(dir string, maxDepth int) ([]string, error) {
|
func ListFiles(dir string, maxDepth int, hideHidden ...bool) ([]string, error) {
|
||||||
entries, err := os.ReadDir(dir)
|
entries, err := os.ReadDir(dir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error listing directory %s: %w", dir, err)
|
return nil, fmt.Errorf("error listing directory %s: %w", dir, err)
|
||||||
}
|
}
|
||||||
|
hideHiddenFiles := len(hideHidden) > 0 && hideHidden[0]
|
||||||
files := make([]string, 0)
|
files := make([]string, 0)
|
||||||
for _, entry := range entries {
|
for _, entry := range entries {
|
||||||
|
if hideHiddenFiles && entry.Name()[0] == '.' {
|
||||||
|
continue
|
||||||
|
}
|
||||||
if entry.IsDir() {
|
if entry.IsDir() {
|
||||||
if maxDepth <= 0 {
|
if maxDepth <= 0 {
|
||||||
continue
|
continue
|
||||||
|
|
|
@ -9,6 +9,7 @@ import (
|
||||||
"github.com/fsnotify/fsnotify"
|
"github.com/fsnotify/fsnotify"
|
||||||
"github.com/rs/zerolog"
|
"github.com/rs/zerolog"
|
||||||
E "github.com/yusing/go-proxy/internal/error"
|
E "github.com/yusing/go-proxy/internal/error"
|
||||||
|
"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"
|
||||||
)
|
)
|
||||||
|
@ -40,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 {
|
||||||
logger.Panic().Err(err).Msg("unable to create fs watcher")
|
logging.Panic().Err(err).Msg("unable to create fs watcher")
|
||||||
}
|
}
|
||||||
if err = w.Add(dirPath); err != nil {
|
if err = w.Add(dirPath); err != nil {
|
||||||
logger.Panic().Err(err).Msg("unable to create fs watcher")
|
logging.Panic().Err(err).Msg("unable to create fs watcher")
|
||||||
}
|
}
|
||||||
helper := &DirWatcher{
|
helper := &DirWatcher{
|
||||||
Logger: logger.With().
|
Logger: logging.With().
|
||||||
Str("type", "dir").
|
Str("type", "dir").
|
||||||
Str("path", dirPath).
|
Str("path", dirPath).
|
||||||
Logger(),
|
Logger(),
|
||||||
|
|
|
@ -9,6 +9,7 @@ import (
|
||||||
"github.com/rs/zerolog"
|
"github.com/rs/zerolog"
|
||||||
D "github.com/yusing/go-proxy/internal/docker"
|
D "github.com/yusing/go-proxy/internal/docker"
|
||||||
E "github.com/yusing/go-proxy/internal/error"
|
E "github.com/yusing/go-proxy/internal/error"
|
||||||
|
"github.com/yusing/go-proxy/internal/logging"
|
||||||
"github.com/yusing/go-proxy/internal/watcher/events"
|
"github.com/yusing/go-proxy/internal/watcher/events"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -55,7 +56,7 @@ func NewDockerWatcher(host string) DockerWatcher {
|
||||||
return DockerWatcher{
|
return DockerWatcher{
|
||||||
host: host,
|
host: host,
|
||||||
clientOwned: true,
|
clientOwned: true,
|
||||||
Logger: logger.With().
|
Logger: logging.With().
|
||||||
Str("type", "docker").
|
Str("type", "docker").
|
||||||
Str("host", host).
|
Str("host", host).
|
||||||
Logger(),
|
Logger(),
|
||||||
|
@ -65,7 +66,7 @@ func NewDockerWatcher(host string) DockerWatcher {
|
||||||
func NewDockerWatcherWithClient(client *D.SharedClient) DockerWatcher {
|
func NewDockerWatcherWithClient(client *D.SharedClient) DockerWatcher {
|
||||||
return DockerWatcher{
|
return DockerWatcher{
|
||||||
client: client,
|
client: client,
|
||||||
Logger: logger.With().
|
Logger: logging.With().
|
||||||
Str("type", "docker").
|
Str("type", "docker").
|
||||||
Str("host", client.DaemonHost()).
|
Str("host", client.DaemonHost()).
|
||||||
Logger(),
|
Logger(),
|
||||||
|
|
|
@ -1,5 +0,0 @@
|
||||||
package watcher
|
|
||||||
|
|
||||||
import "github.com/yusing/go-proxy/internal/logging"
|
|
||||||
|
|
||||||
var logger = logging.With().Str("module", "watcher").Logger()
|
|
Loading…
Add table
Reference in a new issue