mirror of
https://github.com/v2fly/v2ray-core.git
synced 2024-09-06 03:54:22 -04:00
83 lines
2.3 KiB
Go
83 lines
2.3 KiB
Go
package socks
|
|
|
|
import (
|
|
"net"
|
|
|
|
"github.com/v2ray/v2ray-core/common/log"
|
|
v2net "github.com/v2ray/v2ray-core/common/net"
|
|
"github.com/v2ray/v2ray-core/proxy/socks/protocol"
|
|
)
|
|
|
|
const (
|
|
bufferSize = 2 * 1024
|
|
)
|
|
|
|
var udpAddress v2net.Address
|
|
|
|
func (server *SocksServer) ListenUDP(port uint16) error {
|
|
addr := &net.UDPAddr{
|
|
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
|
|
}
|
|
// TODO: make this configurable
|
|
udpAddress = v2net.IPAddress([]byte{127, 0, 0, 1}, port)
|
|
|
|
go server.AcceptPackets(conn)
|
|
return nil
|
|
}
|
|
|
|
func (server *SocksServer) getUDPAddr() v2net.Address {
|
|
return udpAddress
|
|
}
|
|
|
|
func (server *SocksServer) AcceptPackets(conn *net.UDPConn) error {
|
|
for {
|
|
buffer := make([]byte, bufferSize)
|
|
nBytes, addr, err := conn.ReadFromUDP(buffer)
|
|
if err != nil {
|
|
log.Error("Socks failed to read UDP packets: %v", err)
|
|
continue
|
|
}
|
|
log.Info("Client UDP connection from %v", addr)
|
|
request, err := protocol.ReadUDPRequest(buffer[:nBytes])
|
|
if err != nil {
|
|
log.Error("Socks failed to parse UDP request: %v", err)
|
|
continue
|
|
}
|
|
if request.Fragment != 0 {
|
|
log.Warning("Dropping framented UDP packets.")
|
|
// TODO handle fragments
|
|
continue
|
|
}
|
|
|
|
udpPacket := v2net.NewPacket(request.Destination(), request.Data, false)
|
|
log.Info("Send packet to %s with %d bytes", udpPacket.Destination().String(), len(request.Data))
|
|
go server.handlePacket(conn, udpPacket, addr, v2net.IPAddress(request.Address.IP(), request.Address.Port()))
|
|
}
|
|
}
|
|
|
|
func (server *SocksServer) handlePacket(conn *net.UDPConn, packet v2net.Packet, clientAddr *net.UDPAddr, targetAddr v2net.Address) {
|
|
ray := server.vPoint.DispatchToOutbound(packet)
|
|
close(ray.InboundInput())
|
|
|
|
if data, ok := <-ray.InboundOutput(); ok {
|
|
response := &protocol.Socks5UDPRequest {
|
|
Fragment: 0,
|
|
Address: targetAddr,
|
|
Data: data,
|
|
}
|
|
log.Info("Writing back UDP response with %d bytes from %s to %s", len(data), targetAddr.String(), clientAddr.String())
|
|
udpMessage := response.Bytes(nil)
|
|
nBytes, err := conn.WriteToUDP(udpMessage, clientAddr)
|
|
if err != nil {
|
|
log.Error("Socks failed to write UDP message (%d bytes) to %s: %v", nBytes, clientAddr.String(), err)
|
|
}
|
|
}
|
|
}
|