v2ray-core/transport/internet/udp/hub.go

179 lines
3.6 KiB
Go
Raw Normal View History

2016-06-14 20:54:08 +00:00
package udp
2016-01-28 16:43:47 +00:00
import (
"net"
2016-07-13 19:39:18 +00:00
"sync"
2016-01-28 16:43:47 +00:00
2016-12-09 10:35:27 +00:00
"v2ray.com/core/common/buf"
2016-11-18 20:30:03 +00:00
"v2ray.com/core/common/dice"
2016-08-20 18:55:45 +00:00
"v2ray.com/core/common/log"
v2net "v2ray.com/core/common/net"
2016-11-18 20:30:03 +00:00
"v2ray.com/core/common/signal"
2016-08-20 18:55:45 +00:00
"v2ray.com/core/proxy"
"v2ray.com/core/transport/internet/internal"
2016-01-28 16:43:47 +00:00
)
2016-11-18 20:30:03 +00:00
type UDPPayload struct {
2016-12-09 10:35:27 +00:00
payload *buf.Buffer
2016-11-18 20:30:03 +00:00
session *proxy.SessionInfo
}
2016-12-09 10:35:27 +00:00
type UDPPayloadHandler func(*buf.Buffer, *proxy.SessionInfo)
2016-01-28 16:43:47 +00:00
2016-11-18 20:30:03 +00:00
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
}
2016-11-27 20:39:09 +00:00
func (v *UDPPayloadQueue) Enqueue(payload UDPPayload) {
size := len(v.queue)
2016-11-18 20:34:42 +00:00
idx := 0
if size > 1 {
idx = dice.Roll(size)
}
2016-11-18 20:30:03 +00:00
for i := 0; i < size; i++ {
select {
2016-11-27 20:39:09 +00:00
case v.queue[idx%size] <- payload:
2016-11-18 20:30:03 +00:00
return
default:
2016-11-18 20:34:42 +00:00
idx++
2016-11-18 20:30:03 +00:00
}
}
}
2016-11-27 20:39:09 +00:00
func (v *UDPPayloadQueue) Dequeue(queue <-chan UDPPayload) {
2016-11-18 23:34:30 +00:00
for payload := range queue {
2016-11-27 20:39:09 +00:00
v.callback(payload.payload, payload.session)
2016-11-18 20:30:03 +00:00
}
}
2016-11-27 20:39:09 +00:00
func (v *UDPPayloadQueue) Close() {
for _, queue := range v.queue {
2016-11-18 20:30:03 +00:00
close(queue)
}
2016-01-28 16:43:47 +00:00
}
2016-08-15 15:44:46 +00:00
type ListenOption struct {
Callback UDPPayloadHandler
ReceiveOriginalDest bool
2016-11-18 20:30:03 +00:00
Concurrency int
}
type UDPHub struct {
sync.RWMutex
conn *net.UDPConn
cancel *signal.CancelSignal
queue *UDPPayloadQueue
option ListenOption
2016-08-15 15:44:46 +00:00
}
func ListenUDP(address v2net.Address, port v2net.Port, option ListenOption) (*UDPHub, error) {
2016-11-18 20:30:03 +00:00
if option.Concurrency < 1 {
option.Concurrency = 1
}
2016-01-28 16:43:47 +00:00
udpConn, err := net.ListenUDP("udp", &net.UDPAddr{
2016-05-29 14:37:52 +00:00
IP: address.IP(),
2016-01-28 16:43:47 +00:00
Port: int(port),
})
if err != nil {
return nil, err
}
2016-08-15 15:44:46 +00:00
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
}
}
2016-01-28 16:43:47 +00:00
hub := &UDPHub{
2016-08-15 15:44:46 +00:00
conn: udpConn,
2016-11-18 20:30:03 +00:00
queue: NewUDPPayloadQueue(option),
option: option,
cancel: signal.NewCloseSignal(),
2016-01-28 16:43:47 +00:00
}
go hub.start()
return hub, nil
}
2016-11-27 20:39:09 +00:00
func (v *UDPHub) Close() {
v.Lock()
defer v.Unlock()
2016-07-13 19:39:18 +00:00
2016-11-27 20:39:09 +00:00
v.cancel.Cancel()
v.conn.Close()
v.cancel.WaitForDone()
v.queue.Close()
2016-01-28 16:43:47 +00:00
}
2016-11-27 20:39:09 +00:00
func (v *UDPHub) WriteTo(payload []byte, dest v2net.Destination) (int, error) {
return v.conn.WriteToUDP(payload, &net.UDPAddr{
2016-09-20 09:53:05 +00:00
IP: dest.Address.IP(),
Port: int(dest.Port),
2016-01-28 16:43:47 +00:00
})
}
2016-11-27 20:39:09 +00:00
func (v *UDPHub) start() {
v.cancel.WaitThread()
defer v.cancel.FinishThread()
2016-07-13 19:39:18 +00:00
2016-08-15 15:44:46 +00:00
oobBytes := make([]byte, 256)
2016-11-27 20:39:09 +00:00
for v.Running() {
2016-12-09 11:08:25 +00:00
buffer := buf.NewSmall()
2016-12-06 10:03:42 +00:00
var noob int
var addr *net.UDPAddr
2016-12-09 11:08:25 +00:00
err := buffer.AppendSupplier(func(b []byte) (int, error) {
2016-12-06 10:03:42 +00:00
n, nb, _, a, e := ReadUDPMsg(v.conn, b, oobBytes)
noob = nb
addr = a
2016-12-09 11:08:25 +00:00
return n, e
2016-12-06 10:03:42 +00:00
})
2016-01-28 16:43:47 +00:00
if err != nil {
2016-08-19 10:58:26 +00:00
log.Info("UDP|Hub: Failed to read UDP msg: ", err)
2016-01-28 16:43:47 +00:00
buffer.Release()
continue
}
2016-08-15 15:44:46 +00:00
session := new(proxy.SessionInfo)
session.Source = v2net.UDPDestination(v2net.IPAddress(addr.IP), v2net.Port(addr.Port))
2016-11-27 20:39:09 +00:00
if v.option.ReceiveOriginalDest && noob > 0 {
2016-08-15 15:44:46 +00:00
session.Destination = RetrieveOriginalDest(oobBytes[:noob])
}
2016-11-27 20:39:09 +00:00
v.queue.Enqueue(UDPPayload{
2016-11-18 20:30:03 +00:00
payload: buffer,
session: session,
})
2016-01-28 16:43:47 +00:00
}
}
2016-07-13 19:39:18 +00:00
2016-11-27 20:39:09 +00:00
func (v *UDPHub) Running() bool {
return !v.cancel.Cancelled()
2016-07-13 19:39:18 +00:00
}
2016-08-15 19:33:21 +00:00
2016-11-27 20:39:09 +00:00
// Connection return the net.Conn underneath v hub.
2016-08-15 19:33:21 +00:00
// Private: Visible for testing only
2016-11-27 20:39:09 +00:00
func (v *UDPHub) Connection() net.Conn {
return v.conn
2016-08-15 19:33:21 +00:00
}
2016-08-15 20:20:16 +00:00
2016-11-27 20:39:09 +00:00
func (v *UDPHub) Addr() net.Addr {
return v.conn.LocalAddr()
2016-08-15 20:20:16 +00:00
}