diff --git a/core/api/api.go b/core/api/api.go index 4a79f67e5..548a365ac 100755 --- a/core/api/api.go +++ b/core/api/api.go @@ -208,7 +208,7 @@ func StartAPI() { listenAddress = apiConfig.NetworkConfig.GetBindingAddr() } - if util.ContainStr(listenAddress, "0.0.0.0") { + if util.ContainStr(listenAddress, util.ReservedAddress) { ips := util.GetLocalIPs() if len(ips) > 0 { log.Infof("local ips: %v", util.JoinArray(ips, ", ")) diff --git a/core/host/process_info.go b/core/host/process_info.go index 44359e114..45d54459c 100644 --- a/core/host/process_info.go +++ b/core/host/process_info.go @@ -27,6 +27,7 @@ import ( "bufio" "bytes" log "github.com/cihub/seelog" + "infini.sh/framework/core/util" "os/exec" "runtime" "strconv" @@ -113,7 +114,7 @@ func getPortByPid(pid string) []int { func getPortByPidWindows(pid string) []int { //netstat -ano|findStr /V "127.0.0.1" | findStr "780" - cmd := []string{"netstat", "-ano", "findStr", "/V", "127.0.0.1", "findStr", pid} + cmd := []string{"netstat", "-ano", "findStr", "/V", util.LocalAddress, "findStr", pid} var stdout bytes.Buffer c1 := exec.Command(cmd[0], cmd[1]) c2 := exec.Command(cmd[2], cmd[3], cmd[4]) diff --git a/core/util/netutils.go b/core/util/netutils.go index 14826590b..e94a96c84 100755 --- a/core/util/netutils.go +++ b/core/util/netutils.go @@ -35,6 +35,15 @@ import ( "time" ) +// ReservedAddress is the reserved address +const ReservedAddress = "0.0.0.0" + +// LocalIpv6Address is the local ipv6 address +const LocalIpv6Address = "0.0.0.1" + +// LocalAddress is the local ipv4 address +const LocalAddress = "127.0.0.1" + //Class Starting IPAddress Ending IP Address # of Hosts //A 10.0.0.0 10.255.255.255 16,777,216 //B 172.16.0.0 172.31.255.255 1,048,576 @@ -173,7 +182,7 @@ func GetSafetyInternalAddress(addr string) string { if strings.Contains(addr, ":") { array := strings.Split(addr, ":") - if array[0] == "0.0.0.0" { + if array[0] == ReservedAddress { array[0], _ = GetIntranetIP() } return strings.Join(array, ":") @@ -192,7 +201,7 @@ func GetValidAddress(addr string) string { if strings.Index(addr, ":") >= 0 { array := strings.Split(addr, ":") if len(array[0]) == 0 { - array[0] = "127.0.0.1" + array[0] = LocalAddress addr = strings.Join(array, ":") } } @@ -266,11 +275,11 @@ func GetLocalIPs() []string { // IsLocalAddress check if the address is local address func IsLocalAddress(address []string, localIPs []string) bool { for _, add := range address { - if UnifyLocalAddress(add) == LOCAL_ADDRESS { + if UnifyLocalAddress(add) == LocalAddress { continue } - if add == "0.0.0.0" { + if add == ReservedAddress { continue } @@ -360,7 +369,7 @@ func ClientIP(r *http.Request) string { if ip, _, err := net.SplitHostPort(strings.TrimSpace(r.RemoteAddr)); err == nil { if ip == "::1" { - ip = "127.0.0.1" + ip = LocalAddress } return ip } @@ -368,16 +377,14 @@ func ClientIP(r *http.Request) string { return "" } -const LOCAL_ADDRESS = "127.0.0.1" - func UnifyLocalAddress(host string) string { //unify host if ContainStr(host, "localhost") { - host = strings.Replace(host, "localhost", LOCAL_ADDRESS, -1) + host = strings.Replace(host, "localhost", LocalAddress, -1) } else if ContainStr(host, "[::1]") { - host = strings.Replace(host, "[::1]", LOCAL_ADDRESS, -1) + host = strings.Replace(host, "[::1]", LocalAddress, -1) } else if ContainStr(host, "::1") { - host = strings.Replace(host, "::1", LOCAL_ADDRESS, -1) + host = strings.Replace(host, "::1", LocalAddress, -1) } return host } diff --git a/docs/content.en/docs/release-notes/_index.md b/docs/content.en/docs/release-notes/_index.md index 116566317..5d931f87e 100644 --- a/docs/content.en/docs/release-notes/_index.md +++ b/docs/content.en/docs/release-notes/_index.md @@ -19,7 +19,7 @@ Information about release notes of INFINI Framework is provided here. - Add util to http handler, support to parse bool parameter - Handle simplified bulk metdata, parse index from url path (#59) - Improve handling of message read for partially loaded files (#63) - +- Refactor loopback address to use const (#73) ## v1.1.0 (2025-01-11)