1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-04 17:27:23 -05:00

Fix UDP address

This commit is contained in:
V2Ray 2015-10-06 16:44:09 +02:00
parent be59463fd0
commit bbf80541b1

View File

@ -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 {