refactor: remove net.URL and net.CIDR types, improved unmarshal handling

This commit is contained in:
yusing
2025-04-13 07:06:21 +08:00
parent 1eac48e899
commit fce96ff3be
37 changed files with 236 additions and 292 deletions

View File

@@ -4,7 +4,7 @@ import (
"testing"
"time"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/network"
"github.com/docker/docker/client"
"github.com/yusing/go-proxy/internal/common"
@@ -21,7 +21,7 @@ const (
testDockerIP = "172.17.0.123"
)
func makeRoutes(cont *types.Container, dockerHostIP ...string) route.Routes {
func makeRoutes(cont *container.Summary, dockerHostIP ...string) route.Routes {
var p DockerProvider
var host string
if len(dockerHostIP) > 0 {
@@ -64,7 +64,7 @@ func TestApplyLabel(t *testing.T) {
},
},
}
entries := makeRoutes(&types.Container{
entries := makeRoutes(&container.Summary{
Names: dummyNames,
Labels: map[string]string{
D.LabelAliases: "a,b",
@@ -135,7 +135,7 @@ func TestApplyLabel(t *testing.T) {
}
func TestApplyLabelWithAlias(t *testing.T) {
entries := makeRoutes(&types.Container{
entries := makeRoutes(&container.Summary{
Names: dummyNames,
State: "running",
Labels: map[string]string{
@@ -162,7 +162,7 @@ func TestApplyLabelWithAlias(t *testing.T) {
}
func TestApplyLabelWithRef(t *testing.T) {
entries := makeRoutes(&types.Container{
entries := makeRoutes(&container.Summary{
Names: dummyNames,
State: "running",
Labels: map[string]string{
@@ -190,7 +190,7 @@ func TestApplyLabelWithRef(t *testing.T) {
}
func TestApplyLabelWithRefIndexError(t *testing.T) {
c := D.FromDocker(&types.Container{
c := D.FromDocker(&container.Summary{
Names: dummyNames,
State: "running",
Labels: map[string]string{
@@ -204,7 +204,7 @@ func TestApplyLabelWithRefIndexError(t *testing.T) {
_, err := p.routesFromContainerLabels(c)
ExpectError(t, ErrAliasRefIndexOutOfRange, err)
c = D.FromDocker(&types.Container{
c = D.FromDocker(&container.Summary{
Names: dummyNames,
State: "running",
Labels: map[string]string{
@@ -217,7 +217,7 @@ func TestApplyLabelWithRefIndexError(t *testing.T) {
}
func TestDynamicAliases(t *testing.T) {
c := &types.Container{
c := &container.Summary{
Names: []string{"app1"},
State: "running",
Labels: map[string]string{
@@ -240,7 +240,7 @@ func TestDynamicAliases(t *testing.T) {
}
func TestDisableHealthCheck(t *testing.T) {
c := &types.Container{
c := &container.Summary{
Names: dummyNames,
State: "running",
Labels: map[string]string{
@@ -254,7 +254,7 @@ func TestDisableHealthCheck(t *testing.T) {
}
func TestPublicIPLocalhost(t *testing.T) {
c := &types.Container{Names: dummyNames, State: "running"}
c := &container.Summary{Names: dummyNames, State: "running"}
r, ok := makeRoutes(c)["a"]
ExpectTrue(t, ok)
ExpectEqual(t, r.Container.PublicHostname, "127.0.0.1")
@@ -262,7 +262,7 @@ func TestPublicIPLocalhost(t *testing.T) {
}
func TestPublicIPRemote(t *testing.T) {
c := &types.Container{Names: dummyNames, State: "running"}
c := &container.Summary{Names: dummyNames, State: "running"}
raw, ok := makeRoutes(c, testIP)["a"]
ExpectTrue(t, ok)
ExpectEqual(t, raw.Container.PublicHostname, testIP)
@@ -270,9 +270,9 @@ func TestPublicIPRemote(t *testing.T) {
}
func TestPrivateIPLocalhost(t *testing.T) {
c := &types.Container{
c := &container.Summary{
Names: dummyNames,
NetworkSettings: &types.SummaryNetworkSettings{
NetworkSettings: &container.NetworkSettingsSummary{
Networks: map[string]*network.EndpointSettings{
"network": {
IPAddress: testDockerIP,
@@ -287,10 +287,10 @@ func TestPrivateIPLocalhost(t *testing.T) {
}
func TestPrivateIPRemote(t *testing.T) {
c := &types.Container{
c := &container.Summary{
Names: dummyNames,
State: "running",
NetworkSettings: &types.SummaryNetworkSettings{
NetworkSettings: &container.NetworkSettingsSummary{
Networks: map[string]*network.EndpointSettings{
"network": {
IPAddress: testDockerIP,
@@ -309,17 +309,17 @@ func TestStreamDefaultValues(t *testing.T) {
privPort := uint16(1234)
pubPort := uint16(4567)
privIP := "172.17.0.123"
cont := &types.Container{
cont := &container.Summary{
Names: []string{"a"},
State: "running",
NetworkSettings: &types.SummaryNetworkSettings{
NetworkSettings: &container.NetworkSettingsSummary{
Networks: map[string]*network.EndpointSettings{
"network": {
IPAddress: privIP,
},
},
},
Ports: []types.Port{
Ports: []container.Port{
{Type: "udp", PrivatePort: privPort, PublicPort: pubPort},
},
}
@@ -346,7 +346,7 @@ func TestStreamDefaultValues(t *testing.T) {
}
func TestExplicitExclude(t *testing.T) {
r, ok := makeRoutes(&types.Container{
r, ok := makeRoutes(&container.Summary{
Names: dummyNames,
Labels: map[string]string{
D.LabelAliases: "a",
@@ -360,9 +360,9 @@ func TestExplicitExclude(t *testing.T) {
func TestImplicitExcludeDatabase(t *testing.T) {
t.Run("mount path detection", func(t *testing.T) {
r, ok := makeRoutes(&types.Container{
r, ok := makeRoutes(&container.Summary{
Names: dummyNames,
Mounts: []types.MountPoint{
Mounts: []container.MountPoint{
{Source: "/data", Destination: "/var/lib/postgresql/data"},
},
})["a"]
@@ -370,9 +370,9 @@ func TestImplicitExcludeDatabase(t *testing.T) {
ExpectTrue(t, r.ShouldExclude())
})
t.Run("exposed port detection", func(t *testing.T) {
r, ok := makeRoutes(&types.Container{
r, ok := makeRoutes(&container.Summary{
Names: dummyNames,
Ports: []types.Port{
Ports: []container.Port{
{Type: "tcp", PrivatePort: 5432, PublicPort: 5432},
},
})["a"]

View File

@@ -128,7 +128,7 @@ func (p *Provider) loadRoutes() (routes route.Routes, err gperr.Error) {
if err != nil && len(routes) == 0 {
return route.Routes{}, err
}
errs := gperr.NewBuilder("routes error")
errs := gperr.NewBuilder()
errs.Add(err)
// check for exclusion
// set alias and provider, then validate

View File

@@ -6,10 +6,9 @@ import (
"github.com/yusing/go-proxy/agent/pkg/agent"
"github.com/yusing/go-proxy/agent/pkg/agentproxy"
"github.com/yusing/go-proxy/internal/api/v1/favicon"
"github.com/yusing/go-proxy/internal/common"
"github.com/yusing/go-proxy/internal/docker"
"github.com/yusing/go-proxy/internal/gperr"
"github.com/yusing/go-proxy/internal/homepage"
"github.com/yusing/go-proxy/internal/idlewatcher"
"github.com/yusing/go-proxy/internal/logging"
gphttp "github.com/yusing/go-proxy/internal/net/gphttp"
@@ -104,10 +103,10 @@ func (r *ReveseProxyRoute) Start(parent task.Parent) gperr.Error {
switch {
case r.UseIdleWatcher():
waker, err := idlewatcher.NewHTTPWaker(parent, r, r.rp)
waker, err := idlewatcher.NewWatcher(parent, r)
if err != nil {
r.task.Finish(err)
return err
return gperr.Wrap(err, "idlewatcher error")
}
r.handler = waker
r.HealthMon = waker
@@ -191,6 +190,10 @@ func (r *ReveseProxyRoute) ServeHTTP(w http.ResponseWriter, req *http.Request) {
r.handler.ServeHTTP(w, req)
}
func (r *ReveseProxyRoute) ReverseProxy() *reverseproxy.ReverseProxy {
return r.rp
}
func (r *ReveseProxyRoute) HealthMonitor() health.HealthMonitor {
return r.HealthMon
}

View File

@@ -10,7 +10,6 @@ import (
"github.com/yusing/go-proxy/internal/gperr"
"github.com/yusing/go-proxy/internal/homepage"
idlewatcher "github.com/yusing/go-proxy/internal/idlewatcher/types"
net "github.com/yusing/go-proxy/internal/net/types"
"github.com/yusing/go-proxy/internal/task"
"github.com/yusing/go-proxy/internal/utils/strutils"
"github.com/yusing/go-proxy/internal/watcher/health"
@@ -52,8 +51,8 @@ type (
Provider string `json:"provider,omitempty"`
// private fields
LisURL *net.URL `json:"lurl,omitempty"`
ProxyURL *net.URL `json:"purl,omitempty"`
LisURL *url.URL `json:"lurl,omitempty"`
ProxyURL *url.URL `json:"purl,omitempty"`
Idlewatcher *idlewatcher.Config `json:"idlewatcher,omitempty"`
@@ -88,7 +87,7 @@ func (r *Route) Validate() (err gperr.Error) {
if err != nil {
errs.Add(err)
}
r.ProxyURL = gperr.Collect(errs, net.ParseURL, "file://"+r.Root)
r.ProxyURL = gperr.Collect(errs, url.Parse, "file://"+r.Root)
r.Host = ""
r.Port.Proxy = 0
} else {
@@ -98,9 +97,9 @@ func (r *Route) Validate() (err gperr.Error) {
errs.Addf("unexpected listening port for %s scheme", r.Scheme)
}
case route.SchemeTCP, route.SchemeUDP:
r.LisURL = gperr.Collect(errs, net.ParseURL, fmt.Sprintf("%s://:%d", r.Scheme, r.Port.Listening))
r.LisURL = gperr.Collect(errs, url.Parse, fmt.Sprintf("%s://:%d", r.Scheme, r.Port.Listening))
}
r.ProxyURL = gperr.Collect(errs, net.ParseURL, fmt.Sprintf("%s://%s:%d", r.Scheme, r.Host, r.Port.Proxy))
r.ProxyURL = gperr.Collect(errs, url.Parse, fmt.Sprintf("%s://%s:%d", r.Scheme, r.Host, r.Port.Proxy))
}
if !r.UseHealthCheck() && (r.UseLoadBalance() || r.UseIdleWatcher()) {
@@ -160,7 +159,7 @@ func (r *Route) TargetName() string {
return r.Alias
}
func (r *Route) TargetURL() *net.URL {
func (r *Route) TargetURL() *url.URL {
return r.ProxyURL
}

View File

@@ -2,6 +2,7 @@ package rules
import (
"net/http"
"net/url"
"path"
"strconv"
"strings"
@@ -9,7 +10,6 @@ import (
"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"
)
@@ -95,7 +95,7 @@ var commands = map[string]struct {
},
validate: validateURL,
build: func(args any) CommandHandler {
target := args.(*types.URL).String()
target := args.(*url.URL).String()
return ReturningCommand(func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, target, http.StatusTemporaryRedirect)
})
@@ -160,7 +160,7 @@ var commands = map[string]struct {
},
validate: validateAbsoluteURL,
build: func(args any) CommandHandler {
target := args.(*types.URL)
target := args.(*url.URL)
if target.Scheme == "" {
target.Scheme = "http"
}

View File

@@ -1,10 +1,10 @@
package rules
import (
"net"
"net/http"
"github.com/yusing/go-proxy/internal/gperr"
"github.com/yusing/go-proxy/internal/net/types"
"github.com/yusing/go-proxy/internal/utils/strutils"
)
@@ -205,7 +205,7 @@ var checkers = map[string]struct {
},
validate: validateCIDR,
builder: func(args any) CheckFunc {
cidr := args.(types.CIDR)
cidr := args.(*net.IPNet)
return func(cached Cache, r *http.Request) bool {
ip := cached.GetRemoteIP(r)
if ip == nil {

View File

@@ -2,13 +2,14 @@ package rules
import (
"fmt"
"net"
"net/url"
"os"
"path"
"strings"
"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 (
@@ -48,24 +49,24 @@ func toKVOptionalV(args []string) (any, gperr.Error) {
}
}
// validateURL returns types.URL with the URL validated.
// validateURL returns url.URL with the URL validated.
func validateURL(args []string) (any, gperr.Error) {
if len(args) != 1 {
return nil, ErrExpectOneArg
}
u, err := types.ParseURL(args[0])
u, err := url.Parse(args[0])
if err != nil {
return nil, ErrInvalidArguments.With(err)
}
return u, nil
}
// validateAbsoluteURL returns types.URL with the URL validated.
// validateAbsoluteURL returns url.URL with the URL validated.
func validateAbsoluteURL(args []string) (any, gperr.Error) {
if len(args) != 1 {
return nil, ErrExpectOneArg
}
u, err := types.ParseURL(args[0])
u, err := url.Parse(args[0])
if err != nil {
return nil, ErrInvalidArguments.With(err)
}
@@ -86,7 +87,7 @@ func validateCIDR(args []string) (any, gperr.Error) {
if !strings.Contains(args[0], "/") {
args[0] += "/32"
}
cidr, err := types.ParseCIDR(args[0])
_, cidr, err := net.ParseCIDR(args[0])
if err != nil {
return nil, ErrInvalidArguments.With(err)
}

View File

@@ -5,7 +5,6 @@ import (
"errors"
"github.com/rs/zerolog"
"github.com/yusing/go-proxy/internal/docker"
"github.com/yusing/go-proxy/internal/gperr"
"github.com/yusing/go-proxy/internal/idlewatcher"
"github.com/yusing/go-proxy/internal/logging"
@@ -58,10 +57,10 @@ func (r *StreamRoute) Start(parent task.Parent) gperr.Error {
switch {
case r.UseIdleWatcher():
waker, err := idlewatcher.NewStreamWaker(parent, r, r.Stream)
waker, err := idlewatcher.NewWatcher(parent, r)
if err != nil {
r.task.Finish(err)
return err
return gperr.Wrap(err, "idlewatcher error")
}
r.Stream = waker
r.HealthMon = waker

View File

@@ -2,6 +2,7 @@ package route
import (
"net/http"
"net/url"
"github.com/yusing/go-proxy/agent/pkg/agent"
"github.com/yusing/go-proxy/internal/docker"
@@ -22,7 +23,7 @@ type (
task.TaskFinisher
ProviderName() string
TargetName() string
TargetURL() *net.URL
TargetURL() *url.URL
HealthMonitor() health.HealthMonitor
Reference() string