Browse Source

Update socks implementation

pull/40/head
V2Ray 9 years ago
parent
commit
af8baa144d
  1. 14
      proxy/socks/socks.go
  2. 16
      proxy/socks/udp.go

14
proxy/socks/socks.go

@ -5,6 +5,7 @@ import (
"net"
"strconv"
"sync"
"time"
"github.com/v2ray/v2ray-core"
"github.com/v2ray/v2ray-core/common/errors"
@ -148,12 +149,12 @@ func (server *SocksServer) handleSocks5(reader *v2net.TimeOutReader, writer io.W
response.Error = protocol.ErrorSuccess
// Some SOCKS software requires a value other than dest. Let's fake one:
response.Port = uint16(38294)
response.Port = uint16(1717)
response.AddrType = protocol.AddrTypeIPv4
response.IPv4[0] = 127
response.IPv4[0] = 0
response.IPv4[1] = 0
response.IPv4[2] = 0
response.IPv4[3] = 1
response.IPv4[3] = 0
err = protocol.WriteResponse(writer, response)
if err != nil {
@ -197,8 +198,11 @@ func (server *SocksServer) handleUDP(reader *v2net.TimeOutReader, writer io.Writ
}
reader.SetTimeOut(300) /* 5 minutes */
buffer := make([]byte, 1024)
reader.Read(buffer)
v2net.ReadFrom(reader) // Just in case of anything left in the socket
// The TCP connection closes after this method returns. We need to wait until
// the client closes it.
// TODO: get notified from UDP part
<-time.After(5 * time.Minute)
return nil
}

16
proxy/socks/udp.go

@ -16,7 +16,7 @@ var udpAddress v2net.Address
func (server *SocksServer) ListenUDP(port uint16) error {
addr := &net.UDPAddr{
IP: net.IP{127, 0, 0, 1},
IP: net.IP{0, 0, 0, 0},
Port: int(port),
Zone: "",
}
@ -25,7 +25,7 @@ func (server *SocksServer) ListenUDP(port uint16) error {
log.Error("Socks failed to listen UDP on port %d: %v", port, err)
return err
}
udpAddress = v2net.IPAddress([]byte{127, 0, 0, 1}, port)
udpAddress = v2net.IPAddress([]byte{0, 0, 0, 0}, port)
go server.AcceptPackets(conn)
return nil
@ -55,17 +55,21 @@ func (server *SocksServer) AcceptPackets(conn *net.UDPConn) error {
}
udpPacket := v2net.NewPacket(request.Destination(), request.Data, false)
go server.handlePacket(conn, udpPacket, addr, request)
go server.handlePacket(conn, udpPacket, addr)
}
}
func (server *SocksServer) handlePacket(conn *net.UDPConn, packet v2net.Packet, clientAddr *net.UDPAddr, request protocol.Socks5UDPRequest) {
func (server *SocksServer) handlePacket(conn *net.UDPConn, packet v2net.Packet, clientAddr *net.UDPAddr) {
ray := server.vPoint.DispatchToOutbound(packet)
close(ray.InboundInput())
if data, ok := <-ray.InboundOutput(); ok {
request.Data = data
udpMessage := request.Bytes(nil)
response := &protocol.Socks5UDPRequest {
Fragment: 0,
Address: v2net.IPAddress(clientAddr.IP, uint16(clientAddr.Port)),
Data: data,
}
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)

Loading…
Cancel
Save