|
|
@ -1,8 +1,6 @@
|
|
|
|
package socks
|
|
|
|
package socks
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
"github.com/v2ray/v2ray-core/common/alloc"
|
|
|
|
"github.com/v2ray/v2ray-core/common/alloc"
|
|
|
|
"github.com/v2ray/v2ray-core/common/log"
|
|
|
|
"github.com/v2ray/v2ray-core/common/log"
|
|
|
|
v2net "github.com/v2ray/v2ray-core/common/net"
|
|
|
|
v2net "github.com/v2ray/v2ray-core/common/net"
|
|
|
@ -11,63 +9,42 @@ import (
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
func (this *SocksServer) ListenUDP(port v2net.Port) error {
|
|
|
|
func (this *SocksServer) ListenUDP(port v2net.Port) error {
|
|
|
|
addr := &net.UDPAddr{
|
|
|
|
this.udpServer = hub.NewUDPServer(this.packetDispatcher)
|
|
|
|
IP: net.IP{0, 0, 0, 0},
|
|
|
|
udpHub, err := hub.ListenUDP(port, this.handleUDPPayload)
|
|
|
|
Port: int(port),
|
|
|
|
|
|
|
|
Zone: "",
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
conn, err := net.ListenUDP("udp", addr)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Socks: failed to listen UDP on port ", port, ": ", err)
|
|
|
|
log.Error("Socks: Failed to listen on udp port ", port)
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.udpMutex.Lock()
|
|
|
|
this.udpMutex.Lock()
|
|
|
|
this.udpAddress = v2net.UDPDestination(this.config.Address, port)
|
|
|
|
this.udpAddress = v2net.UDPDestination(this.config.Address, port)
|
|
|
|
this.udpConn = conn
|
|
|
|
this.udpHub = udpHub
|
|
|
|
this.udpServer = hub.NewUDPServer(this.packetDispatcher)
|
|
|
|
|
|
|
|
this.udpMutex.Unlock()
|
|
|
|
this.udpMutex.Unlock()
|
|
|
|
|
|
|
|
|
|
|
|
go this.AcceptPackets()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (this *SocksServer) AcceptPackets() error {
|
|
|
|
func (this *SocksServer) handleUDPPayload(payload *alloc.Buffer, source v2net.Destination) {
|
|
|
|
for this.accepting {
|
|
|
|
log.Info("Socks: Client UDP connection from ", source)
|
|
|
|
buffer := alloc.NewBuffer()
|
|
|
|
request, err := protocol.ReadUDPRequest(payload.Value)
|
|
|
|
this.udpMutex.RLock()
|
|
|
|
payload.Release()
|
|
|
|
if !this.accepting {
|
|
|
|
|
|
|
|
this.udpMutex.RUnlock()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
nBytes, addr, err := this.udpConn.ReadFromUDP(buffer.Value)
|
|
|
|
|
|
|
|
this.udpMutex.RUnlock()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
log.Error("Socks: failed to read UDP packets: ", err)
|
|
|
|
|
|
|
|
buffer.Release()
|
|
|
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Info("Socks: Client UDP connection from ", addr)
|
|
|
|
|
|
|
|
request, err := protocol.ReadUDPRequest(buffer.Value[:nBytes])
|
|
|
|
|
|
|
|
buffer.Release()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Socks: failed to parse UDP request: ", err)
|
|
|
|
log.Error("Socks: Failed to parse UDP request: ", err)
|
|
|
|
continue
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if request.Data == nil || request.Data.Len() == 0 {
|
|
|
|
if request.Data.Len() == 0 {
|
|
|
|
continue
|
|
|
|
request.Data.Release()
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if request.Fragment != 0 {
|
|
|
|
if request.Fragment != 0 {
|
|
|
|
log.Warning("Socks: Dropping fragmented UDP packets.")
|
|
|
|
log.Warning("Socks: Dropping fragmented UDP packets.")
|
|
|
|
// TODO handle fragments
|
|
|
|
// TODO handle fragments
|
|
|
|
request.Data.Release()
|
|
|
|
request.Data.Release()
|
|
|
|
continue
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
udpPacket := v2net.NewPacket(request.Destination(), request.Data, false)
|
|
|
|
udpPacket := v2net.NewPacket(request.Destination(), request.Data, false)
|
|
|
|
log.Info("Socks: Send packet to ", udpPacket.Destination(), " with ", request.Data.Len(), " bytes")
|
|
|
|
log.Info("Socks: Send packet to ", udpPacket.Destination(), " with ", request.Data.Len(), " bytes")
|
|
|
|
this.udpServer.Dispatch(
|
|
|
|
this.udpServer.Dispatch(source, udpPacket, func(packet v2net.Packet) {
|
|
|
|
v2net.UDPDestination(v2net.IPAddress(addr.IP), v2net.Port(addr.Port)), udpPacket,
|
|
|
|
|
|
|
|
func(packet v2net.Packet) {
|
|
|
|
|
|
|
|
response := &protocol.Socks5UDPRequest{
|
|
|
|
response := &protocol.Socks5UDPRequest{
|
|
|
|
Fragment: 0,
|
|
|
|
Fragment: 0,
|
|
|
|
Address: udpPacket.Destination().Address(),
|
|
|
|
Address: udpPacket.Destination().Address(),
|
|
|
@ -84,10 +61,7 @@ func (this *SocksServer) AcceptPackets() error {
|
|
|
|
this.udpMutex.RUnlock()
|
|
|
|
this.udpMutex.RUnlock()
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
nBytes, err := this.udpConn.WriteToUDP(udpMessage.Value, &net.UDPAddr{
|
|
|
|
nBytes, err := this.udpHub.WriteTo(udpMessage.Value, packet.Destination())
|
|
|
|
IP: packet.Destination().Address().IP(),
|
|
|
|
|
|
|
|
Port: int(packet.Destination().Port()),
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
this.udpMutex.RUnlock()
|
|
|
|
this.udpMutex.RUnlock()
|
|
|
|
udpMessage.Release()
|
|
|
|
udpMessage.Release()
|
|
|
|
response.Data.Release()
|
|
|
|
response.Data.Release()
|
|
|
@ -95,6 +69,4 @@ func (this *SocksServer) AcceptPackets() error {
|
|
|
|
log.Error("Socks: failed to write UDP message (", nBytes, " bytes) to ", packet.Destination(), ": ", err)
|
|
|
|
log.Error("Socks: failed to write UDP message (", nBytes, " bytes) to ", packet.Destination(), ": ", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|