Fleshing out the framework to add more proxy server types, authentication and settings.

This commit is contained in:
Brandon Scott 2019-01-29 21:15:08 -06:00
parent c87a61b589
commit 06eccde5f4
15 changed files with 241 additions and 30 deletions

View File

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

View File

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

View File

@ -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();
}
}
}
}

View File

@ -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; }
}
}

View File

@ -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(); }
}
}

View File

@ -14,14 +14,15 @@ namespace ProxyServerSharp.Implementation
private Socket _serverSocket; private Socket _serverSocket;
private bool _running;
private Thread _acceptThread; private Thread _acceptThread;
private List<ConnectionInfo> _connections = private List<ConnectionInfo> _connections =
new List<ConnectionInfo>(); new List<ConnectionInfo>();
public Socks4ProxyCore(int port, int transferUnitSize) public Socks4ProxyCore(IProxyServerConfiguration configuration)
{ {
_port = port; _port = configuration.Port;
_transferUnitSize = transferUnitSize; _transferUnitSize = configuration.TransferUnitSize;
} }
public event LocalConnectEventHandler LocalConnect; public event LocalConnectEventHandler LocalConnect;
@ -37,6 +38,8 @@ namespace ProxyServerSharp.Implementation
{ {
SetupServerSocket(); SetupServerSocket();
_running = true;
_acceptThread = new Thread(AcceptConnections); _acceptThread = new Thread(AcceptConnections);
_acceptThread.IsBackground = true; _acceptThread.IsBackground = true;
_acceptThread.Start(); _acceptThread.Start();
@ -44,7 +47,14 @@ namespace ProxyServerSharp.Implementation
public void Shutdown() 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() private void SetupServerSocket()
@ -61,13 +71,18 @@ namespace ProxyServerSharp.Implementation
private void AcceptConnections() private void AcceptConnections()
{ {
while (true) while (_running)
{ {
// Accept a connection // Accept a connection
ConnectionInfo connection = new ConnectionInfo(); ConnectionInfo connection = new ConnectionInfo();
Socket socket = _serverSocket.Accept(); Socket socket = _serverSocket.Accept();
if(_running == false)
{
break;
}
connection.LocalSocket = socket; connection.LocalSocket = socket;
connection.RemoteSocket = new Socket(AddressFamily.InterNetwork, connection.RemoteSocket = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp); SocketType.Stream, ProtocolType.Tcp);
@ -164,7 +179,6 @@ namespace ProxyServerSharp.Implementation
} }
finally finally
{ {
Console.WriteLine("ProcessLocalConnection Cleaning up...");
connection.LocalSocket.Close(); connection.LocalSocket.Close();
connection.RemoteSocket.Close(); connection.RemoteSocket.Close();
lock (_connections) _connections.Remove(connection); lock (_connections) _connections.Remove(connection);

View File

@ -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();
}
}
}

View File

@ -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; }
}
}

View File

@ -40,9 +40,9 @@
// //
// startProxyButton // 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.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.TabIndex = 0;
this.startProxyButton.Text = "Start"; this.startProxyButton.Text = "Start";
this.startProxyButton.UseVisualStyleBackColor = true; this.startProxyButton.UseVisualStyleBackColor = true;
@ -72,13 +72,13 @@
this.debugTextBox.Multiline = true; this.debugTextBox.Multiline = true;
this.debugTextBox.Name = "debugTextBox"; this.debugTextBox.Name = "debugTextBox";
this.debugTextBox.ScrollBars = System.Windows.Forms.ScrollBars.Vertical; 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; this.debugTextBox.TabIndex = 3;
// //
// transferUnitSizeLabel // transferUnitSizeLabel
// //
this.transferUnitSizeLabel.AutoSize = true; 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.Name = "transferUnitSizeLabel";
this.transferUnitSizeLabel.Size = new System.Drawing.Size(128, 13); this.transferUnitSizeLabel.Size = new System.Drawing.Size(128, 13);
this.transferUnitSizeLabel.TabIndex = 4; this.transferUnitSizeLabel.TabIndex = 4;
@ -87,7 +87,7 @@
// transferUnitSizeComboBox // transferUnitSizeComboBox
// //
this.transferUnitSizeComboBox.FormattingEnabled = true; 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.Name = "transferUnitSizeComboBox";
this.transferUnitSizeComboBox.Size = new System.Drawing.Size(120, 21); this.transferUnitSizeComboBox.Size = new System.Drawing.Size(120, 21);
this.transferUnitSizeComboBox.TabIndex = 5; this.transferUnitSizeComboBox.TabIndex = 5;
@ -95,7 +95,7 @@
// portLabel // portLabel
// //
this.portLabel.AutoSize = true; 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.Name = "portLabel";
this.portLabel.Size = new System.Drawing.Size(29, 13); this.portLabel.Size = new System.Drawing.Size(29, 13);
this.portLabel.TabIndex = 6; this.portLabel.TabIndex = 6;
@ -103,18 +103,17 @@
// //
// portTextBox // 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.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.TabIndex = 7;
this.portTextBox.Text = "1080"; this.portTextBox.Text = "1080";
this.portTextBox.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
// //
// MainForm // MainForm
// //
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 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.portTextBox);
this.Controls.Add(this.portLabel); this.Controls.Add(this.portLabel);
this.Controls.Add(this.transferUnitSizeComboBox); this.Controls.Add(this.transferUnitSizeComboBox);
@ -125,7 +124,7 @@
this.Controls.Add(this.startProxyButton); this.Controls.Add(this.startProxyButton);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle; this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.Name = "MainForm"; this.Name = "MainForm";
this.Text = "SOCKS4 Server "; this.Text = "Multi Proxy Server";
this.Load += new System.EventHandler(this.Form1_Load); this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false); this.ResumeLayout(false);
this.PerformLayout(); this.PerformLayout();

