diff --git a/internal/route/route.go b/internal/route/route.go index ad56157..f8f70d4 100644 --- a/internal/route/route.go +++ b/internal/route/route.go @@ -406,10 +406,7 @@ func (r *Route) Finalize() { if pp == 0 { switch { case isDocker: - pp = lowestPort(cont.PrivatePortMapping) - if pp == 0 { - pp = lowestPort(cont.PublicPortMapping) - } + pp = preferredPort(cont.PrivatePortMapping) case r.Scheme == "https": pp = 443 default: @@ -519,9 +516,24 @@ func (r *Route) FinalizeHomepageConfig() { } } -func lowestPort(ports map[int]container.Port) (res int) { +var preferredPortOrder = []int{ + 80, + 8080, + 3000, + 8000, + 443, + 8443, +} + +func preferredPort(portMapping map[int]container.Port) (res int) { + for _, port := range preferredPortOrder { + if _, ok := portMapping[port]; ok { + return port + } + } + // fallback to lowest port cmp := (uint16)(65535) - for port, v := range ports { + for port, v := range portMapping { if v.PrivatePort < cmp { cmp = v.PrivatePort res = port diff --git a/internal/route/route_test.go b/internal/route/route_test.go index 14bcf30..9ce7458 100644 --- a/internal/route/route_test.go +++ b/internal/route/route_test.go @@ -3,6 +3,7 @@ package route import ( "testing" + "github.com/docker/docker/api/types/container" "github.com/yusing/go-proxy/internal/common" "github.com/yusing/go-proxy/internal/docker" loadbalance "github.com/yusing/go-proxy/internal/net/gphttp/loadbalancer/types" @@ -136,3 +137,14 @@ func TestRouteValidate(t *testing.T) { expect.NotNil(t, r.HealthCheck) }) } + +func TestPreferredPort(t *testing.T) { + ports := map[int]container.Port{ + 22: {PrivatePort: 22}, + 1000: {PrivatePort: 1000}, + 3000: {PrivatePort: 80}, + } + + port := preferredPort(ports) + expect.Equal(t, port, 3000) +}