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,
|
||||
|
@ -75,8 +77,7 @@ namespace ProxyServerSharp
|
|||
connection.LocalThread.IsBackground = true;
|
||||
connection.LocalThread.Start(connection);
|
||||
|
||||
if (LocalConnect != null)
|
||||
LocalConnect(this, (IPEndPoint)socket.RemoteEndPoint);
|
||||
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";
|
||||
}
|
||||
|
|
|
@ -44,6 +44,8 @@
|
|||
<Reference Include="System.Xml" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Implementation\ConnectionInfo.cs" />
|
||||
<Compile Include="Interfaces\IProxyCore.cs" />
|
||||
<Compile Include="MainForm.cs">
|
||||
<SubType>Form</SubType>
|
||||
</Compile>
|
||||
|
@ -74,7 +76,7 @@
|
|||
<DependentUpon>Settings.settings</DependentUpon>
|
||||
<DesignTimeSharedInput>True</DesignTimeSharedInput>
|
||||
</Compile>
|
||||
<Compile Include="Core\SOCKS4Server.cs" />
|
||||
<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.
|
||||
|
|
Loading…
Reference in New Issue