mirror of
https://github.com/yusing/godoxy.git
synced 2025-06-09 04:52:35 +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 {
|
||||
ports := make([]types.Port, 0)
|
||||
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 {
|
||||
pubPort, _ := strconv.ParseUint(v.HostPort, 10, 16)
|
||||
privPort, _ := strconv.ParseUint(k.Port(), 10, 16)
|
||||
ports = append(ports, types.Port{
|
||||
IP: v.HostIP,
|
||||
PublicPort: uint16(pubPort),
|
||||
PrivatePort: uint16(privPort),
|
||||
Type: proto,
|
||||
})
|
||||
}
|
||||
}
|
||||
cont := FromDocker(&types.Container{
|
||||
ID: json.ID,
|
||||
Names: []string{json.Name},
|
||||
Names: []string{strings.TrimPrefix(json.Name, "/")},
|
||||
Image: json.Image,
|
||||
Ports: ports,
|
||||
Labels: json.Config.Labels,
|
||||
State: json.State.Status,
|
||||
Status: json.State.Status,
|
||||
Mounts: json.Mounts,
|
||||
NetworkSettings: &types.SummaryNetworkSettings{
|
||||
Networks: json.NetworkSettings.Networks,
|
||||
},
|
||||
}, dockerHost)
|
||||
cont.NetworkMode = string(json.HostConfig.NetworkMode)
|
||||
return cont
|
||||
}
|
||||
|
||||
func (c *Container) setPublicIP() {
|
||||
if c.PublicPortMapping == nil {
|
||||
if !c.Running {
|
||||
return
|
||||
}
|
||||
if strings.HasPrefix(c.DockerHost, "unix://") {
|
||||
|
@ -123,6 +133,9 @@ func (c *Container) setPrivateIP(helper containerHelper) {
|
|||
return
|
||||
}
|
||||
for _, v := range helper.NetworkSettings.Networks {
|
||||
if v.IPAddress == "" {
|
||||
continue
|
||||
}
|
||||
c.PrivateIP = v.IPAddress
|
||||
return
|
||||
}
|
||||
|
|
|
@ -4,6 +4,8 @@ import (
|
|||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/docker/docker/api/types"
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/yusing/go-proxy/internal/common"
|
||||
"github.com/yusing/go-proxy/internal/docker"
|
||||
"github.com/yusing/go-proxy/internal/homepage"
|
||||
|
@ -51,7 +53,7 @@ func (e *RawEntry) FillMissingFields() {
|
|||
e.Host = e.PrivateIP
|
||||
case e.PublicIP != "":
|
||||
e.Host = e.PublicIP
|
||||
default:
|
||||
case !isDocker:
|
||||
e.Host = "localhost"
|
||||
}
|
||||
}
|
||||
|
@ -75,10 +77,14 @@ func (e *RawEntry) FillMissingFields() {
|
|||
} else if pp == "" && e.Scheme == "https" {
|
||||
pp = "443"
|
||||
} else if pp == "" {
|
||||
if p, ok := F.FirstValueOf(e.PrivatePortMapping); ok {
|
||||
pp = U.PortString(p.PrivatePort)
|
||||
if p := lowestPort(e.PrivatePortMapping); p != "" {
|
||||
pp = p
|
||||
} else if p := lowestPort(e.PublicPortMapping); p != "" {
|
||||
pp = p
|
||||
} else if !isDocker {
|
||||
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 {
|
||||
e.HealthCheck.Interval = common.HealthCheckIntervalDefault
|
||||
}
|
||||
if e.HealthCheck.Timeout == 0 {
|
||||
e.HealthCheck.Timeout = common.HealthCheckTimeoutDefault
|
||||
}
|
||||
if e.IdleTimeout == "" {
|
||||
e.IdleTimeout = common.IdleTimeoutDefault
|
||||
}
|
||||
|
@ -133,7 +142,7 @@ func (e *RawEntry) FillMissingFields() {
|
|||
|
||||
e.Port = joinPorts(lp, pp, extra)
|
||||
|
||||
if e.Port == "" {
|
||||
if e.Port == "" || e.Host == "" {
|
||||
e.Port = "0"
|
||||
}
|
||||
}
|
||||
|
@ -165,3 +174,15 @@ func joinPorts(lp string, pp string, extra string) string {
|
|||
}
|
||||
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