2015-09-22 23:50:05 +02:00
|
|
|
package socks
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
|
2015-10-08 14:46:18 +02:00
|
|
|
"github.com/v2ray/v2ray-core/common/alloc"
|
2015-09-22 23:50:05 +02:00
|
|
|
"github.com/v2ray/v2ray-core/common/log"
|
|
|
|
v2net "github.com/v2ray/v2ray-core/common/net"
|
|
|
|
"github.com/v2ray/v2ray-core/proxy/socks/protocol"
|
2016-02-01 15:36:33 +00:00
|
|
|
"github.com/v2ray/v2ray-core/transport/hub"
|
2015-09-22 23:50:05 +02:00
|
|
|
)
|
|
|
|
|
2015-12-02 20:44:01 +00:00
|
|
|
func (this *SocksServer) ListenUDP(port v2net.Port) error {
|
2015-09-22 23:50:05 +02:00
|
|
|
addr := &net.UDPAddr{
|
2015-10-06 11:57:26 +02:00
|
|
|
IP: net.IP{0, 0, 0, 0},
|
2015-09-22 23:50:05 +02:00
|
|
|
Port: int(port),
|
|
|
|
Zone: "",
|
|
|
|
}
|
|
|
|
conn, err := net.ListenUDP("udp", addr)
|
|
|
|
if err != nil {
|
2016-01-18 12:24:33 +01:00
|
|
|
log.Error("Socks: failed to listen UDP on port ", port, ": ", err)
|
2015-09-22 23:50:05 +02:00
|
|
|
return err
|
|
|
|
}
|
2016-01-04 08:40:24 +01:00
|
|
|
this.udpMutex.Lock()
|
2016-01-15 12:43:06 +01:00
|
|
|
this.udpAddress = v2net.UDPDestination(this.config.Address, port)
|
2016-01-03 23:30:37 +01:00
|
|
|
this.udpConn = conn
|
2016-02-01 15:36:33 +00:00
|
|
|
this.udpServer = hub.NewUDPServer(this.packetDispatcher)
|
2016-01-04 08:40:24 +01:00
|
|
|
this.udpMutex.Unlock()
|
2015-09-22 23:50:05 +02:00
|
|
|
|
2016-01-03 23:30:37 +01:00
|
|
|
go this.AcceptPackets()
|
2015-09-22 23:50:05 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-01-03 23:30:37 +01:00
|
|
|
func (this *SocksServer) AcceptPackets() error {
|
|
|
|
for this.accepting {
|
2015-10-08 14:46:18 +02:00
|
|
|
buffer := alloc.NewBuffer()
|
2016-01-04 00:33:25 +01:00
|
|
|
this.udpMutex.RLock()
|
2016-01-03 23:30:37 +01:00
|
|
|
if !this.accepting {
|
2016-01-04 00:33:25 +01:00
|
|
|
this.udpMutex.RUnlock()
|
2016-01-03 23:30:37 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
nBytes, addr, err := this.udpConn.ReadFromUDP(buffer.Value)
|
2016-01-04 00:33:25 +01:00
|
|
|
this.udpMutex.RUnlock()
|
2015-09-22 23:50:05 +02:00
|
|
|
if err != nil {
|
2016-01-18 12:24:33 +01:00
|
|
|
log.Error("Socks: failed to read UDP packets: ", err)
|
2015-10-08 17:41:38 +02:00
|
|
|
buffer.Release()
|
2015-10-06 09:33:37 +02:00
|
|
|
continue
|
2015-09-22 23:50:05 +02:00
|
|
|
}
|
2016-01-18 12:24:33 +01:00
|
|
|
log.Info("Socks: Client UDP connection from ", addr)
|
2015-10-08 23:28:51 +02:00
|
|
|
request, err := protocol.ReadUDPRequest(buffer.Value[:nBytes])
|
2015-10-08 17:41:38 +02:00
|
|
|
buffer.Release()
|
2015-09-22 23:50:05 +02:00
|
|
|
if err != nil {
|
2016-01-18 12:24:33 +01:00
|
|
|
log.Error("Socks: failed to parse UDP request: ", err)
|
2015-11-03 18:20:28 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if request.Data == nil || request.Data.Len() == 0 {
|
2015-10-06 09:33:37 +02:00
|
|
|
continue
|
2015-09-22 23:50:05 +02:00
|
|
|
}
|
|
|
|
if request.Fragment != 0 {
|
2016-01-18 11:43:24 +01:00
|
|
|
log.Warning("Socks: Dropping fragmented UDP packets.")
|
2015-09-22 23:50:05 +02:00
|
|
|
// TODO handle fragments
|
2015-10-08 17:41:38 +02:00
|
|
|
request.Data.Release()
|
2015-09-22 23:50:05 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2015-10-02 15:32:26 +02:00
|
|
|
udpPacket := v2net.NewPacket(request.Destination(), request.Data, false)
|
2016-01-18 12:24:33 +01:00
|
|
|
log.Info("Socks: Send packet to ", udpPacket.Destination(), " with ", request.Data.Len(), " bytes")
|
2016-02-01 15:36:33 +00:00
|
|
|
this.udpServer.Dispatch(
|
|
|
|
v2net.UDPDestination(v2net.IPAddress(addr.IP), v2net.Port(addr.Port)), udpPacket,
|
|
|
|
func(packet v2net.Packet) {
|
|
|
|
response := &protocol.Socks5UDPRequest{
|
|
|
|
Fragment: 0,
|
|
|
|
Address: udpPacket.Destination().Address(),
|
|
|
|
Port: udpPacket.Destination().Port(),
|
|
|
|
Data: packet.Chunk(),
|
|
|
|
}
|
|
|
|
log.Info("Socks: Writing back UDP response with ", response.Data.Len(), " bytes to ", packet.Destination())
|
2015-09-28 21:32:07 +02:00
|
|
|
|
2016-02-01 15:36:33 +00:00
|
|
|
udpMessage := alloc.NewSmallBuffer().Clear()
|
|
|
|
response.Write(udpMessage)
|
2015-10-02 15:32:26 +02:00
|
|
|
|
2016-02-01 15:36:33 +00:00
|
|
|
this.udpMutex.RLock()
|
|
|
|
if !this.accepting {
|
|
|
|
this.udpMutex.RUnlock()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
nBytes, err := this.udpConn.WriteToUDP(udpMessage.Value, &net.UDPAddr{
|
|
|
|
IP: packet.Destination().Address().IP(),
|
|
|
|
Port: int(packet.Destination().Port()),
|
|
|
|
})
|
|
|
|
this.udpMutex.RUnlock()
|
|
|
|
udpMessage.Release()
|
|
|
|
response.Data.Release()
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Socks: failed to write UDP message (", nBytes, " bytes) to ", packet.Destination(), ": ", err)
|
|
|
|
}
|
|
|
|
})
|
2015-09-28 21:32:07 +02:00
|
|
|
}
|
2016-02-01 15:36:33 +00:00
|
|
|
return nil
|
2015-09-28 21:32:07 +02:00
|
|
|
}
|