Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: const for ip zero or loopback address #73

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion core/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, ", "))
Expand Down
3 changes: 2 additions & 1 deletion core/host/process_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"bufio"
"bytes"
log "github.com/cihub/seelog"
"infini.sh/framework/core/util"
"os/exec"
"runtime"
"strconv"
Expand Down Expand Up @@ -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])
Expand Down
27 changes: 17 additions & 10 deletions core/util/netutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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, ":")
Expand All @@ -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, ":")
}
}
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -360,24 +369,22 @@ 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
}

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
}
2 changes: 1 addition & 1 deletion docs/content.en/docs/release-notes/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
Loading