Initial restructure of server
This commit is contained in:
		
							
								
								
									
										5
									
								
								resolver/nameserver.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										5
									
								
								resolver/nameserver.go
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,5 @@
 | 
			
		||||
package resolver
 | 
			
		||||
 | 
			
		||||
type Nameserver struct {
 | 
			
		||||
	address string
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										11
									
								
								resolver/question.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								resolver/question.go
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,11 @@
 | 
			
		||||
package resolver
 | 
			
		||||
 | 
			
		||||
type Question struct {
 | 
			
		||||
	Name  string
 | 
			
		||||
	Type  string
 | 
			
		||||
	Class string
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (q *Question) String() string {
 | 
			
		||||
	return q.Name + " " + q.Class + " " + q.Type
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										288
									
								
								resolver/resolver.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										288
									
								
								resolver/resolver.go
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,288 @@
 | 
			
		||||
package resolver
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"bufio"
 | 
			
		||||
	"errors"
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"meow.tf/joker/godns/log"
 | 
			
		||||
	"meow.tf/joker/godns/settings"
 | 
			
		||||
	"meow.tf/joker/godns/utils"
 | 
			
		||||
	"net"
 | 
			
		||||
	"os"
 | 
			
		||||
	"strconv"
 | 
			
		||||
	"strings"
 | 
			
		||||
	"sync"
 | 
			
		||||
	"time"
 | 
			
		||||
 | 
			
		||||
	"crypto/tls"
 | 
			
		||||
	"github.com/miekg/dns"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type ResolvError struct {
 | 
			
		||||
	qname, net  string
 | 
			
		||||
	nameservers []string
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (e ResolvError) Error() string {
 | 
			
		||||
	errmsg := fmt.Sprintf("%s resolv failed on %s (%s)", e.qname, strings.Join(e.nameservers, "; "), e.net)
 | 
			
		||||
	return errmsg
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type RResp struct {
 | 
			
		||||
	msg        *dns.Msg
 | 
			
		||||
	nameserver string
 | 
			
		||||
	rtt        time.Duration
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type Resolver struct {
 | 
			
		||||
	servers       []string
 | 
			
		||||
	domain_server *suffixTreeNode
 | 
			
		||||
	config        *settings.ResolvSettings
 | 
			
		||||
 | 
			
		||||
	clients map[string]*dns.Client
 | 
			
		||||
	clientLock sync.RWMutex
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func NewResolver(c settings.ResolvSettings) *Resolver {
 | 
			
		||||
	r := &Resolver{
 | 
			
		||||
		servers:       []string{},
 | 
			
		||||
		domain_server: newSuffixTreeRoot(),
 | 
			
		||||
		config:        &c,
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if len(c.ServerListFile) > 0 {
 | 
			
		||||
		r.ReadServerListFile(c.ServerListFile)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if len(c.ResolvFile) > 0 {
 | 
			
		||||
		clientConfig, err := dns.ClientConfigFromFile(c.ResolvFile)
 | 
			
		||||
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			log.Error(":%s is not a valid resolv.conf file\n", c.ResolvFile)
 | 
			
		||||
			log.Error("%s", err)
 | 
			
		||||
			panic(err)
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		for _, server := range clientConfig.Servers {
 | 
			
		||||
			r.servers = append(r.servers, net.JoinHostPort(server, clientConfig.Port))
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if len(c.DOHServer) > 0 {
 | 
			
		||||
		r.servers = append([]string{c.DOHServer}, r.servers...)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return r
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (r *Resolver) parseServerListFile(buf *os.File) {
 | 
			
		||||
	scanner := bufio.NewScanner(buf)
 | 
			
		||||
 | 
			
		||||
	var line string
 | 
			
		||||
	var idx int
 | 
			
		||||
 | 
			
		||||
	for scanner.Scan() {
 | 
			
		||||
		line = strings.TrimSpace(scanner.Text())
 | 
			
		||||
 | 
			
		||||
		if !strings.HasPrefix(line, "server") {
 | 
			
		||||
			continue
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		idx = strings.Index(line, "=")
 | 
			
		||||
 | 
			
		||||
		if idx == -1 {
 | 
			
		||||
			continue
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		line = strings.TrimSpace(line[idx+1:])
 | 
			
		||||
 | 
			
		||||
		if strings.HasPrefix(line, "https://") {
 | 
			
		||||
			r.servers = append(r.servers, line)
 | 
			
		||||
			continue
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		tokens := strings.Split(line, "/")
 | 
			
		||||
		switch len(tokens) {
 | 
			
		||||
		case 3:
 | 
			
		||||
			domain := tokens[1]
 | 
			
		||||
			ip := tokens[2]
 | 
			
		||||
 | 
			
		||||
			if !utils.IsDomain(domain) || !utils.IsIP(ip) {
 | 
			
		||||
				continue
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			r.domain_server.sinsert(strings.Split(domain, "."), ip)
 | 
			
		||||
		case 1:
 | 
			
		||||
			srv_port := strings.Split(line, "#")
 | 
			
		||||
 | 
			
		||||
			if len(srv_port) > 2 {
 | 
			
		||||
				continue
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			ip := ""
 | 
			
		||||
 | 
			
		||||
			if ip = srv_port[0]; !utils.IsIP(ip) {
 | 
			
		||||
				continue
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			port := "53"
 | 
			
		||||
 | 
			
		||||
			if len(srv_port) == 2 {
 | 
			
		||||
				if _, err := strconv.Atoi(srv_port[1]); err != nil {
 | 
			
		||||
					continue
 | 
			
		||||
				}
 | 
			
		||||
 | 
			
		||||
				port = srv_port[1]
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			r.servers = append(r.servers, net.JoinHostPort(ip, port))
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (r *Resolver) ReadServerListFile(path string) {
 | 
			
		||||
	files := strings.Split(path, ";")
 | 
			
		||||
	for _, file := range files {
 | 
			
		||||
		buf, err := os.Open(file)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			panic("Can't open " + file)
 | 
			
		||||
		}
 | 
			
		||||
		r.parseServerListFile(buf)
 | 
			
		||||
		buf.Close()
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Lookup will ask each nameserver in top-to-bottom fashion, starting a new request
 | 
			
		||||
// in every second, and return as early as possbile (have an answer).
 | 
			
		||||
// It returns an error if no request has succeeded.
 | 
			
		||||
func (r *Resolver) Lookup(net string, req *dns.Msg) (message *dns.Msg, err error) {
 | 
			
		||||
	if net == "udp" && r.config.SetEDNS0 {
 | 
			
		||||
		req = req.SetEdns0(65535, true)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	qname := req.Question[0].Name
 | 
			
		||||
 | 
			
		||||
	res := make(chan *RResp, 1)
 | 
			
		||||
	var wg sync.WaitGroup
 | 
			
		||||
	L := func(resolver *Resolver, nameserver string) {
 | 
			
		||||
		defer wg.Done()
 | 
			
		||||
 | 
			
		||||
		c, err := resolver.resolverFor(net, nameserver)
 | 
			
		||||
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			log.Warn("error:%s", err.Error())
 | 
			
		||||
			return
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		r, rtt, err := c.Exchange(req, nameserver)
 | 
			
		||||
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			log.Warn("%s socket error on %s", qname, nameserver)
 | 
			
		||||
			log.Warn("error:%s", err.Error())
 | 
			
		||||
			return
 | 
			
		||||
		}
 | 
			
		||||
		// If SERVFAIL happen, should return immediately and try another upstream resolver.
 | 
			
		||||
		// However, other Error code like NXDOMAIN is an clear response stating
 | 
			
		||||
		// that it has been verified no such domain existas and ask other resolvers
 | 
			
		||||
		// would make no sense. See more about #20
 | 
			
		||||
		if r != nil && r.Rcode != dns.RcodeSuccess {
 | 
			
		||||
			log.Warn("%s failed to get an valid answer on %s", qname, nameserver)
 | 
			
		||||
			if r.Rcode == dns.RcodeServerFailure {
 | 
			
		||||
				return
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
		re := &RResp{r, nameserver, rtt}
 | 
			
		||||
		select {
 | 
			
		||||
		case res <- re:
 | 
			
		||||
		default:
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	ticker := time.NewTicker(time.Duration(r.config.Interval) * time.Millisecond)
 | 
			
		||||
	defer ticker.Stop()
 | 
			
		||||
	// Start lookup on each nameserver top-down, in every second
 | 
			
		||||
	nameservers := r.Nameservers(qname)
 | 
			
		||||
	for _, nameserver := range nameservers {
 | 
			
		||||
		wg.Add(1)
 | 
			
		||||
		go L(r, nameserver)
 | 
			
		||||
		// but exit early, if we have an answer
 | 
			
		||||
		select {
 | 
			
		||||
		case re := <-res:
 | 
			
		||||
			log.Debug("%s resolv on %s rtt: %v", utils.UnFqdn(qname), re.nameserver, re.rtt)
 | 
			
		||||
			return re.msg, nil
 | 
			
		||||
		case <-ticker.C:
 | 
			
		||||
			continue
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	// wait for all the namservers to finish
 | 
			
		||||
	wg.Wait()
 | 
			
		||||
	select {
 | 
			
		||||
	case re := <-res:
 | 
			
		||||
		log.Debug("%s resolv on %s rtt: %v", utils.UnFqdn(qname), re.nameserver, re.rtt)
 | 
			
		||||
		return re.msg, nil
 | 
			
		||||
	default:
 | 
			
		||||
		return nil, ResolvError{qname, net, nameservers}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (r *Resolver) resolverFor(net, nameserver string) (*dns.Client, error) {
 | 
			
		||||
	r.clientLock.RLock()
 | 
			
		||||
	client, exists := r.clients[net]
 | 
			
		||||
	r.clientLock.RUnlock()
 | 
			
		||||
 | 
			
		||||
	if exists {
 | 
			
		||||
		return client, nil
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if net != "tcp" && net != "tcp-tls" && net != "https" && net != "udp" {
 | 
			
		||||
		return nil, errors.New("unknown network type")
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	timeout := r.Timeout()
 | 
			
		||||
 | 
			
		||||
	client = &dns.Client{
 | 
			
		||||
		Net: net,
 | 
			
		||||
		ReadTimeout: timeout,
 | 
			
		||||
		WriteTimeout: timeout,
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if strings.HasSuffix(nameserver, ":853") {
 | 
			
		||||
		client.TLSConfig = &tls.Config{
 | 
			
		||||
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	r.clientLock.Lock()
 | 
			
		||||
	r.clients[net] = client
 | 
			
		||||
	r.clientLock.Lock()
 | 
			
		||||
 | 
			
		||||
	return client, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Namservers return the array of nameservers, with port number appended.
 | 
			
		||||
// '#' in the name is treated as port separator, as with dnsmasq.
 | 
			
		||||
 | 
			
		||||
func (r *Resolver) Nameservers(qname string) []string {
 | 
			
		||||
	queryKeys := strings.Split(qname, ".")
 | 
			
		||||
	queryKeys = queryKeys[:len(queryKeys)-1] // ignore last '.'
 | 
			
		||||
 | 
			
		||||
	ns := []string{}
 | 
			
		||||
	if v, found := r.domain_server.search(queryKeys); found {
 | 
			
		||||
		log.Debug("%s found in domain server list, upstream: %v", qname, v)
 | 
			
		||||
 | 
			
		||||
		ns = append(ns, net.JoinHostPort(v, "53"))
 | 
			
		||||
		//Ensure query the specific upstream nameserver in async Lookup() function.
 | 
			
		||||
		return ns
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	for _, nameserver := range r.servers {
 | 
			
		||||
		ns = append(ns, nameserver)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return ns
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (r *Resolver) Timeout() time.Duration {
 | 
			
		||||
	return time.Duration(r.config.Timeout) * time.Second
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										65
									
								
								resolver/sfx_tree.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										65
									
								
								resolver/sfx_tree.go
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,65 @@
 | 
			
		||||
package resolver
 | 
			
		||||
 | 
			
		||||
type suffixTreeNode struct {
 | 
			
		||||
	key      string
 | 
			
		||||
	value    string
 | 
			
		||||
	children map[string]*suffixTreeNode
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func newSuffixTreeRoot() *suffixTreeNode {
 | 
			
		||||
	return newSuffixTree("", "")
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func newSuffixTree(key string, value string) *suffixTreeNode {
 | 
			
		||||
	root := &suffixTreeNode{
 | 
			
		||||
		key:      key,
 | 
			
		||||
		value:    value,
 | 
			
		||||
		children: map[string]*suffixTreeNode{},
 | 
			
		||||
	}
 | 
			
		||||
	return root
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (node *suffixTreeNode) ensureSubTree(key string) {
 | 
			
		||||
	if _, ok := node.children[key]; !ok {
 | 
			
		||||
		node.children[key] = newSuffixTree(key, "")
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (node *suffixTreeNode) insert(key string, value string) {
 | 
			
		||||
	if c, ok := node.children[key]; ok {
 | 
			
		||||
		c.value = value
 | 
			
		||||
	} else {
 | 
			
		||||
		node.children[key] = newSuffixTree(key, value)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (node *suffixTreeNode) sinsert(keys []string, value string) {
 | 
			
		||||
	if len(keys) == 0 {
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	key := keys[len(keys)-1]
 | 
			
		||||
	if len(keys) > 1 {
 | 
			
		||||
		node.ensureSubTree(key)
 | 
			
		||||
		node.children[key].sinsert(keys[:len(keys)-1], value)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	node.insert(key, value)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (node *suffixTreeNode) search(keys []string) (string, bool) {
 | 
			
		||||
	if len(keys) == 0 {
 | 
			
		||||
		return "", false
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	key := keys[len(keys)-1]
 | 
			
		||||
	if n, ok := node.children[key]; ok {
 | 
			
		||||
		if nextValue, found := n.search(keys[:len(keys)-1]); found {
 | 
			
		||||
			return nextValue, found
 | 
			
		||||
		}
 | 
			
		||||
		return n.value, (n.value != "")
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return "", false
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										53
									
								
								resolver/sfx_tree_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										53
									
								
								resolver/sfx_tree_test.go
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,53 @@
 | 
			
		||||
package resolver
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"strings"
 | 
			
		||||
	"testing"
 | 
			
		||||
 | 
			
		||||
	. "github.com/smartystreets/goconvey/convey"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func Test_Suffix_Tree(t *testing.T) {
 | 
			
		||||
	root := newSuffixTreeRoot()
 | 
			
		||||
 | 
			
		||||
	Convey("Google should not be found", t, func() {
 | 
			
		||||
		root.insert("cn", "114.114.114.114")
 | 
			
		||||
		root.sinsert([]string{"baidu", "cn"}, "166.111.8.28")
 | 
			
		||||
		root.sinsert([]string{"sina", "cn"}, "114.114.114.114")
 | 
			
		||||
 | 
			
		||||
		v, found := root.search(strings.Split("google.com", "."))
 | 
			
		||||
		So(found, ShouldEqual, false)
 | 
			
		||||
 | 
			
		||||
		v, found = root.search(strings.Split("baidu.cn", "."))
 | 
			
		||||
		So(found, ShouldEqual, true)
 | 
			
		||||
		So(v, ShouldEqual, "166.111.8.28")
 | 
			
		||||
	})
 | 
			
		||||
 | 
			
		||||
	Convey("Google should be found", t, func() {
 | 
			
		||||
		root.sinsert(strings.Split("com", "."), "")
 | 
			
		||||
		root.sinsert(strings.Split("google.com", "."), "8.8.8.8")
 | 
			
		||||
		root.sinsert(strings.Split("twitter.com", "."), "8.8.8.8")
 | 
			
		||||
		root.sinsert(strings.Split("scholar.google.com", "."), "208.67.222.222")
 | 
			
		||||
 | 
			
		||||
		v, found := root.search(strings.Split("google.com", "."))
 | 
			
		||||
		So(found, ShouldEqual, true)
 | 
			
		||||
		So(v, ShouldEqual, "8.8.8.8")
 | 
			
		||||
 | 
			
		||||
		v, found = root.search(strings.Split("www.google.com", "."))
 | 
			
		||||
		So(found, ShouldEqual, true)
 | 
			
		||||
		So(v, ShouldEqual, "8.8.8.8")
 | 
			
		||||
 | 
			
		||||
		v, found = root.search(strings.Split("scholar.google.com", "."))
 | 
			
		||||
		So(found, ShouldEqual, true)
 | 
			
		||||
		So(v, ShouldEqual, "208.67.222.222")
 | 
			
		||||
 | 
			
		||||
		v, found = root.search(strings.Split("twitter.com", "."))
 | 
			
		||||
		So(found, ShouldEqual, true)
 | 
			
		||||
		So(v, ShouldEqual, "8.8.8.8")
 | 
			
		||||
 | 
			
		||||
		v, found = root.search(strings.Split("baidu.cn", "."))
 | 
			
		||||
		So(found, ShouldEqual, true)
 | 
			
		||||
		So(v, ShouldEqual, "166.111.8.28")
 | 
			
		||||
	})
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user