mirror of
https://github.com/yusing/godoxy.git
synced 2026-04-20 23:41:23 +02:00
refactor: rename module 'err' to 'gperr' in references
This commit is contained in:
@@ -6,10 +6,7 @@ import (
|
||||
"path/filepath"
|
||||
|
||||
"github.com/yusing/go-proxy/internal/common"
|
||||
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/middleware"
|
||||
metricslogger "github.com/yusing/go-proxy/internal/net/http/middleware/metrics_logger"
|
||||
"github.com/yusing/go-proxy/internal/gperr"
|
||||
"github.com/yusing/go-proxy/internal/route/routes"
|
||||
"github.com/yusing/go-proxy/internal/task"
|
||||
"github.com/yusing/go-proxy/internal/watcher/health"
|
||||
@@ -35,12 +32,12 @@ func handler(root string) http.Handler {
|
||||
return http.FileServer(http.Dir(root))
|
||||
}
|
||||
|
||||
func NewFileServer(base *Route) (*FileServer, E.Error) {
|
||||
func NewFileServer(base *Route) (*FileServer, gperr.Error) {
|
||||
s := &FileServer{Route: base}
|
||||
|
||||
s.Root = filepath.Clean(s.Root)
|
||||
if !path.IsAbs(s.Root) {
|
||||
return nil, E.New("`root` must be an absolute path")
|
||||
return nil, gperr.New("`root` must be an absolute path")
|
||||
}
|
||||
|
||||
s.handler = handler(s.Root)
|
||||
@@ -57,7 +54,7 @@ func NewFileServer(base *Route) (*FileServer, E.Error) {
|
||||
}
|
||||
|
||||
// Start implements task.TaskStarter.
|
||||
func (s *FileServer) Start(parent task.Parent) E.Error {
|
||||
func (s *FileServer) Start(parent task.Parent) gperr.Error {
|
||||
s.task = parent.Subtask("fileserver."+s.TargetName(), false)
|
||||
|
||||
pathPatterns := s.PathPatterns
|
||||
@@ -66,7 +63,7 @@ func (s *FileServer) Start(parent task.Parent) E.Error {
|
||||
case len(pathPatterns) == 1 && pathPatterns[0] == "/":
|
||||
default:
|
||||
mux := gphttp.NewServeMux()
|
||||
patErrs := E.NewBuilder("invalid path pattern(s)")
|
||||
patErrs := gperr.NewBuilder("invalid path pattern(s)")
|
||||
for _, p := range pathPatterns {
|
||||
patErrs.Add(mux.Handle(p, s.handler))
|
||||
}
|
||||
@@ -88,7 +85,7 @@ func (s *FileServer) Start(parent task.Parent) E.Error {
|
||||
s.accessLogger, err = accesslog.NewFileAccessLogger(s.task, s.AccessLog)
|
||||
if err != nil {
|
||||
s.task.Finish(err)
|
||||
return E.Wrap(err)
|
||||
return gperr.Wrap(err)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -8,7 +8,7 @@ import (
|
||||
"github.com/rs/zerolog"
|
||||
"github.com/yusing/go-proxy/internal/common"
|
||||
"github.com/yusing/go-proxy/internal/docker"
|
||||
E "github.com/yusing/go-proxy/internal/error"
|
||||
"github.com/yusing/go-proxy/internal/gperr"
|
||||
"github.com/yusing/go-proxy/internal/logging"
|
||||
"github.com/yusing/go-proxy/internal/route"
|
||||
U "github.com/yusing/go-proxy/internal/utils"
|
||||
@@ -27,7 +27,7 @@ const (
|
||||
aliasRefPrefixAlt = '$'
|
||||
)
|
||||
|
||||
var ErrAliasRefIndexOutOfRange = E.New("index out of range")
|
||||
var ErrAliasRefIndexOutOfRange = gperr.New("index out of range")
|
||||
|
||||
func DockerProviderImpl(name, dockerHost string) (ProviderImpl, error) {
|
||||
if dockerHost == common.DockerHostFromEnv {
|
||||
@@ -60,13 +60,12 @@ func (p *DockerProvider) NewWatcher() watcher.Watcher {
|
||||
return watcher.NewDockerWatcher(p.dockerHost)
|
||||
}
|
||||
|
||||
func (p *DockerProvider) loadRoutesImpl() (route.Routes, E.Error) {
|
||||
containers, err := docker.ListContainers(p.dockerHost)
|
||||
func (p *DockerProvider) loadRoutesImpl() (route.Routes, gperr.Error) {
|
||||
if err != nil {
|
||||
return nil, E.From(err)
|
||||
return nil, gperr.Wrap(err)
|
||||
}
|
||||
|
||||
errs := E.NewBuilder("")
|
||||
errs := gperr.NewBuilder("")
|
||||
routes := make(route.Routes)
|
||||
|
||||
for _, c := range containers {
|
||||
@@ -93,7 +92,7 @@ func (p *DockerProvider) loadRoutesImpl() (route.Routes, E.Error) {
|
||||
|
||||
// Returns a list of proxy entries for a container.
|
||||
// Always non-nil.
|
||||
func (p *DockerProvider) routesFromContainerLabels(container *docker.Container) (route.Routes, E.Error) {
|
||||
func (p *DockerProvider) routesFromContainerLabels(container *docker.Container) (route.Routes, gperr.Error) {
|
||||
if !container.IsExplicit && p.IsExplicitOnly() {
|
||||
return nil, nil
|
||||
}
|
||||
@@ -109,7 +108,7 @@ func (p *DockerProvider) routesFromContainerLabels(container *docker.Container)
|
||||
}
|
||||
}
|
||||
|
||||
errs := E.NewBuilder("label errors")
|
||||
errs := gperr.NewBuilder("label errors")
|
||||
|
||||
m, err := docker.ParseLabels(container.Labels)
|
||||
errs.Add(err)
|
||||
@@ -118,7 +117,7 @@ func (p *DockerProvider) routesFromContainerLabels(container *docker.Container)
|
||||
|
||||
for alias, entryMapAny := range m {
|
||||
if len(alias) == 0 {
|
||||
errs.Add(E.New("empty alias"))
|
||||
errs.Add(gperr.New("empty alias"))
|
||||
continue
|
||||
}
|
||||
|
||||
@@ -132,7 +131,7 @@ func (p *DockerProvider) routesFromContainerLabels(container *docker.Container)
|
||||
panic(fmt.Errorf("invalid entry map type %T", entryMapAny))
|
||||
}
|
||||
if err := yaml.Unmarshal([]byte(yamlStr), &entryMap); err != nil {
|
||||
errs.Add(E.From(err).Subject(alias))
|
||||
errs.Add(gperr.Wrap(err).Subject(alias))
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
package provider
|
||||
|
||||
import (
|
||||
"github.com/yusing/go-proxy/internal/common"
|
||||
E "github.com/yusing/go-proxy/internal/error"
|
||||
"github.com/yusing/go-proxy/internal/gperr"
|
||||
"github.com/yusing/go-proxy/internal/route"
|
||||
"github.com/yusing/go-proxy/internal/route/provider/types"
|
||||
"github.com/yusing/go-proxy/internal/task"
|
||||
@@ -12,19 +11,19 @@ import (
|
||||
type EventHandler struct {
|
||||
provider *Provider
|
||||
|
||||
errs *E.Builder
|
||||
added *E.Builder
|
||||
removed *E.Builder
|
||||
updated *E.Builder
|
||||
errs *gperr.Builder
|
||||
added *gperr.Builder
|
||||
removed *gperr.Builder
|
||||
updated *gperr.Builder
|
||||
}
|
||||
|
||||
func (p *Provider) newEventHandler() *EventHandler {
|
||||
return &EventHandler{
|
||||
provider: p,
|
||||
errs: E.NewBuilder("event errors"),
|
||||
added: E.NewBuilder("added"),
|
||||
removed: E.NewBuilder("removed"),
|
||||
updated: E.NewBuilder("updated"),
|
||||
errs: gperr.NewBuilder("event errors"),
|
||||
added: gperr.NewBuilder("added"),
|
||||
removed: gperr.NewBuilder("removed"),
|
||||
updated: gperr.NewBuilder("updated"),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -121,7 +120,7 @@ func (handler *EventHandler) Update(parent task.Parent, oldRoute *route.Route, n
|
||||
}
|
||||
|
||||
func (handler *EventHandler) Log() {
|
||||
results := E.NewBuilder("event occurred")
|
||||
results := gperr.NewBuilder("event occurred")
|
||||
results.AddFrom(handler.added, false)
|
||||
results.AddFrom(handler.removed, false)
|
||||
results.AddFrom(handler.updated, false)
|
||||
|
||||
@@ -7,7 +7,7 @@ import (
|
||||
|
||||
"github.com/rs/zerolog"
|
||||
"github.com/yusing/go-proxy/internal/common"
|
||||
E "github.com/yusing/go-proxy/internal/error"
|
||||
"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/utils"
|
||||
@@ -33,12 +33,12 @@ func FileProviderImpl(filename string) (ProviderImpl, error) {
|
||||
return impl, nil
|
||||
}
|
||||
|
||||
func validate(data []byte) (routes route.Routes, err E.Error) {
|
||||
func validate(data []byte) (routes route.Routes, err gperr.Error) {
|
||||
err = utils.DeserializeYAML(data, &routes)
|
||||
return
|
||||
}
|
||||
|
||||
func Validate(data []byte) (err E.Error) {
|
||||
func Validate(data []byte) (err gperr.Error) {
|
||||
_, err = validate(data)
|
||||
return
|
||||
}
|
||||
@@ -59,16 +59,16 @@ func (p *FileProvider) Logger() *zerolog.Logger {
|
||||
return &p.l
|
||||
}
|
||||
|
||||
func (p *FileProvider) loadRoutesImpl() (route.Routes, E.Error) {
|
||||
func (p *FileProvider) loadRoutesImpl() (route.Routes, gperr.Error) {
|
||||
data, err := os.ReadFile(p.path)
|
||||
if err != nil {
|
||||
return nil, E.Wrap(err)
|
||||
return nil, gperr.Wrap(err)
|
||||
}
|
||||
routes, err := validate(data)
|
||||
if err != nil && len(routes) == 0 {
|
||||
return nil, E.Wrap(err)
|
||||
return nil, gperr.Wrap(err)
|
||||
}
|
||||
return routes, E.Wrap(err)
|
||||
return routes, gperr.Wrap(err)
|
||||
}
|
||||
|
||||
func (p *FileProvider) NewWatcher() W.Watcher {
|
||||
|
||||
@@ -7,7 +7,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/rs/zerolog"
|
||||
E "github.com/yusing/go-proxy/internal/error"
|
||||
"github.com/yusing/go-proxy/internal/gperr"
|
||||
"github.com/yusing/go-proxy/internal/route"
|
||||
"github.com/yusing/go-proxy/internal/route/provider/types"
|
||||
"github.com/yusing/go-proxy/internal/task"
|
||||
@@ -28,7 +28,7 @@ type (
|
||||
fmt.Stringer
|
||||
ShortName() string
|
||||
IsExplicitOnly() bool
|
||||
loadRoutesImpl() (route.Routes, E.Error)
|
||||
loadRoutesImpl() (route.Routes, gperr.Error)
|
||||
NewWatcher() W.Watcher
|
||||
Logger() *zerolog.Logger
|
||||
}
|
||||
@@ -92,10 +92,10 @@ func (p *Provider) startRoute(parent task.Parent, r *route.Route) E.Error {
|
||||
}
|
||||
|
||||
// Start implements task.TaskStarter.
|
||||
func (p *Provider) Start(parent task.Parent) E.Error {
|
||||
func (p *Provider) Start(parent task.Parent) gperr.Error {
|
||||
t := parent.Subtask("provider."+p.String(), false)
|
||||
|
||||
errs := E.NewBuilder("routes error")
|
||||
errs := gperr.NewBuilder("routes error")
|
||||
for _, r := range p.routes {
|
||||
errs.Add(p.startRoute(t, r))
|
||||
}
|
||||
@@ -109,8 +109,8 @@ func (p *Provider) Start(parent task.Parent) E.Error {
|
||||
handler.Handle(t, events)
|
||||
handler.Log()
|
||||
},
|
||||
func(err E.Error) {
|
||||
E.LogError("event error", err, p.Logger())
|
||||
func(err gperr.Error) {
|
||||
gperr.LogError("event error", err, p.Logger())
|
||||
},
|
||||
)
|
||||
eventQueue.Start(p.watcher.Events(t.Context()))
|
||||
@@ -132,12 +132,12 @@ func (p *Provider) GetRoute(alias string) (r *route.Route, ok bool) {
|
||||
return
|
||||
}
|
||||
|
||||
func (p *Provider) loadRoutes() (routes route.Routes, err E.Error) {
|
||||
func (p *Provider) loadRoutes() (routes route.Routes, err gperr.Error) {
|
||||
routes, err = p.loadRoutesImpl()
|
||||
if err != nil && len(routes) == 0 {
|
||||
return route.Routes{}, err
|
||||
}
|
||||
errs := E.NewBuilder("routes error")
|
||||
errs := gperr.NewBuilder("routes error")
|
||||
errs.Add(err)
|
||||
// check for exclusion
|
||||
// set alias and provider, then validate
|
||||
@@ -156,7 +156,7 @@ func (p *Provider) loadRoutes() (routes route.Routes, err E.Error) {
|
||||
return routes, errs.Error()
|
||||
}
|
||||
|
||||
func (p *Provider) LoadRoutes() (err E.Error) {
|
||||
func (p *Provider) LoadRoutes() (err gperr.Error) {
|
||||
p.routes, err = p.loadRoutes()
|
||||
return
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@ import (
|
||||
"github.com/yusing/go-proxy/internal/common"
|
||||
"github.com/yusing/go-proxy/internal/docker"
|
||||
"github.com/yusing/go-proxy/internal/docker/idlewatcher"
|
||||
E "github.com/yusing/go-proxy/internal/error"
|
||||
"github.com/yusing/go-proxy/internal/gperr"
|
||||
"github.com/yusing/go-proxy/internal/logging"
|
||||
gphttp "github.com/yusing/go-proxy/internal/net/http"
|
||||
"github.com/yusing/go-proxy/internal/net/http/accesslog"
|
||||
@@ -38,8 +38,7 @@ type (
|
||||
|
||||
// var globalMux = http.NewServeMux() // TODO: support regex subdomain matching.
|
||||
|
||||
func NewReverseProxyRoute(base *Route) (*ReveseProxyRoute, E.Error) {
|
||||
trans := gphttp.DefaultTransport
|
||||
func NewReverseProxyRoute(base *Route) (*ReveseProxyRoute, gperr.Error) {
|
||||
httpConfig := base.HTTPConfig
|
||||
|
||||
if httpConfig.NoTLSVerify {
|
||||
@@ -72,7 +71,7 @@ func (r *ReveseProxyRoute) String() string {
|
||||
}
|
||||
|
||||
// Start implements task.TaskStarter.
|
||||
func (r *ReveseProxyRoute) Start(parent task.Parent) E.Error {
|
||||
func (r *ReveseProxyRoute) Start(parent task.Parent) gperr.Error {
|
||||
r.task = parent.Subtask("http."+r.TargetName(), false)
|
||||
|
||||
switch {
|
||||
@@ -103,7 +102,7 @@ func (r *ReveseProxyRoute) Start(parent task.Parent) E.Error {
|
||||
r.rp.AccessLogger, err = accesslog.NewFileAccessLogger(r.task, r.AccessLog)
|
||||
if err != nil {
|
||||
r.task.Finish(err)
|
||||
return E.From(err)
|
||||
return gperr.Wrap(err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -119,7 +118,7 @@ func (r *ReveseProxyRoute) Start(parent task.Parent) E.Error {
|
||||
Str("route", r.TargetName()).
|
||||
Msg("`path_patterns` for reverse proxy is deprecated. Use `rules` instead.")
|
||||
mux := gphttp.NewServeMux()
|
||||
patErrs := E.NewBuilder("invalid path pattern(s)")
|
||||
patErrs := gperr.NewBuilder("invalid path pattern(s)")
|
||||
for _, p := range pathPatterns {
|
||||
patErrs.Add(mux.HandleFunc(p, r.rp.HandlerFunc))
|
||||
}
|
||||
|
||||
@@ -7,12 +7,12 @@ import (
|
||||
|
||||
"github.com/yusing/go-proxy/internal/docker"
|
||||
idlewatcher "github.com/yusing/go-proxy/internal/docker/idlewatcher/types"
|
||||
"github.com/yusing/go-proxy/internal/gperr"
|
||||
"github.com/yusing/go-proxy/internal/homepage"
|
||||
net "github.com/yusing/go-proxy/internal/net/types"
|
||||
"github.com/yusing/go-proxy/internal/task"
|
||||
"github.com/yusing/go-proxy/internal/watcher/health"
|
||||
|
||||
dockertypes "github.com/docker/docker/api/types"
|
||||
"github.com/yusing/go-proxy/internal/common"
|
||||
E "github.com/yusing/go-proxy/internal/error"
|
||||
"github.com/yusing/go-proxy/internal/net/http/accesslog"
|
||||
@@ -65,14 +65,14 @@ func (r Routes) Contains(alias string) bool {
|
||||
return ok
|
||||
}
|
||||
|
||||
func (r *Route) Validate() (err E.Error) {
|
||||
func (r *Route) Validate() (err gperr.Error) {
|
||||
if r.isValidated {
|
||||
return nil
|
||||
}
|
||||
r.isValidated = true
|
||||
r.Finalize()
|
||||
|
||||
errs := E.NewBuilder("entry validation failed")
|
||||
errs := gperr.NewBuilder("entry validation failed")
|
||||
|
||||
switch r.Scheme {
|
||||
case types.SchemeFileServer:
|
||||
@@ -86,18 +86,18 @@ func (r *Route) Validate() (err E.Error) {
|
||||
}
|
||||
fallthrough
|
||||
case types.SchemeTCP, types.SchemeUDP:
|
||||
r.LisURL = E.Collect(errs, net.ParseURL, fmt.Sprintf("%s://%s:%d", r.Scheme, r.Host, r.Port.Listening))
|
||||
r.LisURL = gperr.Collect(errs, net.ParseURL, fmt.Sprintf("%s://:%d", r.Scheme, r.Port.Listening))
|
||||
fallthrough
|
||||
default:
|
||||
if r.LoadBalance != nil && r.LoadBalance.Link == "" {
|
||||
r.LoadBalance = nil
|
||||
}
|
||||
r.ProxyURL = E.Collect(errs, net.ParseURL, fmt.Sprintf("%s://%s:%d", r.Scheme, r.Host, r.Port.Proxy))
|
||||
r.Idlewatcher = E.Collect(errs, idlewatcher.ValidateConfig, r.Container)
|
||||
r.ProxyURL = gperr.Collect(errs, net.ParseURL, fmt.Sprintf("%s://%s:%d", r.Scheme, r.Host, r.Port.Proxy))
|
||||
r.Idlewatcher = gperr.Collect(errs, idlewatcher.ValidateConfig, r.Container)
|
||||
}
|
||||
|
||||
if !r.UseHealthCheck() && (r.UseLoadBalance() || r.UseIdleWatcher()) {
|
||||
errs.Adds("healthCheck.disable cannot be true when loadbalancer or idlewatcher is enabled")
|
||||
errs.Adds("cannot disable healthcheck when loadbalancer or idle watcher is enabled")
|
||||
}
|
||||
|
||||
if errs.HasError() {
|
||||
@@ -118,10 +118,11 @@ func (r *Route) Validate() (err E.Error) {
|
||||
return err
|
||||
}
|
||||
|
||||
func (r *Route) Start(parent task.Parent) (err E.Error) {
|
||||
func (r *Route) Start(parent task.Parent) (err gperr.Error) {
|
||||
if r.impl == nil {
|
||||
return E.New("route not initialized")
|
||||
return gperr.New("route not initialized")
|
||||
}
|
||||
|
||||
return r.impl.Start(parent)
|
||||
}
|
||||
|
||||
|
||||
@@ -41,9 +41,7 @@ func NewCache() Cache {
|
||||
|
||||
// Release clear the contents of the Cached and returns it to the pool.
|
||||
func (c Cache) Release() {
|
||||
for _, k := range cacheKeys {
|
||||
delete(c, k)
|
||||
}
|
||||
clear(c)
|
||||
cachePool.Put(c)
|
||||
}
|
||||
|
||||
|
||||
@@ -6,9 +6,9 @@ import (
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
E "github.com/yusing/go-proxy/internal/error"
|
||||
gphttp "github.com/yusing/go-proxy/internal/net/http"
|
||||
"github.com/yusing/go-proxy/internal/net/http/reverseproxy"
|
||||
"github.com/yusing/go-proxy/internal/gperr"
|
||||
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/types"
|
||||
"github.com/yusing/go-proxy/internal/utils/strutils"
|
||||
)
|
||||
@@ -47,7 +47,7 @@ var commands = map[string]struct {
|
||||
"to": "the path to rewrite to, must start with /",
|
||||
},
|
||||
},
|
||||
validate: func(args []string) (any, E.Error) {
|
||||
validate: func(args []string) (any, gperr.Error) {
|
||||
if len(args) != 2 {
|
||||
return nil, ErrExpectTwoArgs
|
||||
}
|
||||
@@ -109,7 +109,7 @@ var commands = map[string]struct {
|
||||
"text": "the error message to return",
|
||||
},
|
||||
},
|
||||
validate: func(args []string) (any, E.Error) {
|
||||
validate: func(args []string) (any, gperr.Error) {
|
||||
if len(args) != 2 {
|
||||
return nil, ErrExpectTwoArgs
|
||||
}
|
||||
@@ -137,7 +137,7 @@ var commands = map[string]struct {
|
||||
"realm": "the authentication realm",
|
||||
},
|
||||
},
|
||||
validate: func(args []string) (any, E.Error) {
|
||||
validate: func(args []string) (any, gperr.Error) {
|
||||
if len(args) == 1 {
|
||||
return args[0], nil
|
||||
}
|
||||
@@ -164,7 +164,7 @@ var commands = map[string]struct {
|
||||
if target.Scheme == "" {
|
||||
target.Scheme = "http"
|
||||
}
|
||||
rp := reverseproxy.NewReverseProxy("", target, gphttp.DefaultTransport)
|
||||
rp := reverseproxy.NewReverseProxy("", target, gphttp.NewTransport())
|
||||
return ReturningCommand(rp.ServeHTTP)
|
||||
},
|
||||
},
|
||||
@@ -176,7 +176,7 @@ var commands = map[string]struct {
|
||||
"value": "the value to set",
|
||||
},
|
||||
},
|
||||
validate: func(args []string) (any, E.Error) {
|
||||
validate: func(args []string) (any, gperr.Error) {
|
||||
return validateModField(ModFieldSet, args)
|
||||
},
|
||||
build: func(args any) CommandHandler {
|
||||
@@ -191,7 +191,7 @@ var commands = map[string]struct {
|
||||
"value": "the value to add",
|
||||
},
|
||||
},
|
||||
validate: func(args []string) (any, E.Error) {
|
||||
validate: func(args []string) (any, gperr.Error) {
|
||||
return validateModField(ModFieldAdd, args)
|
||||
},
|
||||
build: func(args any) CommandHandler {
|
||||
@@ -205,7 +205,7 @@ var commands = map[string]struct {
|
||||
"field": "the field to remove",
|
||||
},
|
||||
},
|
||||
validate: func(args []string) (any, E.Error) {
|
||||
validate: func(args []string) (any, gperr.Error) {
|
||||
return validateModField(ModFieldRemove, args)
|
||||
},
|
||||
build: func(args any) CommandHandler {
|
||||
|
||||
@@ -1,18 +1,20 @@
|
||||
package rules
|
||||
|
||||
import E "github.com/yusing/go-proxy/internal/error"
|
||||
import (
|
||||
"github.com/yusing/go-proxy/internal/gperr"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrUnterminatedQuotes = E.New("unterminated quotes")
|
||||
ErrUnsupportedEscapeChar = E.New("unsupported escape char")
|
||||
ErrUnknownDirective = E.New("unknown directive")
|
||||
ErrInvalidArguments = E.New("invalid arguments")
|
||||
ErrInvalidOnTarget = E.New("invalid `rule.on` target")
|
||||
ErrInvalidCommandSequence = E.New("invalid command sequence")
|
||||
ErrInvalidSetTarget = E.New("invalid `rule.set` target")
|
||||
ErrUnterminatedQuotes = gperr.New("unterminated quotes")
|
||||
ErrUnsupportedEscapeChar = gperr.New("unsupported escape char")
|
||||
ErrUnknownDirective = gperr.New("unknown directive")
|
||||
ErrInvalidArguments = gperr.New("invalid arguments")
|
||||
ErrInvalidOnTarget = gperr.New("invalid `rule.on` target")
|
||||
ErrInvalidCommandSequence = gperr.New("invalid command sequence")
|
||||
ErrInvalidSetTarget = gperr.New("invalid `rule.set` target")
|
||||
|
||||
ErrExpectNoArg = E.Wrap(ErrInvalidArguments, "expect no arg")
|
||||
ErrExpectOneArg = E.Wrap(ErrInvalidArguments, "expect 1 arg")
|
||||
ErrExpectTwoArgs = E.Wrap(ErrInvalidArguments, "expect 2 args")
|
||||
ErrExpectKVOptionalV = E.Wrap(ErrInvalidArguments, "expect 'key' or 'key value'")
|
||||
ErrExpectNoArg = gperr.Wrap(ErrInvalidArguments, "expect no arg")
|
||||
ErrExpectOneArg = gperr.Wrap(ErrInvalidArguments, "expect 1 arg")
|
||||
ErrExpectTwoArgs = gperr.Wrap(ErrInvalidArguments, "expect 2 args")
|
||||
ErrExpectKVOptionalV = gperr.Wrap(ErrInvalidArguments, "expect 'key' or 'key value'")
|
||||
)
|
||||
|
||||
@@ -3,7 +3,7 @@ package rules
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
E "github.com/yusing/go-proxy/internal/error"
|
||||
"github.com/yusing/go-proxy/internal/gperr"
|
||||
"github.com/yusing/go-proxy/internal/net/types"
|
||||
"github.com/yusing/go-proxy/internal/utils/strutils"
|
||||
)
|
||||
@@ -240,7 +240,7 @@ func (on *RuleOn) Parse(v string) error {
|
||||
lines := strutils.SplitLine(v)
|
||||
checkAnd := make(CheckMatchAll, 0, len(lines))
|
||||
|
||||
errs := E.NewBuilder("rule.on syntax errors")
|
||||
errs := gperr.NewBuilder("rule.on syntax errors")
|
||||
for i, line := range lines {
|
||||
if line == "" {
|
||||
continue
|
||||
@@ -265,11 +265,11 @@ func (on *RuleOn) MarshalText() ([]byte, error) {
|
||||
return []byte(on.String()), nil
|
||||
}
|
||||
|
||||
func parseOn(line string) (Checker, E.Error) {
|
||||
func parseOn(line string) (Checker, gperr.Error) {
|
||||
ors := strutils.SplitRune(line, '|')
|
||||
|
||||
if len(ors) > 1 {
|
||||
errs := E.NewBuilder("rule.on syntax errors")
|
||||
errs := gperr.NewBuilder("rule.on syntax errors")
|
||||
checkOr := make(CheckMatchSingle, len(ors))
|
||||
for i, or := range ors {
|
||||
curCheckers, err := parseOn(or)
|
||||
|
||||
@@ -7,7 +7,7 @@ import (
|
||||
"net/url"
|
||||
"testing"
|
||||
|
||||
E "github.com/yusing/go-proxy/internal/error"
|
||||
"github.com/yusing/go-proxy/internal/gperr"
|
||||
. "github.com/yusing/go-proxy/internal/utils/testing"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
)
|
||||
@@ -16,7 +16,7 @@ func TestParseOn(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
input string
|
||||
wantErr E.Error
|
||||
wantErr gperr.Error
|
||||
}{
|
||||
// header
|
||||
{
|
||||
|
||||
@@ -4,7 +4,7 @@ import (
|
||||
"bytes"
|
||||
"unicode"
|
||||
|
||||
E "github.com/yusing/go-proxy/internal/error"
|
||||
"github.com/yusing/go-proxy/internal/gperr"
|
||||
)
|
||||
|
||||
var escapedChars = map[rune]rune{
|
||||
@@ -23,7 +23,7 @@ var escapedChars = map[rune]rune{
|
||||
//
|
||||
// error 403 "Forbidden 'foo' 'bar'"
|
||||
// error 403 Forbidden\ \"foo\"\ \"bar\".
|
||||
func parse(v string) (subject string, args []string, err E.Error) {
|
||||
func parse(v string) (subject string, args []string, err gperr.Error) {
|
||||
buf := bytes.NewBuffer(make([]byte, 0, len(v)))
|
||||
|
||||
escaped := false
|
||||
|
||||
@@ -4,7 +4,7 @@ import (
|
||||
"strconv"
|
||||
"testing"
|
||||
|
||||
E "github.com/yusing/go-proxy/internal/error"
|
||||
"github.com/yusing/go-proxy/internal/gperr"
|
||||
. "github.com/yusing/go-proxy/internal/utils/testing"
|
||||
)
|
||||
|
||||
@@ -14,7 +14,7 @@ func TestParser(t *testing.T) {
|
||||
input string
|
||||
subject string
|
||||
args []string
|
||||
wantErr E.Error
|
||||
wantErr gperr.Error
|
||||
}{
|
||||
{
|
||||
name: "basic",
|
||||
|
||||
@@ -6,13 +6,13 @@ import (
|
||||
"path"
|
||||
"strings"
|
||||
|
||||
E "github.com/yusing/go-proxy/internal/error"
|
||||
gphttp "github.com/yusing/go-proxy/internal/net/http"
|
||||
"github.com/yusing/go-proxy/internal/gperr"
|
||||
gphttp "github.com/yusing/go-proxy/internal/net/gphttp"
|
||||
"github.com/yusing/go-proxy/internal/net/types"
|
||||
)
|
||||
|
||||
type (
|
||||
ValidateFunc func(args []string) (any, E.Error)
|
||||
ValidateFunc func(args []string) (any, gperr.Error)
|
||||
Tuple[T1, T2 any] struct {
|
||||
First T1
|
||||
Second T2
|
||||
@@ -29,7 +29,7 @@ func (t *Tuple[T1, T2]) String() string {
|
||||
}
|
||||
|
||||
// toStrTuple returns *StrTuple.
|
||||
func toStrTuple(args []string) (any, E.Error) {
|
||||
func toStrTuple(args []string) (any, gperr.Error) {
|
||||
if len(args) != 2 {
|
||||
return nil, ErrExpectTwoArgs
|
||||
}
|
||||
@@ -37,7 +37,7 @@ func toStrTuple(args []string) (any, E.Error) {
|
||||
}
|
||||
|
||||
// toKVOptionalV returns *StrTuple that value is optional.
|
||||
func toKVOptionalV(args []string) (any, E.Error) {
|
||||
func toKVOptionalV(args []string) (any, gperr.Error) {
|
||||
switch len(args) {
|
||||
case 1:
|
||||
return &StrTuple{args[0], ""}, nil
|
||||
@@ -49,7 +49,7 @@ func toKVOptionalV(args []string) (any, E.Error) {
|
||||
}
|
||||
|
||||
// validateURL returns types.URL with the URL validated.
|
||||
func validateURL(args []string) (any, E.Error) {
|
||||
func validateURL(args []string) (any, gperr.Error) {
|
||||
if len(args) != 1 {
|
||||
return nil, ErrExpectOneArg
|
||||
}
|
||||
@@ -61,7 +61,7 @@ func validateURL(args []string) (any, E.Error) {
|
||||
}
|
||||
|
||||
// validateAbsoluteURL returns types.URL with the URL validated.
|
||||
func validateAbsoluteURL(args []string) (any, E.Error) {
|
||||
func validateAbsoluteURL(args []string) (any, gperr.Error) {
|
||||
if len(args) != 1 {
|
||||
return nil, ErrExpectOneArg
|
||||
}
|
||||
@@ -79,7 +79,7 @@ func validateAbsoluteURL(args []string) (any, E.Error) {
|
||||
}
|
||||
|
||||
// validateCIDR returns types.CIDR with the CIDR validated.
|
||||
func validateCIDR(args []string) (any, E.Error) {
|
||||
func validateCIDR(args []string) (any, gperr.Error) {
|
||||
if len(args) != 1 {
|
||||
return nil, ErrExpectOneArg
|
||||
}
|
||||
@@ -94,7 +94,7 @@ func validateCIDR(args []string) (any, E.Error) {
|
||||
}
|
||||
|
||||
// validateURLPath returns string with the path validated.
|
||||
func validateURLPath(args []string) (any, E.Error) {
|
||||
func validateURLPath(args []string) (any, gperr.Error) {
|
||||
if len(args) != 1 {
|
||||
return nil, ErrExpectOneArg
|
||||
}
|
||||
@@ -112,8 +112,8 @@ func validateURLPath(args []string) (any, E.Error) {
|
||||
}
|
||||
|
||||
// validateURLPaths returns []string with each element validated.
|
||||
func validateURLPaths(paths []string) (any, E.Error) {
|
||||
errs := E.NewBuilder("invalid url paths")
|
||||
func validateURLPaths(paths []string) (any, gperr.Error) {
|
||||
errs := gperr.NewBuilder("invalid url paths")
|
||||
for i, p := range paths {
|
||||
val, err := validateURLPath([]string{p})
|
||||
if err != nil {
|
||||
@@ -129,7 +129,7 @@ func validateURLPaths(paths []string) (any, E.Error) {
|
||||
}
|
||||
|
||||
// validateFSPath returns string with the path validated.
|
||||
func validateFSPath(args []string) (any, E.Error) {
|
||||
func validateFSPath(args []string) (any, gperr.Error) {
|
||||
if len(args) != 1 {
|
||||
return nil, ErrExpectOneArg
|
||||
}
|
||||
@@ -141,7 +141,7 @@ func validateFSPath(args []string) (any, E.Error) {
|
||||
}
|
||||
|
||||
// validateMethod returns string with the method validated.
|
||||
func validateMethod(args []string) (any, E.Error) {
|
||||
func validateMethod(args []string) (any, gperr.Error) {
|
||||
if len(args) != 1 {
|
||||
return nil, ErrExpectOneArg
|
||||
}
|
||||
@@ -153,7 +153,7 @@ func validateMethod(args []string) (any, E.Error) {
|
||||
}
|
||||
|
||||
// validateUserBCryptPassword returns *HashedCrendential with the password validated.
|
||||
func validateUserBCryptPassword(args []string) (any, E.Error) {
|
||||
func validateUserBCryptPassword(args []string) (any, gperr.Error) {
|
||||
if len(args) != 2 {
|
||||
return nil, ErrExpectTwoArgs
|
||||
}
|
||||
@@ -161,7 +161,7 @@ func validateUserBCryptPassword(args []string) (any, E.Error) {
|
||||
}
|
||||
|
||||
// validateModField returns CommandHandler with the field validated.
|
||||
func validateModField(mod FieldModifier, args []string) (CommandHandler, E.Error) {
|
||||
func validateModField(mod FieldModifier, args []string) (CommandHandler, gperr.Error) {
|
||||
setField, ok := modFields[args[0]]
|
||||
if !ok {
|
||||
return nil, ErrInvalidSetTarget.Subject(args[0])
|
||||
|
||||
@@ -30,7 +30,7 @@ type StreamRoute struct {
|
||||
l zerolog.Logger
|
||||
}
|
||||
|
||||
func NewStreamRoute(base *Route) (route.Route, E.Error) {
|
||||
func NewStreamRoute(base *Route) (route.Route, gperr.Error) {
|
||||
// TODO: support non-coherent scheme
|
||||
return &StreamRoute{
|
||||
Route: base,
|
||||
@@ -46,7 +46,7 @@ func (r *StreamRoute) String() string {
|
||||
}
|
||||
|
||||
// Start implements task.TaskStarter.
|
||||
func (r *StreamRoute) Start(parent task.Parent) E.Error {
|
||||
func (r *StreamRoute) Start(parent task.Parent) gperr.Error {
|
||||
r.task = parent.Subtask("stream." + r.TargetName())
|
||||
r.Stream = NewStream(r)
|
||||
parent.OnCancel("finish", func() {
|
||||
@@ -78,14 +78,14 @@ func (r *StreamRoute) Start(parent task.Parent) E.Error {
|
||||
|
||||
if err := r.Stream.Setup(); err != nil {
|
||||
r.task.Finish(err)
|
||||
return E.From(err)
|
||||
return gperr.Wrap(err)
|
||||
}
|
||||
|
||||
r.l.Info().Int("port", r.Port.Listening).Msg("listening")
|
||||
|
||||
if r.HealthMon != nil {
|
||||
if err := r.HealthMon.Start(r.task); err != nil {
|
||||
E.LogWarn("health monitor error", err, &r.l)
|
||||
gperr.LogWarn("health monitor error", err, &r.l)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -125,7 +125,7 @@ func (r *StreamRoute) acceptConnections() {
|
||||
select {
|
||||
case <-r.task.Context().Done():
|
||||
default:
|
||||
E.LogError("accept connection error", err, &r.l)
|
||||
gperr.LogError("accept connection error", err, &r.l)
|
||||
}
|
||||
r.task.Finish(err)
|
||||
return
|
||||
@@ -136,7 +136,7 @@ func (r *StreamRoute) acceptConnections() {
|
||||
go func() {
|
||||
err := r.Stream.Handle(conn)
|
||||
if err != nil && !errors.Is(err, context.Canceled) {
|
||||
E.LogError("handle connection error", err, &r.l)
|
||||
gperr.LogError("handle connection error", err, &r.l)
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@ package types
|
||||
import (
|
||||
"strconv"
|
||||
|
||||
E "github.com/yusing/go-proxy/internal/error"
|
||||
"github.com/yusing/go-proxy/internal/gperr"
|
||||
"github.com/yusing/go-proxy/internal/utils/strutils"
|
||||
)
|
||||
|
||||
@@ -13,8 +13,8 @@ type Port struct {
|
||||
}
|
||||
|
||||
var (
|
||||
ErrInvalidPortSyntax = E.New("invalid port syntax, expect [listening_port:]target_port")
|
||||
ErrPortOutOfRange = E.New("port out of range")
|
||||
ErrInvalidPortSyntax = gperr.New("invalid port syntax, expect [listening_port:]target_port")
|
||||
ErrPortOutOfRange = gperr.New("port out of range")
|
||||
)
|
||||
|
||||
// Parse implements strutils.Parser.
|
||||
@@ -28,7 +28,7 @@ func (p *Port) Parse(v string) (err error) {
|
||||
var err2 error
|
||||
p.Listening, err = strconv.Atoi(parts[0])
|
||||
p.Proxy, err2 = strconv.Atoi(parts[1])
|
||||
err = E.Join(err, err2)
|
||||
err = gperr.Join(err, err2)
|
||||
default:
|
||||
return ErrInvalidPortSyntax.Subject(v)
|
||||
}
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
package types
|
||||
|
||||
import (
|
||||
E "github.com/yusing/go-proxy/internal/error"
|
||||
"github.com/yusing/go-proxy/internal/gperr"
|
||||
)
|
||||
|
||||
type Scheme string
|
||||
|
||||
var ErrInvalidScheme = E.New("invalid scheme")
|
||||
var ErrInvalidScheme = gperr.New("invalid scheme")
|
||||
|
||||
const (
|
||||
SchemeHTTP Scheme = "http"
|
||||
@@ -16,7 +16,7 @@ const (
|
||||
SchemeFileServer Scheme = "fileserver"
|
||||
)
|
||||
|
||||
func (s Scheme) Validate() E.Error {
|
||||
func (s Scheme) Validate() gperr.Error {
|
||||
switch s {
|
||||
case SchemeHTTP, SchemeHTTPS,
|
||||
SchemeTCP, SchemeUDP, SchemeFileServer:
|
||||
|
||||
@@ -6,7 +6,7 @@ import (
|
||||
"net"
|
||||
"sync"
|
||||
|
||||
E "github.com/yusing/go-proxy/internal/error"
|
||||
"github.com/yusing/go-proxy/internal/gperr"
|
||||
"github.com/yusing/go-proxy/internal/logging"
|
||||
"github.com/yusing/go-proxy/internal/net/types"
|
||||
F "github.com/yusing/go-proxy/internal/utils/functional"
|
||||
@@ -192,7 +192,7 @@ func (w *UDPForwarder) Handle(streamConn types.StreamConn) error {
|
||||
}
|
||||
|
||||
func (w *UDPForwarder) Close() error {
|
||||
errs := E.NewBuilder("errors closing udp conn")
|
||||
errs := gperr.NewBuilder("errors closing udp conn")
|
||||
w.mu.Lock()
|
||||
defer w.mu.Unlock()
|
||||
w.connMap.RangeAll(func(key string, conn *UDPConn) {
|
||||
|
||||
Reference in New Issue
Block a user