forked from v2ray/v2ray-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhub.go
178 lines (156 loc) · 3.61 KB
/
hub.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
package udp
import (
"net"
"sync"
"v2ray.com/core/common/buf"
"v2ray.com/core/common/dice"
"v2ray.com/core/common/log"
v2net "v2ray.com/core/common/net"
"v2ray.com/core/common/signal"
"v2ray.com/core/proxy"
"v2ray.com/core/transport/internet/internal"
)
type UDPPayload struct {
payload *buf.Buffer
session *proxy.SessionInfo
}
type UDPPayloadHandler func(*buf.Buffer, *proxy.SessionInfo)
type UDPPayloadQueue struct {
queue []chan UDPPayload
callback UDPPayloadHandler
}
func NewUDPPayloadQueue(option ListenOption) *UDPPayloadQueue {
queue := &UDPPayloadQueue{
callback: option.Callback,
queue: make([]chan UDPPayload, option.Concurrency),
}
for i := range queue.queue {
queue.queue[i] = make(chan UDPPayload, 64)
go queue.Dequeue(queue.queue[i])
}
return queue
}
func (v *UDPPayloadQueue) Enqueue(payload UDPPayload) {
size := len(v.queue)
idx := 0
if size > 1 {
idx = dice.Roll(size)
}
for i := 0; i < size; i++ {
select {
case v.queue[idx%size] <- payload:
return
default:
idx++
}
}
}
func (v *UDPPayloadQueue) Dequeue(queue <-chan UDPPayload) {
for payload := range queue {
v.callback(payload.payload, payload.session)
}
}
func (v *UDPPayloadQueue) Close() {
for _, queue := range v.queue {
close(queue)
}
}
type ListenOption struct {
Callback UDPPayloadHandler
ReceiveOriginalDest bool
Concurrency int
}
type UDPHub struct {
sync.RWMutex
conn *net.UDPConn
cancel *signal.CancelSignal
queue *UDPPayloadQueue
option ListenOption
}
func ListenUDP(address v2net.Address, port v2net.Port, option ListenOption) (*UDPHub, error) {
if option.Concurrency < 1 {
option.Concurrency = 1
}
udpConn, err := net.ListenUDP("udp", &net.UDPAddr{
IP: address.IP(),
Port: int(port),
})
if err != nil {
return nil, err
}
if option.ReceiveOriginalDest {
fd, err := internal.GetSysFd(udpConn)
if err != nil {
log.Warning("UDP|Listener: Failed to get fd: ", err)
return nil, err
}
err = SetOriginalDestOptions(fd)
if err != nil {
log.Warning("UDP|Listener: Failed to set socket options: ", err)
return nil, err
}
}
hub := &UDPHub{
conn: udpConn,
queue: NewUDPPayloadQueue(option),
option: option,
cancel: signal.NewCloseSignal(),
}
go hub.start()
return hub, nil
}
func (v *UDPHub) Close() {
v.Lock()
defer v.Unlock()
v.cancel.Cancel()
v.conn.Close()
v.cancel.WaitForDone()
v.queue.Close()
}
func (v *UDPHub) WriteTo(payload []byte, dest v2net.Destination) (int, error) {
return v.conn.WriteToUDP(payload, &net.UDPAddr{
IP: dest.Address.IP(),
Port: int(dest.Port),
})
}
func (v *UDPHub) start() {
v.cancel.WaitThread()
defer v.cancel.FinishThread()
oobBytes := make([]byte, 256)
for v.Running() {
buffer := buf.NewSmall()
var noob int
var addr *net.UDPAddr
err := buffer.AppendSupplier(func(b []byte) (int, error) {
n, nb, _, a, e := ReadUDPMsg(v.conn, b, oobBytes)
noob = nb
addr = a
return n, e
})
if err != nil {
log.Info("UDP|Hub: Failed to read UDP msg: ", err)
buffer.Release()
continue
}
session := new(proxy.SessionInfo)
session.Source = v2net.UDPDestination(v2net.IPAddress(addr.IP), v2net.Port(addr.Port))
if v.option.ReceiveOriginalDest && noob > 0 {
session.Destination = RetrieveOriginalDest(oobBytes[:noob])
}
v.queue.Enqueue(UDPPayload{
payload: buffer,
session: session,
})
}
}
func (v *UDPHub) Running() bool {
return !v.cancel.Cancelled()
}
// Connection return the net.Conn underneath v hub.
// Private: Visible for testing only
func (v *UDPHub) Connection() net.Conn {
return v.conn
}
func (v *UDPHub) Addr() net.Addr {
return v.conn.LocalAddr()
}