@@ -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
267276func 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-
373380func 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}
0 commit comments