140 lines
2.8 KiB
Go
Raw Normal View History

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