2015-09-22 17:50:05 -04:00
|
|
|
package socks
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
|
2015-10-08 08:46:18 -04:00
|
|
|
"github.com/v2ray/v2ray-core/common/alloc"
|
2015-09-22 17:50:05 -04:00
|
|
|
"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
|
|
|
|
)
|
|
|
|
|
2015-10-03 18:21:06 -04:00
|
|
|
var udpAddress v2net.Address
|
|
|
|
|
2015-09-22 17:50:05 -04:00
|
|
|
func (server *SocksServer) ListenUDP(port uint16) error {
|
|
|
|
addr := &net.UDPAddr{
|
2015-10-06 05:57:26 -04:00
|
|
|
IP: net.IP{0, 0, 0, 0},
|
2015-09-22 17:50:05 -04:00
|
|
|
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
|
|
|
|
}
|
2015-10-06 11:24:57 -04:00
|
|
|
// TODO: make this configurable
|
2015-10-06 10:44:09 -04:00
|
|
|
udpAddress = v2net.IPAddress([]byte{127, 0, 0, 1}, port)
|
2015-09-22 17:50:05 -04:00
|
|
|
|
|
|
|
go server.AcceptPackets(conn)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-10-03 18:21:06 -04:00
|
|
|
func (server *SocksServer) getUDPAddr() v2net.Address {
|
|
|
|
return udpAddress
|
|
|
|
}
|
|
|
|
|
2015-09-22 17:50:05 -04:00
|
|
|
func (server *SocksServer) AcceptPackets(conn *net.UDPConn) error {
|
|
|
|
for {
|
2015-10-08 08:46:18 -04:00
|
|
|
buffer := alloc.NewBuffer()
|
|
|
|
nBytes, addr, err := conn.ReadFromUDP(buffer.Value)
|
2015-09-22 17:50:05 -04:00
|
|
|
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-09-22 17:50:05 -04:00
|
|
|
}
|
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()
|
2015-09-22 17:50:05 -04:00
|
|
|
if err != nil {
|
|
|
|
log.Error("Socks failed to parse UDP request: %v", err)
|
2015-10-08 11:41:38 -04:00
|
|
|
request.Data.Release()
|
2015-10-06 03:33:37 -04:00
|
|
|
continue
|
2015-09-22 17:50:05 -04:00
|
|
|
}
|
|
|
|
if request.Fragment != 0 {
|
2015-10-10 11:30:37 -04:00
|
|
|
log.Warning("Dropping fragmented UDP packets.")
|
2015-09-22 17:50:05 -04:00
|
|
|
// TODO handle fragments
|
2015-10-08 11:41:38 -04:00
|
|
|
request.Data.Release()
|
2015-09-22 17:50:05 -04:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2015-10-02 09:32:26 -04:00
|
|
|
udpPacket := v2net.NewPacket(request.Destination(), request.Data, false)
|
2015-10-08 08:46:18 -04:00
|
|
|
log.Info("Send packet to %s with %d bytes", udpPacket.Destination().String(), request.Data.Len())
|
2015-10-06 11:24:39 -04:00
|
|
|
go server.handlePacket(conn, udpPacket, addr, request.Address)
|
2015-09-22 17:50:05 -04:00
|
|
|
}
|
|
|
|
}
|
2015-09-28 15:32:07 -04:00
|
|
|
|
2015-10-06 10:44:09 -04:00
|
|
|
func (server *SocksServer) handlePacket(conn *net.UDPConn, packet v2net.Packet, clientAddr *net.UDPAddr, targetAddr v2net.Address) {
|
2015-10-02 09:32:26 -04:00
|
|
|
ray := server.vPoint.DispatchToOutbound(packet)
|
|
|
|
close(ray.InboundInput())
|
|
|
|
|
|
|
|
if data, ok := <-ray.InboundOutput(); ok {
|
2015-10-06 11:24:57 -04:00
|
|
|
response := &protocol.Socks5UDPRequest{
|
|
|
|
Fragment: 0,
|
|
|
|
Address: targetAddr,
|
|
|
|
Data: data,
|
|
|
|
}
|
2015-10-08 08:46:18 -04:00
|
|
|
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
|
|
|
|
|
|
|
nBytes, err := conn.WriteToUDP(udpMessage.Value, clientAddr)
|
2015-10-08 17:29:06 -04:00
|
|
|
udpMessage.Release()
|
2015-10-08 08:46:18 -04:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|