public class TcpProxyConnection
{
TextWriter fileText;
FileStream fileBinary;
string FileName = null;
protected TcpConnection _Client;
protected TcpConnection _Server;
protected TcpProxy _Parent;
public TcpProxyConnection(TcpProxy Parent, TcpConnection Client, IPEndPoint RemoteEndPoint)
{
try
{
_Parent = Parent;
try
{
if (FormTcpSimpleProxy.LogFlag)
{
FileName = FormTcpSimpleProxy.GetNewFileName();
fileText = File.CreateText(FileName);
fileText.WriteLine("Connected to {0}", RemoteEndPoint);
}
TcpConnection cnn = new TcpConnection();
cnn.Connect(RemoteEndPoint.Address.ToString(), RemoteEndPoint.Port);
_Server = cnn;
}
catch (Exception ex)
{
Client.Close();
FormTcpSimpleProxy.Log("TcpProxyConnection() Error: {0}", ex.Message);
return;
}
_Client = Client;
_Client.DataReceived += new DataReceivedEventHandler(_Client_DataReceived);
_Client.Disconnected += new DisconnectedEventHandler(_Client_Disconnected);
_Server.DataReceived += new DataReceivedEventHandler(_Server_DataReceived);
_Server.Disconnected += new DisconnectedEventHandler(_Server_Disconnected);
_Client.BeginReceiving();
_Server.BeginReceiving();
}
catch (Exception ex)
{
Console.WriteLine("TcpProxyConnection() - " + ex.Message);
Console.WriteLine(ex.StackTrace);
}
}
private void _Client_DataReceived(object Sender, DataReceivedEventArgs e)
{
try
{
if (_Server != null)
{
if (FormTcpSimpleProxy.LogFlag)
{
if (IsBinary(e.Buffer))
{
fileText.WriteLine("\r\nClient Binary Data: {0} bytes (not logged)", e.Buffer.Length);
}
else
{
fileText.WriteLine("\r\nClient Text:\r\n{0}", System.Text.Encoding.ASCII.GetString(e.Buffer));
}
fileText.Flush();
}
try
{
_Server.SendNow(e.Buffer, 0, e.Buffer.Length);
}
catch (Exception ex)
{
FormTcpSimpleProxy.Log("_Client_DataReceived() Error: {0}", ex.Message);
ForceClose();
}
}
}
catch (Exception ex)
{
Console.WriteLine("TcpProxyConnection._Server_Disconnected() - " + ex.Message);
Console.WriteLine(ex.StackTrace);
}
}
private void ForceClose()
{
try
{
if (fileText != null)
{
fileText.Close();
fileText = null;
if (fileBinary != null)
{
fileBinary.Close();
fileBinary = null;
}
}
}
catch { }
try
{
_Client.Close();
}
catch { }
_Client = null;
try
{
_Server.Close();
}
catch { }
_Server = null;
_Parent.RemoveConnection(this);
}
private void _Client_Disconnected(object Sender, DisconnectedEventArgs e)
{
ForceClose();
}
private void _Server_DataReceived(object Sender, DataReceivedEventArgs e)
{
try
{
if (_Client != null)
{
if (FormTcpSimpleProxy.LogFlag)
{
if (IsBinary(e.Buffer))
{
fileText.WriteLine("\r\nServer Binary Data: {0} bytes", e.Buffer.Length);
if (fileBinary == null)
{
fileBinary = File.Create(FileName + ".bin");
}
fileBinary.Write(e.Buffer, 0, e.Buffer.Length);
fileBinary.Flush();
}
else
{
fileText.WriteLine("\r\nServer Text:\r\n{0}", System.Text.Encoding.ASCII.GetString(e.Buffer));
}
fileText.Flush();
}
try
{
_Client.SendNow(e.Buffer, 0, e.Buffer.Length);
}
catch (Exception ex)
{
FormTcpSimpleProxy.Log("_Server_DataReceived() Error: {0}", ex.Message);
ForceClose();
}
}
}
catch (Exception ex)
{
Console.WriteLine("TcpProxyConnection._Server_Disconnected() - " + ex.Message);
Console.WriteLine(ex.StackTrace);
}
}
private void _Server_Disconnected(object Sender, DisconnectedEventArgs e)
{
try
{
ForceClose();
}
catch (Exception ex)
{
Console.WriteLine("TcpProxyConnection._Server_Disconnected() - " + ex.Message);
Console.WriteLine(ex.StackTrace);
}
}
public void Close()
{
try
{
if (_Server != null)
{
_Server.Close();
_Server = null;
}
if (_Client != null)
{
_Client.Close();
_Client = null;
}
ForceClose();
}
catch (Exception ex)
{
Console.WriteLine("TcpProxyConnection.Close() - " + ex.Message);
Console.WriteLine(ex.StackTrace);
}
}
public bool IsBinary(byte[] buffer)
{
return IsBinary(buffer, 0, buffer.Length);
}
public bool IsBinary(byte[] buffer, int Start, int Length)
{
for (int i = 0; i < Length; i++)
{
byte b = buffer[Start + i];
if (b != 10 && b != 13 && b != 9 && b < 32)
{
return true;
}
}
return false;
}
}