2016-06-14 20:54:08 +00:00
|
|
|
package udp
|
2016-01-28 16:43:47 +00:00
|
|
|
|
|
|
|
import (
|
2018-09-17 13:12:58 +00:00
|
|
|
"context"
|
|
|
|
|
2016-12-09 10:35:27 +00:00
|
|
|
"v2ray.com/core/common/buf"
|
2017-08-29 12:32:54 +00:00
|
|
|
"v2ray.com/core/common/net"
|
2018-09-17 13:12:58 +00:00
|
|
|
"v2ray.com/core/transport/internet"
|
2016-01-28 16:43:47 +00:00
|
|
|
)
|
|
|
|
|
2016-12-21 14:48:39 +00:00
|
|
|
// Payload represents a single UDP payload.
|
|
|
|
type Payload struct {
|
2018-08-11 20:35:01 +00:00
|
|
|
Content *buf.Buffer
|
|
|
|
Source net.Destination
|
|
|
|
OriginalDestination net.Destination
|
2016-11-18 20:30:03 +00:00
|
|
|
}
|
|
|
|
|
2018-02-12 12:14:11 +00:00
|
|
|
type HubOption func(h *Hub)
|
2016-11-18 20:30:03 +00:00
|
|
|
|
2018-10-01 09:03:23 +00:00
|
|
|
func HubCapacity(capacity int) HubOption {
|
2018-02-12 12:14:11 +00:00
|
|
|
return func(h *Hub) {
|
2018-10-01 09:03:23 +00:00
|
|
|
h.capacity = capacity
|
2016-11-18 20:30:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-12 12:14:11 +00:00
|
|
|
func HubReceiveOriginalDestination(r bool) HubOption {
|
|
|
|
return func(h *Hub) {
|
|
|
|
h.recvOrigDest = r
|
2016-11-18 20:30:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-21 14:48:39 +00:00
|
|
|
type Hub struct {
|
2018-02-12 12:14:11 +00:00
|
|
|
conn *net.UDPConn
|
2018-08-11 20:35:01 +00:00
|
|
|
cache chan *Payload
|
2018-02-12 12:14:11 +00:00
|
|
|
capacity int
|
|
|
|
recvOrigDest bool
|
2016-08-15 15:44:46 +00:00
|
|
|
}
|
|
|
|
|
2018-09-17 13:12:58 +00:00
|
|
|
func ListenUDP(ctx context.Context, address net.Address, port net.Port, options ...HubOption) (*Hub, error) {
|
2018-02-12 12:14:11 +00:00
|
|
|
hub := &Hub{
|
2018-02-12 14:08:20 +00:00
|
|
|
capacity: 256,
|
2018-02-12 12:14:11 +00:00
|
|
|
recvOrigDest: false,
|
|
|
|
}
|
|
|
|
for _, opt := range options {
|
|
|
|
opt(hub)
|
|
|
|
}
|
|
|
|
|
2018-09-17 13:12:58 +00:00
|
|
|
streamSettings := internet.StreamSettingsFromContext(ctx)
|
|
|
|
if streamSettings != nil && streamSettings.SocketSettings != nil && streamSettings.SocketSettings.ReceiveOriginalDestAddress {
|
|
|
|
hub.recvOrigDest = true
|
|
|
|
}
|
2018-08-11 20:35:01 +00:00
|
|
|
|
2018-09-17 13:12:58 +00:00
|
|
|
udpConn, err := internet.ListenSystemPacket(ctx, &net.UDPAddr{
|
|
|
|
IP: address.IP(),
|
|
|
|
Port: int(port),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2016-08-15 15:44:46 +00:00
|
|
|
}
|
2018-09-17 13:12:58 +00:00
|
|
|
newError("listening UDP on ", address, ":", port).WriteToLog()
|
|
|
|
hub.conn = udpConn.(*net.UDPConn)
|
|
|
|
hub.cache = make(chan *Payload, hub.capacity)
|
2018-02-12 12:14:11 +00:00
|
|
|
|
2018-08-11 20:35:01 +00:00
|
|
|
go hub.start()
|
2016-01-28 16:43:47 +00:00
|
|
|
return hub, nil
|
|
|
|
}
|
|
|
|
|
2018-02-12 10:53:41 +00:00
|
|
|
// Close implements net.Listener.
|
2018-02-08 14:39:46 +00:00
|
|
|
func (h *Hub) Close() error {
|
2017-10-28 19:28:50 +00:00
|
|
|
h.conn.Close()
|
2018-02-08 14:39:46 +00:00
|
|
|
return nil
|
2016-01-28 16:43:47 +00:00
|
|
|
}
|
|
|
|
|
2017-10-28 19:28:50 +00:00
|
|
|
func (h *Hub) WriteTo(payload []byte, dest net.Destination) (int, error) {
|
|
|
|
return h.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
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-08-11 20:35:01 +00:00
|
|
|
func (h *Hub) start() {
|
|
|
|
c := h.cache
|
2018-02-12 12:14:11 +00:00
|
|
|
defer close(c)
|
|
|
|
|
2016-08-15 15:44:46 +00:00
|
|
|
oobBytes := make([]byte, 256)
|
2017-02-13 22:03:36 +00:00
|
|
|
|
2018-02-08 21:09:41 +00:00
|
|
|
for {
|
2017-04-15 19:19:21 +00:00
|
|
|
buffer := buf.New()
|
2016-12-06 10:03:42 +00:00
|
|
|
var noob int
|
|
|
|
var addr *net.UDPAddr
|
2018-09-01 19:20:06 +00:00
|
|
|
err := buffer.Reset(func(b []byte) (int, error) {
|
2017-10-28 19:28:50 +00:00
|
|
|
n, nb, _, a, e := ReadUDPMsg(h.conn, b, oobBytes)
|
2016-12-06 10:03:42 +00:00
|
|
|
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 {
|
2017-12-19 20:28:12 +00:00
|
|
|
newError("failed to read UDP msg").Base(err).WriteToLog()
|
2016-01-28 16:43:47 +00:00
|
|
|
buffer.Release()
|
2018-02-08 21:09:41 +00:00
|
|
|
break
|
2016-01-28 16:43:47 +00:00
|
|
|
}
|
2016-08-15 15:44:46 +00:00
|
|
|
|
2018-09-01 19:20:06 +00:00
|
|
|
if buffer.IsEmpty() {
|
|
|
|
buffer.Release()
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2018-02-12 12:14:11 +00:00
|
|
|
payload := &Payload{
|
2018-08-11 20:35:01 +00:00
|
|
|
Content: buffer,
|
|
|
|
Source: net.UDPDestination(net.IPAddress(addr.IP), net.Port(addr.Port)),
|
2017-01-26 19:46:44 +00:00
|
|
|
}
|
2018-02-12 12:14:11 +00:00
|
|
|
if h.recvOrigDest && noob > 0 {
|
2018-08-11 20:35:01 +00:00
|
|
|
payload.OriginalDestination = RetrieveOriginalDest(oobBytes[:noob])
|
|
|
|
if payload.OriginalDestination.IsValid() {
|
|
|
|
newError("UDP original destination: ", payload.OriginalDestination).AtDebug().WriteToLog()
|
2018-02-12 03:55:39 +00:00
|
|
|
} else {
|
2018-02-12 10:52:57 +00:00
|
|
|
newError("failed to read UDP original destination").WriteToLog()
|
2018-02-12 03:55:39 +00:00
|
|
|
}
|
2016-08-15 15:44:46 +00:00
|
|
|
}
|
2018-02-12 12:14:11 +00:00
|
|
|
|
|
|
|
select {
|
|
|
|
case c <- payload:
|
|
|
|
default:
|
2018-09-01 19:20:06 +00:00
|
|
|
buffer.Release()
|
|
|
|
payload.Content = nil
|
2018-02-12 12:14:11 +00:00
|
|
|
}
|
|
|
|
|
2016-01-28 16:43:47 +00:00
|
|
|
}
|
|
|
|
}
|
2016-07-13 19:39:18 +00:00
|
|
|
|
2018-02-12 10:53:41 +00:00
|
|
|
// Addr implements net.Listener.
|
2017-10-28 19:28:50 +00:00
|
|
|
func (h *Hub) Addr() net.Addr {
|
|
|
|
return h.conn.LocalAddr()
|
2016-08-15 20:20:16 +00:00
|
|
|
}
|
2018-08-11 20:35:01 +00:00
|
|
|
|
|
|
|
func (h *Hub) Receive() <-chan *Payload {
|
|
|
|
return h.cache
|
|
|
|
}
|