diff --git a/internal/api/handler.go b/internal/api/handler.go index 6ddd0843..ad47f2da 100644 --- a/internal/api/handler.go +++ b/internal/api/handler.go @@ -9,7 +9,6 @@ import ( "github.com/yusing/go-proxy/internal/api/v1/auth" . "github.com/yusing/go-proxy/internal/api/v1/utils" "github.com/yusing/go-proxy/internal/common" - "github.com/yusing/go-proxy/internal/config" ) type ServeMux struct{ *http.ServeMux } @@ -26,8 +25,6 @@ func NewHandler() http.Handler { mux := NewServeMux() mux.HandleFunc("GET", "/v1", v1.Index) mux.HandleFunc("GET", "/v1/version", v1.GetVersion) - // mux.HandleFunc("GET", "/v1/checkhealth", v1.CheckHealth) - // mux.HandleFunc("HEAD", "/v1/checkhealth", v1.CheckHealth) mux.HandleFunc("POST", "/v1/login", auth.LoginHandler) mux.HandleFunc("GET", "/v1/logout", auth.LogoutHandler) mux.HandleFunc("POST", "/v1/logout", auth.LogoutHandler) @@ -59,9 +56,3 @@ func checkHost(f http.HandlerFunc) http.HandlerFunc { f(w, r) } } - -func wrap(cfg *config.Config, f func(cfg *config.Config, w http.ResponseWriter, r *http.Request)) http.HandlerFunc { - return func(w http.ResponseWriter, r *http.Request) { - f(cfg, w, r) - } -} diff --git a/internal/api/v1/auth/auth.go b/internal/api/v1/auth/auth.go index 82960087..647c279d 100644 --- a/internal/api/v1/auth/auth.go +++ b/internal/api/v1/auth/auth.go @@ -32,8 +32,6 @@ var ( const tokenExpiration = 24 * time.Hour -const jwtClaimKeyUsername = "username" - func validatePassword(cred *Credentials) error { if cred.Username != common.APIUser { return ErrInvalidUsername.Subject(cred.Username) @@ -114,7 +112,7 @@ func checkToken(w http.ResponseWriter, r *http.Request) (ok bool) { var claims Claims token, err := jwt.ParseWithClaims(tokenCookie.Value, &claims, func(t *jwt.Token) (interface{}, error) { if _, ok := t.Method.(*jwt.SigningMethodHMAC); !ok { - return nil, fmt.Errorf("Unexpected signing method: %v", t.Header["alg"]) + return nil, fmt.Errorf("unexpected signing method: %v", t.Header["alg"]) } return common.APIJWTSecret, nil }) diff --git a/internal/api/v1/checkhealth.go b/internal/api/v1/checkhealth.go deleted file mode 100644 index 9360b802..00000000 --- a/internal/api/v1/checkhealth.go +++ /dev/null @@ -1,28 +0,0 @@ -package v1 - -import ( - "net/http" - - . "github.com/yusing/go-proxy/internal/api/v1/utils" - "github.com/yusing/go-proxy/internal/watcher/health" -) - -func CheckHealth(w http.ResponseWriter, r *http.Request) { - target := r.FormValue("target") - if target == "" { - HandleErr(w, r, ErrMissingKey("target"), http.StatusBadRequest) - return - } - - result, ok := health.Inspect(target) - if !ok { - HandleErr(w, r, ErrNotFound("target", target), http.StatusNotFound) - return - } - json, err := result.MarshalJSON() - if err != nil { - HandleErr(w, r, err) - return - } - RespondJSON(w, r, json) -} diff --git a/internal/autocert/constants.go b/internal/autocert/constants.go index ffe0cc3f..308d3dd4 100644 --- a/internal/autocert/constants.go +++ b/internal/autocert/constants.go @@ -1,8 +1,6 @@ package autocert import ( - "errors" - "github.com/go-acme/lego/v4/providers/dns/clouddns" "github.com/go-acme/lego/v4/providers/dns/cloudflare" "github.com/go-acme/lego/v4/providers/dns/duckdns" @@ -31,7 +29,3 @@ var providersGenMap = map[string]ProviderGenerator{ ProviderDuckdns: providerGenerator(duckdns.NewDefaultConfig, duckdns.NewDNSProviderConfig), ProviderOVH: providerGenerator(ovh.NewDefaultConfig, ovh.NewDNSProviderConfig), } - -var ( - ErrGetCertFailure = errors.New("get certificate failed") -) diff --git a/internal/autocert/provider.go b/internal/autocert/provider.go index 4f136736..b887173c 100644 --- a/internal/autocert/provider.go +++ b/internal/autocert/provider.go @@ -3,6 +3,7 @@ package autocert import ( "crypto/tls" "crypto/x509" + "errors" "os" "path" "reflect" @@ -35,6 +36,8 @@ type ( CertExpiries map[string]time.Time ) +var ErrGetCertFailure = errors.New("get certificate failed") + func (p *Provider) GetCert(_ *tls.ClientHelloInfo) (*tls.Certificate, error) { if p.tlsCert == nil { return nil, ErrGetCertFailure @@ -248,10 +251,7 @@ func (p *Provider) renewIfNeeded() E.Error { return nil } - if err := p.ObtainCert(); err != nil { - return err - } - return nil + return p.ObtainCert() } func getCertExpiries(cert *tls.Certificate) (CertExpiries, error) { diff --git a/internal/common/args.go b/internal/common/args.go index 35563e7a..e9f6f576 100644 --- a/internal/common/args.go +++ b/internal/common/args.go @@ -21,7 +21,6 @@ const ( CommandDebugListEntries = "debug-ls-entries" CommandDebugListProviders = "debug-ls-providers" CommandDebugListMTrace = "debug-ls-mtrace" - CommandDebugListTasks = "debug-ls-tasks" ) var ValidCommands = []string{ @@ -35,7 +34,6 @@ var ValidCommands = []string{ CommandDebugListEntries, CommandDebugListProviders, CommandDebugListMTrace, - CommandDebugListTasks, } func GetArgs() Args { diff --git a/internal/error/utils.go b/internal/error/utils.go index 6bd6c73d..eeda0c9d 100644 --- a/internal/error/utils.go +++ b/internal/error/utils.go @@ -1,12 +1,9 @@ package error import ( - "errors" "fmt" ) -var ErrInvalidErrorJson = errors.New("invalid error json") - func newError(message string) error { return errStr(message) } diff --git a/internal/route/stream.go b/internal/route/stream.go index 31b3a689..cf22ff64 100755 --- a/internal/route/stream.go +++ b/internal/route/stream.go @@ -19,7 +19,7 @@ import ( type StreamRoute struct { *entry.StreamEntry - stream net.Stream `json:"-"` + stream net.Stream HealthMon health.HealthMonitor `json:"health"` diff --git a/internal/watcher/health/logger.go b/internal/watcher/health/logger.go deleted file mode 100644 index 3735a4c0..00000000 --- a/internal/watcher/health/logger.go +++ /dev/null @@ -1,7 +0,0 @@ -package health - -import ( - "github.com/yusing/go-proxy/internal/logging" -) - -var logger = logging.With().Str("module", "health_mon").Logger() diff --git a/internal/watcher/health/monitor.go b/internal/watcher/health/monitor.go index 4a7d852e..281ad849 100644 --- a/internal/watcher/health/monitor.go +++ b/internal/watcher/health/monitor.go @@ -13,7 +13,6 @@ import ( "github.com/yusing/go-proxy/internal/notif" "github.com/yusing/go-proxy/internal/task" U "github.com/yusing/go-proxy/internal/utils" - F "github.com/yusing/go-proxy/internal/utils/functional" ) type ( @@ -31,11 +30,7 @@ type ( } ) -var monMap = F.NewMapOf[string, HealthMonitor]() - -var ( - ErrNegativeInterval = errors.New("negative interval") -) +var ErrNegativeInterval = errors.New("negative interval") func newMonitor(url types.URL, config *HealthCheckConfig, healthCheckFunc HealthCheckFunc) *monitor { mon := &monitor{ @@ -49,16 +44,11 @@ func newMonitor(url types.URL, config *HealthCheckConfig, healthCheckFunc Health return mon } -func Inspect(service string) (HealthMonitor, bool) { - return monMap.Load(service) -} - func (mon *monitor) ContextWithTimeout(cause string) (ctx context.Context, cancel context.CancelFunc) { if mon.task != nil { return context.WithTimeoutCause(mon.task.Context(), mon.config.Timeout, errors.New(cause)) - } else { - return context.WithTimeoutCause(context.Background(), mon.config.Timeout, errors.New(cause)) } + return context.WithTimeoutCause(context.Background(), mon.config.Timeout, errors.New(cause)) } // Start implements task.TaskStarter. @@ -85,9 +75,6 @@ func (mon *monitor) Start(routeSubtask task.Task) E.Error { return } - monMap.Store(mon.service, mon) - defer monMap.Delete(mon.service) - ticker := time.NewTicker(mon.config.Interval) defer ticker.Stop()