mirror of
https://github.com/m13253/dns-over-https.git
synced 2026-03-30 16:25:39 +00:00
Merge pull request #83 from sanyo0714/globalip_use_iptree
Use ipTree to determine the global IP
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -17,3 +17,4 @@ doh-server/doh-server
|
||||
.glide/
|
||||
|
||||
.idea/
|
||||
vendor/
|
||||
1
go.mod
1
go.mod
@@ -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.31
|
||||
golang.org/x/crypto v0.0.0-20200728195943-123391ffb6de // indirect
|
||||
golang.org/x/net v0.0.0-20200707034311-ab3426394381
|
||||
|
||||
2
go.sum
2
go.sum
@@ -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.31 h1:sJFOl9BgwbYAWOGEwr61FU28pqsBNdpRBnhGXtO06Oo=
|
||||
github.com/miekg/dns v1.1.31/go.mod h1:KNUDUusw/aVsxyTYZM1oqvCicbwhgbNgztCETuNZ7xM=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
|
||||
@@ -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
15
json-dns/globalip_test.go
Normal 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}))
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user