Use ipTree to determine the global IP

This commit is contained in:
sanyo
2020-07-16 17:11:34 +08:00
parent 6e99d8153a
commit 31ea7c520d
4 changed files with 69 additions and 50 deletions

1
go.mod
View File

@@ -5,6 +5,7 @@ go 1.12
require (
github.com/BurntSushi/toml v0.3.1
github.com/gorilla/handlers v1.4.0
github.com/infobloxopen/go-trees v0.0.0-20190313150506-2af4e13f9062
github.com/miekg/dns v1.1.22
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550 // indirect
golang.org/x/net v0.0.0-20191027093000-83d349e8ac1a

2
go.sum
View File

@@ -2,6 +2,8 @@ github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/gorilla/handlers v1.4.0 h1:XulKRWSQK5uChr4pEgSE4Tc/OcmnU9GJuSwdog/tZsA=
github.com/gorilla/handlers v1.4.0/go.mod h1:Qkdc/uu4tH4g6mTK6auzZ766c4CA0Ng8+o/OAirnOIQ=
github.com/infobloxopen/go-trees v0.0.0-20190313150506-2af4e13f9062 h1:d3VSuNcgTCn21dNMm8g412Fck/XWFmMj4nJhhHT7ZZ0=
github.com/infobloxopen/go-trees v0.0.0-20190313150506-2af4e13f9062/go.mod h1:PcNJqIlcX/dj3DTG/+QQnRvSgTMG6CLpRMjWcv4+J6w=
github.com/miekg/dns v1.1.14 h1:wkQWn9wIp4mZbwW8XV6Km6owkvRPbOiV004ZM2CkGvA=
github.com/miekg/dns v1.1.14/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg=
github.com/miekg/dns v1.1.22 h1:Jm64b3bO9kP43ddLjL2EY3Io6bmy1qGb9Xxz6TqS6rc=

View File

@@ -24,106 +24,107 @@
package jsonDNS
import (
"github.com/infobloxopen/go-trees/iptree"
"net"
)
// RFC6890
var localIPv4Nets = []net.IPNet{
var defaultFilter *iptree.Tree
func init() {
defaultFilter = iptree.NewTree()
// RFC6890
// This host on this network
net.IPNet{
defaultFilter.InplaceInsertNet(&net.IPNet{
net.IP{0, 0, 0, 0},
net.IPMask{255, 0, 0, 0},
},
}, struct{}{})
// Private-Use Networks
net.IPNet{
defaultFilter.InplaceInsertNet(&net.IPNet{
net.IP{10, 0, 0, 0},
net.IPMask{255, 0, 0, 0},
},
}, struct{}{})
// Shared Address Space
net.IPNet{
defaultFilter.InplaceInsertNet(&net.IPNet{
net.IP{100, 64, 0, 0},
net.IPMask{255, 192, 0, 0},
},
}, struct{}{})
// Loopback
net.IPNet{
defaultFilter.InplaceInsertNet(&net.IPNet{
net.IP{127, 0, 0, 0},
net.IPMask{255, 0, 0, 0},
},
}, struct{}{})
// Link Local
net.IPNet{
defaultFilter.InplaceInsertNet(&net.IPNet{
net.IP{169, 254, 0, 0},
net.IPMask{255, 255, 0, 0},
},
}, struct{}{})
// Private-Use Networks
net.IPNet{
defaultFilter.InplaceInsertNet(&net.IPNet{
net.IP{172, 16, 0, 0},
net.IPMask{255, 240, 0, 0},
},
}, struct{}{})
// DS-Lite
net.IPNet{
defaultFilter.InplaceInsertNet(&net.IPNet{
net.IP{192, 0, 0, 0},
net.IPMask{255, 255, 255, 248},
},
}, struct{}{})
// 6to4 Relay Anycast
net.IPNet{
defaultFilter.InplaceInsertNet(&net.IPNet{
net.IP{192, 88, 99, 0},
net.IPMask{255, 255, 255, 0},
},
}, struct{}{})
// Private-Use Networks
net.IPNet{
defaultFilter.InplaceInsertNet(&net.IPNet{
net.IP{192, 168, 0, 0},
net.IPMask{255, 255, 0, 0},
},
}, struct{}{})
// Reserved for Future Use & Limited Broadcast
net.IPNet{
defaultFilter.InplaceInsertNet(&net.IPNet{
net.IP{240, 0, 0, 0},
net.IPMask{240, 0, 0, 0},
},
}
}, struct{}{})
// RFC6890
var localIPv6Nets = []net.IPNet{
// RFC6890
// Unspecified & Loopback Address
net.IPNet{
defaultFilter.InplaceInsertNet(&net.IPNet{
net.IP{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
net.IPMask{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe},
},
}, struct{}{})
// Discard-Only Prefix
net.IPNet{
defaultFilter.InplaceInsertNet(&net.IPNet{
net.IP{0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
net.IPMask{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
},
}, struct{}{})
// Unique-Local
net.IPNet{
defaultFilter.InplaceInsertNet(&net.IPNet{
net.IP{0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
net.IPMask{0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
},
}, struct{}{})
// Linked-Scoped Unicast
net.IPNet{
defaultFilter.InplaceInsertNet(&net.IPNet{
net.IP{0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
net.IPMask{0xff, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
},
}, struct{}{})
}
func IsGlobalIP(ip net.IP) bool {
if ip == nil {
return false
}
if ipv4 := ip.To4(); len(ipv4) == net.IPv4len {
for _, ipnet := range localIPv4Nets {
if ipnet.Contains(ip) {
return false
}
}
return true
}
if len(ip) == net.IPv6len {
for _, ipnet := range localIPv6Nets {
if ipnet.Contains(ip) {
return false
}
}
return true
}
return true
_, contained := defaultFilter.GetByIP(ip)
return !contained
}

15
json-dns/globalip_test.go Normal file
View File

@@ -0,0 +1,15 @@
package jsonDNS
import (
"fmt"
"net"
"testing"
)
func TestFindIp(t *testing.T) {
fmt.Println(IsGlobalIP(net.IP{127, 0, 0, 1}))
fmt.Println(IsGlobalIP(net.IP{192, 168, 0, 0}))
fmt.Println(IsGlobalIP(net.IP{110, 100, 100, 100}))
}