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,
@ -75,8 +77,7 @@ namespace ProxyServerSharp
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

@ -44,6 +44,8 @@
<Reference Include="System.Xml" /> <Reference Include="System.Xml" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="Implementation\ConnectionInfo.cs" />
<Compile Include="Interfaces\IProxyCore.cs" />
<Compile Include="MainForm.cs"> <Compile Include="MainForm.cs">
<SubType>Form</SubType> <SubType>Form</SubType>
</Compile> </Compile>
@ -74,7 +76,7 @@
<DependentUpon>Settings.settings</DependentUpon> <DependentUpon>Settings.settings</DependentUpon>
<DesignTimeSharedInput>True</DesignTimeSharedInput> <DesignTimeSharedInput>True</DesignTimeSharedInput>
</Compile> </Compile>
<Compile Include="Core\SOCKS4Server.cs" /> <Compile Include="Implementation\Socks4ProxyCore.cs" />
</ItemGroup> </ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> <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.