forked from v2fly/v2ray-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hub.go
177 lines (157 loc) · 3.72 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
package udp
import (
"context"
"v2ray.com/core/app/log"
"v2ray.com/core/common/buf"
"v2ray.com/core/common/dice"
"v2ray.com/core/common/net"
)
// Payload represents a single UDP payload.
type Payload struct {
payload *buf.Buffer
source net.Destination
originalDest net.Destination
}
// PayloadHandler is function to handle Payload.
type PayloadHandler func(payload *buf.Buffer, source net.Destination, originalDest net.Destination)
// PayloadQueue is a queue of Payload.
type PayloadQueue struct {
queue []chan Payload
callback PayloadHandler
}
// NewPayloadQueue returns a new PayloadQueue.
func NewPayloadQueue(option ListenOption) *PayloadQueue {
queue := &PayloadQueue{
callback: option.Callback,
queue: make([]chan Payload, option.Concurrency),
}
for i := range queue.queue {
queue.queue[i] = make(chan Payload, 64)
go queue.Dequeue(queue.queue[i])
}
return queue
}
func (v *PayloadQueue) Enqueue(payload Payload) {
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 *PayloadQueue) Dequeue(queue <-chan Payload) {
for payload := range queue {
v.callback(payload.payload, payload.source, payload.originalDest)
}
}
func (v *PayloadQueue) Close() {
for _, queue := range v.queue {
close(queue)
}
}
type ListenOption struct {
Callback PayloadHandler
ReceiveOriginalDest bool
Concurrency int
}
type Hub struct {
conn *net.UDPConn
cancel context.CancelFunc
queue *PayloadQueue
option ListenOption
}
func ListenUDP(address net.Address, port net.Port, option ListenOption) (*Hub, 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
}
log.Trace(newError("listening UDP on ", address, ":", port))
if option.ReceiveOriginalDest {
rawConn, err := udpConn.SyscallConn()
if err != nil {
return nil, newError("failed to get fd").Base(err)
}
err = rawConn.Control(func(fd uintptr) {
if err := SetOriginalDestOptions(int(fd)); err != nil {
log.Trace(newError("failed to set socket options").Base(err))
}
})
if err != nil {
return nil, newError("failed to control socket").Base(err)
}
}
ctx, cancel := context.WithCancel(context.Background())
hub := &Hub{
conn: udpConn,
queue: NewPayloadQueue(option),
option: option,
cancel: cancel,
}
go hub.start(ctx)
return hub, nil
}
func (v *Hub) Close() {
v.cancel()
v.conn.Close()
}
func (v *Hub) WriteTo(payload []byte, dest net.Destination) (int, error) {
return v.conn.WriteToUDP(payload, &net.UDPAddr{
IP: dest.Address.IP(),
Port: int(dest.Port),
})
}
func (v *Hub) start(ctx context.Context) {
oobBytes := make([]byte, 256)
L:
for {
select {
case <-ctx.Done():
break L
default:
}
buffer := buf.New()
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.Trace(newError("failed to read UDP msg").Base(err))
buffer.Release()
continue
}
payload := Payload{
payload: buffer,
}
payload.source = net.UDPDestination(net.IPAddress(addr.IP), net.Port(addr.Port))
if v.option.ReceiveOriginalDest && noob > 0 {
payload.originalDest = RetrieveOriginalDest(oobBytes[:noob])
}
v.queue.Enqueue(payload)
}
v.queue.Close()
}
// Connection returns the net.Conn underneath this hub.
// Private: Visible for testing only
func (v *Hub) Connection() net.Conn {
return v.conn
}
func (v *Hub) Addr() net.Addr {
return v.conn.LocalAddr()
}