File tree Expand file tree Collapse file tree 2 files changed +31
-2
lines changed Expand file tree Collapse file tree 2 files changed +31
-2
lines changed Original file line number Diff line number Diff line change 1414
1515package ip
1616
17- import "fmt"
17+ import (
18+ "fmt"
19+
20+ "github.com/casbin/caswaf/util"
21+ )
1822
1923func InitIpDb () {
2024 err := Init ("ip/17monipdb.dat" )
@@ -24,6 +28,11 @@ func InitIpDb() {
2428}
2529
2630func IsAbroadIp (ip string ) bool {
31+ // If it's an intranet IP, it's not abroad
32+ if util .IsIntranetIp (ip ) {
33+ return false
34+ }
35+
2736 info , err := Find (ip )
2837 if err != nil {
2938 fmt .Printf ("error: ip = %s, error = %s\n " , ip , err .Error ())
Original file line number Diff line number Diff line change 1414
1515package util
1616
17- import "os"
17+ import (
18+ "net"
19+ "os"
20+ )
1821
1922var hostname = ""
2023
@@ -30,3 +33,20 @@ func init() {
3033func GetHostname () string {
3134 return hostname
3235}
36+
37+ func IsIntranetIp (ip string ) bool {
38+ ipStr , _ , err := net .SplitHostPort (ip )
39+ if err != nil {
40+ ipStr = ip
41+ }
42+
43+ parsedIP := net .ParseIP (ipStr )
44+ if parsedIP == nil {
45+ return false
46+ }
47+
48+ return parsedIP .IsPrivate () ||
49+ parsedIP .IsLoopback () ||
50+ parsedIP .IsLinkLocalUnicast () ||
51+ parsedIP .IsLinkLocalMulticast ()
52+ }
You can’t perform that action at this time.
0 commit comments