-
Notifications
You must be signed in to change notification settings - Fork 10
/
client.go
147 lines (125 loc) · 3.37 KB
/
client.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
package client
import (
"context"
"fmt"
"log"
"net"
"net/netip"
"sync"
"time"
"github.com/SenseUnit/dtlspipe/util"
"github.com/pion/dtls/v2"
"github.com/pion/transport/v2/udp"
)
const (
MaxPktBuf = 65536
Backlog = 1024
)
type Client struct {
listener net.Listener
dtlsConfig *dtls.Config
remoteDialFn func(context.Context, string) (net.Conn, error)
psk func([]byte) ([]byte, error)
timeout time.Duration
idleTimeout time.Duration
baseCtx context.Context
cancelCtx func()
staleMode util.StaleMode
workerWG sync.WaitGroup
timeLimitFunc func() time.Duration
allowFunc func(net.Addr, net.Addr) bool
}
func New(cfg *Config) (*Client, error) {
cfg = cfg.populateDefaults()
baseCtx, cancelCtx := context.WithCancel(cfg.BaseContext)
client := &Client{
remoteDialFn: cfg.RemoteDialFunc,
timeout: cfg.Timeout,
psk: cfg.PSKCallback,
idleTimeout: cfg.IdleTimeout,
baseCtx: baseCtx,
cancelCtx: cancelCtx,
staleMode: cfg.StaleMode,
timeLimitFunc: cfg.TimeLimitFunc,
allowFunc: cfg.AllowFunc,
}
lAddrPort, err := netip.ParseAddrPort(cfg.BindAddress)
if err != nil {
cancelCtx()
return nil, fmt.Errorf("can't parse bind address: %w", err)
}
client.dtlsConfig = &dtls.Config{
ExtendedMasterSecret: dtls.RequireExtendedMasterSecret,
ConnectContextMaker: client.contextMaker,
PSK: client.psk,
PSKIdentityHint: []byte(cfg.PSKIdentity),
MTU: cfg.MTU,
CipherSuites: cfg.CipherSuites,
EllipticCurves: cfg.EllipticCurves,
}
lc := udp.ListenConfig{
Backlog: Backlog,
}
listener, err := lc.Listen("udp", net.UDPAddrFromAddrPort(lAddrPort))
if err != nil {
cancelCtx()
return nil, fmt.Errorf("client listen failed: %w", err)
}
client.listener = listener
go client.listen()
return client, nil
}
func (client *Client) listen() {
defer client.Close()
for client.baseCtx.Err() == nil {
conn, err := client.listener.Accept()
if err != nil {
log.Printf("conn accept failed: %v", err)
continue
}
if !client.allowFunc(conn.LocalAddr(), conn.RemoteAddr()) {
continue
}
client.workerWG.Add(1)
go func(conn net.Conn) {
defer client.workerWG.Done()
defer conn.Close()
client.serve(conn)
}(conn)
}
}
func (client *Client) serve(conn net.Conn) {
log.Printf("[+] conn %s <=> %s", conn.LocalAddr(), conn.RemoteAddr())
defer log.Printf("[-] conn %s <=> %s", conn.LocalAddr(), conn.RemoteAddr())
defer conn.Close()
ctx := client.baseCtx
tl := client.timeLimitFunc()
if tl != 0 {
newCtx, cancel := context.WithTimeout(ctx, tl)
defer cancel()
ctx = newCtx
}
dialCtx, cancel := context.WithTimeout(ctx, client.timeout)
defer cancel()
remoteConn, err := client.remoteDialFn(dialCtx, "udp")
if err != nil {
log.Printf("remote dial failed: %v", err)
return
}
defer remoteConn.Close()
remoteConn, err = dtls.ClientWithContext(dialCtx, remoteConn, client.dtlsConfig)
if err != nil {
log.Printf("DTLS handshake with remote server failed: %v", err)
return
}
util.PairConn(ctx, conn, remoteConn, client.idleTimeout, client.staleMode)
}
func (client *Client) contextMaker() (context.Context, func()) {
return context.WithTimeout(client.baseCtx, client.timeout)
}
func (client *Client) Close() error {
client.cancelCtx()
err := client.listener.Close()
client.workerWG.Wait()
return err
}