1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-22 18:17:52 -05:00
v2fly/proxy/socks/udp.go

101 lines
2.7 KiB
Go
Raw Normal View History

package socks
import (
"net"
"github.com/v2ray/v2ray-core/common/alloc"
"github.com/v2ray/v2ray-core/common/log"
v2net "github.com/v2ray/v2ray-core/common/net"
"github.com/v2ray/v2ray-core/proxy/socks/protocol"
)
2015-12-02 15:44:01 -05:00
func (this *SocksServer) ListenUDP(port v2net.Port) error {
addr := &net.UDPAddr{
2015-10-06 05:57:26 -04:00
IP: net.IP{0, 0, 0, 0},
Port: int(port),
Zone: "",
}
conn, err := net.ListenUDP("udp", addr)
if err != nil {
log.Error("Socks failed to listen UDP on port %d: %v", port, err)
return err
}
2016-01-04 02:40:24 -05:00
this.udpMutex.Lock()
this.udpAddress = v2net.UDPDestination(v2net.IPAddress(this.config.IP()), port)
this.udpConn = conn
2016-01-04 02:40:24 -05:00
this.udpMutex.Unlock()
go this.AcceptPackets()
return nil
}
func (this *SocksServer) AcceptPackets() error {
for this.accepting {
buffer := alloc.NewBuffer()
2016-01-03 18:33:25 -05:00
this.udpMutex.RLock()
if !this.accepting {
2016-01-03 18:33:25 -05:00
this.udpMutex.RUnlock()
return nil
}
nBytes, addr, err := this.udpConn.ReadFromUDP(buffer.Value)
2016-01-03 18:33:25 -05:00
this.udpMutex.RUnlock()
if err != nil {
log.Error("Socks failed to read UDP packets: %v", err)
2015-10-08 11:41:38 -04:00
buffer.Release()
2015-10-06 03:33:37 -04:00
continue
}
2015-10-06 11:24:57 -04:00
log.Info("Client UDP connection from %v", addr)
2015-10-08 17:28:51 -04:00
request, err := protocol.ReadUDPRequest(buffer.Value[:nBytes])
2015-10-08 11:41:38 -04:00
buffer.Release()
if err != nil {
log.Error("Socks failed to parse UDP request: %v", err)
continue
}
if request.Data == nil || request.Data.Len() == 0 {
2015-10-06 03:33:37 -04:00
continue
}
if request.Fragment != 0 {
2015-10-10 11:30:37 -04:00
log.Warning("Dropping fragmented UDP packets.")
// TODO handle fragments
2015-10-08 11:41:38 -04:00
request.Data.Release()
continue
}
2015-10-02 09:32:26 -04:00
udpPacket := v2net.NewPacket(request.Destination(), request.Data, false)
log.Info("Send packet to %s with %d bytes", udpPacket.Destination().String(), request.Data.Len())
go this.handlePacket(udpPacket, addr, request.Address, request.Port)
}
return nil
}
2015-09-28 15:32:07 -04:00
func (this *SocksServer) handlePacket(packet v2net.Packet, clientAddr *net.UDPAddr, targetAddr v2net.Address, port v2net.Port) {
2015-12-05 16:55:45 -05:00
ray := this.space.PacketDispatcher().DispatchToOutbound(packet)
2015-10-02 09:32:26 -04:00
close(ray.InboundInput())
2015-10-14 03:56:04 -04:00
for data := range ray.InboundOutput() {
2015-10-06 11:24:57 -04:00
response := &protocol.Socks5UDPRequest{
Fragment: 0,
Address: targetAddr,
2015-12-16 17:53:38 -05:00
Port: port,
2015-10-06 11:24:57 -04:00
Data: data,
}
log.Info("Writing back UDP response with %d bytes from %s to %s", data.Len(), targetAddr.String(), clientAddr.String())
2015-10-08 17:29:06 -04:00
udpMessage := alloc.NewSmallBuffer().Clear()
response.Write(udpMessage)
2015-10-08 17:28:51 -04:00
2016-01-03 18:33:25 -05:00
this.udpMutex.RLock()
if !this.accepting {
2016-01-03 18:33:25 -05:00
this.udpMutex.RUnlock()
return
}
nBytes, err := this.udpConn.WriteToUDP(udpMessage.Value, clientAddr)
2016-01-03 18:33:25 -05:00
this.udpMutex.RUnlock()
2015-10-08 17:29:06 -04:00
udpMessage.Release()
response.Data.Release()
2015-10-05 10:59:56 -04:00
if err != nil {
log.Error("Socks failed to write UDP message (%d bytes) to %s: %v", nBytes, clientAddr.String(), err)
}
2015-09-28 15:32:07 -04:00
}
}