From c87a61b5896c74823952018e2ce54e6504897152 Mon Sep 17 00:00:00 2001 From: Brandon Scott Date: Tue, 29 Jan 2019 20:06:57 -0600 Subject: [PATCH] Upgraded project --- .../Implementation/Socks4ProxyCore.cs | 422 +++++++++--------- .../Properties/Resources.Designer.cs | 4 +- .../Properties/Settings.Designer.cs | 4 +- ProxyServerSharp/ProxyServerSharp.csproj | 8 +- ProxyServerSharp/app.config | 3 + 5 files changed, 224 insertions(+), 217 deletions(-) create mode 100644 ProxyServerSharp/app.config diff --git a/ProxyServerSharp/Implementation/Socks4ProxyCore.cs b/ProxyServerSharp/Implementation/Socks4ProxyCore.cs index 3e1715c..1be9869 100644 --- a/ProxyServerSharp/Implementation/Socks4ProxyCore.cs +++ b/ProxyServerSharp/Implementation/Socks4ProxyCore.cs @@ -1,213 +1,213 @@ -using System; -using System.Collections.Generic; -using System.Net; -using System.Net.Sockets; -using System.Threading; -using ProxyServerSharp.Interfaces; - -namespace ProxyServerSharp.Implementation -{ - class Socks4ProxyCore: IProxyCore - { - private readonly int _port; - private readonly int _transferUnitSize; - - private Socket _serverSocket; - - private Thread _acceptThread; - private List _connections = - new List(); - - public Socks4ProxyCore(int port, int transferUnitSize) - { - _port = port; - _transferUnitSize = transferUnitSize; - } - - public event LocalConnectEventHandler LocalConnect; - public event LocalDisconnectEventHandler LocalDisconnect; - public event LocalSentEventHandler LocalSent; - public event LocalReceiveEventHandler LocalReceive; - public event RemoteConnectEventHandler RemoteConnect; - public event RemoteDisconnectEventHandler RemoteDisconnect; - public event RemoteSendEventHandler RemoteSend; - public event RemoteReceivedEventHandler RemoteReceive; - - public void Start() - { - SetupServerSocket(); - - _acceptThread = new Thread(AcceptConnections); - _acceptThread.IsBackground = true; - _acceptThread.Start(); - } - - public void Shutdown() - { - throw new NotImplementedException(); - } - - private void SetupServerSocket() - { - IPEndPoint myEndpoint = new IPEndPoint(IPAddress.Loopback, - _port); - - // Create the socket, bind it, and start listening - _serverSocket = new Socket(myEndpoint.Address.AddressFamily, - SocketType.Stream, ProtocolType.Tcp); - _serverSocket.Bind(myEndpoint); - _serverSocket.Listen((int)SocketOptionName.MaxConnections); - } - - private void AcceptConnections() - { - while (true) - { - // Accept a connection - ConnectionInfo connection = new ConnectionInfo(); - - Socket socket = _serverSocket.Accept(); - - connection.LocalSocket = socket; - connection.RemoteSocket = new Socket(AddressFamily.InterNetwork, - SocketType.Stream, ProtocolType.Tcp); - - // Create the thread for the receives. - connection.LocalThread = new Thread(ProcessLocalConnection); - connection.LocalThread.IsBackground = true; +using System; +using System.Collections.Generic; +using System.Net; +using System.Net.Sockets; +using System.Threading; +using ProxyServerSharp.Interfaces; + +namespace ProxyServerSharp.Implementation +{ + class Socks4ProxyCore: IProxyCore + { + private readonly int _port; + private readonly int _transferUnitSize; + + private Socket _serverSocket; + + private Thread _acceptThread; + private List _connections = + new List(); + + public Socks4ProxyCore(int port, int transferUnitSize) + { + _port = port; + _transferUnitSize = transferUnitSize; + } + + public event LocalConnectEventHandler LocalConnect; + public event LocalDisconnectEventHandler LocalDisconnect; + public event LocalSentEventHandler LocalSent; + public event LocalReceiveEventHandler LocalReceive; + public event RemoteConnectEventHandler RemoteConnect; + public event RemoteDisconnectEventHandler RemoteDisconnect; + public event RemoteSendEventHandler RemoteSend; + public event RemoteReceivedEventHandler RemoteReceive; + + public void Start() + { + SetupServerSocket(); + + _acceptThread = new Thread(AcceptConnections); + _acceptThread.IsBackground = true; + _acceptThread.Start(); + } + + public void Shutdown() + { + throw new NotImplementedException(); + } + + private void SetupServerSocket() + { + IPEndPoint myEndpoint = new IPEndPoint(IPAddress.Loopback, + _port); + + // Create the socket, bind it, and start listening + _serverSocket = new Socket(myEndpoint.Address.AddressFamily, + SocketType.Stream, ProtocolType.Tcp); + _serverSocket.Bind(myEndpoint); + _serverSocket.Listen((int)SocketOptionName.MaxConnections); + } + + private void AcceptConnections() + { + while (true) + { + // Accept a connection + ConnectionInfo connection = new ConnectionInfo(); + + Socket socket = _serverSocket.Accept(); + + connection.LocalSocket = socket; + connection.RemoteSocket = new Socket(AddressFamily.InterNetwork, + SocketType.Stream, ProtocolType.Tcp); + + // Create the thread for the receives. + connection.LocalThread = new Thread(ProcessLocalConnection); + connection.LocalThread.IsBackground = true; connection.LocalThread.Start(connection); - LocalConnect?.Invoke(this, (IPEndPoint)socket.RemoteEndPoint); - - // Store the socket - lock (_connections) _connections.Add(connection); - } - } - - private void ProcessLocalConnection(object state) - { - ConnectionInfo connection = (ConnectionInfo)state; - int bytesRead = 0; - - byte[] buffer = new byte[_transferUnitSize]; - try - { - // we are setting up the socks! - bytesRead = connection.LocalSocket.Receive(buffer); - - Console.WriteLine("ProcessLocalConnection::Receive bytesRead={0}", bytesRead); - for (int i = 0; i < bytesRead; i++) - Console.Write("{0:X2} ", buffer[i]); - Console.Write("\n"); - - if (bytesRead > 0) - { - if (buffer[0] == 0x04 && buffer[1] == 0x01) - { - int remotePort = buffer[2] << 8 | buffer[3]; - byte[] ipAddressBuffer = new byte[4]; - Buffer.BlockCopy(buffer, 4, ipAddressBuffer, 0, 4); - - IPEndPoint remoteEndPoint = new IPEndPoint(new IPAddress(ipAddressBuffer), remotePort); - - connection.RemoteSocket.Connect(remoteEndPoint); - if (connection.RemoteSocket.Connected) - { - Console.WriteLine("Connected to remote!"); - - RemoteConnect?.Invoke(this, remoteEndPoint); - - byte[] socksResponse = new byte[] { - 0x00, 0x5a, - buffer[2], buffer[3], // port - buffer[4], buffer[5], buffer[6], buffer[7] // IP - }; - connection.LocalSocket.Send(socksResponse); - - // Create the thread for the receives. - connection.RemoteThread = new Thread(ProcessRemoteConnection); - connection.RemoteThread.IsBackground = true; - connection.RemoteThread.Start(connection); - } - else - { - Console.WriteLine("Connection failed."); - byte[] socksResponse = new byte[] { - 0x04, - 0x5b, - buffer[2], buffer[3], // port - buffer[4], buffer[5], buffer[6], buffer[7] // IP - }; - connection.LocalSocket.Send(socksResponse); - return; - - } - } - } - else if (bytesRead == 0) return; - - // start receiving actual data - while (true) - { - bytesRead = connection.LocalSocket.Receive(buffer); - if (bytesRead == 0) { - Console.WriteLine("Local connection closed!"); - break; - } else { - connection.RemoteSocket.Send(buffer, bytesRead, SocketFlags.None); - } - } - } - catch (Exception exc) - { - Console.WriteLine("Exception: " + exc); - } - finally - { - Console.WriteLine("ProcessLocalConnection Cleaning up..."); - connection.LocalSocket.Close(); - connection.RemoteSocket.Close(); - lock (_connections) _connections.Remove(connection); - } - } - - private void ProcessRemoteConnection(object state) - { - ConnectionInfo connection = (ConnectionInfo)state; - int bytesRead = 0; - - byte[] buffer = new byte[_transferUnitSize]; - try - { - // start receiving actual data - while (true) - { - bytesRead = connection.RemoteSocket.Receive(buffer); - if (bytesRead == 0) { - Console.WriteLine("Remote connection closed!"); - break; - } else { - connection.LocalSocket.Send(buffer, bytesRead, SocketFlags.None); - } - } - } - catch (SocketException exc) - { - Console.WriteLine("Socket exception: " + exc.SocketErrorCode); - } - catch (Exception exc) - { - Console.WriteLine("Exception: " + exc); - } - finally - { - Console.WriteLine("ProcessRemoteConnection Cleaning up..."); - connection.LocalSocket.Close(); - connection.RemoteSocket.Close(); - lock (_connections) - _connections.Remove(connection); - } - } - } - -} + LocalConnect?.Invoke(this, (IPEndPoint)socket.RemoteEndPoint); + + // Store the socket + lock (_connections) _connections.Add(connection); + } + } + + private void ProcessLocalConnection(object state) + { + ConnectionInfo connection = (ConnectionInfo)state; + int bytesRead = 0; + + byte[] buffer = new byte[_transferUnitSize]; + try + { + // we are setting up the socks! + bytesRead = connection.LocalSocket.Receive(buffer); + + Console.WriteLine("ProcessLocalConnection::Receive bytesRead={0}", bytesRead); + for (int i = 0; i < bytesRead; i++) + Console.Write("{0:X2} ", buffer[i]); + Console.Write("\n"); + + if (bytesRead > 0) + { + if (buffer[0] == 0x04 && buffer[1] == 0x01) + { + int remotePort = buffer[2] << 8 | buffer[3]; + byte[] ipAddressBuffer = new byte[4]; + Buffer.BlockCopy(buffer, 4, ipAddressBuffer, 0, 4); + + IPEndPoint remoteEndPoint = new IPEndPoint(new IPAddress(ipAddressBuffer), remotePort); + + connection.RemoteSocket.Connect(remoteEndPoint); + if (connection.RemoteSocket.Connected) + { + Console.WriteLine("Connected to remote!"); + + RemoteConnect?.Invoke(this, remoteEndPoint); + + byte[] socksResponse = new byte[] { + 0x00, 0x5a, + buffer[2], buffer[3], // port + buffer[4], buffer[5], buffer[6], buffer[7] // IP + }; + connection.LocalSocket.Send(socksResponse); + + // Create the thread for the receives. + connection.RemoteThread = new Thread(ProcessRemoteConnection); + connection.RemoteThread.IsBackground = true; + connection.RemoteThread.Start(connection); + } + else + { + Console.WriteLine("Connection failed."); + byte[] socksResponse = new byte[] { + 0x04, + 0x5b, + buffer[2], buffer[3], // port + buffer[4], buffer[5], buffer[6], buffer[7] // IP + }; + connection.LocalSocket.Send(socksResponse); + return; + + } + } + } + else if (bytesRead == 0) return; + + // start receiving actual data + while (true) + { + bytesRead = connection.LocalSocket.Receive(buffer); + if (bytesRead == 0) { + Console.WriteLine("Local connection closed!"); + break; + } else { + connection.RemoteSocket.Send(buffer, bytesRead, SocketFlags.None); + } + } + } + catch (Exception exc) + { + Console.WriteLine("Exception: " + exc); + } + finally + { + Console.WriteLine("ProcessLocalConnection Cleaning up..."); + connection.LocalSocket.Close(); + connection.RemoteSocket.Close(); + lock (_connections) _connections.Remove(connection); + } + } + + private void ProcessRemoteConnection(object state) + { + ConnectionInfo connection = (ConnectionInfo)state; + int bytesRead = 0; + + byte[] buffer = new byte[_transferUnitSize]; + try + { + // start receiving actual data + while (true) + { + bytesRead = connection.RemoteSocket.Receive(buffer); + if (bytesRead == 0) { + Console.WriteLine("Remote connection closed!"); + break; + } else { + connection.LocalSocket.Send(buffer, bytesRead, SocketFlags.None); + } + } + } + catch (SocketException exc) + { + Console.WriteLine("Socket exception: " + exc.SocketErrorCode); + } + catch (Exception exc) + { + Console.WriteLine("Exception: " + exc); + } + finally + { + Console.WriteLine("ProcessRemoteConnection Cleaning up..."); + connection.LocalSocket.Close(); + connection.RemoteSocket.Close(); + lock (_connections) + _connections.Remove(connection); + } + } + } + +} diff --git a/ProxyServerSharp/Properties/Resources.Designer.cs b/ProxyServerSharp/Properties/Resources.Designer.cs index 1b81cf9..b975fbb 100644 --- a/ProxyServerSharp/Properties/Resources.Designer.cs +++ b/ProxyServerSharp/Properties/Resources.Designer.cs @@ -1,7 +1,7 @@ //------------------------------------------------------------------------------ // // This code was generated by a tool. -// Runtime Version:4.0.30319.34209 +// Runtime Version:4.0.30319.42000 // // Changes to this file may cause incorrect behavior and will be lost if // the code is regenerated. @@ -19,7 +19,7 @@ namespace ProxyServerSharp.Properties { // class via a tool like ResGen or Visual Studio. // To add or remove a member, edit your .ResX file then rerun ResGen // with the /str option, or rebuild your VS project. - [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")] + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "15.0.0.0")] [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] internal class Resources { diff --git a/ProxyServerSharp/Properties/Settings.Designer.cs b/ProxyServerSharp/Properties/Settings.Designer.cs index d848422..fe9d43b 100644 --- a/ProxyServerSharp/Properties/Settings.Designer.cs +++ b/ProxyServerSharp/Properties/Settings.Designer.cs @@ -1,7 +1,7 @@ //------------------------------------------------------------------------------ // // This code was generated by a tool. -// Runtime Version:4.0.30319.34209 +// Runtime Version:4.0.30319.42000 // // Changes to this file may cause incorrect behavior and will be lost if // the code is regenerated. @@ -12,7 +12,7 @@ namespace ProxyServerSharp.Properties { [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] - [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "11.0.0.0")] + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "15.8.0.0")] internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase { private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings()))); diff --git a/ProxyServerSharp/ProxyServerSharp.csproj b/ProxyServerSharp/ProxyServerSharp.csproj index 755493f..4c0b8e0 100644 --- a/ProxyServerSharp/ProxyServerSharp.csproj +++ b/ProxyServerSharp/ProxyServerSharp.csproj @@ -1,5 +1,5 @@  - + Debug AnyCPU @@ -10,13 +10,14 @@ Properties ProxyServerSharp ProxyServerSharp - v2.0 + v4.7.1 512 3.5 + true @@ -26,6 +27,7 @@ DEBUG;TRACE prompt 4 + false pdbonly @@ -34,6 +36,7 @@ TRACE prompt 4 + false @@ -67,6 +70,7 @@ Resources.resx True + SettingsSingleFileGenerator Settings.Designer.cs diff --git a/ProxyServerSharp/app.config b/ProxyServerSharp/app.config new file mode 100644 index 0000000..d7c60fd --- /dev/null +++ b/ProxyServerSharp/app.config @@ -0,0 +1,3 @@ + + +