-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathconn.go
180 lines (149 loc) · 2.94 KB
/
conn.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
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// Copyright © by Jeff Foley 2022-2024. All rights reserved.
// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
// SPDX-License-Identifier: Apache-2.0
package resolve
import (
"errors"
"fmt"
"net"
"sync"
"time"
"github.com/caffix/queue"
"github.com/miekg/dns"
)
const headerSize = 12
type resp struct {
Msg *dns.Msg
Addr net.Addr
}
type connection struct {
conn net.PacketConn
done chan struct{}
}
type connections struct {
sync.Mutex
done chan struct{}
conns []*connection
resps queue.Queue
nextWrite int
cpus int
}
func newConnections(cpus int, resps queue.Queue) *connections {
if cpus < 100 {
cpus = 100
}
conns := &connections{
resps: resps,
done: make(chan struct{}),
cpus: cpus,
}
conns.Lock()
defer conns.Unlock()
for i := 0; i < cpus; i++ {
if err := conns.Add(); err != nil {
conns.Close()
return nil
}
}
go conns.rotations()
return conns
}
func (r *connections) Close() {
r.Lock()
defer r.Unlock()
if r.conns != nil {
close(r.done)
for _, c := range r.conns {
close(c.done)
}
r.conns = nil
}
}
func (r *connections) rotations() {
t := time.NewTicker(10 * time.Second)
defer t.Stop()
for {
select {
case <-r.done:
return
case <-t.C:
r.rotate()
}
}
}
func (r *connections) rotate() {
r.Lock()
defer r.Unlock()
for _, c := range r.conns {
go func(c *connection) {
t := time.NewTimer(10 * time.Second)
defer t.Stop()
<-t.C
close(c.done)
}(c)
}
r.conns = []*connection{}
for i := 0; i < r.cpus; i++ {
_ = r.Add()
}
}
func (r *connections) Next() net.PacketConn {
r.Lock()
defer r.Unlock()
if r.conns == nil || len(r.conns) == 0 {
return nil
}
cur := r.nextWrite
r.nextWrite = (r.nextWrite + 1) % len(r.conns)
return r.conns[cur].conn
}
func (r *connections) Add() error {
conn, err := r.ListenPacket()
if err != nil {
return err
}
_ = conn.SetDeadline(time.Time{})
c := &connection{
conn: conn,
done: make(chan struct{}),
}
r.conns = append(r.conns, c)
go r.responses(c)
return nil
}
func (r *connections) WriteMsg(msg *dns.Msg, addr net.Addr) error {
var n int
var err error
var out []byte
if out, err = msg.Pack(); err == nil {
err = errors.New("failed to obtain a connection")
if conn := r.Next(); conn != nil {
_ = conn.SetWriteDeadline(time.Now().Add(500 * time.Millisecond))
n, err = conn.WriteTo(out, addr)
if err == nil && n < len(out) {
err = fmt.Errorf("only wrote %d bytes of the %d byte message", n, len(out))
}
}
}
return err
}
func (r *connections) responses(c *connection) {
b := make([]byte, dns.DefaultMsgSize)
for {
select {
case <-c.done:
_ = c.conn.Close()
return
default:
}
if n, addr, err := c.conn.ReadFrom(b); err == nil && n >= headerSize {
m := new(dns.Msg)
if err := m.Unpack(b[:n]); err == nil && len(m.Question) > 0 {
r.resps.Append(&resp{
Msg: m,
Addr: addr,
})
}
}
}
}