mirror of
https://github.com/xjasonlyu/tun2socks.git
synced 2025-10-17 14:22:25 +08:00
update fakedns module
This commit is contained in:
@@ -5,13 +5,15 @@ import (
|
|||||||
"net"
|
"net"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
trie "github.com/xjasonlyu/tun2socks/common/dns/domain-trie"
|
|
||||||
D "github.com/miekg/dns"
|
D "github.com/miekg/dns"
|
||||||
|
"github.com/xjasonlyu/tun2socks/common/cache"
|
||||||
|
trie "github.com/xjasonlyu/tun2socks/common/domain-trie"
|
||||||
|
"github.com/xjasonlyu/tun2socks/common/fakeip"
|
||||||
)
|
)
|
||||||
|
|
||||||
type handler func(w D.ResponseWriter, r *D.Msg)
|
type handler func(w D.ResponseWriter, r *D.Msg)
|
||||||
|
|
||||||
func withFakeIP(cache *Cache, pool *Pool) handler {
|
func withFakeIP(cache *cache.Cache, pool *fakeip.Pool) handler {
|
||||||
return func(w D.ResponseWriter, r *D.Msg) {
|
return func(w D.ResponseWriter, r *D.Msg) {
|
||||||
q := r.Question[0]
|
q := r.Question[0]
|
||||||
|
|
||||||
@@ -88,7 +90,7 @@ func withHost(hosts *trie.Trie, next handler) handler {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func strToHosts(str string) *trie.Trie {
|
func lineToHosts(str string) *trie.Trie {
|
||||||
// trim `'` `"` ` ` char
|
// trim `'` `"` ` ` char
|
||||||
str = strings.Trim(str, "' \"")
|
str = strings.Trim(str, "' \"")
|
||||||
if str == "" {
|
if str == "" {
|
||||||
@@ -110,7 +112,7 @@ func strToHosts(str string) *trie.Trie {
|
|||||||
return tree
|
return tree
|
||||||
}
|
}
|
||||||
|
|
||||||
func newHandler(hosts *trie.Trie, cache *Cache, pool *Pool) handler {
|
func newHandler(hosts *trie.Trie, cache *cache.Cache, pool *fakeip.Pool) handler {
|
||||||
if hosts != nil {
|
if hosts != nil {
|
||||||
return withHost(hosts, withFakeIP(cache, pool))
|
return withHost(hosts, withFakeIP(cache, pool))
|
||||||
}
|
}
|
||||||
|
@@ -7,6 +7,8 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
D "github.com/miekg/dns"
|
D "github.com/miekg/dns"
|
||||||
|
"github.com/xjasonlyu/tun2socks/common/cache"
|
||||||
|
"github.com/xjasonlyu/tun2socks/common/fakeip"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@@ -18,8 +20,7 @@ var (
|
|||||||
|
|
||||||
type Server struct {
|
type Server struct {
|
||||||
*D.Server
|
*D.Server
|
||||||
p *Pool
|
c *cache.Cache
|
||||||
c *Cache
|
|
||||||
h handler
|
h handler
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -65,31 +66,22 @@ func (s *Server) IPToHost(ip net.IP) string {
|
|||||||
return strings.TrimRight(fqdn, ".")
|
return strings.TrimRight(fqdn, ".")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) IsFakeIP(ip net.IP) bool {
|
func NewServer(fakeIPRange, hostsLine string) (*Server, error) {
|
||||||
c := ip2uint32(ip)
|
|
||||||
if c >= s.p.min && c <= s.p.max {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewServer(fakeIPRange, hostsMap string) (*Server, error) {
|
|
||||||
_, ipnet, err := net.ParseCIDR(fakeIPRange)
|
_, ipnet, err := net.ParseCIDR(fakeIPRange)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
pool, err := NewPool(ipnet)
|
pool, err := fakeip.New(ipnet)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
cache := NewCache(cacheDuration)
|
hosts := lineToHosts(hostsLine)
|
||||||
hosts := strToHosts(hostsMap)
|
cacheItem := cache.New(cacheDuration)
|
||||||
handler := newHandler(hosts, cache, pool)
|
handler := newHandler(hosts, cacheItem, pool)
|
||||||
|
|
||||||
return &Server{
|
return &Server{
|
||||||
p: pool,
|
c: cacheItem,
|
||||||
c: cache,
|
|
||||||
h: handler,
|
h: handler,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
@@ -6,9 +6,10 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
D "github.com/miekg/dns"
|
D "github.com/miekg/dns"
|
||||||
|
"github.com/xjasonlyu/tun2socks/common/cache"
|
||||||
)
|
)
|
||||||
|
|
||||||
func putMsgToCache(c *Cache, key string, msg *D.Msg) {
|
func putMsgToCache(c *cache.Cache, key string, msg *D.Msg) {
|
||||||
var ttl time.Duration
|
var ttl time.Duration
|
||||||
if len(msg.Answer) != 0 {
|
if len(msg.Answer) != 0 {
|
||||||
ttl = time.Duration(msg.Answer[0].Header().Ttl) * time.Second
|
ttl = time.Duration(msg.Answer[0].Header().Ttl) * time.Second
|
||||||
|
Reference in New Issue
Block a user