From af8baa144d48b1e7456e76b053271c6242a0a131 Mon Sep 17 00:00:00 2001 From: V2Ray Date: Tue, 6 Oct 2015 11:57:26 +0200 Subject: [PATCH] Update socks implementation --- proxy/socks/socks.go | 14 +++++++++----- proxy/socks/udp.go | 16 ++++++++++------ 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/proxy/socks/socks.go b/proxy/socks/socks.go index 327f85ea..e2cb4507 100644 --- a/proxy/socks/socks.go +++ b/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 } diff --git a/proxy/socks/udp.go b/proxy/socks/udp.go index 2174e5a0..c7bd1f03 100644 --- a/proxy/socks/udp.go +++ b/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)