From bbf80541b15ab8aa996e4b9d6e923d7cd41dae8d Mon Sep 17 00:00:00 2001 From: V2Ray Date: Tue, 6 Oct 2015 16:44:09 +0200 Subject: [PATCH] Fix UDP address --- proxy/socks/udp.go | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/proxy/socks/udp.go b/proxy/socks/udp.go index c7bd1f037..a95d7201a 100644 --- a/proxy/socks/udp.go +++ b/proxy/socks/udp.go @@ -25,7 +25,8 @@ 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{0, 0, 0, 0}, port) + // TODO: make this configurable + udpAddress = v2net.IPAddress([]byte{127, 0, 0, 1}, port) go server.AcceptPackets(conn) return nil @@ -43,6 +44,7 @@ func (server *SocksServer) AcceptPackets(conn *net.UDPConn) error { 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) @@ -55,20 +57,22 @@ func (server *SocksServer) AcceptPackets(conn *net.UDPConn) error { } udpPacket := v2net.NewPacket(request.Destination(), request.Data, false) - go server.handlePacket(conn, udpPacket, addr) + 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) { +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: v2net.IPAddress(clientAddr.IP, uint16(clientAddr.Port)), + 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 {