You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
v2ray-core/transport/internet/udp/hub.go

185 lines
3.9 KiB

package udp
9 years ago
import (
"net"
8 years ago
"sync"
9 years ago
"v2ray.com/core/common/alloc"
8 years ago
"v2ray.com/core/common/dice"
"v2ray.com/core/common/log"
v2net "v2ray.com/core/common/net"
8 years ago
"v2ray.com/core/common/signal"
"v2ray.com/core/proxy"
"v2ray.com/core/transport/internet/internal"
9 years ago
)
8 years ago
type UDPPayload struct {
payload *alloc.Buffer
session *proxy.SessionInfo
}
type UDPPayloadHandler func(*alloc.Buffer, *proxy.SessionInfo)
9 years ago
8 years ago
type UDPPayloadQueue struct {
queue []chan UDPPayload
callback UDPPayloadHandler
cancel *signal.CancelSignal
}
func NewUDPPayloadQueue(option ListenOption) *UDPPayloadQueue {
queue := &UDPPayloadQueue{
callback: option.Callback,
cancel: signal.NewCloseSignal(),
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)
8 years ago
idx := 0
if size > 1 {
idx = dice.Roll(size)
}
8 years ago
for i := 0; i < size; i++ {
select {
8 years ago
case this.queue[idx%size] <- payload:
8 years ago
return
default:
8 years ago
idx++
8 years ago
}
}
}
func (this *UDPPayloadQueue) Dequeue(queue <-chan UDPPayload) {
this.cancel.WaitThread()
defer this.cancel.FinishThread()
for !this.cancel.Cancelled() {
payload, open := <-queue
if !open {
return
}
this.callback(payload.payload, payload.session)
}
}
func (this *UDPPayloadQueue) Close() {
this.cancel.Cancel()
for _, queue := range this.queue {
close(queue)
}
this.cancel.WaitForDone()
9 years ago
}
type ListenOption struct {
Callback UDPPayloadHandler
ReceiveOriginalDest bool
8 years ago
Concurrency int
}
type UDPHub struct {
sync.RWMutex
conn *net.UDPConn
pool *alloc.BufferPool
cancel *signal.CancelSignal
queue *UDPPayloadQueue
option ListenOption
}
func ListenUDP(address v2net.Address, port v2net.Port, option ListenOption) (*UDPHub, error) {
8 years ago
if option.Concurrency < 1 {
option.Concurrency = 1
}
9 years ago
udpConn, err := net.ListenUDP("udp", &net.UDPAddr{
IP: address.IP(),
9 years ago
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
}
}
9 years ago
hub := &UDPHub{
conn: udpConn,
pool: alloc.NewBufferPool(2048, 64),
8 years ago
queue: NewUDPPayloadQueue(option),
option: option,
cancel: signal.NewCloseSignal(),
9 years ago
}
go hub.start()
return hub, nil
}
func (this *UDPHub) Close() {
8 years ago
this.Lock()
defer this.Unlock()
8 years ago
this.cancel.Cancel()
9 years ago
this.conn.Close()
8 years ago
this.cancel.WaitForDone()
this.queue.Close()
9 years ago
}
func (this *UDPHub) WriteTo(payload []byte, dest v2net.Destination) (int, error) {
return this.conn.WriteToUDP(payload, &net.UDPAddr{
IP: dest.Address.IP(),
Port: int(dest.Port),
9 years ago
})
}
func (this *UDPHub) start() {
8 years ago
this.cancel.WaitThread()
defer this.cancel.FinishThread()
8 years ago
oobBytes := make([]byte, 256)
8 years ago
for this.Running() {
buffer := this.pool.Allocate()
nBytes, noob, _, addr, err := ReadUDPMsg(this.conn, buffer.Value, oobBytes)
9 years ago
if err != nil {
log.Info("UDP|Hub: Failed to read UDP msg: ", err)
9 years ago
buffer.Release()
continue
}
buffer.Slice(0, nBytes)
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])
}
8 years ago
this.queue.Enqueue(UDPPayload{
payload: buffer,
session: session,
})
9 years ago
}
}
8 years ago
func (this *UDPHub) Running() bool {
8 years ago
return !this.cancel.Cancelled()
8 years ago
}
// Connection return the net.Conn underneath this hub.
// Private: Visible for testing only
func (this *UDPHub) Connection() net.Conn {
return this.conn
}
8 years ago
func (this *UDPHub) Addr() net.Addr {
return this.conn.LocalAddr()
}