1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-07-08 14:24:36 -04:00
v2fly/transport/internet/udp/hub.go

172 lines
3.6 KiB
Go
Raw Normal View History

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