no longer exclude a stopped container that have user specified port

This commit is contained in:
yusing 2024-09-25 00:56:33 +08:00
parent 99216ffe59
commit 498082f7e5
2 changed files with 17 additions and 9 deletions

View file

@ -54,15 +54,17 @@ func (e *RawEntry) FillMissingFields() bool {
}
}
if e.PublicPortMapping != nil && e.NetworkMode != "host" {
if isDocker && e.NetworkMode != "host" {
if _, ok := e.PublicPortMapping[e.Port]; !ok { // port is not exposed, but specified
// try to fallback to first public port
if len(e.PublicPortMapping) == 0 {
return false
}
for _, p := range e.PublicPortMapping {
if p, ok := F.FirstValueOf(e.PublicPortMapping); ok {
e.Port = fmt.Sprint(p.PublicPort)
break
}
// ignore only if it is NOT RUNNING
// because stopped containers
// will have empty port mapping got from docker
if e.Running {
return false
}
}
}
@ -77,9 +79,6 @@ func (e *RawEntry) FillMissingFields() bool {
} else if e.Port == "443" {
e.Scheme = "https"
} else if isDocker {
if e.Port == "" {
return false
}
if p, ok := e.PublicPortMapping[e.Port]; ok {
if p.Type == "udp" {
e.Scheme = "udp"
@ -87,6 +86,7 @@ func (e *RawEntry) FillMissingFields() bool {
e.Scheme = "http"
}
} else {
// port is not exposed, no way to determine
return false
}
} else {

View file

@ -0,0 +1,8 @@
package functional
func FirstValueOf[KT comparable, VT any](m map[KT]VT) (_ VT, ok bool) {
for _, v := range m {
return v, true
}
return
}