Worked on core interface, seperated out ConnectionInfo class, added more event handlers.
This commit is contained in:
parent
2f58b4a76f
commit
caff2c3c25
|
@ -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; }
|
||||
}
|
||||
}
|
|
@ -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<ConnectionInfo> _connections =
|
||||
new List<ConnectionInfo>();
|
||||
|
||||
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,
|
|
@ -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();
|
||||
}
|
||||
}
|
|
@ -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";
|
||||
}
|
||||
|
|
|
@ -1,87 +1,89 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<PropertyGroup>
|
||||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
||||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
|
||||
<ProductVersion>9.0.30729</ProductVersion>
|
||||
<SchemaVersion>2.0</SchemaVersion>
|
||||
<ProjectGuid>{FF0A1461-B06D-4B10-9E9E-811913808C28}</ProjectGuid>
|
||||
<OutputType>WinExe</OutputType>
|
||||
<AppDesignerFolder>Properties</AppDesignerFolder>
|
||||
<RootNamespace>ProxyServerSharp</RootNamespace>
|
||||
<AssemblyName>ProxyServerSharp</AssemblyName>
|
||||
<TargetFrameworkVersion>v2.0</TargetFrameworkVersion>
|
||||
<FileAlignment>512</FileAlignment>
|
||||
<FileUpgradeFlags>
|
||||
</FileUpgradeFlags>
|
||||
<UpgradeBackupLocation>
|
||||
</UpgradeBackupLocation>
|
||||
<OldToolsVersion>3.5</OldToolsVersion>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
<DebugType>full</DebugType>
|
||||
<Optimize>false</Optimize>
|
||||
<OutputPath>bin\Debug\</OutputPath>
|
||||
<DefineConstants>DEBUG;TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
|
||||
<DebugType>pdbonly</DebugType>
|
||||
<Optimize>true</Optimize>
|
||||
<OutputPath>bin\Release\</OutputPath>
|
||||
<DefineConstants>TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.Data" />
|
||||
<Reference Include="System.Deployment" />
|
||||
<Reference Include="System.Drawing" />
|
||||
<Reference Include="System.Windows.Forms" />
|
||||
<Reference Include="System.Xml" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="MainForm.cs">
|
||||
<SubType>Form</SubType>
|
||||
</Compile>
|
||||
<Compile Include="MainForm.Designer.cs">
|
||||
<DependentUpon>MainForm.cs</DependentUpon>
|
||||
</Compile>
|
||||
<Compile Include="Program.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<EmbeddedResource Include="MainForm.resx">
|
||||
<DependentUpon>MainForm.cs</DependentUpon>
|
||||
</EmbeddedResource>
|
||||
<EmbeddedResource Include="Properties\Resources.resx">
|
||||
<Generator>ResXFileCodeGenerator</Generator>
|
||||
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
|
||||
<SubType>Designer</SubType>
|
||||
</EmbeddedResource>
|
||||
<Compile Include="Properties\Resources.Designer.cs">
|
||||
<AutoGen>True</AutoGen>
|
||||
<DependentUpon>Resources.resx</DependentUpon>
|
||||
<DesignTime>True</DesignTime>
|
||||
</Compile>
|
||||
<None Include="Properties\Settings.settings">
|
||||
<Generator>SettingsSingleFileGenerator</Generator>
|
||||
<LastGenOutput>Settings.Designer.cs</LastGenOutput>
|
||||
</None>
|
||||
<Compile Include="Properties\Settings.Designer.cs">
|
||||
<AutoGen>True</AutoGen>
|
||||
<DependentUpon>Settings.settings</DependentUpon>
|
||||
<DesignTimeSharedInput>True</DesignTimeSharedInput>
|
||||
</Compile>
|
||||
<Compile Include="Core\SOCKS4Server.cs" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<PropertyGroup>
|
||||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
||||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
|
||||
<ProductVersion>9.0.30729</ProductVersion>
|
||||
<SchemaVersion>2.0</SchemaVersion>
|
||||
<ProjectGuid>{FF0A1461-B06D-4B10-9E9E-811913808C28}</ProjectGuid>
|
||||
<OutputType>WinExe</OutputType>
|
||||
<AppDesignerFolder>Properties</AppDesignerFolder>
|
||||
<RootNamespace>ProxyServerSharp</RootNamespace>
|
||||
<AssemblyName>ProxyServerSharp</AssemblyName>
|
||||
<TargetFrameworkVersion>v2.0</TargetFrameworkVersion>
|
||||
<FileAlignment>512</FileAlignment>
|
||||
<FileUpgradeFlags>
|
||||
</FileUpgradeFlags>
|
||||
<UpgradeBackupLocation>
|
||||
</UpgradeBackupLocation>
|
||||
<OldToolsVersion>3.5</OldToolsVersion>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
<DebugType>full</DebugType>
|
||||
<Optimize>false</Optimize>
|
||||
<OutputPath>bin\Debug\</OutputPath>
|
||||
<DefineConstants>DEBUG;TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
|
||||
<DebugType>pdbonly</DebugType>
|
||||
<Optimize>true</Optimize>
|
||||
<OutputPath>bin\Release\</OutputPath>
|
||||
<DefineConstants>TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.Data" />
|
||||
<Reference Include="System.Deployment" />
|
||||
<Reference Include="System.Drawing" />
|
||||
<Reference Include="System.Windows.Forms" />
|
||||
<Reference Include="System.Xml" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Implementation\ConnectionInfo.cs" />
|
||||
<Compile Include="Interfaces\IProxyCore.cs" />
|
||||
<Compile Include="MainForm.cs">
|
||||
<SubType>Form</SubType>
|
||||
</Compile>
|
||||
<Compile Include="MainForm.Designer.cs">
|
||||
<DependentUpon>MainForm.cs</DependentUpon>
|
||||
</Compile>
|
||||
<Compile Include="Program.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<EmbeddedResource Include="MainForm.resx">
|
||||
<DependentUpon>MainForm.cs</DependentUpon>
|
||||
</EmbeddedResource>
|
||||
<EmbeddedResource Include="Properties\Resources.resx">
|
||||
<Generator>ResXFileCodeGenerator</Generator>
|
||||
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
|
||||
<SubType>Designer</SubType>
|
||||
</EmbeddedResource>
|
||||
<Compile Include="Properties\Resources.Designer.cs">
|
||||
<AutoGen>True</AutoGen>
|
||||
<DependentUpon>Resources.resx</DependentUpon>
|
||||
<DesignTime>True</DesignTime>
|
||||
</Compile>
|
||||
<None Include="Properties\Settings.settings">
|
||||
<Generator>SettingsSingleFileGenerator</Generator>
|
||||
<LastGenOutput>Settings.Designer.cs</LastGenOutput>
|
||||
</None>
|
||||
<Compile Include="Properties\Settings.Designer.cs">
|
||||
<AutoGen>True</AutoGen>
|
||||
<DependentUpon>Settings.settings</DependentUpon>
|
||||
<DesignTimeSharedInput>True</DesignTimeSharedInput>
|
||||
</Compile>
|
||||
<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.
|
||||
Other similar extension points exist, see Microsoft.Common.targets.
|
||||
<Target Name="BeforeBuild">
|
||||
</Target>
|
||||
<Target Name="AfterBuild">
|
||||
</Target>
|
||||
-->
|
||||
-->
|
||||
</Project>
|
Loading…
Reference in New Issue