-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProxySwapDataTask.cs
More file actions
208 lines (177 loc) · 8.93 KB
/
ProxySwapDataTask.cs
File metadata and controls
208 lines (177 loc) · 8.93 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
using System;
using System.Net;
using System.Net.Sockets;
using System.Net.Security;
using System.IO;
using System.Security.Authentication;
using System.Security.Cryptography.X509Certificates;
namespace WinProxy
{
/// <summary>
/// Summary description for ProxyDataTask.
/// </summary>
public class ProxySwapDataTask
{
ProxyConnection m_conn;
public ProxySwapDataTask(ProxyConnection conn)
{
m_conn = conn;
}
public static void connectForwardServerCallBack(IAsyncResult ar)
{
ProxyConnection conn = (ProxyConnection)ar.AsyncState;
conn.serverSocket.EndConnect(ar);
ProxySwapDataTask proxy = new ProxySwapDataTask(conn);
//now we have both server socket and client socket, we can pass the data back and forth for both side
System.Diagnostics.Debug.Assert(true == conn.clientSocket.Connected);
System.Diagnostics.Debug.Assert(true == conn.serverSocket.Connected);
WinTunnel.WriteTextToConsole(string.Format("ProxyConnection#{0}-- client socket or server socket start receiving data....",
conn.connNumber));
WinTunnel.connMgr.AddConnection(conn);
conn.clientStream = conn.clientSocket.GetStream();
if (conn.m_bHttpsClient)
{
SslStream sslStream = new SslStream(conn.clientSocket.GetStream(), false);
sslStream.AuthenticateAsServer(WinTunnel.CertificateForClientConnection, false, SslProtocols.Tls, true);
conn.clientStream = sslStream;
}
conn.serverStream = conn.serverSocket.GetStream();
if (conn.m_bHttpsServer)
{
SslStream sslStream = new SslStream(conn.serverStream, false, new RemoteCertificateValidationCallback(ValidateServerCertificate), null);
sslStream.AuthenticateAsClient(conn.serverName);
conn.serverStream = sslStream;
}
conn.clientStream.BeginRead(conn.clientReadBuffer, 0, ProxyConnection.BUFFER_SIZE, new AsyncCallback(clientReadCallBack), conn);
//Read data from the server socket
conn.serverStream.BeginRead(conn.serverReadBuffer, 0, ProxyConnection.BUFFER_SIZE, new AsyncCallback(serverReadCallBack), conn);
}
// The following method is invoked by the RemoteCertificateValidationDelegate.
public static bool ValidateServerCertificate(
object sender,
X509Certificate certificate,
X509Chain chain,
SslPolicyErrors sslPolicyErrors)
{
if (sslPolicyErrors == SslPolicyErrors.None)
return true;
Console.WriteLine("Certificate error: {0}", sslPolicyErrors);
// Do not allow this client to communicate with unauthenticated servers.
return false;
}
//the client socket gets data from client side, need to forward it to server socket, and then
//start to read from client socket again
private static void clientReadCallBack(IAsyncResult ar)
{
ProxyConnection conn = (ProxyConnection)ar.AsyncState;
if (!conn.isShutdown)
{
WinTunnel.WriteTextToConsole("client socket receives data callback called");
int numBytes = 0;
try
{
if (conn.clientSocket != null)
{
numBytes = conn.clientStream.EndRead(ar);
WinTunnel.WriteTextToConsole(string.Format("client socket receives data: {0}", numBytes));
if (numBytes > 0) //write to the server side socket
{
//copy the bytes to the server send buffer and call send
Array.Copy(conn.clientReadBuffer, 0, conn.serverSendBuffer, 0, numBytes);
//log the client request
Logger.log(conn.clientReadBuffer, 0, numBytes, "C{0}:----------------Client Request----------------: ",
conn.connNumber);
conn.serverNumBytes += numBytes;
conn.serverStream.Write(conn.serverSendBuffer, 0, numBytes);
//continue to read
conn.clientStream.BeginRead(conn.clientReadBuffer, 0, ProxyConnection.BUFFER_SIZE,
new AsyncCallback(clientReadCallBack), conn);
}
else
{
//do not close socket even if no data to read, as we still need write to it? need test to confirm this
WinTunnel.WriteTextToConsole(string.Format("ProxyConnection#{0}: Detected Client Socket disconnect via read.",
conn.connNumber));
//conn.Release();
}
}
else
{
WinTunnel.connMgr.Release(conn);
}
}
catch (SocketException se)
{
if (!conn.isShutdown)
{
WinTunnel.WriteTextToConsole(string.Format("ProxyConnection#{0}: Socket Error occurred when reading data from the client socket. Error Code is: {1}.",
conn.connNumber, se.ErrorCode));
WinTunnel.connMgr.Release(conn);
}
}
catch (Exception e)
{
if (!conn.isShutdown)
{
WinTunnel.WriteTextToConsole(string.Format("ProxyConnection#{0}: Error occurred when reading data from the client socket. The error is: {1}.",
conn.connNumber, e));
WinTunnel.connMgr.Release(conn);
}
}
}
}
//The server socket reads some data from server, needs to forward the data to client side, and then starts to
//read data again
private static void serverReadCallBack(IAsyncResult ar)
{
WinTunnel.WriteTextToConsole("server socket receives data callback called");
ProxyConnection conn = (ProxyConnection)ar.AsyncState;
int numBytes = 0;
if (!conn.isShutdown)
{
try
{
numBytes = conn.serverStream.EndRead(ar);
WinTunnel.WriteTextToConsole(string.Format("server socket receives data: {0}", numBytes));
if (numBytes > 0) //write to the client side socket
{
//copy the bytes to the client send buffer and call send
Array.Copy(conn.serverReadBuffer, 0, conn.clientSendBuffer, 0, numBytes);
//log the server response request
Logger.log(conn.serverReadBuffer, 0, numBytes, "C{0}**************Server response**************::",
conn.connNumber);
conn.clientNumBytes += numBytes;
conn.clientStream.Write(conn.clientSendBuffer, 0, numBytes);
conn.serverStream.BeginRead(conn.serverReadBuffer, 0, ProxyConnection.BUFFER_SIZE,
new AsyncCallback(serverReadCallBack), conn);
}
else
{
//Server must have disconnected the socket
WinTunnel.WriteTextToConsole(string.Format("ProxyConnection#{0}: Detected Server Socket disconnect via read.",
conn.connNumber));
WinTunnel.connMgr.Release(conn);
}
}
catch (SocketException se)
{
if (!conn.isShutdown)
{
WinTunnel.WriteTextToConsole(string.Format("ProxyConnection#{0}: Socket Error occurred when reading data from the server socket. Error Code is: {1}.",
conn.connNumber, se.ErrorCode));
WinTunnel.connMgr.Release(conn);
}
}
catch (Exception e)
{
if (!conn.isShutdown)
{
WinTunnel.WriteTextToConsole(string.Format("ProxyConnection#{0}: Error occurred when reading data from the server socket. The error is: {1}.",
conn.connNumber, e));
WinTunnel.connMgr.Release(conn);
}
}
}
}
}
}