golang DNS服務器的簡單實現操作

簡單的DNS服務器

提供一個簡單的可以查詢域名和反向查詢的DNS服務器。

dig命令主要用來從 DNS 域名服務器查詢主機地址信息。

查找www.baidu.com的ip (A記錄):

命令:dig @127.0.0.1 www.baidu.com

在這裡插入圖片描述

根據ip查找對應域名 (PTR記錄):

命令:dig @127.0.0.1 -x 220.181.38.150

在這裡插入圖片描述

源碼 :

package main
import (
	"fmt"
	"net"
	"golang.org/x/net/dns/dnsmessage"
)
func main() {
	conn, err := net.ListenUDP("udp", &net.UDPAddr{Port: 53})
	if err != nil {
		panic(err)
	}
	defer conn.Close()
	fmt.Println("Listing ...")
	for {
		buf := make([]byte, 512)
		_, addr, _ := conn.ReadFromUDP(buf)
		var msg dnsmessage.Message
		if err := msg.Unpack(buf); err != nil {
			fmt.Println(err)
			continue
		}
		go ServerDNS(addr, conn, msg)
	}
}
// address books
var (
	addressBookOfA = map[string][4]byte{
		"www.baidu.com.": [4]byte{220, 181, 38, 150},
	}
	addressBookOfPTR = map[string]string{
		"150.38.181.220.in-addr.arpa.": "www.baidu.com.",
	}
)
// ServerDNS serve
func ServerDNS(addr *net.UDPAddr, conn *net.UDPConn, msg dnsmessage.Message) {
	// query info
	if len(msg.Questions) < 1 {
		return
	}
	question := msg.Questions[0]
	var (
		queryTypeStr = question.Type.String()
		queryNameStr = question.Name.String()
		queryType    = question.Type
		queryName, _ = dnsmessage.NewName(queryNameStr)
	)
	fmt.Printf("[%s] queryName: [%s]\n", queryTypeStr, queryNameStr)
	// find record
	var resource dnsmessage.Resource
	switch queryType {
	case dnsmessage.TypeA:
		if rst, ok := addressBookOfA[queryNameStr]; ok {
			resource = NewAResource(queryName, rst)
		} else {
			fmt.Printf("not fount A record queryName: [%s] \n", queryNameStr)
			Response(addr, conn, msg)
			return
		}
	case dnsmessage.TypePTR:
		if rst, ok := addressBookOfPTR[queryName.String()]; ok {
			resource = NewPTRResource(queryName, rst)
		} else {
			fmt.Printf("not fount PTR record queryName: [%s] \n", queryNameStr)
			Response(addr, conn, msg)
			return
		}
	default:
		fmt.Printf("not support dns queryType: [%s] \n", queryTypeStr)
		return
	}
	// send response
	msg.Response = true
	msg.Answers = append(msg.Answers, resource)
	Response(addr, conn, msg)
}
// Response return
func Response(addr *net.UDPAddr, conn *net.UDPConn, msg dnsmessage.Message) {
	packed, err := msg.Pack()
	if err != nil {
		fmt.Println(err)
		return
	}
	if _, err := conn.WriteToUDP(packed, addr); err != nil {
		fmt.Println(err)
	}
}
// NewAResource A record
func NewAResource(query dnsmessage.Name, a [4]byte) dnsmessage.Resource {
	return dnsmessage.Resource{
		Header: dnsmessage.ResourceHeader{
			Name:  query,
			Class: dnsmessage.ClassINET,
			TTL:   600,
		},
		Body: &dnsmessage.AResource{
			A: a,
		},
	}
}
// NewPTRResource PTR record
func NewPTRResource(query dnsmessage.Name, ptr string) dnsmessage.Resource {
	name, _ := dnsmessage.NewName(ptr)
	return dnsmessage.Resource{
		Header: dnsmessage.ResourceHeader{
			Name:  query,
			Class: dnsmessage.ClassINET,
		},
		Body: &dnsmessage.PTRResource{
			PTR: name,
		},
	}
}

補充:Golang自定義DNS Nameserver

某些情況下我們希望程序通過自定義Nameserver去查詢域名,而不希望通過操作系統給定的Nameserver,本文介紹如何在Golang中實現自定義Nameserver。

DNS解析過程

Golang中一般通過net.Resolver的LookupHost(ctx context.Context, host string) (addrs []string, err error)去實現域名解析,

解析過程如下:

檢查本地hosts文件是否存在解析記錄,存在即返回解析地址

不存在即根據resolv.conf中讀取的nameserver發起遞歸查詢

nameserver不斷的向上級nameserver發起迭代查詢

nameserver最終返回查詢結果給請求者

用戶可以通過修改/etc/resolv.conf來添加特定的nameserver,但某些場景下我們不希望更改系統配置。比如在kubernetes中,作為sidecar服務需要通過service去訪問其他集群內服務,必須更改dnsPolicy為ClusterFirst,但這可能會影響其他容器的DNS查詢效率。

自定義Nameserver

在Golang中自定義Nameserver,需要我們自己實現一個Resolver,如果是httpClient需要自定義DialContext()

Resolver實現如下:

// 默認dialer
dialer := &net.Dialer{
  Timeout: 1 * time.Second,
}
// 定義resolver
resolver := &net.Resolver{
 Dial: func(ctx context.Context, network, address string) (net.Conn, error) {
  return dialer.DialContext(ctx, "tcp", nameserver) // 通過tcp請求nameserver解析域名
 },
}

自定義Dialer如下:

type Dialer struct {
 dialer     *net.Dialer
 resolver   *net.Resolver
 nameserver string
}
// NewDialer create a Dialer with user's nameserver.
func NewDialer(dialer *net.Dialer, nameserver string) (*Dialer, error) {
 conn, err := dialer.Dial("tcp", nameserver)
 if err != nil {
  return nil, err
 }
 defer conn.Close()
 return &Dialer{
  dialer: dialer,
  resolver: &net.Resolver{
   Dial: func(ctx context.Context, network, address string) (net.Conn, error) {
    return dialer.DialContext(ctx, "tcp", nameserver)
   },
  },
  nameserver: nameserver, // 用戶設置的nameserver
 }, nil
}
// DialContext connects to the address on the named network using
// the provided context.
func (d *Dialer) DialContext(ctx context.Context, network, address string) (net.Conn, error) {
 host, port, err := net.SplitHostPort(address)
 if err != nil {
  return nil, err
 }
 ips, err := d.resolver.LookupHost(ctx, host) // 通過自定義nameserver查詢域名
 for _, ip := range ips {
    // 創建鏈接
  conn, err := d.dialer.DialContext(ctx, network, ip+":"+port)
  if err == nil {
   return conn, nil
  }
 }
 return d.dialer.DialContext(ctx, network, address)
}

httpClient中自定義DialContext()如下:

ndialer, _ := NewDialer(dialer, nameserver)
client := &http.Client{
  Transport: &http.Transport{
    DialContext:         ndialer.DialContext,
    TLSHandshakeTimeout: 10 * time.Second,
  },
  Timeout: timeout,
}

總結

通過以上實現可解決自定義Nameserver,也可以在Dailer中添加緩存,實現DNS緩存。

推薦閱讀: