Worked on core interface, seperated out ConnectionInfo class, added more event handlers.

This commit is contained in:
Brandon Scott 2019-01-29 19:56:53 -06:00
parent 2f58b4a76f
commit caff2c3c25
5 changed files with 162 additions and 112 deletions

View File

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

View File

@ -1,41 +1,38 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Net.Sockets;
using System.Net; 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; private readonly int _port;
public Thread LocalThread; private readonly int _transferUnitSize;
public Socket RemoteSocket;
public Thread RemoteThread;
}
public delegate void ConnectEventHandler(object sender, IPEndPoint iep);
public delegate void ConnectionLogHandler(object sender, int code, string message);
class SOCKS4Server
{
private Socket _serverSocket; private Socket _serverSocket;
private int _port;
private int _transferUnitSize;
private Thread _acceptThread; private Thread _acceptThread;
private List<ConnectionInfo> _connections = private List<ConnectionInfo> _connections =
new List<ConnectionInfo>(); new List<ConnectionInfo>();
public event ConnectEventHandler LocalConnect; public Socks4ProxyCore(int port, int transferUnitSize)
public event ConnectEventHandler RemoteConnect;
public SOCKS4Server(int port, int transferUnitSize)
{ {
_port = port; _port = port;
_transferUnitSize = transferUnitSize; _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() public void Start()
{ {
SetupServerSocket(); SetupServerSocket();
@ -45,6 +42,11 @@ namespace ProxyServerSharp
_acceptThread.Start(); _acceptThread.Start();
} }
public void Shutdown()
{
throw new NotImplementedException();
}
private void SetupServerSocket() private void SetupServerSocket()
{ {
IPEndPoint myEndpoint = new IPEndPoint(IPAddress.Loopback, IPEndPoint myEndpoint = new IPEndPoint(IPAddress.Loopback,
@ -73,10 +75,9 @@ namespace ProxyServerSharp
// Create the thread for the receives. // Create the thread for the receives.
connection.LocalThread = new Thread(ProcessLocalConnection); connection.LocalThread = new Thread(ProcessLocalConnection);
connection.LocalThread.IsBackground = true; connection.LocalThread.IsBackground = true;
connection.LocalThread.Start(connection); connection.LocalThread.Start(connection);
if (LocalConnect != null) LocalConnect?.Invoke(this, (IPEndPoint)socket.RemoteEndPoint);
LocalConnect(this, (IPEndPoint)socket.RemoteEndPoint);
// Store the socket // Store the socket
lock (_connections) _connections.Add(connection); lock (_connections) _connections.Add(connection);
@ -114,8 +115,7 @@ namespace ProxyServerSharp
{ {
Console.WriteLine("Connected to remote!"); Console.WriteLine("Connected to remote!");
if (RemoteConnect != null) RemoteConnect?.Invoke(this, remoteEndPoint);
RemoteConnect(this, remoteEndPoint);
byte[] socksResponse = new byte[] { byte[] socksResponse = new byte[] {
0x00, 0x5a, 0x00, 0x5a,

View File

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

View File

@ -5,12 +5,14 @@ using System.Data;
using System.Drawing; using System.Drawing;
using System.Text; using System.Text;
using System.Windows.Forms; using System.Windows.Forms;
using ProxyServerSharp.Implementation;
using ProxyServerSharp.Interfaces;
namespace ProxyServerSharp namespace ProxyServerSharp
{ {
public partial class MainForm : Form public partial class MainForm : Form
{ {
SOCKS4Server server; Socks4ProxyCore _proxyCore;
public MainForm() public MainForm()
{ {
@ -51,11 +53,11 @@ namespace ProxyServerSharp
{ {
if (startProxyButton.Text == "Start") if (startProxyButton.Text == "Start")
{ {
server = new SOCKS4Server(int.Parse(portTextBox.Text), _proxyCore = new Socks4ProxyCore(int.Parse(portTextBox.Text),
int.Parse((string)transferUnitSizeComboBox.SelectedItem)); int.Parse((string)transferUnitSizeComboBox.SelectedItem));
server.LocalConnect += new ConnectEventHandler(server_LocalConnect); _proxyCore.LocalConnect += new LocalConnectEventHandler(server_LocalConnect);
server.RemoteConnect += new ConnectEventHandler(server_RemoteConnect); _proxyCore.RemoteConnect += new RemoteConnectEventHandler(server_RemoteConnect);
server.Start(); _proxyCore.Start();
statusLabel.Text = "Started"; statusLabel.Text = "Started";
startProxyButton.Text = "Stop"; startProxyButton.Text = "Stop";
} }

View File

@ -1,87 +1,89 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup> <PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>9.0.30729</ProductVersion> <ProductVersion>9.0.30729</ProductVersion>
<SchemaVersion>2.0</SchemaVersion> <SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{FF0A1461-B06D-4B10-9E9E-811913808C28}</ProjectGuid> <ProjectGuid>{FF0A1461-B06D-4B10-9E9E-811913808C28}</ProjectGuid>
<OutputType>WinExe</OutputType> <OutputType>WinExe</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder> <AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>ProxyServerSharp</RootNamespace> <RootNamespace>ProxyServerSharp</RootNamespace>
<AssemblyName>ProxyServerSharp</AssemblyName> <AssemblyName>ProxyServerSharp</AssemblyName>
<TargetFrameworkVersion>v2.0</TargetFrameworkVersion> <TargetFrameworkVersion>v2.0</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment> <FileAlignment>512</FileAlignment>
<FileUpgradeFlags> <FileUpgradeFlags>
</FileUpgradeFlags> </FileUpgradeFlags>
<UpgradeBackupLocation> <UpgradeBackupLocation>
</UpgradeBackupLocation> </UpgradeBackupLocation>
<OldToolsVersion>3.5</OldToolsVersion> <OldToolsVersion>3.5</OldToolsVersion>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols> <DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType> <DebugType>full</DebugType>
<Optimize>false</Optimize> <Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath> <OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants> <DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport> <ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel> <WarningLevel>4</WarningLevel>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType> <DebugType>pdbonly</DebugType>
<Optimize>true</Optimize> <Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath> <OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants> <DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport> <ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel> <WarningLevel>4</WarningLevel>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Reference Include="System" /> <Reference Include="System" />
<Reference Include="System.Data" /> <Reference Include="System.Data" />
<Reference Include="System.Deployment" /> <Reference Include="System.Deployment" />
<Reference Include="System.Drawing" /> <Reference Include="System.Drawing" />
<Reference Include="System.Windows.Forms" /> <Reference Include="System.Windows.Forms" />
<Reference Include="System.Xml" /> <Reference Include="System.Xml" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="MainForm.cs"> <Compile Include="Implementation\ConnectionInfo.cs" />
<SubType>Form</SubType> <Compile Include="Interfaces\IProxyCore.cs" />
</Compile> <Compile Include="MainForm.cs">
<Compile Include="MainForm.Designer.cs"> <SubType>Form</SubType>
<DependentUpon>MainForm.cs</DependentUpon> </Compile>
</Compile> <Compile Include="MainForm.Designer.cs">
<Compile Include="Program.cs" /> <DependentUpon>MainForm.cs</DependentUpon>
<Compile Include="Properties\AssemblyInfo.cs" /> </Compile>
<EmbeddedResource Include="MainForm.resx"> <Compile Include="Program.cs" />
<DependentUpon>MainForm.cs</DependentUpon> <Compile Include="Properties\AssemblyInfo.cs" />
</EmbeddedResource> <EmbeddedResource Include="MainForm.resx">
<EmbeddedResource Include="Properties\Resources.resx"> <DependentUpon>MainForm.cs</DependentUpon>
<Generator>ResXFileCodeGenerator</Generator> </EmbeddedResource>
<LastGenOutput>Resources.Designer.cs</LastGenOutput> <EmbeddedResource Include="Properties\Resources.resx">
<SubType>Designer</SubType> <Generator>ResXFileCodeGenerator</Generator>
</EmbeddedResource> <LastGenOutput>Resources.Designer.cs</LastGenOutput>
<Compile Include="Properties\Resources.Designer.cs"> <SubType>Designer</SubType>
<AutoGen>True</AutoGen> </EmbeddedResource>
<DependentUpon>Resources.resx</DependentUpon> <Compile Include="Properties\Resources.Designer.cs">
<DesignTime>True</DesignTime> <AutoGen>True</AutoGen>
</Compile> <DependentUpon>Resources.resx</DependentUpon>
<None Include="Properties\Settings.settings"> <DesignTime>True</DesignTime>
<Generator>SettingsSingleFileGenerator</Generator> </Compile>
<LastGenOutput>Settings.Designer.cs</LastGenOutput> <None Include="Properties\Settings.settings">
</None> <Generator>SettingsSingleFileGenerator</Generator>
<Compile Include="Properties\Settings.Designer.cs"> <LastGenOutput>Settings.Designer.cs</LastGenOutput>
<AutoGen>True</AutoGen> </None>
<DependentUpon>Settings.settings</DependentUpon> <Compile Include="Properties\Settings.Designer.cs">
<DesignTimeSharedInput>True</DesignTimeSharedInput> <AutoGen>True</AutoGen>
</Compile> <DependentUpon>Settings.settings</DependentUpon>
<Compile Include="Core\SOCKS4Server.cs" /> <DesignTimeSharedInput>True</DesignTimeSharedInput>
</ItemGroup> </Compile>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> <Compile Include="Implementation\Socks4ProxyCore.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it. <!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets. Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild"> <Target Name="BeforeBuild">
</Target> </Target>
<Target Name="AfterBuild"> <Target Name="AfterBuild">
</Target> </Target>
--> -->
</Project> </Project>