forked from vpnhood/VpnHood
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TcpHost.cs
418 lines (363 loc) · 18.1 KB
/
TcpHost.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
408
409
410
411
412
413
414
415
416
417
418
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Security;
using System.Net.Sockets;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using VpnHood.Common.Exceptions;
using VpnHood.Common.Logging;
using VpnHood.Common.Messaging;
using VpnHood.Common.Net;
using VpnHood.Common.Utils;
using VpnHood.Server.Exceptions;
using VpnHood.Tunneling;
using VpnHood.Tunneling.Messaging;
namespace VpnHood.Server;
internal class TcpHost : IAsyncDisposable
{
private const int ServerProtocolVersion = 2;
private readonly TimeSpan _requestTimeout = TimeSpan.FromSeconds(60);
private CancellationTokenSource? _cancellationTokenSource;
private readonly SessionManager _sessionManager;
private readonly SslCertificateManager _sslCertificateManager;
private readonly List<TcpListener> _tcpListeners = new();
private Task? _startTask;
private bool _disposed;
public bool IsIpV6Supported { get; set; }
public IpRange[]? NetFilterPacketCaptureIncludeIpRanges { get; set; }
public IpRange[]? NetFilterIncludeIpRanges { get; set; }
public bool IsStarted { get; private set; }
public IPEndPoint[] TcpEndPoints { get; private set; } = Array.Empty<IPEndPoint>();
public TcpHost(SessionManager sessionManager, SslCertificateManager sslCertificateManager)
{
_sslCertificateManager = sslCertificateManager ?? throw new ArgumentNullException(nameof(sslCertificateManager));
_sessionManager = sessionManager;
}
public void Start(IPEndPoint[] tcpEndPoints)
{
if (_disposed) throw new ObjectDisposedException(GetType().Name);
if (IsStarted) throw new Exception($"{nameof(TcpHost)} is already Started!");
if (tcpEndPoints.Length == 0) throw new Exception("No TcpEndPoint has been configured!");
_cancellationTokenSource = new CancellationTokenSource();
var cancellationToken = _cancellationTokenSource.Token;
IsStarted = true;
try
{
var tasks = new List<Task>();
lock (_tcpListeners)
{
foreach (var tcpEndPoint in tcpEndPoints)
{
VhLogger.Instance.LogInformation("Start listening on TcpEndPoint: {TcpEndPoint}", VhLogger.Format(tcpEndPoint));
cancellationToken.ThrowIfCancellationRequested();
var tcpListener = new TcpListener(tcpEndPoint);
tcpListener.Start();
_tcpListeners.Add(tcpListener);
tasks.Add(ListenTask(tcpListener, cancellationToken));
}
}
TcpEndPoints = tcpEndPoints;
_startTask = Task.WhenAll(tasks);
}
catch
{
IsStarted = false;
throw;
}
}
public async Task Stop()
{
VhLogger.Instance.LogTrace($"Stopping {nameof(TcpHost)}...");
_cancellationTokenSource?.Cancel();
_sslCertificateManager.ClearCache();
lock (_tcpListeners)
{
foreach (var tcpListener in _tcpListeners)
tcpListener.Stop();
_tcpListeners.Clear();
}
if (_startTask != null)
{
await _startTask;
_startTask = null;
}
IsStarted = false;
}
private async Task ListenTask(TcpListener tcpListener, CancellationToken cancellationToken)
{
var localEp = (IPEndPoint)tcpListener.LocalEndpoint;
var errorCounter = 0;
const int maxErrorCount = 200;
// Listening for new connection
while (!cancellationToken.IsCancellationRequested)
{
try
{
var tcpClient = await tcpListener.AcceptTcpClientAsync();
_ = ProcessClient(tcpClient, cancellationToken);
errorCounter = 0;
}
catch (SocketException ex) when (ex.SocketErrorCode == SocketError.OperationAborted)
{
errorCounter++;
}
catch (ObjectDisposedException)
{
errorCounter++;
}
catch (Exception ex)
{
errorCounter++;
VhLogger.Instance.LogError(GeneralEventId.Tcp, ex, "TcpHost could not AcceptTcpClient. ErrorCounter: {ErrorCounter}", errorCounter);
if (errorCounter > maxErrorCount)
{
VhLogger.Instance.LogError("Too many unexpected errors in AcceptTcpClient. Stopping the TcpHost...");
_ = Stop();
}
}
}
tcpListener.Stop();
VhLogger.Instance.LogInformation($"Listening on {VhLogger.Format(localEp)} has been stopped.");
}
private static async Task<SslStream> AuthenticateAsServerAsync(TcpClient tcpClient, X509Certificate certificate,
CancellationToken cancellationToken)
{
try
{
VhLogger.Instance.LogInformation(GeneralEventId.Tcp, $"TLS Authenticating. CertSubject: {certificate.Subject}...");
var sslStream = new SslStream(tcpClient.GetStream(), true);
await sslStream.AuthenticateAsServerAsync(
new SslServerAuthenticationOptions
{
ServerCertificate = certificate,
ClientCertificateRequired = false,
CertificateRevocationCheckMode = X509RevocationMode.NoCheck
},
cancellationToken);
return sslStream;
}
catch (Exception ex)
{
throw new TlsAuthenticateException("TLS Authentication error.", ex);
}
}
private async Task ProcessClient(TcpClient tcpClient, CancellationToken cancellationToken)
{
using var scope = VhLogger.Instance.BeginScope($"RemoteEp: {VhLogger.Format(tcpClient.Client.RemoteEndPoint)}");
// add timeout to cancellationToken
using var timeoutCt = new CancellationTokenSource(_requestTimeout);
using var cancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(timeoutCt.Token, cancellationToken);
cancellationToken = cancellationTokenSource.Token;
TcpClientStream? tcpClientStream = null;
var succeeded = false;
try
{
// find certificate by ip
var certificate = await _sslCertificateManager.GetCertificate((IPEndPoint)tcpClient.Client.LocalEndPoint);
// establish SSL
var sslStream = await AuthenticateAsServerAsync(tcpClient, certificate, cancellationToken);
// process request
tcpClientStream = new TcpClientStream(tcpClient, sslStream);
await ProcessRequest(tcpClientStream, cancellationToken);
succeeded = true;
}
catch (ObjectDisposedException)
{
VhLogger.Instance.LogTrace(GeneralEventId.Tcp, "Connection has been closed.");
}
catch (TlsAuthenticateException ex) when (ex.InnerException is OperationCanceledException)
{
VhLogger.Instance.LogInformation(GeneralEventId.Tls, "Client TLS authentication has been canceled.");
}
catch (TlsAuthenticateException ex)
{
VhLogger.Instance.LogInformation(GeneralEventId.Tls, ex, "Error in Client TLS authentication.");
}
catch (SessionException ex) when (tcpClientStream != null)
{
// reply the error to caller if it is SessionException
// Should not reply anything when user is unknown
await StreamUtil.WriteJsonAsync(tcpClientStream.Stream, new SessionResponseBase(ex.SessionResponseBase),
cancellationToken);
if (ex is ISelfLog loggable)
loggable.Log();
else
VhLogger.Instance.LogInformation(ex.SessionResponseBase.ErrorCode == SessionErrorCode.GeneralError ? GeneralEventId.Tcp : GeneralEventId.Session, ex,
"Could not process the request. SessionErrorCode: {SessionErrorCode}", ex.SessionResponseBase.ErrorCode);
}
catch (Exception ex) when (tcpClientStream != null)
{
// return 401 for ANY non SessionException to keep server's anonymity
// Server should always return 404 as error
var unauthorizedResponse =
"HTTP/1.1 401 Unauthorized\r\n" + "Content-Length: 0\r\n" +
$"Date: {DateTime.UtcNow:r}\r\n" +
"Server: Kestrel\r\n" + "WWW-Authenticate: Bearer\r\n";
await tcpClientStream.Stream.WriteAsync(Encoding.UTF8.GetBytes(unauthorizedResponse), cancellationToken);
if (ex is ISelfLog loggable)
loggable.Log();
else
VhLogger.Instance.LogInformation(GeneralEventId.Tcp, ex, "Could not process the request and return 401.");
}
catch (Exception ex)
{
if (ex is ISelfLog loggable)
loggable.Log();
else
VhLogger.Instance.LogInformation(ex, "TcpHost could not process this request.");
}
finally
{
if (!succeeded)
{
tcpClientStream?.Dispose();
tcpClient.Dispose();
}
}
}
private async Task ProcessRequest(TcpClientStream tcpClientStream, CancellationToken cancellationToken)
{
// read version
VhLogger.Instance.LogTrace(GeneralEventId.Tcp, "Waiting for request...");
var buffer = new byte[2];
var res = await tcpClientStream.Stream.ReadAsync(buffer, 0, buffer.Length, cancellationToken);
if (res != buffer.Length)
throw new Exception("Connection closed before receiving any request!");
// check request version
var version = buffer[0];
if (version != 1)
throw new NotSupportedException("The request version is not supported!");
// read request code
var requestCode = (RequestCode)buffer[1];
switch (requestCode)
{
case RequestCode.Hello:
await ProcessHello(tcpClientStream, cancellationToken);
break;
case RequestCode.TcpDatagramChannel:
await ProcessTcpDatagramChannel(tcpClientStream, cancellationToken);
break;
case RequestCode.TcpProxyChannel:
await ProcessTcpProxyChannel(tcpClientStream, cancellationToken);
break;
case RequestCode.UdpChannel:
await ProcessUdpChannel(tcpClientStream, cancellationToken);
break;
case RequestCode.Bye:
await ProcessBye(tcpClientStream, cancellationToken);
break;
default:
throw new NotSupportedException($"Unknown requestCode. requestCode: {requestCode}");
}
}
private async Task ProcessHello(TcpClientStream tcpClientStream, CancellationToken cancellationToken)
{
var ipEndPointPair = tcpClientStream.IpEndPointPair;
// reading request
VhLogger.Instance.LogTrace(GeneralEventId.Session, "Processing hello request... ClientIp: {ClientIp}",
VhLogger.Format(ipEndPointPair.RemoteEndPoint.Address));
var request = await StreamUtil.ReadJsonAsync<HelloRequest>(tcpClientStream.Stream, cancellationToken);
// creating a session
VhLogger.Instance.LogTrace(GeneralEventId.Session, "Creating a session... TokenId: {TokenId}, ClientId: {ClientId}, ClientVersion: {ClientVersion}, UserAgent: {UserAgent}",
VhLogger.FormatId(request.TokenId), VhLogger.FormatId(request.ClientInfo.ClientId), request.ClientInfo.ClientVersion, request.ClientInfo.UserAgent);
var sessionResponse = await _sessionManager.CreateSession(request, ipEndPointPair);
var session = _sessionManager.GetSessionById(sessionResponse.SessionId) ?? throw new InvalidOperationException("Session is lost!");
session.UseUdpChannel = request.UseUdpChannel;
// check client version; unfortunately it must be after CreateSession to preserver server anonymity
if (request.ClientInfo == null || request.ClientInfo.ProtocolVersion < 2)
throw new ServerSessionException(tcpClientStream.RemoteEndPoint, session, SessionErrorCode.UnsupportedClient,
"This client is outdated and not supported anymore! Please update your app.");
// Report new session
var clientIp = _sessionManager.TrackingOptions.TrackClientIpValue ? VhLogger.Format(ipEndPointPair.RemoteEndPoint.Address) : "*";
VhLogger.Instance.LogInformation(GeneralEventId.SessionTrack,
"SessionId: {SessionId-5}\t{Mode,-5}\tTokenId: {TokenId}\tClientCount: {ClientCount,-3}\tClientId: {ClientId}\tClientIp: {ClientIp-15}\tVersion: {Version}\tOS: {OS}",
VhLogger.FormatSessionId(session.SessionId), "New", VhLogger.FormatId(request.TokenId), session.SessionResponse.AccessUsage?.ActiveClientCount, VhLogger.FormatId(request.ClientInfo.ClientId), clientIp, request.ClientInfo.ClientVersion, UserAgentParser.GetOperatingSystem(request.ClientInfo.UserAgent));
VhLogger.Instance.LogInformation(GeneralEventId.Session, "SessionId: {SessionId}, Agent: {Agent}", VhLogger.FormatSessionId(session.SessionId), request.ClientInfo.UserAgent);
//tracking
if (_sessionManager.TrackingOptions.IsEnabled)
{
VhLogger.Instance.LogInformation(GeneralEventId.Track,
"{Proto}; SessionId {SessionId}; TokenId {TokenId}; ClientIp {clientIp}".Replace("; ", "\t"),
"NewS", VhLogger.FormatSessionId(session.SessionId), VhLogger.FormatId(request.TokenId), clientIp);
}
// reply hello session
VhLogger.Instance.LogTrace(GeneralEventId.Session,
$"Replying Hello response. SessionId: {VhLogger.FormatSessionId(sessionResponse.SessionId)}");
var helloResponse = new HelloSessionResponse(sessionResponse)
{
SessionId = sessionResponse.SessionId,
SessionKey = sessionResponse.SessionKey,
UdpKey = session.UdpChannel?.Key,
UdpPort = session.UdpChannel?.LocalPort ?? 0,
ServerVersion = _sessionManager.ServerVersion,
ServerProtocolVersion = ServerProtocolVersion,
SuppressedTo = sessionResponse.SuppressedTo,
AccessUsage = sessionResponse.AccessUsage,
MaxDatagramChannelCount = session.Tunnel.MaxDatagramChannelCount,
ClientPublicAddress = ipEndPointPair.RemoteEndPoint.Address,
IncludeIpRanges = NetFilterIncludeIpRanges,
PacketCaptureIncludeIpRanges = NetFilterPacketCaptureIncludeIpRanges,
IsIpV6Supported = IsIpV6Supported,
ErrorCode = SessionErrorCode.Ok
};
await StreamUtil.WriteJsonAsync(tcpClientStream.Stream, helloResponse, cancellationToken);
tcpClientStream.Dispose();
}
private async Task ProcessUdpChannel(TcpClientStream tcpClientStream, CancellationToken cancellationToken)
{
VhLogger.Instance.LogTrace(GeneralEventId.Udp, "Reading a TcpDatagramChannel request...");
var request = await StreamUtil.ReadJsonAsync<UdpChannelRequest>(tcpClientStream.Stream, cancellationToken);
// finding session
var session = await _sessionManager.GetSession(request, tcpClientStream.IpEndPointPair);
// enable udp
session.UseUdpChannel = true;
if (session.UdpChannel == null)
throw new InvalidOperationException("UdpChannel is not initialized.");
// send OK reply
await StreamUtil.WriteJsonAsync(tcpClientStream.Stream, new UdpChannelSessionResponse(session.SessionResponse)
{
UdpKey = session.UdpChannel.Key,
UdpPort = session.UdpChannel.LocalPort
}, cancellationToken);
tcpClientStream.Dispose();
}
private async Task ProcessBye(TcpClientStream tcpClientStream, CancellationToken cancellationToken)
{
VhLogger.Instance.LogTrace(GeneralEventId.TcpProxyChannel, $"Reading the {RequestCode.Bye} request ...");
var request = await StreamUtil.ReadJsonAsync<RequestBase>(tcpClientStream.Stream, cancellationToken);
// finding session
using var scope = VhLogger.Instance.BeginScope($"SessionId: {VhLogger.FormatSessionId(request.SessionId)}");
var session = await _sessionManager.GetSession(request, tcpClientStream.IpEndPointPair);
// Before calling CloseSession session must be validated by GetSession
await _sessionManager.CloseSession(session.SessionId);
tcpClientStream.Dispose();
}
private async Task ProcessTcpDatagramChannel(TcpClientStream tcpClientStream, CancellationToken cancellationToken)
{
VhLogger.Instance.LogTrace(GeneralEventId.TcpProxyChannel, $"Reading the {nameof(TcpDatagramChannelRequest)} ...");
var request = await StreamUtil.ReadJsonAsync<TcpDatagramChannelRequest>(tcpClientStream.Stream, cancellationToken);
// finding session
using var scope = VhLogger.Instance.BeginScope($"SessionId: {VhLogger.FormatSessionId(request.SessionId)}");
var session = await _sessionManager.GetSession(request, tcpClientStream.IpEndPointPair);
await session.ProcessTcpDatagramChannelRequest(tcpClientStream, cancellationToken);
}
private async Task ProcessTcpProxyChannel(TcpClientStream tcpClientStream, CancellationToken cancellationToken)
{
VhLogger.Instance.LogInformation(GeneralEventId.TcpProxyChannel, $"Reading the {nameof(TcpProxyChannelRequest)} ...");
var request = await StreamUtil.ReadJsonAsync<TcpProxyChannelRequest>(tcpClientStream.Stream, cancellationToken);
// find session
using var scope = VhLogger.Instance.BeginScope($"SessionId: {VhLogger.FormatSessionId(request.SessionId)}");
var session = await _sessionManager.GetSession(request, tcpClientStream.IpEndPointPair);
await session.ProcessTcpChannelRequest(tcpClientStream, request, cancellationToken);
}
public async ValueTask DisposeAsync()
{
if (_disposed) return;
_disposed = true;
await Stop();
}
}