-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassociate_single.go
More file actions
168 lines (142 loc) · 4.14 KB
/
Copy pathassociate_single.go
File metadata and controls
168 lines (142 loc) · 4.14 KB
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
package socks5
import (
"github.com/meteorite/scope"
"net"
"sync"
)
type ipAssoc struct {
*oneClientUDPRemotes
tcpCount int32 // guarded by SingleUDPPortAssociate::mtx
}
type SingleUDPPortAssociate struct {
udpAddr *AddrSpec
connFactory RemoteUDPConnFactory
log ErrorLogger
mtx sync.Mutex
ipAssocs map[string]*ipAssoc
ctxServer ContextGo
}
// Creates new SingleUDPPortAssociate.
// Either ListenAndServeUDPPort or ServeUDPPort MUST be called after creation to start serving UDP port. It is
// convenient to call ListenAndServeUDPPort from Handler.OnStartServe.
//
// udpAddr MUST contain IP to listen UDP port on. It also MAY contain FQDN, if it should be sent to clients in
// associate responses. udpAddr MUST have valid Port, if is started with ServeUDPPort, otherwise it MAY have Port == 0
// (then Port will be chosen automatically in ListenAndServeUDPPort)
func MakeSingleUDPPortAssociate(udpAddr *AddrSpec, connFactory RemoteUDPConnFactory, log ErrorLogger) *SingleUDPPortAssociate {
return &SingleUDPPortAssociate{
udpAddr: udpAddr,
connFactory: connFactory,
log: log,
ipAssocs: make(map[string]*ipAssoc),
}
}
// ListenAndServeUDPPort is used to create incoming UDP port and serve on it
func (s *SingleUDPPortAssociate) ListenAndServeUDPPort(ctxServer ContextGo, udpNet string) error {
addr := net.UDPAddr{
Port: s.udpAddr.Port,
IP: s.udpAddr.IP,
}
c, err := net.ListenUDP(udpNet, &addr)
if err != nil {
return err
}
if s.udpAddr.Port == 0 {
s.udpAddr.Port = c.LocalAddr().(*net.UDPAddr).Port
}
ctxServer.Go(func() error { return s.ServeUDPPort(ctxServer, c) })
return nil
}
func (s *SingleUDPPortAssociate) ServeUDPPort(ctxServer ContextGo, udpConn *net.UDPConn) error {
defer scope.Closer(ctxServer.Ctx(), udpConn).Close()
s.ctxServer = ctxServer
sendBack := MakeSendBackTo(udpConn)
buffer := make([]byte, s.connFactory.MaxUDPPacketSize())
for {
n, src, err := udpConn.ReadFromUDP(buffer)
if err != nil {
return err
}
// s.log.Printf("client %s:%d udp packet: %d bytes", src.IP.String(), src.Port, n)
fromIP := src.IP.String()
assoc := s.getAssoc(fromIP)
if assoc == nil {
s.log.Printf("udp packet from unauthorized IP: %s", fromIP)
continue
}
if err := assoc.forwardClientPktToRemote(ctxServer, src, buffer[:n], s.connFactory, sendBack); err != nil {
s.log.Printf("%v", err)
}
}
}
func (s *SingleUDPPortAssociate) addAssoc(fromIP string) ContextGo {
s.mtx.Lock()
defer s.mtx.Unlock()
assoc, found := s.ipAssocs[fromIP]
if found {
assoc.tcpCount++
return assoc.ctxClient
} else {
g, _ := scope.Group(s.ctxServer.Ctx())
s.ipAssocs[fromIP] = &ipAssoc{oneClientUDPRemotes: makeOneClientUDPRemotes(g), tcpCount: 1}
return g
}
}
func (s *SingleUDPPortAssociate) delAssoc(fromIP string) {
s.mtx.Lock()
locked := true
defer func() {
if locked {
s.mtx.Unlock()
}
}()
assoc, found := s.ipAssocs[fromIP]
if !found {
return
}
assoc.tcpCount--
if assoc.tcpCount <= 0 {
delete(s.ipAssocs, fromIP)
s.mtx.Unlock()
locked = false
assoc.stopAll()
assoc.ctxClient.Cancel()
assoc.ctxClient.WaitQuietly()
}
}
func (s *SingleUDPPortAssociate) getAssoc(from string) *ipAssoc {
s.mtx.Lock()
defer s.mtx.Unlock()
return s.ipAssocs[from]
}
func (s *SingleUDPPortAssociate) OnAssociate(conn net.Conn) error {
from, _, err := net.SplitHostPort(conn.RemoteAddr().String())
if err != nil {
return onAssociateSendError(conn, err)
}
ctxClient := s.addAssoc(from)
defer s.delAssoc(from)
defer scope.Closer(ctxClient.Ctx(), conn).Close()
return onAssociateReplyUdpAddrAndWaitForClose(conn, s.udpAddr, s.log)
}
//func (s *SingleUDPPortAssociate) onUDPPkt(ctx context.Context, client *net.UDPAddr, pkt []byte, sendBack UDPSendBackTo) {
// destAddr, data, err := ParseUdpPacket(pkt)
// if err != nil {
// return
// }
//
// assoc := s.getAssoc(client.IP.String())
// if assoc == nil {
// return
// }
//
// w, err := assoc.getOrAddRemote(ctx, client, s.connFactory, sendBack)
// if err != nil {
// return
// }
// w.refreshLastTS()
// if err = w.conn.Send(ctx, data, destAddr); err != nil {
// assoc.delRemote(uint16(client.Port), w)
// _ = w.conn.Close()
// }
//}