mirror of
https://github.com/yusing/godoxy.git
synced 2025-06-09 13:02:33 +02:00
fixed problems related to reload and port selection
This commit is contained in:
parent
d47b672aa5
commit
51c6eb4597
2 changed files with 41 additions and 7 deletions
|
@ -75,31 +75,41 @@ func FromDocker(c *types.Container, dockerHost string) (res *Container) {
|
||||||
func FromJSON(json types.ContainerJSON, dockerHost string) *Container {
|
func FromJSON(json types.ContainerJSON, dockerHost string) *Container {
|
||||||
ports := make([]types.Port, 0)
|
ports := make([]types.Port, 0)
|
||||||
for k, bindings := range json.NetworkSettings.Ports {
|
for k, bindings := range json.NetworkSettings.Ports {
|
||||||
|
privPortStr, proto := k.Port(), k.Proto()
|
||||||
|
privPort, _ := strconv.ParseUint(privPortStr, 10, 16)
|
||||||
|
ports = append(ports, types.Port{
|
||||||
|
PrivatePort: uint16(privPort),
|
||||||
|
Type: proto,
|
||||||
|
})
|
||||||
for _, v := range bindings {
|
for _, v := range bindings {
|
||||||
pubPort, _ := strconv.ParseUint(v.HostPort, 10, 16)
|
pubPort, _ := strconv.ParseUint(v.HostPort, 10, 16)
|
||||||
privPort, _ := strconv.ParseUint(k.Port(), 10, 16)
|
|
||||||
ports = append(ports, types.Port{
|
ports = append(ports, types.Port{
|
||||||
IP: v.HostIP,
|
IP: v.HostIP,
|
||||||
PublicPort: uint16(pubPort),
|
PublicPort: uint16(pubPort),
|
||||||
PrivatePort: uint16(privPort),
|
PrivatePort: uint16(privPort),
|
||||||
|
Type: proto,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
cont := FromDocker(&types.Container{
|
cont := FromDocker(&types.Container{
|
||||||
ID: json.ID,
|
ID: json.ID,
|
||||||
Names: []string{json.Name},
|
Names: []string{strings.TrimPrefix(json.Name, "/")},
|
||||||
Image: json.Image,
|
Image: json.Image,
|
||||||
Ports: ports,
|
Ports: ports,
|
||||||
Labels: json.Config.Labels,
|
Labels: json.Config.Labels,
|
||||||
State: json.State.Status,
|
State: json.State.Status,
|
||||||
Status: json.State.Status,
|
Status: json.State.Status,
|
||||||
|
Mounts: json.Mounts,
|
||||||
|
NetworkSettings: &types.SummaryNetworkSettings{
|
||||||
|
Networks: json.NetworkSettings.Networks,
|
||||||
|
},
|
||||||
}, dockerHost)
|
}, dockerHost)
|
||||||
cont.NetworkMode = string(json.HostConfig.NetworkMode)
|
cont.NetworkMode = string(json.HostConfig.NetworkMode)
|
||||||
return cont
|
return cont
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Container) setPublicIP() {
|
func (c *Container) setPublicIP() {
|
||||||
if c.PublicPortMapping == nil {
|
if !c.Running {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if strings.HasPrefix(c.DockerHost, "unix://") {
|
if strings.HasPrefix(c.DockerHost, "unix://") {
|
||||||
|
@ -123,6 +133,9 @@ func (c *Container) setPrivateIP(helper containerHelper) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
for _, v := range helper.NetworkSettings.Networks {
|
for _, v := range helper.NetworkSettings.Networks {
|
||||||
|
if v.IPAddress == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
c.PrivateIP = v.IPAddress
|
c.PrivateIP = v.IPAddress
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,8 @@ import (
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/docker/docker/api/types"
|
||||||
|
"github.com/sirupsen/logrus"
|
||||||
"github.com/yusing/go-proxy/internal/common"
|
"github.com/yusing/go-proxy/internal/common"
|
||||||
"github.com/yusing/go-proxy/internal/docker"
|
"github.com/yusing/go-proxy/internal/docker"
|
||||||
"github.com/yusing/go-proxy/internal/homepage"
|
"github.com/yusing/go-proxy/internal/homepage"
|
||||||
|
@ -51,7 +53,7 @@ func (e *RawEntry) FillMissingFields() {
|
||||||
e.Host = e.PrivateIP
|
e.Host = e.PrivateIP
|
||||||
case e.PublicIP != "":
|
case e.PublicIP != "":
|
||||||
e.Host = e.PublicIP
|
e.Host = e.PublicIP
|
||||||
default:
|
case !isDocker:
|
||||||
e.Host = "localhost"
|
e.Host = "localhost"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -75,10 +77,14 @@ func (e *RawEntry) FillMissingFields() {
|
||||||
} else if pp == "" && e.Scheme == "https" {
|
} else if pp == "" && e.Scheme == "https" {
|
||||||
pp = "443"
|
pp = "443"
|
||||||
} else if pp == "" {
|
} else if pp == "" {
|
||||||
if p, ok := F.FirstValueOf(e.PrivatePortMapping); ok {
|
if p := lowestPort(e.PrivatePortMapping); p != "" {
|
||||||
pp = U.PortString(p.PrivatePort)
|
pp = p
|
||||||
|
} else if p := lowestPort(e.PublicPortMapping); p != "" {
|
||||||
|
pp = p
|
||||||
} else if !isDocker {
|
} else if !isDocker {
|
||||||
pp = "80"
|
pp = "80"
|
||||||
|
} else {
|
||||||
|
logrus.Debugf("no port found for %s", e.Alias)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -118,6 +124,9 @@ func (e *RawEntry) FillMissingFields() {
|
||||||
if e.HealthCheck.Interval == 0 {
|
if e.HealthCheck.Interval == 0 {
|
||||||
e.HealthCheck.Interval = common.HealthCheckIntervalDefault
|
e.HealthCheck.Interval = common.HealthCheckIntervalDefault
|
||||||
}
|
}
|
||||||
|
if e.HealthCheck.Timeout == 0 {
|
||||||
|
e.HealthCheck.Timeout = common.HealthCheckTimeoutDefault
|
||||||
|
}
|
||||||
if e.IdleTimeout == "" {
|
if e.IdleTimeout == "" {
|
||||||
e.IdleTimeout = common.IdleTimeoutDefault
|
e.IdleTimeout = common.IdleTimeoutDefault
|
||||||
}
|
}
|
||||||
|
@ -133,7 +142,7 @@ func (e *RawEntry) FillMissingFields() {
|
||||||
|
|
||||||
e.Port = joinPorts(lp, pp, extra)
|
e.Port = joinPorts(lp, pp, extra)
|
||||||
|
|
||||||
if e.Port == "" {
|
if e.Port == "" || e.Host == "" {
|
||||||
e.Port = "0"
|
e.Port = "0"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -165,3 +174,15 @@ func joinPorts(lp string, pp string, extra string) string {
|
||||||
}
|
}
|
||||||
return strings.Join(s, ":")
|
return strings.Join(s, ":")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func lowestPort(ports map[string]types.Port) string {
|
||||||
|
var cmp uint16
|
||||||
|
var res string
|
||||||
|
for port, v := range ports {
|
||||||
|
if v.PrivatePort < cmp || cmp == 0 {
|
||||||
|
cmp = v.PrivatePort
|
||||||
|
res = port
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return res
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue