-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
378 lines (341 loc) · 8.31 KB
/
main.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
package main
import (
"flag"
"fmt"
"io"
"log"
"net"
"os"
"os/signal"
"sort"
"strconv"
"strings"
"syscall"
"time"
"github.com/miekg/dns"
)
var (
flags = flag.NewFlagSet("goproxy", flag.ExitOnError)
udp bool
srv bool
dnsServer string
dnsInterval time.Duration
timeout time.Duration
verbose bool
debug bool
)
func main() {
parseFlags()
if len(flags.Args()) < 2 {
if debug {
log.Printf("Remaining arguments after parsing flags: %+v\n", flags.Args())
}
usage()
os.Exit(1)
}
if dnsServer != "" && !strings.Contains(dnsServer, ":") && !strings.Contains(dnsServer, "/") {
dnsServer = net.JoinHostPort(dnsServer, "53")
}
// ignore HUP and PIPE signals
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGHUP, syscall.SIGPIPE)
go func() {
for range c {
}
}()
// channels to pass DNS updates and new incoming connections
resolver := make(chan []string, 1)
manager := make(chan net.Conn, 10)
connectTo := flags.Args()[1:]
if verbose {
log.Printf("Will connect to %v\n", connectTo)
}
if dnsServer != "" {
if verbose {
log.Printf("DNS server provided: `%s`, will refresh every %v\n", dnsServer, dnsInterval)
}
go refreshDns(connectTo, resolver)
} else {
resolver <- connectTo
}
listenOn := flags.Arg(0)
if verbose {
proto := "tcp"
if udp {
proto = "udp"
}
log.Printf("Will listen on `%s://%s`\n", proto, listenOn)
}
if udp {
laddr, err := net.ResolveUDPAddr("udp", listenOn)
if err != nil {
log.Fatalf("Error resolving `%s`: %v\n", listenOn, err)
}
conn, err := net.ListenUDP("udp", laddr)
if err != nil {
log.Fatalf("Failed to setup UDP listener on `%s`: %v\n", listenOn, err)
}
manager <- conn
manageUdp(resolver, manager)
} else {
listener, err := net.Listen("tcp", listenOn)
if err != nil {
log.Fatalf("Failed to setup TCP listener on `%s`: %v\n", listenOn, err)
}
go manageTcp(resolver, manager)
for {
conn, err := listener.Accept()
if err != nil {
log.Printf("Failed to accept connection: %v\n", err)
} else {
manager <- conn
}
}
}
}
func usage() {
fmt.Fprintf(os.Stderr,
`Usage: %s [flags] [listen-ip]:port [connect-to-ip]:port
Flags:
`, os.Args[0])
flags.PrintDefaults()
}
func parseFlags() {
flags.BoolVar(&udp, "udp", false, "UDP mode")
flags.BoolVar(&srv, "srv", false, "Query DNS for SRV records, -dns must be specified")
flags.StringVar(&dnsServer, "dns", "", "DNS server address, supply host[:port]; will use system default if not set")
flags.DurationVar(&dnsInterval, "dns-interval", 20*time.Second, "Time interval between DNS queries")
flags.DurationVar(&timeout, "timeout", 10*time.Second, "TCP connect timeout")
flags.BoolVar(&verbose, "verbose", false, "Print noticeable info")
flags.BoolVar(&debug, "debug", false, "Print debug level info")
flags.Usage = usage
flags.Parse(os.Args[1:])
if debug {
verbose = true
}
}
type HostPort struct {
host, port string
resolve bool
}
func queryDns(dnsClient *dns.Client, name string, qType uint16) []HostPort {
if qType != dns.TypeA && qType != dns.TypeSRV {
log.Fatalf("Unsupported DNS query type `%s` resolving `%s`", dns.TypeToString[qType], name)
}
req := &dns.Msg{}
req.SetQuestion(name, qType)
if debug {
log.Printf("Querying DNS for `%s` type %s\n", name, dns.TypeToString[qType])
}
resp, _, err := dnsClient.Exchange(req, dnsServer)
if err != nil {
log.Printf("Error resolving `%s`: %v\n", name, err)
return nil
}
if req.Id != resp.Id {
log.Printf("DNS ID mismatch, request: %d, response: %d\n", req.Id, resp.Id)
return nil
}
var resolved []HostPort
for _, r := range resp.Answer {
if qType == dns.TypeA {
if a, ok := r.(*dns.A); ok {
ip := a.A.String()
if debug {
log.Printf("Resolved `%s` to `%s`\n", name, ip)
}
resolved = append(resolved, HostPort{host: ip})
}
} else {
if srv, ok := r.(*dns.SRV); ok {
target := srv.Target
port := strconv.Itoa(int(srv.Port))
if debug {
log.Printf("Resolved `%s` to `%s`\n", name, net.JoinHostPort(target, port))
}
resolved = append(resolved, HostPort{host: target, port: port})
}
}
}
if verbose && len(resolved) == 0 {
log.Printf("DNS response has no %s records for `%s`: %+v\n", dns.TypeToString[qType], name, resp)
}
return resolved
}
func refreshDns(connectTo []string, dnsUpdates chan []string) {
var targets []HostPort
noDnsRequired := true
for _, target := range connectTo {
var host, port string
if srv {
host = target
} else {
var err error
host, port, err = net.SplitHostPort(target)
if err != nil {
log.Fatalf("Error parsing `%s`: %v\n", target, err)
}
}
resolve := host != "" && net.ParseIP(host) == nil
if noDnsRequired && resolve {
noDnsRequired = false
}
if host != "" {
host = dns.Fqdn(host)
}
targets = append(targets, HostPort{host, port, resolve})
}
if noDnsRequired {
if verbose && dnsServer != "" {
log.Printf("Only port/IP provided in `%v`, DNS server address is unused\n", connectTo)
}
dnsUpdates <- connectTo
return
}
// https://pkg.go.dev/github.com/miekg/dns#Client
// https://github.com/benschw/dns-clb-go/blob/master/dns/lib.go
dnsClient := &dns.Client{Net: "tcp"}
var resolvedTargets []string
queryDns := func() {
var newTargets []string
for _, target := range targets {
if !target.resolve {
newTargets = append(newTargets, net.JoinHostPort(target.host, target.port))
continue
}
if srv {
srvTargets := queryDns(dnsClient, target.host, dns.TypeSRV)
for _, srvTarget := range srvTargets {
ips := queryDns(dnsClient, srvTarget.host, dns.TypeA)
for _, ip := range ips {
newTargets = append(newTargets, net.JoinHostPort(ip.host, srvTarget.port))
}
}
} else {
ips := queryDns(dnsClient, target.host, dns.TypeA)
for _, ip := range ips {
newTargets = append(newTargets, net.JoinHostPort(ip.host, target.port))
}
}
}
sort.Strings(newTargets)
update := false
if len(resolvedTargets) != len(newTargets) {
update = true
}
if !update {
for i, newTarget := range newTargets {
if resolvedTargets[i] != newTarget {
update = true
break
}
}
}
if update {
dnsUpdates <- newTargets
if verbose {
log.Printf("Connect target changed: %v\n", newTargets)
}
resolvedTargets = newTargets
}
}
queryDns()
ticker := time.NewTicker(dnsInterval)
defer ticker.Stop()
for {
select {
case <-ticker.C:
queryDns()
}
}
}
func manageTcp(resolver chan []string, connections chan net.Conn) {
var connectTo []string
var i uint
for {
select {
case connectTo = <-resolver:
case in := <-connections:
if len(connectTo) > 0 {
go forwardTcp(in, connectTo[i%uint(len(connectTo))])
i++
} else {
if debug {
log.Print("Don't know where to connect, closing incoming connection\n")
}
in.Close()
}
}
}
}
func forwardTcp(conn net.Conn, connectTo string) {
if debug {
log.Print("Accepted connection\n")
}
fwd, err := net.DialTimeout("tcp", connectTo, timeout)
if err != nil {
log.Printf("Conection to `%s` failed: %v\n", connectTo, err)
conn.Close()
return
}
close := func() {
fwd.Close()
conn.Close()
}
go func() {
defer close()
w, err := io.Copy(fwd, conn)
if debug {
log.Printf("Incoming TCP connection closed: %v; %v bytes forwarded\n", err, w)
}
}()
go func() {
defer close()
w, err := io.Copy(conn, fwd)
if debug {
log.Printf("Outgoing TCP connection closed: %v; %v bytes forwarded\n", err, w)
}
}()
}
func manageUdp(resolver chan []string, connections chan net.Conn) {
var in, out net.Conn
var i uint
for {
select {
case connectTo := <-resolver:
if out != nil {
out.Close()
out = nil
}
if len(connectTo) > 0 {
_out, err := net.Dial("udp", connectTo[i%uint(len(connectTo))])
i++
if err != nil {
log.Printf("Conection to `%s` failed: %v\n", connectTo, err)
} else {
out = _out
if in != nil {
go forwardUdp(in, out)
}
}
}
case _in := <-connections:
in = _in
if out != nil {
go forwardUdp(in, out)
}
}
}
}
func forwardUdp(from net.Conn, to net.Conn) {
for {
w, err := io.Copy(to, from)
if debug {
log.Printf("UDP forwarding interrupted: %v; %v bytes forwarded\n", err, w)
}
if strings.Contains(err.Error(), "closed network connection") {
break
}
time.Sleep(1 * time.Second)
}
}