2016-06-14 16:54:08 -04:00
|
|
|
package udp
|
2016-01-28 11:43:47 -05:00
|
|
|
|
|
|
|
import (
|
2017-02-13 16:39:50 -05:00
|
|
|
"context"
|
2016-01-28 11:43:47 -05:00
|
|
|
|
2016-12-09 05:35:27 -05:00
|
|
|
"v2ray.com/core/common/buf"
|
2016-11-18 15:30:03 -05:00
|
|
|
"v2ray.com/core/common/dice"
|
2017-08-29 08:32:54 -04:00
|
|
|
"v2ray.com/core/common/net"
|
2016-01-28 11:43:47 -05:00
|
|
|
)
|
|
|
|
|
2016-12-21 09:48:39 -05:00
|
|
|
// Payload represents a single UDP payload.
|
|
|
|
type Payload struct {
|
2017-01-26 14:46:44 -05:00
|
|
|
payload *buf.Buffer
|
2017-08-29 08:32:54 -04:00
|
|
|
source net.Destination
|
|
|
|
originalDest net.Destination
|
2016-11-18 15:30:03 -05:00
|
|
|
}
|
|
|
|
|
2016-12-21 09:48:39 -05:00
|
|
|
// PayloadHandler is function to handle Payload.
|
2017-08-29 08:32:54 -04:00
|
|
|
type PayloadHandler func(payload *buf.Buffer, source net.Destination, originalDest net.Destination)
|
2016-01-28 11:43:47 -05:00
|
|
|
|
2016-12-21 09:48:39 -05:00
|
|
|
// PayloadQueue is a queue of Payload.
|
|
|
|
type PayloadQueue struct {
|
|
|
|
queue []chan Payload
|
|
|
|
callback PayloadHandler
|
2016-11-18 15:30:03 -05:00
|
|
|
}
|
|
|
|
|
2016-12-21 09:48:39 -05:00
|
|
|
// NewPayloadQueue returns a new PayloadQueue.
|
|
|
|
func NewPayloadQueue(option ListenOption) *PayloadQueue {
|
|
|
|
queue := &PayloadQueue{
|
2016-11-18 15:30:03 -05:00
|
|
|
callback: option.Callback,
|
2016-12-21 09:48:39 -05:00
|
|
|
queue: make([]chan Payload, option.Concurrency),
|
2016-11-18 15:30:03 -05:00
|
|
|
}
|
|
|
|
for i := range queue.queue {
|
2016-12-21 09:48:39 -05:00
|
|
|
queue.queue[i] = make(chan Payload, 64)
|
2016-11-18 15:30:03 -05:00
|
|
|
go queue.Dequeue(queue.queue[i])
|
|
|
|
}
|
|
|
|
return queue
|
|
|
|
}
|
|
|
|
|
2017-10-28 15:28:50 -04:00
|
|
|
// Enqueue adds the payload to the end of this queue.
|
|
|
|
func (q *PayloadQueue) Enqueue(payload Payload) {
|
|
|
|
size := len(q.queue)
|
2016-11-18 15:34:42 -05:00
|
|
|
idx := 0
|
|
|
|
if size > 1 {
|
|
|
|
idx = dice.Roll(size)
|
|
|
|
}
|
2016-11-18 15:30:03 -05:00
|
|
|
for i := 0; i < size; i++ {
|
|
|
|
select {
|
2017-10-28 15:28:50 -04:00
|
|
|
case q.queue[idx%size] <- payload:
|
2016-11-18 15:30:03 -05:00
|
|
|
return
|
|
|
|
default:
|
2016-11-18 15:34:42 -05:00
|
|
|
idx++
|
2016-11-18 15:30:03 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-28 15:28:50 -04:00
|
|
|
func (q *PayloadQueue) Dequeue(queue <-chan Payload) {
|
2016-11-18 18:34:30 -05:00
|
|
|
for payload := range queue {
|
2017-10-28 15:28:50 -04:00
|
|
|
q.callback(payload.payload, payload.source, payload.originalDest)
|
2016-11-18 15:30:03 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-28 15:28:50 -04:00
|
|
|
func (q *PayloadQueue) Close() {
|
|
|
|
for _, queue := range q.queue {
|
2016-11-18 15:30:03 -05:00
|
|
|
close(queue)
|
|
|
|
}
|
2016-01-28 11:43:47 -05:00
|
|
|
}
|
|
|
|
|
2016-08-15 11:44:46 -04:00
|
|
|
type ListenOption struct {
|
2016-12-21 09:48:39 -05:00
|
|
|
Callback PayloadHandler
|
2016-08-15 11:44:46 -04:00
|
|
|
ReceiveOriginalDest bool
|
2016-11-18 15:30:03 -05:00
|
|
|
Concurrency int
|
|
|
|
}
|
|
|
|
|
2016-12-21 09:48:39 -05:00
|
|
|
type Hub struct {
|
2016-11-18 15:30:03 -05:00
|
|
|
conn *net.UDPConn
|
2017-02-13 16:39:50 -05:00
|
|
|
cancel context.CancelFunc
|
2016-12-21 09:48:39 -05:00
|
|
|
queue *PayloadQueue
|
2016-11-18 15:30:03 -05:00
|
|
|
option ListenOption
|
2016-08-15 11:44:46 -04:00
|
|
|
}
|
|
|
|
|
2017-08-29 08:32:54 -04:00
|
|
|
func ListenUDP(address net.Address, port net.Port, option ListenOption) (*Hub, error) {
|
2016-11-18 15:30:03 -05:00
|
|
|
if option.Concurrency < 1 {
|
|
|
|
option.Concurrency = 1
|
|
|
|
}
|
2016-01-28 11:43:47 -05:00
|
|
|
udpConn, err := net.ListenUDP("udp", &net.UDPAddr{
|
2016-05-29 10:37:52 -04:00
|
|
|
IP: address.IP(),
|
2016-01-28 11:43:47 -05:00
|
|
|
Port: int(port),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-12-19 15:28:12 -05:00
|
|
|
newError("listening UDP on ", address, ":", port).WriteToLog()
|
2016-08-15 11:44:46 -04:00
|
|
|
if option.ReceiveOriginalDest {
|
2017-08-25 15:42:07 -04:00
|
|
|
rawConn, err := udpConn.SyscallConn()
|
2016-08-15 11:44:46 -04:00
|
|
|
if err != nil {
|
2017-04-08 19:43:25 -04:00
|
|
|
return nil, newError("failed to get fd").Base(err)
|
2016-08-15 11:44:46 -04:00
|
|
|
}
|
2017-08-25 15:42:07 -04:00
|
|
|
err = rawConn.Control(func(fd uintptr) {
|
|
|
|
if err := SetOriginalDestOptions(int(fd)); err != nil {
|
2017-12-19 15:28:12 -05:00
|
|
|
newError("failed to set socket options").Base(err).WriteToLog()
|
2017-08-25 15:42:07 -04:00
|
|
|
}
|
|
|
|
})
|
2016-08-15 11:44:46 -04:00
|
|
|
if err != nil {
|
2017-08-25 15:42:07 -04:00
|
|
|
return nil, newError("failed to control socket").Base(err)
|
2016-08-15 11:44:46 -04:00
|
|
|
}
|
|
|
|
}
|
2017-02-13 16:39:50 -05:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
2016-12-21 09:48:39 -05:00
|
|
|
hub := &Hub{
|
2016-08-15 11:44:46 -04:00
|
|
|
conn: udpConn,
|
2016-12-21 09:48:39 -05:00
|
|
|
queue: NewPayloadQueue(option),
|
2016-11-18 15:30:03 -05:00
|
|
|
option: option,
|
2017-02-13 16:39:50 -05:00
|
|
|
cancel: cancel,
|
2016-01-28 11:43:47 -05:00
|
|
|
}
|
2017-02-13 16:39:50 -05:00
|
|
|
go hub.start(ctx)
|
2016-01-28 11:43:47 -05:00
|
|
|
return hub, nil
|
|
|
|
}
|
|
|
|
|
2017-10-28 15:28:50 -04:00
|
|
|
func (h *Hub) Close() {
|
|
|
|
h.cancel()
|
|
|
|
h.conn.Close()
|
2016-01-28 11:43:47 -05:00
|
|
|
}
|
|
|
|
|
2017-10-28 15:28:50 -04:00
|
|
|
func (h *Hub) WriteTo(payload []byte, dest net.Destination) (int, error) {
|
|
|
|
return h.conn.WriteToUDP(payload, &net.UDPAddr{
|
2016-09-20 05:53:05 -04:00
|
|
|
IP: dest.Address.IP(),
|
|
|
|
Port: int(dest.Port),
|
2016-01-28 11:43:47 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-10-28 15:28:50 -04:00
|
|
|
func (h *Hub) start(ctx context.Context) {
|
2016-08-15 11:44:46 -04:00
|
|
|
oobBytes := make([]byte, 256)
|
2017-02-13 17:03:36 -05:00
|
|
|
L:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
break L
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
2017-04-15 15:19:21 -04:00
|
|
|
buffer := buf.New()
|
2016-12-06 05:03:42 -05:00
|
|
|
var noob int
|
|
|
|
var addr *net.UDPAddr
|
2016-12-09 06:08:25 -05:00
|
|
|
err := buffer.AppendSupplier(func(b []byte) (int, error) {
|
2017-10-28 15:28:50 -04:00
|
|
|
n, nb, _, a, e := ReadUDPMsg(h.conn, b, oobBytes)
|
2016-12-06 05:03:42 -05:00
|
|
|
noob = nb
|
|
|
|
addr = a
|
2016-12-09 06:08:25 -05:00
|
|
|
return n, e
|
2016-12-06 05:03:42 -05:00
|
|
|
})
|
|
|
|
|
2016-01-28 11:43:47 -05:00
|
|
|
if err != nil {
|
2017-12-19 15:28:12 -05:00
|
|
|
newError("failed to read UDP msg").Base(err).WriteToLog()
|
2016-01-28 11:43:47 -05:00
|
|
|
buffer.Release()
|
|
|
|
continue
|
|
|
|
}
|
2016-08-15 11:44:46 -04:00
|
|
|
|
2017-01-26 14:46:44 -05:00
|
|
|
payload := Payload{
|
|
|
|
payload: buffer,
|
|
|
|
}
|
2017-08-29 08:32:54 -04:00
|
|
|
payload.source = net.UDPDestination(net.IPAddress(addr.IP), net.Port(addr.Port))
|
2017-10-28 15:28:50 -04:00
|
|
|
if h.option.ReceiveOriginalDest && noob > 0 {
|
2017-01-26 14:46:44 -05:00
|
|
|
payload.originalDest = RetrieveOriginalDest(oobBytes[:noob])
|
2016-08-15 11:44:46 -04:00
|
|
|
}
|
2017-10-28 15:28:50 -04:00
|
|
|
h.queue.Enqueue(payload)
|
2016-01-28 11:43:47 -05:00
|
|
|
}
|
2017-10-28 15:28:50 -04:00
|
|
|
h.queue.Close()
|
2016-01-28 11:43:47 -05:00
|
|
|
}
|
2016-07-13 15:39:18 -04:00
|
|
|
|
2017-10-28 15:28:50 -04:00
|
|
|
func (h *Hub) Addr() net.Addr {
|
|
|
|
return h.conn.LocalAddr()
|
2016-08-15 16:20:16 -04:00
|
|
|
}
|