feat: proxmox idlewatcher (#88)

* feat: idle sleep for proxmox LXCs

* refactor: replace deprecated docker api types

* chore(api): remove debug task list endpoint

* refactor: move servemux to gphttp/servemux; favicon.go to v1/favicon

* refactor: introduce Pool interface, move agent_pool to agent module

* refactor: simplify api code

* feat: introduce debug api

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

* chore: update Makefile for debug build tag, update README

* chore: add gperr.Unwrap method

* feat: relative time and duration formatting

* chore: add ROOT_DIR environment variable, refactor

* migration: move homepage override and icon cache to $BASE_DIR/data, add migration code

* fix: nil dereference on marshalling service health

* fix: wait for route deletion

* chore: enhance tasks debuggability

* feat: stdout access logger and MultiWriter

* fix(agent): remove agent properly on verify error

* fix(metrics): disk exclusion logic and added corresponding tests

* chore: update schema and prettify, fix package.json and Makefile

* fix: I/O buffer not being shrunk before putting back to pool

* feat: enhanced error handling module

* chore: deps upgrade

* feat: better value formatting and handling

---------

Co-authored-by: yusing <yusing@6uo.me>
This commit is contained in:
Yuzerion
2025-04-16 14:52:33 +08:00
committed by GitHub
parent 88f3a95b61
commit 57292f0fe8
173 changed files with 4131 additions and 2096 deletions

View File

@@ -84,7 +84,7 @@ func (s *FileServer) Start(parent task.Parent) gperr.Error {
if s.UseAccessLog() {
var err error
s.accessLogger, err = accesslog.NewFileAccessLogger(s.task, s.AccessLog)
s.accessLogger, err = accesslog.NewAccessLogger(s.task, s.AccessLog)
if err != nil {
s.task.Finish(err)
return gperr.Wrap(err)

View File

@@ -66,8 +66,8 @@ func (p *DockerProvider) loadRoutesImpl() (route.Routes, gperr.Error) {
return nil, gperr.Wrap(err)
}
errs := gperr.NewBuilder("")
routes := make(route.Routes)
errs := gperr.NewBuilder()
routes := make(route.Routes, len(containers))
for _, c := range containers {
container := docker.FromDocker(&c, p.dockerHost)
@@ -111,7 +111,7 @@ func (p *DockerProvider) routesFromContainerLabels(container *docker.Container)
errs := gperr.NewBuilder("label errors")
m, err := docker.ParseLabels(container.Labels)
m, err := docker.ParseLabels(container.RouteConfig)
errs.Add(err)
var wildcardProps docker.LabelMap

View File

@@ -4,10 +4,9 @@ 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"
D "github.com/yusing/go-proxy/internal/docker"
"github.com/yusing/go-proxy/internal/route"
T "github.com/yusing/go-proxy/internal/route/types"
@@ -21,7 +20,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,15 +63,15 @@ func TestApplyLabel(t *testing.T) {
},
},
}
entries := makeRoutes(&types.Container{
entries := makeRoutes(&container.Summary{
Names: dummyNames,
Labels: map[string]string{
D.LabelAliases: "a,b",
D.LabelIdleTimeout: "",
D.LabelStopMethod: common.StopMethodDefault,
D.LabelStopMethod: "stop",
D.LabelStopSignal: "SIGTERM",
D.LabelStopTimeout: common.StopTimeoutDefault,
D.LabelWakeTimeout: common.WakeTimeoutDefault,
D.LabelStopTimeout: "1h",
D.LabelWakeTimeout: "10s",
"proxy.*.no_tls_verify": "true",
"proxy.*.scheme": "https",
"proxy.*.host": "app",
@@ -110,20 +109,16 @@ func TestApplyLabel(t *testing.T) {
ExpectEqual(t, a.Middlewares, middlewaresExpect)
ExpectEqual(t, len(b.Middlewares), 0)
ExpectEqual(t, a.Container.IdleTimeout, "")
ExpectEqual(t, b.Container.IdleTimeout, "")
ExpectEqual(t, a.Container.StopTimeout, common.StopTimeoutDefault)
ExpectEqual(t, b.Container.StopTimeout, common.StopTimeoutDefault)
ExpectEqual(t, a.Container.StopMethod, common.StopMethodDefault)
ExpectEqual(t, b.Container.StopMethod, common.StopMethodDefault)
ExpectEqual(t, a.Container.WakeTimeout, common.WakeTimeoutDefault)
ExpectEqual(t, b.Container.WakeTimeout, common.WakeTimeoutDefault)
ExpectEqual(t, a.Container.StopSignal, "SIGTERM")
ExpectEqual(t, b.Container.StopSignal, "SIGTERM")
ExpectEqual(t, a.Container.IdlewatcherConfig.IdleTimeout, 0)
ExpectEqual(t, b.Container.IdlewatcherConfig.IdleTimeout, 0)
ExpectEqual(t, a.Container.IdlewatcherConfig.StopTimeout, time.Hour)
ExpectEqual(t, b.Container.IdlewatcherConfig.StopTimeout, time.Hour)
ExpectEqual(t, a.Container.IdlewatcherConfig.StopMethod, "stop")
ExpectEqual(t, b.Container.IdlewatcherConfig.StopMethod, "stop")
ExpectEqual(t, a.Container.IdlewatcherConfig.WakeTimeout, 10*time.Second)
ExpectEqual(t, b.Container.IdlewatcherConfig.WakeTimeout, 10*time.Second)
ExpectEqual(t, a.Container.IdlewatcherConfig.StopSignal, "SIGTERM")
ExpectEqual(t, b.Container.IdlewatcherConfig.StopSignal, "SIGTERM")
ExpectEqual(t, a.Homepage.Show, true)
ExpectEqual(t, a.Homepage.Icon.Value, "png/adguard-home.png")
@@ -135,7 +130,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 +157,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 +185,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 +199,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 +212,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 +235,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 +249,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 +257,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 +265,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 +282,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 +304,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 +341,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 +355,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 +365,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

@@ -23,7 +23,7 @@ type FileProvider struct {
func FileProviderImpl(filename string) ProviderImpl {
return &FileProvider{
fileName: filename,
path: path.Join(common.ConfigBasePath, filename),
path: path.Join(common.ConfigDir, filename),
l: logging.With().Str("type", "file").Str("name", filename).Logger(),
}
}

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
@@ -117,7 +116,7 @@ func (r *ReveseProxyRoute) Start(parent task.Parent) gperr.Error {
if r.UseAccessLog() {
var err error
r.rp.AccessLogger, err = accesslog.NewFileAccessLogger(r.task, r.AccessLog)
r.rp.AccessLogger, err = accesslog.NewAccessLogger(r.task, r.AccessLog)
if err != nil {
r.task.Finish(err)
return gperr.Wrap(err)
@@ -168,7 +167,7 @@ func (r *ReveseProxyRoute) Start(parent task.Parent) gperr.Error {
r.addToLoadBalancer(parent)
} else {
routes.SetHTTPRoute(r.TargetName(), r)
r.task.OnCancel("entrypoint_remove_route", func() {
r.task.OnFinished("entrypoint_remove_route", func() {
routes.DeleteHTTPRoute(r.TargetName())
})
}
@@ -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

@@ -1,8 +1,11 @@
package route
import (
"context"
"fmt"
"net/url"
"strings"
"time"
"github.com/yusing/go-proxy/agent/pkg/agent"
@@ -10,7 +13,9 @@ 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/logging"
gpnet "github.com/yusing/go-proxy/internal/net"
"github.com/yusing/go-proxy/internal/proxmox"
"github.com/yusing/go-proxy/internal/task"
"github.com/yusing/go-proxy/internal/utils/strutils"
"github.com/yusing/go-proxy/internal/watcher/health"
@@ -43,6 +48,8 @@ type (
Homepage *homepage.ItemConfig `json:"homepage,omitempty"`
AccessLog *accesslog.Config `json:"access_log,omitempty"`
Idlewatcher *idlewatcher.Config `json:"idlewatcher,omitempty"`
Metadata `deserialize:"-"`
}
@@ -52,16 +59,16 @@ type (
Provider string `json:"provider,omitempty"`
// private fields
LisURL *net.URL `json:"lurl,omitempty"`
ProxyURL *net.URL `json:"purl,omitempty"`
Idlewatcher *idlewatcher.Config `json:"idlewatcher,omitempty"`
LisURL *url.URL `json:"lurl,omitempty"`
ProxyURL *url.URL `json:"purl,omitempty"`
impl route.Route
}
Routes map[string]*Route
)
const DefaultHost = "localhost"
func (r Routes) Contains(alias string) bool {
_, ok := r[alias]
return ok
@@ -81,6 +88,70 @@ func (r *Route) Validate() (err gperr.Error) {
}
}
if r.Idlewatcher != nil && r.Idlewatcher.Proxmox != nil {
node := r.Idlewatcher.Proxmox.Node
vmid := r.Idlewatcher.Proxmox.VMID
if node == "" {
return gperr.Errorf("node (proxmox node name) is required")
}
if vmid <= 0 {
return gperr.Errorf("vmid (lxc id) is required")
}
if r.Host == DefaultHost {
containerName := r.Idlewatcher.ContainerName()
// get ip addresses of the vmid
node, ok := proxmox.Nodes.Get(node)
if !ok {
return gperr.Errorf("proxmox node %s not found in pool", node)
}
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
ips, err := node.LXCGetIPs(ctx, vmid)
if err != nil {
return gperr.Errorf("failed to get ip addresses of vmid %d: %w", vmid, err)
}
if len(ips) == 0 {
return gperr.Multiline().
Addf("no ip addresses found for %s", containerName).
Adds("make sure you have set static ip address for container instead of dhcp").
Subject(containerName)
}
l := logging.With().Str("container", containerName).Logger()
l.Info().Msg("checking if container is running")
running, err := node.LXCIsRunning(ctx, vmid)
if err != nil {
return gperr.New("failed to check container state").With(err)
}
if !running {
l.Info().Msg("starting container")
if err := node.LXCAction(ctx, vmid, proxmox.LXCStart); err != nil {
return gperr.New("failed to start container").With(err)
}
}
l.Info().Msgf("finding reachable ip addresses")
for _, ip := range ips {
if ok, _ := gpnet.PingWithTCPFallback(ctx, ip, r.Port.Proxy); ok {
r.Host = ip.String()
l.Info().Msgf("using ip %s", r.Host)
break
}
}
if r.Host == DefaultHost {
return gperr.Multiline().
Addf("no reachable ip addresses found, tried %d IPs", len(ips)).
AddLines(ips).
Subject(containerName)
}
}
}
errs := gperr.NewBuilder("entry validation failed")
if r.Scheme == route.SchemeFileServer {
@@ -88,19 +159,19 @@ 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 {
switch r.Scheme {
case route.SchemeHTTP, route.SchemeHTTPS:
if r.Port.Listening != 0 {
errs.Addf("unexpected listening port for %s scheme", r.Scheme)
case route.SchemeHTTP, route.SchemeHTTPS:
if r.Port.Listening != 0 {
errs.Addf("unexpected listening port for %s scheme", r.Scheme)
}
case route.SchemeTCP, route.SchemeUDP:
r.LisURL = gperr.Collect(errs, url.Parse, fmt.Sprintf("%s://:%d", r.Scheme, r.Port.Listening))
}
case route.SchemeTCP, route.SchemeUDP:
r.LisURL = gperr.Collect(errs, net.ParseURL, 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()) {
@@ -146,8 +217,8 @@ func (r *Route) Started() bool {
}
func (r *Route) Reference() string {
if r.Docker != nil {
return r.Docker.Image.Name
if r.Container != nil {
return r.Container.Image.Name
}
return r.Alias
}
@@ -160,7 +231,7 @@ func (r *Route) TargetName() string {
return r.Alias
}
func (r *Route) TargetURL() *net.URL {
func (r *Route) TargetURL() *url.URL {
return r.ProxyURL
}
@@ -190,6 +261,10 @@ func (r *Route) HealthMonitor() health.HealthMonitor {
}
func (r *Route) IdlewatcherConfig() *idlewatcher.Config {
cont := r.Container
if cont != nil && cont.IdlewatcherConfig != nil {
return cont.IdlewatcherConfig
}
return r.Idlewatcher
}
@@ -255,7 +330,8 @@ func (r *Route) UseLoadBalance() bool {
}
func (r *Route) UseIdleWatcher() bool {
return r.Idlewatcher != nil && r.Idlewatcher.IdleTimeout > 0
cfg := r.IdlewatcherConfig()
return cfg != nil && cfg.IdleTimeout > 0
}
func (r *Route) UseHealthCheck() bool {
@@ -276,7 +352,7 @@ func (r *Route) Finalize() {
if r.Host == "" {
switch {
case !isDocker:
r.Host = "localhost"
r.Host = DefaultHost
case cont.PrivateHostname != "":
r.Host = cont.PrivateHostname
case cont.PublicHostname != "":
@@ -379,7 +455,7 @@ func (r *Route) FinalizeHomepageConfig() {
panic("alias is empty")
}
isDocker := r.Container != nil
isDocker := r.IsDocker()
if r.Homepage == nil {
r.Homepage = &homepage.ItemConfig{Show: true}

View File

@@ -98,7 +98,7 @@ func TestRouteValidate(t *testing.T) {
Host: "example.com",
Port: route.Port{Proxy: 80},
Metadata: Metadata{
DockerContainer: &docker.Container{
Container: &docker.Container{
ContainerID: "test-id",
Image: &docker.ContainerImage{
Name: "test-image",

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
@@ -85,7 +84,7 @@ func (r *StreamRoute) Start(parent task.Parent) gperr.Error {
go r.acceptConnections()
routes.SetStreamRoute(r.TargetName(), r)
r.task.OnCancel("entrypoint_remove_route", func() {
r.task.OnFinished("entrypoint_remove_route", func() {
routes.DeleteStreamRoute(r.TargetName())
})
return nil

View File

@@ -7,7 +7,7 @@ import (
"net"
"time"
"github.com/yusing/go-proxy/internal/net/types"
gpnet "github.com/yusing/go-proxy/internal/net/types"
U "github.com/yusing/go-proxy/internal/utils"
)
@@ -15,7 +15,7 @@ type (
Stream struct {
*StreamRoute
listener types.StreamListener
listener gpnet.StreamListener
targetAddr net.Addr
}
)
@@ -56,7 +56,7 @@ func (stream *Stream) Setup() error {
}
// in case ListeningPort was zero, get the actual port
stream.Port.Listening = tcpListener.Addr().(*net.TCPAddr).Port
stream.listener = types.NetListener(tcpListener)
stream.listener = gpnet.NetListener(tcpListener)
case "udp":
stream.targetAddr, err = net.ResolveUDPAddr("udp", stream.ProxyURL.Host)
if err != nil {
@@ -80,7 +80,7 @@ func (stream *Stream) Setup() error {
return nil
}
func (stream *Stream) Accept() (conn types.StreamConn, err error) {
func (stream *Stream) Accept() (conn gpnet.StreamConn, err error) {
if stream.listener == nil {
return nil, errors.New("listener is nil")
}
@@ -100,7 +100,7 @@ func (stream *Stream) Accept() (conn types.StreamConn, err error) {
}
}
func (stream *Stream) Handle(conn types.StreamConn) error {
func (stream *Stream) Handle(conn gpnet.StreamConn) error {
switch conn := conn.(type) {
case *UDPConn:
switch stream := stream.listener.(type) {

View File

@@ -39,6 +39,7 @@ func TestHTTPConfigDeserialize(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cfg := Route{}
tt.input["host"] = "internal"
err := utils.MapUnmarshalValidate(tt.input, &cfg)
if err != nil {
ExpectNoError(t, err)

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

View File

@@ -8,7 +8,7 @@ import (
"github.com/yusing/go-proxy/internal/gperr"
"github.com/yusing/go-proxy/internal/logging"
"github.com/yusing/go-proxy/internal/net/types"
gpnet "github.com/yusing/go-proxy/internal/net/types"
F "github.com/yusing/go-proxy/internal/utils/functional"
)
@@ -57,7 +57,7 @@ func (w *UDPForwarder) Addr() net.Addr {
return w.forwarder.LocalAddr()
}
func (w *UDPForwarder) Accept() (types.StreamConn, error) {
func (w *UDPForwarder) Accept() (gpnet.StreamConn, error) {
buf := newUDPBuf()
addr, err := w.readFromListener(buf)
if err != nil {
@@ -161,7 +161,7 @@ func (w *UDPForwarder) getInitConn(conn *UDPConn, key string) (*UDPConn, error)
return dst, nil
}
func (w *UDPForwarder) Handle(streamConn types.StreamConn) error {
func (w *UDPForwarder) Handle(streamConn gpnet.StreamConn) error {
conn, ok := streamConn.(*UDPConn)
if !ok {
panic("unexpected conn type")