/// <summary> /// Processes bytes received from client /// </summary> /// <param name="bytes">Bytes to process and react to.</param> /// <returns>Returns true on success, false on failure (with error in Error property)</returns> public bool Process(MemoryStream stream, NetworkStream connection) { if (State == SOCKS5States.Disconnected) { // SOCKS5 HANDSHAKE var packet = new SOCKS5.PacketHandshake(stream); if (packet.METHODS != 0x0) throw new UDProxyException("Authentication is not yet supported!", new NotImplementedException()); UDProxy.DebugInfo("SOCKS5", "Responding to handshake request"); if ( Respond(new PacketReplyHandshake(), connection) ) { State = SOCKS5States.Connected; return true; } } else if (State == SOCKS5States.Connected) { // SOCKS5 Connection Requests // We're connected, so we're dealing with requests var packet = new SOCKS5.PacketRequest(stream); PacketReplyRequest reply = new PacketReplyRequest(packet); // Set our client's remote endpoint expected port to the one it's given us // Weird type casting is nessecary for safe translation MyClient.Port = (int)BitConverter.ToUInt16(packet.PORT, 0); // It's not ok :( if (reply.REP != SOCKS5Protocol.REP.OK) { Respond(reply, connection); return false; } switch (packet.CMD) { case SOCKS5Protocol.CMD.BIND: throw new UDProxyException("CMD BIND not yet supported!"); case SOCKS5Protocol.CMD.CONNECT: throw new UDProxyException("CMD CONNECT not yet supported!"); case SOCKS5Protocol.CMD.UDP_ASSOC: UDPConnection.TryBegin(ref reply, MyClient); Respond(reply, connection); break; } } else { throw new UDProxyException("Unknown error (unimplemented function?)"); } return false; }
public static void TryBegin(ref PacketReplyRequest reply, IPEndPoint client) { // Housekeeping if (MyThread != null) { MyThread.Abort(); } if (MyUDP != null) { MyUDP.Close(); } TotalUp = 0; TotalDown = 0; try { // Set our client's expected IP and Port from SOCKS5 MyClient = client; if (MyClient.Port == 0) { DebugWarn("Client's port is 0, going to have to guess source port (normal with V2/V3 viewers)."); hasClientPort = false; } // Get target's IP and ports from config (less lag) ExternalIP = (IPAddress)UDProxy.gConfiguration.Config["MyExternalIP"]; TargetIP = (IPAddress)UDProxy.gConfiguration.Config["TargetIP"]; TargetPorts = (List <ushort>)UDProxy.gConfiguration.Config["TargetPorts"]; MyUDP = new UdpClient((ushort)UDProxy.gConfiguration.Config["ListenPort"]); MyUDP.AllowNatTraversal(false); MyUDP.DontFragment = true; MyUDP.EnableBroadcast = true; MyThread = new Thread(new ThreadStart(StreamLoop)); } catch (SocketException e) { reply.REP = SOCKS5Protocol.REP.GENERAL_FAILURE; throw new UDProxyException("Socket failure: " + e.Message); } DebugInfo("UDP Endpoint has been set up."); MyThread.Start(); }
public static void TryBegin(ref PacketReplyRequest reply, IPEndPoint client) { // Housekeeping if (MyThread != null) MyThread.Abort(); if (MyUDP != null) MyUDP.Close(); TotalUp = 0; TotalDown = 0; try { // Set our client's expected IP and Port from SOCKS5 MyClient = client; if (MyClient.Port == 0) { DebugWarn("Client's port is 0, going to have to guess source port (normal with V2/V3 viewers)."); hasClientPort = false; } // Get target's IP and ports from config (less lag) ExternalIP = (IPAddress)UDProxy.gConfiguration.Config["MyExternalIP"]; TargetIP = (IPAddress)UDProxy.gConfiguration.Config["TargetIP"]; TargetPorts = (List<ushort>)UDProxy.gConfiguration.Config["TargetPorts"]; MyUDP = new UdpClient( (ushort)UDProxy.gConfiguration.Config["ListenPort"] ); MyUDP.AllowNatTraversal(false); MyUDP.DontFragment = true; MyUDP.EnableBroadcast = true; MyThread = new Thread(new ThreadStart(StreamLoop)); } catch (SocketException e) { reply.REP = SOCKS5Protocol.REP.GENERAL_FAILURE; throw new UDProxyException("Socket failure: " + e.Message); } DebugInfo("UDP Endpoint has been set up."); MyThread.Start(); }