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