-
Notifications
You must be signed in to change notification settings - Fork 12
/
writer.go
60 lines (48 loc) · 1.5 KB
/
writer.go
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
package fastdns
import (
"net"
"net/netip"
)
// A ResponseWriter interface is used by an DNS handler to construct an DNS response.
type ResponseWriter interface {
// LocalAddr returns the netip.AddrPort of the server
LocalAddr() netip.AddrPort
// RemoteAddr returns the netip.AddrPort of the client that sent the current request.
RemoteAddr() netip.AddrPort
// Write writes a raw buffer back to the client.
Write([]byte) (int, error)
}
// MemResponseWriter is an implementation of ResponseWriter that supports write response to memory.
type MemResponseWriter struct {
Data []byte
Raddr netip.AddrPort
Laddr netip.AddrPort
}
// RemoteAddr returns the netip.AddrPort of the client that sent the current request.
func (rw *MemResponseWriter) RemoteAddr() netip.AddrPort {
return rw.Raddr
}
// LocalAddr returns the netip.AddrPort of the server
func (rw *MemResponseWriter) LocalAddr() netip.AddrPort {
return rw.Laddr
}
// Write writes a raw buffer back to the memory buffer.
func (rw *MemResponseWriter) Write(p []byte) (n int, err error) {
rw.Data = append(rw.Data, p...)
n = len(p)
return
}
type udpResponseWriter struct {
Conn *net.UDPConn
AddrPort netip.AddrPort
}
func (rw *udpResponseWriter) RemoteAddr() netip.AddrPort {
return rw.AddrPort
}
func (rw *udpResponseWriter) LocalAddr() netip.AddrPort {
return rw.Conn.LocalAddr().(*net.UDPAddr).AddrPort()
}
func (rw *udpResponseWriter) Write(p []byte) (n int, err error) {
n, _, err = rw.Conn.WriteMsgUDPAddrPort(p, nil, rw.AddrPort)
return
}