From caff2c3c25006baf02e0894cb04fcbe5c650ebf2 Mon Sep 17 00:00:00 2001 From: Brandon Scott Date: Tue, 29 Jan 2019 19:56:53 -0600 Subject: [PATCH] Worked on core interface, seperated out ConnectionInfo class, added more event handlers. --- .../Implementation/ConnectionInfo.cs | 16 ++ .../Socks4ProxyCore.cs} | 54 +++--- ProxyServerSharp/Interfaces/IProxyCore.cs | 30 ++++ ProxyServerSharp/MainForm.cs | 12 +- ProxyServerSharp/ProxyServerSharp.csproj | 162 +++++++++--------- 5 files changed, 162 insertions(+), 112 deletions(-) create mode 100644 ProxyServerSharp/Implementation/ConnectionInfo.cs rename ProxyServerSharp/{Core/SOCKS4Server.cs => Implementation/Socks4ProxyCore.cs} (87%) create mode 100644 ProxyServerSharp/Interfaces/IProxyCore.cs diff --git a/ProxyServerSharp/Implementation/ConnectionInfo.cs b/ProxyServerSharp/Implementation/ConnectionInfo.cs new file mode 100644 index 0000000..e00b23f --- /dev/null +++ b/ProxyServerSharp/Implementation/ConnectionInfo.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Net.Sockets; +using System.Text; +using System.Threading; + +namespace ProxyServerSharp.Implementation +{ + public class ConnectionInfo + { + public Socket LocalSocket { get; set; } + public Thread LocalThread { get; set; } + public Socket RemoteSocket { get; set; } + public Thread RemoteThread { get; set; } + } +} diff --git a/ProxyServerSharp/Core/SOCKS4Server.cs b/ProxyServerSharp/Implementation/Socks4ProxyCore.cs similarity index 87% rename from ProxyServerSharp/Core/SOCKS4Server.cs rename to ProxyServerSharp/Implementation/Socks4ProxyCore.cs index 19d5c3f..3e1715c 100644 --- a/ProxyServerSharp/Core/SOCKS4Server.cs +++ b/ProxyServerSharp/Implementation/Socks4ProxyCore.cs @@ -1,41 +1,38 @@ using System; using System.Collections.Generic; -using System.Text; -using System.Threading; -using System.Net.Sockets; using System.Net; +using System.Net.Sockets; +using System.Threading; +using ProxyServerSharp.Interfaces; -namespace ProxyServerSharp +namespace ProxyServerSharp.Implementation { - class ConnectionInfo + class Socks4ProxyCore: IProxyCore { - public Socket LocalSocket; - public Thread LocalThread; - public Socket RemoteSocket; - public Thread RemoteThread; - } + private readonly int _port; + private readonly int _transferUnitSize; - public delegate void ConnectEventHandler(object sender, IPEndPoint iep); - public delegate void ConnectionLogHandler(object sender, int code, string message); - - class SOCKS4Server - { private Socket _serverSocket; - private int _port; - private int _transferUnitSize; + private Thread _acceptThread; private List _connections = new List(); - public event ConnectEventHandler LocalConnect; - public event ConnectEventHandler RemoteConnect; - - public SOCKS4Server(int port, int transferUnitSize) + 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(); @@ -45,6 +42,11 @@ namespace ProxyServerSharp _acceptThread.Start(); } + public void Shutdown() + { + throw new NotImplementedException(); + } + private void SetupServerSocket() { IPEndPoint myEndpoint = new IPEndPoint(IPAddress.Loopback, @@ -73,10 +75,9 @@ namespace ProxyServerSharp // Create the thread for the receives. connection.LocalThread = new Thread(ProcessLocalConnection); connection.LocalThread.IsBackground = true; - connection.LocalThread.Start(connection); - - if (LocalConnect != null) - LocalConnect(this, (IPEndPoint)socket.RemoteEndPoint); + connection.LocalThread.Start(connection); + + LocalConnect?.Invoke(this, (IPEndPoint)socket.RemoteEndPoint); // Store the socket lock (_connections) _connections.Add(connection); @@ -114,8 +115,7 @@ namespace ProxyServerSharp { Console.WriteLine("Connected to remote!"); - if (RemoteConnect != null) - RemoteConnect(this, remoteEndPoint); + RemoteConnect?.Invoke(this, remoteEndPoint); byte[] socksResponse = new byte[] { 0x00, 0x5a, diff --git a/ProxyServerSharp/Interfaces/IProxyCore.cs b/ProxyServerSharp/Interfaces/IProxyCore.cs new file mode 100644 index 0000000..8afbce0 --- /dev/null +++ b/ProxyServerSharp/Interfaces/IProxyCore.cs @@ -0,0 +1,30 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace ProxyServerSharp.Interfaces +{ + public delegate void LocalConnectEventHandler(object sender, System.Net.IPEndPoint iep); + public delegate void LocalDisconnectEventHandler(object sender); + public delegate void LocalSentEventHandler(object sender); + public delegate void LocalReceiveEventHandler(object sender); + public delegate void RemoteConnectEventHandler(object sender, System.Net.IPEndPoint iep); + public delegate void RemoteDisconnectEventHandler(object sender); + public delegate void RemoteSendEventHandler(object sender); + public delegate void RemoteReceivedEventHandler(object sender); + + public interface IProxyCore + { + event LocalConnectEventHandler LocalConnect; + event LocalDisconnectEventHandler LocalDisconnect; + event LocalSentEventHandler LocalSent; + event LocalReceiveEventHandler LocalReceive; + event RemoteConnectEventHandler RemoteConnect; + event RemoteDisconnectEventHandler RemoteDisconnect; + event RemoteSendEventHandler RemoteSend; + event RemoteReceivedEventHandler RemoteReceive; + + void Start(); + void Shutdown(); + } +} diff --git a/ProxyServerSharp/MainForm.cs b/ProxyServerSharp/MainForm.cs index f6b5e56..eb1d8fa 100644 --- a/ProxyServerSharp/MainForm.cs +++ b/ProxyServerSharp/MainForm.cs @@ -5,12 +5,14 @@ using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; +using ProxyServerSharp.Implementation; +using ProxyServerSharp.Interfaces; namespace ProxyServerSharp { public partial class MainForm : Form { - SOCKS4Server server; + Socks4ProxyCore _proxyCore; public MainForm() { @@ -51,11 +53,11 @@ namespace ProxyServerSharp { if (startProxyButton.Text == "Start") { - server = new SOCKS4Server(int.Parse(portTextBox.Text), + _proxyCore = new Socks4ProxyCore(int.Parse(portTextBox.Text), int.Parse((string)transferUnitSizeComboBox.SelectedItem)); - server.LocalConnect += new ConnectEventHandler(server_LocalConnect); - server.RemoteConnect += new ConnectEventHandler(server_RemoteConnect); - server.Start(); + _proxyCore.LocalConnect += new LocalConnectEventHandler(server_LocalConnect); + _proxyCore.RemoteConnect += new RemoteConnectEventHandler(server_RemoteConnect); + _proxyCore.Start(); statusLabel.Text = "Started"; startProxyButton.Text = "Stop"; } diff --git a/ProxyServerSharp/ProxyServerSharp.csproj b/ProxyServerSharp/ProxyServerSharp.csproj index 33ede24..755493f 100644 --- a/ProxyServerSharp/ProxyServerSharp.csproj +++ b/ProxyServerSharp/ProxyServerSharp.csproj @@ -1,87 +1,89 @@ - - - - Debug - AnyCPU - 9.0.30729 - 2.0 - {FF0A1461-B06D-4B10-9E9E-811913808C28} - WinExe - Properties - ProxyServerSharp - ProxyServerSharp - v2.0 - 512 - - - - - 3.5 - - - true - full - false - bin\Debug\ - DEBUG;TRACE - prompt - 4 - - - pdbonly - true - bin\Release\ - TRACE - prompt - 4 - - - - - - - - - - - - Form - - - MainForm.cs - - - - - MainForm.cs - - - ResXFileCodeGenerator - Resources.Designer.cs - Designer - - - True - Resources.resx - True - - - SettingsSingleFileGenerator - Settings.Designer.cs - - - True - Settings.settings - True - - - - + + + + Debug + AnyCPU + 9.0.30729 + 2.0 + {FF0A1461-B06D-4B10-9E9E-811913808C28} + WinExe + Properties + ProxyServerSharp + ProxyServerSharp + v2.0 + 512 + + + + + 3.5 + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + Form + + + MainForm.cs + + + + + MainForm.cs + + + ResXFileCodeGenerator + Resources.Designer.cs + Designer + + + True + Resources.resx + True + + + SettingsSingleFileGenerator + Settings.Designer.cs + + + True + Settings.settings + True + + + + + --> \ No newline at end of file