From dc3575c8fdef5478691a5e77eba886fb13638eac Mon Sep 17 00:00:00 2001 From: yusing Date: Wed, 25 Sep 2024 03:43:47 +0800 Subject: [PATCH] Refactoring --- Makefile | 2 +- src/autocert/state.go | 6 +++--- src/docker/container.go | 23 ---------------------- src/docker/proxy_properties.go | 22 +++++++++++++++++++++ src/proxy/provider/docker_provider.go | 21 ++++++++++---------- src/proxy/provider/docker_provider_test.go | 9 +++++---- src/utils/serialization.go | 1 - 7 files changed, 42 insertions(+), 42 deletions(-) create mode 100644 src/docker/proxy_properties.go diff --git a/Makefile b/Makefile index 51a1f8fd..0b628531 100755 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ .PHONY: all build up quick-restart restart logs get udp-server -all: build quick-restart logs +all: debug setup: mkdir -p config certs diff --git a/src/autocert/state.go b/src/autocert/state.go index ffe308d6..76082a64 100644 --- a/src/autocert/state.go +++ b/src/autocert/state.go @@ -3,7 +3,7 @@ package autocert type CertState int const ( - CertStateValid CertState = 0 - CertStateExpired CertState = iota - CertStateMismatch CertState = iota + CertStateValid CertState = iota + CertStateExpired + CertStateMismatch ) diff --git a/src/docker/container.go b/src/docker/container.go index e825e6b1..b7fcc026 100644 --- a/src/docker/container.go +++ b/src/docker/container.go @@ -9,31 +9,11 @@ import ( U "github.com/yusing/go-proxy/utils" ) -type ProxyProperties struct { - DockerHost string `yaml:"-" json:"docker_host"` - ContainerName string `yaml:"-" json:"container_name"` - ImageName string `yaml:"-" json:"image_name"` - PublicPortMapping PortMapping `yaml:"-" json:"public_port_mapping"` // non-zero publicPort:types.Port - PrivatePortMapping PortMapping `yaml:"-" json:"private_port_mapping"` // privatePort:types.Port - NetworkMode string `yaml:"-" json:"network_mode"` - - Aliases []string `yaml:"-" json:"aliases"` - IsExcluded bool `yaml:"-" json:"is_excluded"` - IdleTimeout string `yaml:"-" json:"idle_timeout"` - WakeTimeout string `yaml:"-" json:"wake_timeout"` - StopMethod string `yaml:"-" json:"stop_method"` - StopTimeout string `yaml:"-" json:"stop_timeout"` // stop_method = "stop" only - StopSignal string `yaml:"-" json:"stop_signal"` // stop_method = "stop" | "kill" only - Running bool `yaml:"-" json:"running"` -} - type Container struct { *types.Container *ProxyProperties } -type PortMapping = map[string]types.Port - func FromDocker(c *types.Container, dockerHost string) (res Container) { res.Container = c res.ProxyProperties = &ProxyProperties{ @@ -119,9 +99,6 @@ func (c Container) getPublicPortMapping() PortMapping { func (c Container) getPrivatePortMapping() PortMapping { res := make(PortMapping) for _, v := range c.Ports { - if v.PublicPort == 0 { - continue - } res[fmt.Sprint(v.PrivatePort)] = v } return res diff --git a/src/docker/proxy_properties.go b/src/docker/proxy_properties.go new file mode 100644 index 00000000..ed791477 --- /dev/null +++ b/src/docker/proxy_properties.go @@ -0,0 +1,22 @@ +package docker + +import "github.com/docker/docker/api/types" + +type PortMapping = map[string]types.Port +type ProxyProperties struct { + DockerHost string `yaml:"-" json:"docker_host"` + ContainerName string `yaml:"-" json:"container_name"` + ImageName string `yaml:"-" json:"image_name"` + PublicPortMapping PortMapping `yaml:"-" json:"public_port_mapping"` // non-zero publicPort:types.Port + PrivatePortMapping PortMapping `yaml:"-" json:"private_port_mapping"` // privatePort:types.Port + NetworkMode string `yaml:"-" json:"network_mode"` + + Aliases []string `yaml:"-" json:"aliases"` + IsExcluded bool `yaml:"-" json:"is_excluded"` + IdleTimeout string `yaml:"-" json:"idle_timeout"` + WakeTimeout string `yaml:"-" json:"wake_timeout"` + StopMethod string `yaml:"-" json:"stop_method"` + StopTimeout string `yaml:"-" json:"stop_timeout"` // stop_method = "stop" only + StopSignal string `yaml:"-" json:"stop_signal"` // stop_method = "stop" | "kill" only + Running bool `yaml:"-" json:"running"` +} diff --git a/src/proxy/provider/docker_provider.go b/src/proxy/provider/docker_provider.go index 0f37066d..ad3ac6ce 100755 --- a/src/proxy/provider/docker_provider.go +++ b/src/proxy/provider/docker_provider.go @@ -139,17 +139,18 @@ func (p *DockerProvider) entriesFromContainerLabels(container D.Container) (M.Ra // selecting correct host port replacePrivPorts := func() { - if container.HostConfig.NetworkMode != "host" { - entries.RangeAll(func(_ string, entry *M.RawEntry) { - entryPortSplit := strings.Split(entry.Port, ":") - n := len(entryPortSplit) - // if the port matches the proxy port, replace it with the public port - if p, ok := container.PrivatePortMapping[entryPortSplit[n-1]]; ok { - entryPortSplit[n-1] = fmt.Sprint(p.PublicPort) - entry.Port = strings.Join(entryPortSplit, ":") - } - }) + if container.HostConfig.NetworkMode == "host" { + return } + entries.RangeAll(func(_ string, entry *M.RawEntry) { + entryPortSplit := strings.Split(entry.Port, ":") + n := len(entryPortSplit) + // if the port matches the proxy port, replace it with the public port + if p, ok := container.PrivatePortMapping[entryPortSplit[n-1]]; ok { + entryPortSplit[n-1] = fmt.Sprint(p.PublicPort) + entry.Port = strings.Join(entryPortSplit, ":") + } + }) } replacePrivPorts() diff --git a/src/proxy/provider/docker_provider_test.go b/src/proxy/provider/docker_provider_test.go index 353fe079..7bd9a523 100644 --- a/src/proxy/provider/docker_provider_test.go +++ b/src/proxy/provider/docker_provider_test.go @@ -264,6 +264,7 @@ func TestImplicitExcludeNoExposedPort(t *testing.T) { Ports: []types.Port{ {Type: "tcp", PrivatePort: 6379, PublicPort: 0}, // not exposed }, + State: "running", }, "")) ExpectNoError(t, err.Error()) @@ -271,7 +272,7 @@ func TestImplicitExcludeNoExposedPort(t *testing.T) { ExpectFalse(t, ok) } -func TestExcludeNonExposedPort(t *testing.T) { +func TestNotExcludeSpecifiedPort(t *testing.T) { var p DockerProvider entries, err := p.entriesFromContainerLabels(D.FromDocker(&types.Container{ Image: "redis", @@ -280,13 +281,13 @@ func TestExcludeNonExposedPort(t *testing.T) { {Type: "tcp", PrivatePort: 6379, PublicPort: 0}, // not exposed }, Labels: map[string]string{ - "proxy.redis.port": "6379:6379", // should be excluded even specified + "proxy.redis.port": "6379:6379", // but specified in label }, }, "")) ExpectNoError(t, err.Error()) _, ok := entries.Load("redis") - ExpectFalse(t, ok) + ExpectTrue(t, ok) } func TestNotExcludeNonExposedPortHostNetwork(t *testing.T) { @@ -298,7 +299,7 @@ func TestNotExcludeNonExposedPortHostNetwork(t *testing.T) { {Type: "tcp", PrivatePort: 6379, PublicPort: 0}, // not exposed }, Labels: map[string]string{ - "proxy.redis.port": "6379:6379", // should be excluded even specified + "proxy.redis.port": "6379:6379", }, } cont.HostConfig.NetworkMode = "host" diff --git a/src/utils/serialization.go b/src/utils/serialization.go index d6ef4974..28582d93 100644 --- a/src/utils/serialization.go +++ b/src/utils/serialization.go @@ -100,7 +100,6 @@ func Serialize(data any) (SerializedObject, E.NestedError) { } } default: - // return nil, fmt.Errorf("unsupported type: %s", value.Kind()) return nil, E.Unsupported("type", value.Kind()) }