diff --git a/ProxyServerSharp/Enums/AuthenticationType.cs b/ProxyServerSharp/Enums/AuthenticationType.cs new file mode 100644 index 0000000..9aa997d --- /dev/null +++ b/ProxyServerSharp/Enums/AuthenticationType.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProxyServerSharp.Enums +{ + public enum AuthenticationType + { + None = 0x00, + UsernamePassword = 0x02 + } +} diff --git a/ProxyServerSharp/Enums/ProxyType.cs b/ProxyServerSharp/Enums/ProxyType.cs new file mode 100644 index 0000000..4fb5ebf --- /dev/null +++ b/ProxyServerSharp/Enums/ProxyType.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProxyServerSharp.Enums +{ + public enum ProxyType + { + Http, + Tls, + Socks4, + Socks5 + } +} diff --git a/ProxyServerSharp/Implementation/ProxyCoreFactory.cs b/ProxyServerSharp/Implementation/ProxyCoreFactory.cs new file mode 100644 index 0000000..090d11d --- /dev/null +++ b/ProxyServerSharp/Implementation/ProxyCoreFactory.cs @@ -0,0 +1,23 @@ +using ProxyServerSharp.Enums; +using ProxyServerSharp.Interfaces; +using System; + +namespace ProxyServerSharp.Implementation +{ + public class ProxyCoreFactory + { + public static IProxyCore Create(IProxyServerConfiguration configuration, ProxyType proxyType) + { + switch(proxyType) + { + case ProxyType.Socks4: + return new Socks4ProxyCore(configuration); + case ProxyType.Socks5: + return new Socks5ProxyCore(configuration); + + default: + throw new NotImplementedException(); + } + } + } +} diff --git a/ProxyServerSharp/Implementation/ProxyServerConfiguration.cs b/ProxyServerSharp/Implementation/ProxyServerConfiguration.cs new file mode 100644 index 0000000..543e22c --- /dev/null +++ b/ProxyServerSharp/Implementation/ProxyServerConfiguration.cs @@ -0,0 +1,17 @@ +using ProxyServerSharp.Enums; +using ProxyServerSharp.Interfaces; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProxyServerSharp.Implementation +{ + public class ProxyServerConfiguration : IProxyServerConfiguration + { + public int Port { get; set; } + public int TransferUnitSize { get; set; } + public AuthenticationType AuthenticationType { get; set; } + } +} diff --git a/ProxyServerSharp/Implementation/SettingsProxyServerConfiguration.cs b/ProxyServerSharp/Implementation/SettingsProxyServerConfiguration.cs new file mode 100644 index 0000000..0515bb0 --- /dev/null +++ b/ProxyServerSharp/Implementation/SettingsProxyServerConfiguration.cs @@ -0,0 +1,17 @@ +using ProxyServerSharp.Enums; +using ProxyServerSharp.Interfaces; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProxyServerSharp.Implementation +{ + class SettingsProxyServerConfiguration : IProxyServerConfiguration + { + public int Port { get => Properties.Settings.Default.Port; set => Properties.Settings.Default.Port = value; } + public int TransferUnitSize { get => Properties.Settings.Default.TransferUnitSize; set => Properties.Settings.Default.TransferUnitSize = value; } + public AuthenticationType AuthenticationType { get => throw new NotImplementedException(); set => throw new NotImplementedException(); } + } +} diff --git a/ProxyServerSharp/Implementation/Socks4ProxyCore.cs b/ProxyServerSharp/Implementation/Socks4ProxyCore.cs index 1be9869..02e2973 100644 --- a/ProxyServerSharp/Implementation/Socks4ProxyCore.cs +++ b/ProxyServerSharp/Implementation/Socks4ProxyCore.cs @@ -14,14 +14,15 @@ namespace ProxyServerSharp.Implementation private Socket _serverSocket; + private bool _running; private Thread _acceptThread; private List _connections = new List(); - public Socks4ProxyCore(int port, int transferUnitSize) + public Socks4ProxyCore(IProxyServerConfiguration configuration) { - _port = port; - _transferUnitSize = transferUnitSize; + _port = configuration.Port; + _transferUnitSize = configuration.TransferUnitSize; } public event LocalConnectEventHandler LocalConnect; @@ -37,6 +38,8 @@ namespace ProxyServerSharp.Implementation { SetupServerSocket(); + _running = true; + _acceptThread = new Thread(AcceptConnections); _acceptThread.IsBackground = true; _acceptThread.Start(); @@ -44,7 +47,14 @@ namespace ProxyServerSharp.Implementation public void Shutdown() { - throw new NotImplementedException(); + _running = false; + foreach(ConnectionInfo connection in _connections) + { + connection.LocalSocket.Shutdown(SocketShutdown.Both); + connection.LocalSocket.Close(); + connection.RemoteSocket.Shutdown(SocketShutdown.Both); + connection.RemoteSocket.Close(); + } } private void SetupServerSocket() @@ -61,13 +71,18 @@ namespace ProxyServerSharp.Implementation private void AcceptConnections() { - while (true) + while (_running) { // Accept a connection ConnectionInfo connection = new ConnectionInfo(); Socket socket = _serverSocket.Accept(); + if(_running == false) + { + break; + } + connection.LocalSocket = socket; connection.RemoteSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); @@ -164,7 +179,6 @@ namespace ProxyServerSharp.Implementation } finally { - Console.WriteLine("ProcessLocalConnection Cleaning up..."); connection.LocalSocket.Close(); connection.RemoteSocket.Close(); lock (_connections) _connections.Remove(connection); diff --git a/ProxyServerSharp/Implementation/Socks5ProxyCore.cs b/ProxyServerSharp/Implementation/Socks5ProxyCore.cs new file mode 100644 index 0000000..0cc966c --- /dev/null +++ b/ProxyServerSharp/Implementation/Socks5ProxyCore.cs @@ -0,0 +1,40 @@ +using ProxyServerSharp.Interfaces; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProxyServerSharp.Implementation +{ + public class Socks5ProxyCore : IProxyCore + { + 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; + + private readonly int _port; + private readonly int _transferUnitSize; + + public Socks5ProxyCore(IProxyServerConfiguration configuration) + { + _port = configuration.Port; + _transferUnitSize = configuration.TransferUnitSize; + } + + public void Shutdown() + { + throw new NotImplementedException(); + } + + public void Start() + { + throw new NotImplementedException(); + } + } +} diff --git a/ProxyServerSharp/Interfaces/IProxyServerConfiguration.cs b/ProxyServerSharp/Interfaces/IProxyServerConfiguration.cs new file mode 100644 index 0000000..e13c3c5 --- /dev/null +++ b/ProxyServerSharp/Interfaces/IProxyServerConfiguration.cs @@ -0,0 +1,11 @@ +using ProxyServerSharp.Enums; + +namespace ProxyServerSharp.Interfaces +{ + public interface IProxyServerConfiguration + { + int Port { get; set; } + int TransferUnitSize { get; set; } + AuthenticationType AuthenticationType { get; set; } + } +} diff --git a/ProxyServerSharp/MainForm.Designer.cs b/ProxyServerSharp/MainForm.Designer.cs index da8e725..beaccfa 100644 --- a/ProxyServerSharp/MainForm.Designer.cs +++ b/ProxyServerSharp/MainForm.Designer.cs @@ -40,9 +40,9 @@ // // startProxyButton // - this.startProxyButton.Location = new System.Drawing.Point(448, 168); + this.startProxyButton.Location = new System.Drawing.Point(615, 441); this.startProxyButton.Name = "startProxyButton"; - this.startProxyButton.Size = new System.Drawing.Size(64, 24); + this.startProxyButton.Size = new System.Drawing.Size(90, 24); this.startProxyButton.TabIndex = 0; this.startProxyButton.Text = "Start"; this.startProxyButton.UseVisualStyleBackColor = true; @@ -72,13 +72,13 @@ this.debugTextBox.Multiline = true; this.debugTextBox.Name = "debugTextBox"; this.debugTextBox.ScrollBars = System.Windows.Forms.ScrollBars.Vertical; - this.debugTextBox.Size = new System.Drawing.Size(504, 128); + this.debugTextBox.Size = new System.Drawing.Size(697, 403); this.debugTextBox.TabIndex = 3; // // transferUnitSizeLabel // this.transferUnitSizeLabel.AutoSize = true; - this.transferUnitSizeLabel.Location = new System.Drawing.Point(8, 172); + this.transferUnitSizeLabel.Location = new System.Drawing.Point(12, 445); this.transferUnitSizeLabel.Name = "transferUnitSizeLabel"; this.transferUnitSizeLabel.Size = new System.Drawing.Size(128, 13); this.transferUnitSizeLabel.TabIndex = 4; @@ -87,7 +87,7 @@ // transferUnitSizeComboBox // this.transferUnitSizeComboBox.FormattingEnabled = true; - this.transferUnitSizeComboBox.Location = new System.Drawing.Point(144, 168); + this.transferUnitSizeComboBox.Location = new System.Drawing.Point(148, 441); this.transferUnitSizeComboBox.Name = "transferUnitSizeComboBox"; this.transferUnitSizeComboBox.Size = new System.Drawing.Size(120, 21); this.transferUnitSizeComboBox.TabIndex = 5; @@ -95,7 +95,7 @@ // portLabel // this.portLabel.AutoSize = true; - this.portLabel.Location = new System.Drawing.Point(8, 196); + this.portLabel.Location = new System.Drawing.Point(12, 469); this.portLabel.Name = "portLabel"; this.portLabel.Size = new System.Drawing.Size(29, 13); this.portLabel.TabIndex = 6; @@ -103,18 +103,17 @@ // // portTextBox // - this.portTextBox.Location = new System.Drawing.Point(144, 192); + this.portTextBox.Location = new System.Drawing.Point(148, 465); this.portTextBox.Name = "portTextBox"; - this.portTextBox.Size = new System.Drawing.Size(72, 20); + this.portTextBox.Size = new System.Drawing.Size(54, 20); this.portTextBox.TabIndex = 7; this.portTextBox.Text = "1080"; - this.portTextBox.TextAlign = System.Windows.Forms.HorizontalAlignment.Center; // // MainForm // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(537, 222); + this.ClientSize = new System.Drawing.Size(717, 494); this.Controls.Add(this.portTextBox); this.Controls.Add(this.portLabel); this.Controls.Add(this.transferUnitSizeComboBox); @@ -125,7 +124,7 @@ this.Controls.Add(this.startProxyButton); this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle; this.Name = "MainForm"; - this.Text = "SOCKS4 Server "; + this.Text = "Multi Proxy Server"; this.Load += new System.EventHandler(this.Form1_Load); this.ResumeLayout(false); this.PerformLayout(); diff --git a/ProxyServerSharp/MainForm.cs b/ProxyServerSharp/MainForm.cs index eb1d8fa..61251f8 100644 --- a/ProxyServerSharp/MainForm.cs +++ b/ProxyServerSharp/MainForm.cs @@ -12,7 +12,7 @@ namespace ProxyServerSharp { public partial class MainForm : Form { - Socks4ProxyCore _proxyCore; + IProxyCore _proxyCore; public MainForm() { @@ -53,17 +53,26 @@ namespace ProxyServerSharp { if (startProxyButton.Text == "Start") { - _proxyCore = new Socks4ProxyCore(int.Parse(portTextBox.Text), - int.Parse((string)transferUnitSizeComboBox.SelectedItem)); + var config = new ProxyServerConfiguration() + { + Port = int.Parse(portTextBox.Text), + TransferUnitSize = int.Parse((string)transferUnitSizeComboBox.SelectedItem) + }; + + _proxyCore = ProxyCoreFactory.Create(config, Enums.ProxyType.Socks4); _proxyCore.LocalConnect += new LocalConnectEventHandler(server_LocalConnect); _proxyCore.RemoteConnect += new RemoteConnectEventHandler(server_RemoteConnect); _proxyCore.Start(); + statusLabel.Text = "Started"; startProxyButton.Text = "Stop"; } else { - // + _proxyCore.Shutdown(); + + statusLabel.Text = "Stopped"; + startProxyButton.Text = "Start"; } } } diff --git a/ProxyServerSharp/MainForm.resx b/ProxyServerSharp/MainForm.resx index 19dc0dd..d58980a 100644 --- a/ProxyServerSharp/MainForm.resx +++ b/ProxyServerSharp/MainForm.resx @@ -112,9 +112,9 @@ 2.0 - System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 \ No newline at end of file diff --git a/ProxyServerSharp/Properties/Settings.Designer.cs b/ProxyServerSharp/Properties/Settings.Designer.cs index fe9d43b..fc1d26e 100644 --- a/ProxyServerSharp/Properties/Settings.Designer.cs +++ b/ProxyServerSharp/Properties/Settings.Designer.cs @@ -22,5 +22,29 @@ namespace ProxyServerSharp.Properties { return defaultInstance; } } + + [global::System.Configuration.UserScopedSettingAttribute()] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Configuration.DefaultSettingValueAttribute("1080")] + public int Port { + get { + return ((int)(this["Port"])); + } + set { + this["Port"] = value; + } + } + + [global::System.Configuration.UserScopedSettingAttribute()] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Configuration.DefaultSettingValueAttribute("4096")] + public int TransferUnitSize { + get { + return ((int)(this["TransferUnitSize"])); + } + set { + this["TransferUnitSize"] = value; + } + } } } diff --git a/ProxyServerSharp/Properties/Settings.settings b/ProxyServerSharp/Properties/Settings.settings index 3964565..7bfba20 100644 --- a/ProxyServerSharp/Properties/Settings.settings +++ b/ProxyServerSharp/Properties/Settings.settings @@ -1,7 +1,12 @@  - - - - - - + + + + + 1080 + + + 4096 + + + \ No newline at end of file diff --git a/ProxyServerSharp/ProxyServerSharp.csproj b/ProxyServerSharp/ProxyServerSharp.csproj index 4c0b8e0..0bac4ad 100644 --- a/ProxyServerSharp/ProxyServerSharp.csproj +++ b/ProxyServerSharp/ProxyServerSharp.csproj @@ -47,8 +47,15 @@ + + + + + + + Form diff --git a/ProxyServerSharp/app.config b/ProxyServerSharp/app.config index d7c60fd..620c61f 100644 --- a/ProxyServerSharp/app.config +++ b/ProxyServerSharp/app.config @@ -1,3 +1,18 @@ - + + +
+ + + + + + 1080 + + + 4096 + + + +