golang DNS服务器的简单实现操作

时间:2022-06-01 16:40:14

简单的DNS服务器

提供一个简单的可以查询域名和反向查询的DNS服务器。

dig命令主要用来从 DNS 域名服务器查询主机地址信息。

查找www.baidu.com的ip (A记录):

命令:dig @127.0.0.1 www.baidu.com

golang DNS服务器的简单实现操作

根据ip查找对应域名 (PTR记录):

命令:dig @127.0.0.1 -x 220.181.38.150

golang DNS服务器的简单实现操作

源码 :

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
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实现如下:

?
1
2
3
4
5
6
7
8
9
10
// 默认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如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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()如下:

?
1
2
3
4
5
6
7
8
ndialer, _ := NewDialer(dialer, nameserver)
client := &http.Client{
  Transport: &http.Transport{
    DialContext:         ndialer.DialContext,
    TLSHandshakeTimeout: 10 * time.Second,
  },
  Timeout: timeout,
}

总结

通过以上实现可解决自定义Nameserver,也可以在Dailer中添加缓存,实现DNS缓存。

以上为个人经验,希望能给大家一个参考,也希望大家多多支持服务器之家。如有错误或未考虑完全的地方,望不吝赐教。

原文链接:https://blog.csdn.net/qq_27068845/article/details/104597845