Skip to content

Commit 5131afa

Browse files
refactor: const for ip zero or loopback address (#73)
* refactor: const for ip zero or loopback address * docs: update release notes * chore: typo to AnyAddress --------- Co-authored-by: hardy <luohf@infinilabs.com>
1 parent 9bd8d1c commit 5131afa

File tree

4 files changed

+23
-13
lines changed

4 files changed

+23
-13
lines changed

core/api/api.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ func StartAPI() {
208208
listenAddress = apiConfig.NetworkConfig.GetBindingAddr()
209209
}
210210

211-
if util.ContainStr(listenAddress, "0.0.0.0") {
211+
if util.ContainStr(listenAddress, util.AnyAddress) {
212212
ips := util.GetLocalIPs()
213213
if len(ips) > 0 {
214214
log.Infof("local ips: %v", util.JoinArray(ips, ", "))

core/host/process_info.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"bufio"
2828
"bytes"
2929
log "github.com/cihub/seelog"
30+
"infini.sh/framework/core/util"
3031
"os/exec"
3132
"runtime"
3233
"strconv"
@@ -113,7 +114,7 @@ func getPortByPid(pid string) []int {
113114

114115
func getPortByPidWindows(pid string) []int {
115116
//netstat -ano|findStr /V "127.0.0.1" | findStr "780"
116-
cmd := []string{"netstat", "-ano", "findStr", "/V", "127.0.0.1", "findStr", pid}
117+
cmd := []string{"netstat", "-ano", "findStr", "/V", util.LocalAddress, "findStr", pid}
117118
var stdout bytes.Buffer
118119
c1 := exec.Command(cmd[0], cmd[1])
119120
c2 := exec.Command(cmd[2], cmd[3], cmd[4])

core/util/netutils.go

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,15 @@ import (
3535
"time"
3636
)
3737

38+
// AnyAddress is the reserved address
39+
const AnyAddress = "0.0.0.0"
40+
41+
// LocalIpv6Address is the local ipv6 address
42+
const LocalIpv6Address = "0.0.0.1"
43+
44+
// LocalAddress is the local ipv4 address
45+
const LocalAddress = "127.0.0.1"
46+
3847
//Class Starting IPAddress Ending IP Address # of Hosts
3948
//A 10.0.0.0 10.255.255.255 16,777,216
4049
//B 172.16.0.0 172.31.255.255 1,048,576
@@ -173,7 +182,7 @@ func GetSafetyInternalAddress(addr string) string {
173182

174183
if strings.Contains(addr, ":") {
175184
array := strings.Split(addr, ":")
176-
if array[0] == "0.0.0.0" {
185+
if array[0] == AnyAddress {
177186
array[0], _ = GetIntranetIP()
178187
}
179188
return strings.Join(array, ":")
@@ -192,7 +201,7 @@ func GetValidAddress(addr string) string {
192201
if strings.Index(addr, ":") >= 0 {
193202
array := strings.Split(addr, ":")
194203
if len(array[0]) == 0 {
195-
array[0] = "127.0.0.1"
204+
array[0] = LocalAddress
196205
addr = strings.Join(array, ":")
197206
}
198207
}
@@ -266,11 +275,11 @@ func GetLocalIPs() []string {
266275
// IsLocalAddress check if the address is local address
267276
func IsLocalAddress(address []string, localIPs []string) bool {
268277
for _, add := range address {
269-
if UnifyLocalAddress(add) == LOCAL_ADDRESS {
278+
if UnifyLocalAddress(add) == LocalAddress {
270279
continue
271280
}
272281

273-
if add == "0.0.0.0" {
282+
if add == AnyAddress {
274283
continue
275284
}
276285

@@ -360,24 +369,22 @@ func ClientIP(r *http.Request) string {
360369

361370
if ip, _, err := net.SplitHostPort(strings.TrimSpace(r.RemoteAddr)); err == nil {
362371
if ip == "::1" {
363-
ip = "127.0.0.1"
372+
ip = LocalAddress
364373
}
365374
return ip
366375
}
367376

368377
return ""
369378
}
370379

371-
const LOCAL_ADDRESS = "127.0.0.1"
372-
373380
func UnifyLocalAddress(host string) string {
374381
//unify host
375382
if ContainStr(host, "localhost") {
376-
host = strings.Replace(host, "localhost", LOCAL_ADDRESS, -1)
383+
host = strings.Replace(host, "localhost", LocalAddress, -1)
377384
} else if ContainStr(host, "[::1]") {
378-
host = strings.Replace(host, "[::1]", LOCAL_ADDRESS, -1)
385+
host = strings.Replace(host, "[::1]", LocalAddress, -1)
379386
} else if ContainStr(host, "::1") {
380-
host = strings.Replace(host, "::1", LOCAL_ADDRESS, -1)
387+
host = strings.Replace(host, "::1", LocalAddress, -1)
381388
}
382389
return host
383390
}

docs/content.en/docs/release-notes/_index.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@ Information about release notes of INFINI Framework is provided here.
2121

2222
### Improvements
2323

24+
- Refactor loopback address to use const (#73)
2425
- Add debug message for `queue` comsumer (#77)
2526

27+
2628
## v1.1.1 (2025-01-24)
2729

2830
### Features
@@ -40,7 +42,7 @@ Information about release notes of INFINI Framework is provided here.
4042
- Handle simplified bulk metdata, parse index from url path (#59)
4143
- Improve handling of message read for partially loaded files (#63)
4244

43-
## v1.1.0 (2025-01-11)
45+
## v1.1.0 (2025-01-11)
4446

4547
### Breaking changes
4648

0 commit comments

Comments
 (0)