-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathHttpsReverseProxy.cs
407 lines (326 loc) · 13.4 KB
/
HttpsReverseProxy.cs
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
namespace HttpReverseProxy
{
using HttpReverseProxyLib;
using HttpReverseProxyLib.DataTypes.Class;
using HttpReverseProxyLib.DataTypes.Enum;
using HttpReverseProxyLib.DataTypes.Interface;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Security;
using System.Net.Sockets;
using System.Reflection;
using System.Security.Authentication;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading;
public sealed class HttpsReverseProxy : HttpReverseProxyBasis, IPluginHost
{
#region MEMBERS
private static X509Certificate2 serverCertificate2;
private static RemoteCertificateValidationCallback remoteCertificateValidation = new RemoteCertificateValidationCallback(delegate { return true; });
private TcpListener tcpListener;
private Thread tcpListenerThread;
// private Lib.PluginCalls pluginCalls;
#endregion
#region PROPERTIES
public static HttpsReverseProxy Server { get; set; } = new HttpsReverseProxy();
public IPAddress ListeningIpInterface { get; private set; } = IPAddress.Any;
public int ListeningPort { get; private set; } = Config.LocalHttpsServerPort;
public List<IPlugin> LoadedPlugins { get; private set; } = Config.LoadedPlugins;
#endregion
#region PUBLIC
public override bool Start(int localServerPort, string certificateFilePath)
{
// Initialize general values
Config.RemoteHostIp = "0.0.0.0";
//this.pluginCalls = new Lib.PluginCalls();
// Load all plugins
//this.LoadAllPlugins();
// Start listener
serverCertificate2 = new X509Certificate2(certificateFilePath, string.Empty);
this.tcpListener = new TcpListener(this.ListeningIpInterface, localServerPort);
Console.WriteLine("Thumbprint:" + serverCertificate2.Thumbprint);
Console.WriteLine("Subject:" + serverCertificate2.Subject);
Console.WriteLine("FriendlyName:" + serverCertificate2.FriendlyName);
Logging.Instance.LogMessage("TcpListener", ProxyProtocol.Undefined, Loglevel.Info, "HTTPS reverse proxy server started on port {0} with certificate file {1}. Certificate subject: {2}, issuer:{3}", localServerPort, Path.GetFileName(certificateFilePath), serverCertificate2.Subject, serverCertificate2.Issuer);
try
{
this.tcpListener.Start();
}
catch (Exception ex)
{
Logging.Instance.LogMessage("TcpListener", ProxyProtocol.Undefined, Loglevel.Error, "ProxyServer.Start(EXCEPTION): {0}", ex.Message);
return false;
}
this.tcpListenerThread = new Thread(new ParameterizedThreadStart(HandleHttpsClient));
this.tcpListenerThread.Start(this.tcpListener);
return true;
}
public override void Stop()
{
if (this.tcpListener == null)
{
return;
}
this.tcpListener.Stop();
// Wait for cRemoteSocket to finish processing current connections...
if (this.tcpListenerThread?.IsAlive == true)
{
this.tcpListenerThread.Abort();
this.tcpListenerThread.Join();
}
// Unload loaded plugins
//this.UnloadAllPlugins();
}
public void LoadPlugin(string pluginFileFullPath)
{
Assembly pluginAssembly;
if ((pluginAssembly = Assembly.LoadFile(pluginFileFullPath)) == null)
{
throw new Exception("The plugin file could not be loaded");
}
try
{
var fileName = Path.GetFileName(pluginFileFullPath);
fileName = Path.GetFileNameWithoutExtension(fileName);
var pluginName = $"HttpReverseProxy.Plugin.{fileName}.{fileName}";
Type objType = pluginAssembly.GetType(pluginName, false, false);
object tmpPluginObj = Activator.CreateInstance(objType, true);
if (!(tmpPluginObj is IPlugin))
{
throw new Exception("The plugin file does not support the required plugin interface");
}
var tmpPlugin = (IPlugin)tmpPluginObj;
if (Config.LoadedPlugins.Find(elem => elem.Config.Name == tmpPlugin.Config.Name) != null)
{
throw new Exception("This plugin was loaded already");
}
tmpPlugin.OnLoad(this);
}
catch (Exception ex)
{
var filename = Path.GetFileName(pluginFileFullPath);
Console.WriteLine($"An error occurred while loading HTTPS plugin file \"{filename}\": {ex.Message}\r\n{ex.StackTrace}");
}
}
#endregion
#region PRIVATE
private static void HandleHttpsClient(object tcpListenerObj)
{
TcpListener tcpListener = (TcpListener)tcpListenerObj;
try
{
while (true)
{
Logging.Instance.LogMessage("TcpListener", ProxyProtocol.Undefined, Loglevel.Debug, "Waiting for incoming HTTPS request");
TcpClient tcpClient = tcpListener.AcceptTcpClient();
tcpClient.NoDelay = true;
while (!ThreadPool.QueueUserWorkItem(new WaitCallback(HttpsReverseProxy.InitiateHttpsClientRequestProcessing), tcpClient))
{
;
}
}
}
catch (ThreadAbortException taex)
{
Console.WriteLine($"HandleHttpsClient(ThreadAbortException): {taex.Message}");
}
catch (SocketException sex)
{
Console.WriteLine($"HandleHttpsClient(SocketException): {sex.Message}");
}
catch (Exception ex)
{
Console.WriteLine($"HandleHttpsClient(Exception): {ex.Message}");
}
}
private static void InitiateHttpsClientRequestProcessing(object clientTcpObj)
{
TcpClient tcpClient = (TcpClient)clientTcpObj;
var clientIp = string.Empty;
var clientPort = string.Empty;
var clientMac = string.Empty;
RequestObj requestObj = new RequestObj(Config.DefaultRemoteHost, ProxyProtocol.Https);
// Determine tcpClient IP and MAC address.
try
{
Logging.Instance.LogMessage("TcpListener", ProxyProtocol.Https, Loglevel.Debug, "InitiateHttpsClientRequestProcessing(): New HTTPS request initiated");
string[] splitter = tcpClient.Client.RemoteEndPoint.ToString().Split(new char[] { ':' });
clientIp = splitter[0];
clientPort = splitter[1];
}
catch (Exception ex)
{
Console.WriteLine($"InitiateHttpsClientRequestProcessing(Exception): {ex.Message}");
}
try
{
clientMac = Lib.Common.GetMacFromNetworkComputer(clientIp);
}
catch (Exception)
{
clientMac = "00:00:00:00:00:00";
}
requestObj.SrcMac = clientMac;
requestObj.SrcIp = clientIp;
requestObj.SrcPort = clientPort;
requestObj.TcpClientConnection = tcpClient;
// Open tcpClient system's data clientStream
try
{
var sslStream = new SslStream(requestObj.TcpClientConnection.GetStream(), false, new RemoteCertificateValidationCallback(remoteCertificateValidation));
sslStream.AuthenticateAsServer(serverCertificate2, false, SslProtocols.Tls | SslProtocols.Tls11 | SslProtocols.Tls12 | SslProtocols.Ssl3, false);
requestObj.ClientRequestObj.ClientBinaryReader = new MyBinaryReader(requestObj.ProxyProtocol, sslStream, 8192, Encoding.UTF8, requestObj.Id);
requestObj.ClientRequestObj.ClientBinaryWriter = new BinaryWriter(sslStream);
RequestHandlerHttp requestHandler = new RequestHandlerHttp(requestObj);
requestHandler.ProcessClientRequest();
}
catch (Exception ex)
{
Logging.Instance.LogMessage(requestObj.Id, requestObj.ProxyProtocol, Loglevel.Error, $"ProxyServer.InitiateHttpsClientRequestProcessing(EXCEPTION): {ex.Message}\r\n{ex.GetType().ToString()}");
if (ex.InnerException is Exception)
{
Logging.Instance.LogMessage(requestObj.Id, requestObj.ProxyProtocol, Loglevel.Error, $"ProxyServer.InitiateHttpsClientRequestProcessing(INNEREXCEPTION): {ex.InnerException.Message}, {ex.GetType().ToString()}");
}
}
finally
{
if (requestObj.ClientRequestObj.ClientBinaryReader != null)
{
requestObj.ClientRequestObj.ClientBinaryReader.Close();
Logging.Instance.LogMessage(requestObj.Id, requestObj.ProxyProtocol, Loglevel.Debug, "ProxyServer.InitiateHttpsClientRequestProcessing(): ClientBinaryReader.Close()");
}
if (requestObj.ClientRequestObj.ClientBinaryWriter != null)
{
requestObj.ClientRequestObj.ClientBinaryWriter.Close();
Logging.Instance.LogMessage(requestObj.Id, requestObj.ProxyProtocol, Loglevel.Debug, "ProxyServer.InitiateHttpsClientRequestProcessing(): ClientBinaryWriter.Close()");
}
if (requestObj.ServerRequestHandler != null)
{
requestObj.ServerRequestHandler.CloseServerConnection();
Logging.Instance.LogMessage(requestObj.Id, requestObj.ProxyProtocol, Loglevel.Debug, "ProxyServer.InitiateHttpsClientRequestProcessing(): ServerRequestHandler.CloseServerConnection())");
}
if (requestObj.TcpClientConnection != null)
{
requestObj.TcpClientConnection.Close();
Logging.Instance.LogMessage(requestObj.Id, requestObj.ProxyProtocol, Loglevel.Debug, "ProxyServer.InitiateHttpsClientRequestProcessing(): TcpClientConnection.Close()");
}
}
}
/// <summary>
///
/// </summary>
private void LoadAllPlugins()
{
string pluginsPath = Path.Combine(Directory.GetCurrentDirectory(), "plugins");
string[] pluginDirs;
if (!Directory.Exists(pluginsPath))
{
return;
}
pluginDirs = Directory.GetDirectories(pluginsPath);
// Iterate through all plugin directories
foreach (string tmpPluginDir in pluginDirs)
{
string[] pluginFiles = Directory.GetFiles(tmpPluginDir, "*.dll");
// Load all plugin files, instantiate an object and initialize plugin.
foreach (string pluginFileFullPath in pluginFiles)
{
try
{
this.LoadPlugin(pluginFileFullPath);
}
catch (Exception ex)
{
Console.WriteLine("An error occurred while loading the plugin \"{0}\": {1}", Path.GetFileNameWithoutExtension(pluginFileFullPath), ex.Message);
}
}
}
}
private void UnloadAllPlugins()
{
List<IPlugin> tmpPluginList = new List<IPlugin>();
tmpPluginList.AddRange(Config.LoadedPlugins);
foreach (IPlugin tmpPlugin in tmpPluginList)
{
tmpPlugin.OnUnload();
Config.LoadedPlugins.Remove(tmpPlugin);
}
}
#endregion
#region SSL debugging methods (copied from MSDN)
static void DisplaySecurityLevel(SslStream stream)
{
Console.WriteLine("Cipher: {0} strength {1}", stream.CipherAlgorithm, stream.CipherStrength);
Console.WriteLine("Hash: {0} strength {1}", stream.HashAlgorithm, stream.HashStrength);
Console.WriteLine("Key exchange: {0} strength {1}", stream.KeyExchangeAlgorithm, stream.KeyExchangeStrength);
Console.WriteLine("Protocol: {0}", stream.SslProtocol);
}
static void DisplaySecurityServices(SslStream stream)
{
Console.WriteLine("Is authenticated: {0} as server? {1}", stream.IsAuthenticated, stream.IsServer);
Console.WriteLine("IsSigned: {0}", stream.IsSigned);
Console.WriteLine("Is Encrypted: {0}", stream.IsEncrypted);
}
static void DisplayStreamProperties(SslStream stream)
{
Console.WriteLine("Can read: {0}, write {1}", stream.CanRead, stream.CanWrite);
Console.WriteLine("Can timeout: {0}", stream.CanTimeout);
}
static void DisplayCertificateInformation(SslStream stream)
{
Console.WriteLine("Certificate revocation list checked: {0}", stream.CheckCertRevocationStatus);
X509Certificate localCertificate = stream.LocalCertificate;
if (stream.LocalCertificate != null)
{
Console.WriteLine("Local cert was issued to {0} and is valid from {1} until {2}.",
localCertificate.Subject,
localCertificate.GetEffectiveDateString(),
localCertificate.GetExpirationDateString());
}
else
{
Console.WriteLine("Local certificate is null.");
}
// Display the properties of the client's certificate.
X509Certificate remoteCertificate = stream.RemoteCertificate;
if (stream.RemoteCertificate != null)
{
Console.WriteLine("Remote cert was issued to {0} and is valid from {1} until {2}.",
remoteCertificate.Subject,
remoteCertificate.GetEffectiveDateString(),
remoteCertificate.GetExpirationDateString());
}
else
{
Console.WriteLine("Remote certificate is null.");
}
}
#endregion
#region INTERFACE: IPluginHost
/// <summary>
///
/// </summary>
/// <param name="pluginData"></param>
public void RegisterPlugin(IPlugin pluginData)
{
if (pluginData == null)
{
return;
}
lock (Config.LoadedPlugins)
{
List<IPlugin> foundPlugins = Config.LoadedPlugins.FindAll(elem => elem.Config.Name == pluginData.Config.Name);
if (foundPlugins == null || foundPlugins.Count <= 0)
{
Config.AddNewPlugin(pluginData);
Logging.Instance.LogMessage("HttpsReverseProxy", ProxyProtocol.Undefined, Loglevel.Info, "Registered plugin \"{0}\"", pluginData.Config.Name);
}
}
}
public Logging LoggingInst { get { return Logging.Instance; } set { } }
#endregion
}
}