mirror of
https://github.com/m13253/dns-over-https.git
synced 2026-08-22 14:10:13 +00:00
Merge pull request #83 from sanyo0714/globalip_use_iptree
Use ipTree to determine the global IP
This commit is contained in:
@@ -17,3 +17,4 @@ doh-server/doh-server
|
|||||||
.glide/
|
.glide/
|
||||||
|
|
||||||
.idea/
|
.idea/
|
||||||
|
vendor/
|
||||||
@@ -5,6 +5,7 @@ go 1.12
|
|||||||
require (
|
require (
|
||||||
github.com/BurntSushi/toml v0.3.1
|
github.com/BurntSushi/toml v0.3.1
|
||||||
github.com/gorilla/handlers v1.4.0
|
github.com/gorilla/handlers v1.4.0
|
||||||
|
github.com/infobloxopen/go-trees v0.0.0-20190313150506-2af4e13f9062
|
||||||
github.com/miekg/dns v1.1.31
|
github.com/miekg/dns v1.1.31
|
||||||
golang.org/x/crypto v0.0.0-20200728195943-123391ffb6de // indirect
|
golang.org/x/crypto v0.0.0-20200728195943-123391ffb6de // indirect
|
||||||
golang.org/x/net v0.0.0-20200707034311-ab3426394381
|
golang.org/x/net v0.0.0-20200707034311-ab3426394381
|
||||||
|
|||||||
@@ -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/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 h1:XulKRWSQK5uChr4pEgSE4Tc/OcmnU9GJuSwdog/tZsA=
|
||||||
github.com/gorilla/handlers v1.4.0/go.mod h1:Qkdc/uu4tH4g6mTK6auzZ766c4CA0Ng8+o/OAirnOIQ=
|
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 h1:sJFOl9BgwbYAWOGEwr61FU28pqsBNdpRBnhGXtO06Oo=
|
||||||
github.com/miekg/dns v1.1.31/go.mod h1:KNUDUusw/aVsxyTYZM1oqvCicbwhgbNgztCETuNZ7xM=
|
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=
|
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||||
|
|||||||
+51
-50
@@ -24,106 +24,107 @@
|
|||||||
package jsonDNS
|
package jsonDNS
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/infobloxopen/go-trees/iptree"
|
||||||
"net"
|
"net"
|
||||||
)
|
)
|
||||||
|
|
||||||
// RFC6890
|
var defaultFilter *iptree.Tree
|
||||||
var localIPv4Nets = []net.IPNet{
|
|
||||||
|
func init() {
|
||||||
|
defaultFilter = iptree.NewTree()
|
||||||
|
|
||||||
|
// RFC6890
|
||||||
// This host on this network
|
// This host on this network
|
||||||
net.IPNet{
|
defaultFilter.InplaceInsertNet(&net.IPNet{
|
||||||
net.IP{0, 0, 0, 0},
|
net.IP{0, 0, 0, 0},
|
||||||
net.IPMask{255, 0, 0, 0},
|
net.IPMask{255, 0, 0, 0},
|
||||||
},
|
}, struct{}{})
|
||||||
|
|
||||||
// Private-Use Networks
|
// Private-Use Networks
|
||||||
net.IPNet{
|
defaultFilter.InplaceInsertNet(&net.IPNet{
|
||||||
net.IP{10, 0, 0, 0},
|
net.IP{10, 0, 0, 0},
|
||||||
net.IPMask{255, 0, 0, 0},
|
net.IPMask{255, 0, 0, 0},
|
||||||
},
|
}, struct{}{})
|
||||||
|
|
||||||
// Shared Address Space
|
// Shared Address Space
|
||||||
net.IPNet{
|
defaultFilter.InplaceInsertNet(&net.IPNet{
|
||||||
net.IP{100, 64, 0, 0},
|
net.IP{100, 64, 0, 0},
|
||||||
net.IPMask{255, 192, 0, 0},
|
net.IPMask{255, 192, 0, 0},
|
||||||
},
|
}, struct{}{})
|
||||||
|
|
||||||
// Loopback
|
// Loopback
|
||||||
net.IPNet{
|
defaultFilter.InplaceInsertNet(&net.IPNet{
|
||||||
net.IP{127, 0, 0, 0},
|
net.IP{127, 0, 0, 0},
|
||||||
net.IPMask{255, 0, 0, 0},
|
net.IPMask{255, 0, 0, 0},
|
||||||
},
|
}, struct{}{})
|
||||||
|
|
||||||
// Link Local
|
// Link Local
|
||||||
net.IPNet{
|
defaultFilter.InplaceInsertNet(&net.IPNet{
|
||||||
net.IP{169, 254, 0, 0},
|
net.IP{169, 254, 0, 0},
|
||||||
net.IPMask{255, 255, 0, 0},
|
net.IPMask{255, 255, 0, 0},
|
||||||
},
|
}, struct{}{})
|
||||||
|
|
||||||
// Private-Use Networks
|
// Private-Use Networks
|
||||||
net.IPNet{
|
defaultFilter.InplaceInsertNet(&net.IPNet{
|
||||||
net.IP{172, 16, 0, 0},
|
net.IP{172, 16, 0, 0},
|
||||||
net.IPMask{255, 240, 0, 0},
|
net.IPMask{255, 240, 0, 0},
|
||||||
},
|
}, struct{}{})
|
||||||
|
|
||||||
// DS-Lite
|
// DS-Lite
|
||||||
net.IPNet{
|
defaultFilter.InplaceInsertNet(&net.IPNet{
|
||||||
net.IP{192, 0, 0, 0},
|
net.IP{192, 0, 0, 0},
|
||||||
net.IPMask{255, 255, 255, 248},
|
net.IPMask{255, 255, 255, 248},
|
||||||
},
|
}, struct{}{})
|
||||||
|
|
||||||
// 6to4 Relay Anycast
|
// 6to4 Relay Anycast
|
||||||
net.IPNet{
|
defaultFilter.InplaceInsertNet(&net.IPNet{
|
||||||
net.IP{192, 88, 99, 0},
|
net.IP{192, 88, 99, 0},
|
||||||
net.IPMask{255, 255, 255, 0},
|
net.IPMask{255, 255, 255, 0},
|
||||||
},
|
}, struct{}{})
|
||||||
|
|
||||||
// Private-Use Networks
|
// Private-Use Networks
|
||||||
net.IPNet{
|
defaultFilter.InplaceInsertNet(&net.IPNet{
|
||||||
net.IP{192, 168, 0, 0},
|
net.IP{192, 168, 0, 0},
|
||||||
net.IPMask{255, 255, 0, 0},
|
net.IPMask{255, 255, 0, 0},
|
||||||
},
|
}, struct{}{})
|
||||||
|
|
||||||
// Reserved for Future Use & Limited Broadcast
|
// Reserved for Future Use & Limited Broadcast
|
||||||
net.IPNet{
|
defaultFilter.InplaceInsertNet(&net.IPNet{
|
||||||
net.IP{240, 0, 0, 0},
|
net.IP{240, 0, 0, 0},
|
||||||
net.IPMask{240, 0, 0, 0},
|
net.IPMask{240, 0, 0, 0},
|
||||||
},
|
}, struct{}{})
|
||||||
}
|
|
||||||
|
|
||||||
// RFC6890
|
// RFC6890
|
||||||
var localIPv6Nets = []net.IPNet{
|
|
||||||
// Unspecified & Loopback Address
|
// 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.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},
|
net.IPMask{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe},
|
||||||
},
|
}, struct{}{})
|
||||||
|
|
||||||
// Discard-Only Prefix
|
// 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.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},
|
net.IPMask{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
|
||||||
},
|
}, struct{}{})
|
||||||
|
|
||||||
// Unique-Local
|
// 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.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},
|
net.IPMask{0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
|
||||||
},
|
}, struct{}{})
|
||||||
|
|
||||||
// Linked-Scoped Unicast
|
// 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.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},
|
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 {
|
func IsGlobalIP(ip net.IP) bool {
|
||||||
if ip == nil {
|
if ip == nil {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
if ipv4 := ip.To4(); len(ipv4) == net.IPv4len {
|
_, contained := defaultFilter.GetByIP(ip)
|
||||||
for _, ipnet := range localIPv4Nets {
|
return !contained
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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