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

114 lines
2.4 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-08-20 18:55:45 +00:00
"v2ray.com/core/common/alloc"
"v2ray.com/core/common/log"
v2net "v2ray.com/core/common/net"
"v2ray.com/core/proxy"
"v2ray.com/core/transport/internet/internal"
2016-01-28 16:43:47 +00:00
)
2016-08-15 15:44:46 +00:00
type UDPPayloadHandler func(*alloc.Buffer, *proxy.SessionInfo)
2016-01-28 16:43:47 +00:00
type UDPHub struct {
2016-07-13 19:39:18 +00:00
sync.RWMutex
2016-01-28 16:43:47 +00:00
conn *net.UDPConn
2016-08-15 15:44:46 +00:00
option ListenOption
2016-01-28 16:43:47 +00:00
accepting bool
2016-11-01 10:46:34 +00:00
pool *alloc.BufferPool
2016-01-28 16:43:47 +00:00
}
2016-08-15 15:44:46 +00:00
type ListenOption struct {
Callback UDPPayloadHandler
ReceiveOriginalDest bool
}
func ListenUDP(address v2net.Address, port v2net.Port, option ListenOption) (*UDPHub, error) {
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,
option: option,
2016-11-01 10:46:34 +00:00
pool: alloc.NewBufferPool(2048, 64),
2016-01-28 16:43:47 +00:00
}
go hub.start()
return hub, nil
}
func (this *UDPHub) Close() {
2016-07-13 19:39:18 +00:00
this.Lock()
defer this.Unlock()
2016-01-28 16:43:47 +00:00
this.accepting = false
this.conn.Close()
}
func (this *UDPHub) WriteTo(payload []byte, dest v2net.Destination) (int, error) {
return this.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
})
}
func (this *UDPHub) start() {
2016-07-13 19:39:18 +00:00
this.Lock()
2016-01-29 13:39:55 +00:00
this.accepting = true
2016-07-13 19:39:18 +00:00
this.Unlock()
2016-08-15 15:44:46 +00:00
oobBytes := make([]byte, 256)
2016-07-13 19:39:18 +00:00
for this.Running() {
2016-11-01 10:46:34 +00:00
buffer := this.pool.Allocate()
2016-08-19 11:00:55 +00:00
nBytes, noob, _, addr, err := ReadUDPMsg(this.conn, buffer.Value, oobBytes)
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
}
buffer.Slice(0, nBytes)
2016-08-15 15:44:46 +00: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])
}
go this.option.Callback(buffer, session)
2016-01-28 16:43:47 +00:00
}
}
2016-07-13 19:39:18 +00:00
func (this *UDPHub) Running() bool {
this.RLock()
defer this.RUnlock()
return this.accepting
}
2016-08-15 19:33:21 +00: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 20:20:16 +00:00
func (this *UDPHub) Addr() net.Addr {
return this.conn.LocalAddr()
}