1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-09 11:51:02 -05:00
v2fly/transport/internet/udp/hub.go

133 lines
2.7 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 (
2018-09-17 09:12:58 -04:00
"context"
2021-02-16 15:31:50 -05:00
"github.com/v2fly/v2ray-core/v4/common/buf"
"github.com/v2fly/v2ray-core/v4/common/net"
"github.com/v2fly/v2ray-core/v4/common/protocol/udp"
"github.com/v2fly/v2ray-core/v4/transport/internet"
2016-01-28 11:43:47 -05:00
)
2018-02-12 07:14:11 -05:00
type HubOption func(h *Hub)
2016-11-18 15:30:03 -05:00
func HubCapacity(capacity int) HubOption {
2018-02-12 07:14:11 -05:00
return func(h *Hub) {
h.capacity = capacity
2016-11-18 15:30:03 -05:00
}
}
2018-02-12 07:14:11 -05:00
func HubReceiveOriginalDestination(r bool) HubOption {
return func(h *Hub) {
h.recvOrigDest = r
2016-11-18 15:30:03 -05:00
}
}
2016-12-21 09:48:39 -05:00
type Hub struct {
2018-02-12 07:14:11 -05:00
conn *net.UDPConn
2019-01-05 13:49:21 -05:00
cache chan *udp.Packet
2018-02-12 07:14:11 -05:00
capacity int
recvOrigDest bool
2016-08-15 11:44:46 -04:00
}
func ListenUDP(ctx context.Context, address net.Address, port net.Port, streamSettings *internet.MemoryStreamConfig, options ...HubOption) (*Hub, error) {
2018-02-12 07:14:11 -05:00
hub := &Hub{
2018-02-12 09:08:20 -05:00
capacity: 256,
2018-02-12 07:14:11 -05:00
recvOrigDest: false,
}
for _, opt := range options {
opt(hub)
}
var sockopt *internet.SocketConfig
if streamSettings != nil {
sockopt = streamSettings.SocketSettings
}
if sockopt != nil && sockopt.ReceiveOriginalDestAddress {
2018-09-17 09:12:58 -04:00
hub.recvOrigDest = true
}
2018-08-11 16:35:01 -04:00
2018-09-17 09:12:58 -04:00
udpConn, err := internet.ListenSystemPacket(ctx, &net.UDPAddr{
IP: address.IP(),
Port: int(port),
}, sockopt)
2018-09-17 09:12:58 -04:00
if err != nil {
return nil, err
2016-08-15 11:44:46 -04:00
}
2018-09-17 09:12:58 -04:00
newError("listening UDP on ", address, ":", port).WriteToLog()
hub.conn = udpConn.(*net.UDPConn)
2019-01-05 13:49:21 -05:00
hub.cache = make(chan *udp.Packet, hub.capacity)
2018-02-12 07:14:11 -05:00
2018-08-11 16:35:01 -04:00
go hub.start()
2016-01-28 11:43:47 -05:00
return hub, nil
}
2018-02-12 05:53:41 -05:00
// Close implements net.Listener.
2018-02-08 09:39:46 -05:00
func (h *Hub) Close() error {
2017-10-28 15:28:50 -04:00
h.conn.Close()
2018-02-08 09:39:46 -05:00
return nil
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
})
}
2018-08-11 16:35:01 -04:00
func (h *Hub) start() {
c := h.cache
2018-02-12 07:14:11 -05:00
defer close(c)
2016-08-15 11:44:46 -04:00
oobBytes := make([]byte, 256)
2017-02-13 17:03:36 -05:00
2018-02-08 16:09:41 -05:00
for {
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
2018-11-02 16:34:04 -04:00
rawBytes := buffer.Extend(buf.Size)
2016-12-06 05:03:42 -05:00
2018-11-02 16:34:04 -04:00
n, noob, _, addr, err := ReadUDPMsg(h.conn, rawBytes, oobBytes)
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()
2018-02-08 16:09:41 -05:00
break
2016-01-28 11:43:47 -05:00
}
2018-11-02 16:34:04 -04:00
buffer.Resize(0, int32(n))
2016-08-15 11:44:46 -04:00
2018-09-01 15:20:06 -04:00
if buffer.IsEmpty() {
buffer.Release()
continue
}
2019-01-05 13:49:21 -05:00
payload := &udp.Packet{
Payload: buffer,
2018-08-11 16:35:01 -04:00
Source: net.UDPDestination(net.IPAddress(addr.IP), net.Port(addr.Port)),
}
2018-02-12 07:14:11 -05:00
if h.recvOrigDest && noob > 0 {
2019-01-05 13:49:21 -05:00
payload.Target = RetrieveOriginalDest(oobBytes[:noob])
if payload.Target.IsValid() {
newError("UDP original destination: ", payload.Target).AtDebug().WriteToLog()
} else {
2018-02-12 05:52:57 -05:00
newError("failed to read UDP original destination").WriteToLog()
}
2016-08-15 11:44:46 -04:00
}
2018-02-12 07:14:11 -05:00
select {
case c <- payload:
default:
2018-09-01 15:20:06 -04:00
buffer.Release()
2019-01-05 13:49:21 -05:00
payload.Payload = nil
2018-02-12 07:14:11 -05:00
}
2016-01-28 11:43:47 -05:00
}
}
2016-07-13 15:39:18 -04:00
2018-02-12 05:53:41 -05:00
// Addr implements net.Listener.
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
}
2018-08-11 16:35:01 -04:00
2019-01-05 13:49:21 -05:00
func (h *Hub) Receive() <-chan *udp.Packet {
2018-08-11 16:35:01 -04:00
return h.cache
}