semihalev / sdns
1
package mock
2

3
import (
4
	"net"
5

6
	"github.com/miekg/dns"
7
)
8

9
// Writer type
10
type Writer struct {
11
	msg *dns.Msg
12

13
	proto string
14

15
	localAddr  net.Addr
16
	remoteAddr net.Addr
17

18
	remoteip net.IP
19
}
20

21
// NewWriter return writer
22
func NewWriter(proto, addr string) *Writer {
23 1
	w := &Writer{}
24

25 1
	switch proto {
26 1
	case "tcp", "tcp-tls":
27 1
		w.localAddr = &net.TCPAddr{IP: net.IPv4(127, 0, 0, 1), Port: 53}
28 1
		w.remoteAddr, _ = net.ResolveTCPAddr("tcp", addr)
29 1
		w.remoteip = w.remoteAddr.(*net.TCPAddr).IP
30 1
		w.proto = "tcp"
31

32 1
	case "udp":
33 1
		w.localAddr = &net.UDPAddr{IP: net.IPv4(127, 0, 0, 1), Port: 53}
34 1
		w.remoteAddr, _ = net.ResolveUDPAddr("udp", addr)
35 1
		w.remoteip = w.remoteAddr.(*net.UDPAddr).IP
36 1
		w.proto = "udp"
37
	}
38

39 1
	return w
40
}
41

42
// Rcode return message response code
43
func (w *Writer) Rcode() int {
44 1
	if w.msg == nil {
45 1
		return dns.RcodeServerFailure
46
	}
47

48 1
	return w.msg.Rcode
49
}
50

51
// Msg return current dns message
52
func (w *Writer) Msg() *dns.Msg {
53 1
	return w.msg
54
}
55

56
// Write func
57
func (w *Writer) Write(b []byte) (int, error) {
58 1
	w.msg = new(dns.Msg)
59 1
	err := w.msg.Unpack(b)
60 1
	if err != nil {
61 1
		return 0, err
62
	}
63 1
	return len(b), nil
64
}
65

66
// WriteMsg func
67
func (w *Writer) WriteMsg(msg *dns.Msg) error {
68 1
	w.msg = msg
69 1
	return nil
70
}
71

72
// Written func
73
func (w *Writer) Written() bool {
74 1
	return w.msg != nil
75
}
76

77
// RemoteIP func
78 1
func (w *Writer) RemoteIP() net.IP { return w.remoteip }
79

80
// Proto func
81 1
func (w *Writer) Proto() string { return w.proto }
82

83
// Reset func
84 0
func (w *Writer) Reset(rw dns.ResponseWriter) {}
85

86
// Close func
87 1
func (w *Writer) Close() error { return nil }
88

89
// Hijack func
90 0
func (w *Writer) Hijack() {}
91

92
// LocalAddr func
93 1
func (w *Writer) LocalAddr() net.Addr { return w.localAddr }
94

95
// RemoteAddr func
96 1
func (w *Writer) RemoteAddr() net.Addr { return w.remoteAddr }
97

98
// TsigStatus func
99 1
func (w *Writer) TsigStatus() error { return nil }
100

101
// TsigTimersOnly func
102 0
func (w *Writer) TsigTimersOnly(ok bool) {}
103

104
// Internal func
105 1
func (w *Writer) Internal() bool { return true }

Read our documentation on viewing source code .

Loading