View File

@ -12,7 +12,7 @@ namespace ProxyServerSharp
{ {
public partial class MainForm : Form public partial class MainForm : Form
{ {
Socks4ProxyCore _proxyCore; IProxyCore _proxyCore;
public MainForm() public MainForm()
{ {
@ -53,17 +53,26 @@ namespace ProxyServerSharp
{ {
if (startProxyButton.Text == "Start") if (startProxyButton.Text == "Start")
{ {
_proxyCore = new Socks4ProxyCore(int.Parse(portTextBox.Text), var config = new ProxyServerConfiguration()
int.Parse((string)transferUnitSizeComboBox.SelectedItem)); {
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.LocalConnect += new LocalConnectEventHandler(server_LocalConnect);
_proxyCore.RemoteConnect += new RemoteConnectEventHandler(server_RemoteConnect); _proxyCore.RemoteConnect += new RemoteConnectEventHandler(server_RemoteConnect);
_proxyCore.Start(); _proxyCore.Start();
statusLabel.Text = "Started"; statusLabel.Text = "Started";
startProxyButton.Text = "Stop"; startProxyButton.Text = "Stop";
} }
else else
{ {
// _proxyCore.Shutdown();
statusLabel.Text = "Stopped";
startProxyButton.Text = "Start";
} }
} }
} }

View File

@ -112,9 +112,9 @@
<value>2.0</value> <value>2.0</value>
</resheader> </resheader>
<resheader name="reader"> <resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value> <value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader> </resheader>
<resheader name="writer"> <resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value> <value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader> </resheader>
</root> </root>

View File

@ -22,5 +22,29 @@ namespace ProxyServerSharp.Properties {
return defaultInstance; 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;
}
}
} }
} }

View File

@ -1,7 +1,12 @@
<?xml version='1.0' encoding='utf-8'?> <?xml version='1.0' encoding='utf-8'?>
<SettingsFile xmlns="http://schemas.microsoft.com/VisualStudio/2004/01/settings" CurrentProfile="(Default)"> <SettingsFile xmlns="http://schemas.microsoft.com/VisualStudio/2004/01/settings" CurrentProfile="(Default)" GeneratedClassNamespace="ProxyServerSharp.Properties" GeneratedClassName="Settings">
<Profiles> <Profiles />
<Profile Name="(Default)" /> <Settings>
</Profiles> <Setting Name="Port" Type="System.Int32" Scope="User">
<Settings /> <Value Profile="(Default)">1080</Value>
</Setting>
<Setting Name="TransferUnitSize" Type="System.Int32" Scope="User">
<Value Profile="(Default)">4096</Value>
</Setting>
</Settings>
</SettingsFile> </SettingsFile>

View File

@ -47,8 +47,15 @@
<Reference Include="System.Xml" /> <Reference Include="System.Xml" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="Enums\AuthenticationType.cs" />
<Compile Include="Implementation\ConnectionInfo.cs" /> <Compile Include="Implementation\ConnectionInfo.cs" />
<Compile Include="Implementation\ProxyCoreFactory.cs" />
<Compile Include="Enums\ProxyType.cs" />
<Compile Include="Implementation\ProxyServerConfiguration.cs" />
<Compile Include="Implementation\SettingsProxyServerConfiguration.cs" />
<Compile Include="Implementation\Socks5ProxyCore.cs" />
<Compile Include="Interfaces\IProxyCore.cs" /> <Compile Include="Interfaces\IProxyCore.cs" />
<Compile Include="Interfaces\IProxyServerConfiguration.cs" />
<Compile Include="MainForm.cs"> <Compile Include="MainForm.cs">
<SubType>Form</SubType> <SubType>Form</SubType>
</Compile> </Compile>

View File

@ -1,3 +1,18 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<configuration> <configuration>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.1"/></startup></configuration> <configSections>
<sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="ProxyServerSharp.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
</sectionGroup>
</configSections>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.1"/></startup><userSettings>
<ProxyServerSharp.Properties.Settings>
<setting name="Port" serializeAs="String">
<value>1080</value>
</setting>
<setting name="TransferUnitSize" serializeAs="String">
<value>4096</value>
</setting>
</ProxyServerSharp.Properties.Settings>
</userSettings>
</configuration>