-
-
Notifications
You must be signed in to change notification settings - Fork 980
Expand file tree
/
Copy pathProxyConnector.cs
More file actions
100 lines (88 loc) · 3.84 KB
/
ProxyConnector.cs
File metadata and controls
100 lines (88 loc) · 3.84 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
using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
namespace Renci.SshNet.Connection
{
/// <summary>
/// Represents a connector that uses a proxy server to establish a connection to a given SSH
/// endpoint.
/// </summary>
internal abstract class ProxyConnector : ConnectorBase
{
protected ProxyConnector(ISocketFactory socketFactory, ILoggerFactory loggerFactory)
: base(socketFactory, loggerFactory)
{
}
protected abstract void HandleProxyConnect(IConnectionInfo connectionInfo, Socket socket);
// ToDo: Performs async/sync fallback, true async version should be implemented in derived classes
protected virtual
#if NET
async
#endif
Task HandleProxyConnectAsync(IConnectionInfo connectionInfo, Socket socket, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
#if NET
await using (cancellationToken.Register(o => ((Socket)o).Dispose(), socket, useSynchronizationContext: false).ConfigureAwait(continueOnCapturedContext: false))
#else
using (cancellationToken.Register(o => ((Socket)o).Dispose(), socket, useSynchronizationContext: false))
#endif
{
#pragma warning disable MA0042 // Do not use blocking calls in an async method; false positive caused by https://github.com/meziantou/Meziantou.Analyzer/issues/613
HandleProxyConnect(connectionInfo, socket);
#pragma warning restore MA0042 // Do not use blocking calls in an async method
}
#if !NET
return Task.CompletedTask;
#endif
}
/// <summary>
/// Connects to a SSH endpoint using the specified <see cref="IConnectionInfo"/>.
/// </summary>
/// <param name="connectionInfo">The <see cref="IConnectionInfo"/> to use to establish a connection to a SSH endpoint.</param>
/// <returns>
/// A <see cref="Socket"/> connected to the SSH endpoint represented by the specified <see cref="IConnectionInfo"/>.
/// </returns>
public override Socket Connect(IConnectionInfo connectionInfo)
{
var socket = SocketConnect(new DnsEndPoint(connectionInfo.ProxyHost, connectionInfo.ProxyPort), connectionInfo.Timeout);
try
{
HandleProxyConnect(connectionInfo, socket);
return socket;
}
catch (Exception)
{
socket.Shutdown(SocketShutdown.Both);
socket.Dispose();
throw;
}
}
/// <summary>
/// Asynchronously connects to a SSH endpoint using the specified <see cref="IConnectionInfo"/>.
/// </summary>
/// <param name="connectionInfo">The <see cref="IConnectionInfo"/> to use to establish a connection to a SSH endpoint.</param>
/// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
/// <returns>
/// A <see cref="Socket"/> connected to the SSH endpoint represented by the specified <see cref="IConnectionInfo"/>.
/// </returns>
public override async Task<Socket> ConnectAsync(IConnectionInfo connectionInfo, CancellationToken cancellationToken)
{
var socket = await SocketConnectAsync(new DnsEndPoint(connectionInfo.ProxyHost, connectionInfo.ProxyPort), cancellationToken).ConfigureAwait(false);
try
{
await HandleProxyConnectAsync(connectionInfo, socket, cancellationToken).ConfigureAwait(false);
return socket;
}
catch (Exception)
{
socket.Shutdown(SocketShutdown.Both);
socket.Dispose();
throw;
}
}
}
}