1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
|
public Proxy()
{
IPEndPoint endpoint = new IPEndPoint(IPAddress.Any, 8081);
TcpListener listener = new TcpListener(endpoint);
listener.Start();
while (true)
{
TcpClient client = listener.AcceptTcpClient();
if (client != null)
{
Thread t = new Thread(() =>
{
/**
* Client => Proxy
**/
NetworkStream nwStream = client.GetStream();
List<byte[]> _buffer_send = new List<byte[]>();
byte[] buffer = new byte[client.ReceiveBufferSize];
int rec;
string data = "";
do
{
// relay message to the server.
rec = nwStream.Read(buffer, 0, client.ReceiveBufferSize);
_buffer_send.Add(buffer);
data += Encoding.ASCII.GetString(buffer, 0, rec);
} while (nwStream.DataAvailable);
nwStream.Flush();
Console.WriteLine(data.Trim());
Console.WriteLine("=================================================");
/**
* Parse Client Header
*
**/
string hostname = "localhost";
/**
* Proxy => Host
**/
TcpClient _client = new TcpClient(hostname, 443);
NetworkStream _nwStream = _client.GetStream();
// relay message to the server.
foreach (byte[] buffer_req in _buffer_send)
{
_nwStream.Write(buffer_req, 0, buffer_req.Length);
}
_nwStream.Flush();
/**
* Host => Proxy
**/
List<byte[]> _buffer_back = new List<byte[]>();
int _rec;
string _data = "";
byte[] _buffer = new byte[client.ReceiveBufferSize];
do
{
// relay message to the server.
_rec = _nwStream.Read(_buffer, 0, client.ReceiveBufferSize);
_buffer_back.Add(_buffer);
_data += Encoding.ASCII.GetString(_buffer, 0, _rec);
} while (_nwStream.DataAvailable);
_nwStream.Flush();
Console.WriteLine(_data.Trim());
Console.WriteLine("=================================================");
/**
* Proxy => Client
**/
// relay message to the server.
foreach (byte[] buffer_res in _buffer_back)
{
nwStream.Write(buffer_res, 0, buffer_res.Length);
}
nwStream.Flush();
_nwStream.Close();
_client.Close();
nwStream.Close();
client.Close();
});
t.Start();
//client.Close();
//listener.Stop();
//Console.ReadLine();
}
}
} |
Partager