Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions agent/app/service/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -1429,7 +1429,7 @@ func checkPortStats(ports []dto.PortHelper) (nat.PortMap, error) {
portMap[nat.Port(fmt.Sprintf("%d/%s", containerStart+i, port.Protocol))] = []nat.PortBinding{bindItem}
}
for i := hostStart; i <= hostEnd; i++ {
if common.ScanPort(i) {
if common.ScanPortWithIP(port.HostIP, i) {
return portMap, buserr.WithDetail("ErrPortInUsed", i, nil)
}
}
Expand All @@ -1440,7 +1440,7 @@ func checkPortStats(ports []dto.PortHelper) (nat.PortMap, error) {
} else {
portItem, _ = strconv.Atoi(port.HostPort)
}
if common.ScanPort(portItem) {
if common.ScanPortWithIP(port.HostIP, portItem) {
return portMap, buserr.WithDetail("ErrPortInUsed", portItem, nil)
}
bindItem := nat.PortBinding{HostPort: strconv.Itoa(portItem), HostIP: port.HostIP}
Expand Down
18 changes: 18 additions & 0 deletions agent/utils/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,24 @@ func ScanPortWithProto(port int, proto string) bool {
return ScanPort(port)
}

func ScanPortWithIP(ip string, port int) bool {
if len(ip) == 0 {
return ScanPort(port)
}
address := net.JoinHostPort(ip, fmt.Sprintf("%d", port))
timeout := time.Second * 2
conn, err := net.DialTimeout("tcp", address, timeout)

if err != nil {
if opErr, ok := err.(*net.OpError); ok && opErr.Timeout() {
return true
}
return false
}
defer conn.Close()
return true
}

func IsNum(s string) bool {
_, err := strconv.ParseFloat(s, 64)
return err == nil
Expand Down
Loading