Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches

◆ EstablishSocks5TunnelAsync()

static async ValueTask System.Net.Http.SocksHelper.EstablishSocks5TunnelAsync ( Stream stream,
string host,
int port,
Uri proxyUri,
NetworkCredential credentials,
bool async )
inlinestaticprivate

Definition at line 45 of file SocksHelper.cs.

46 {
47 byte[] buffer = ArrayPool<byte>.Shared.Rent(513);
48 try
49 {
50 buffer[0] = 5;
51 if (credentials == null)
52 {
53 buffer[1] = 1;
54 buffer[2] = 0;
55 }
56 else
57 {
58 buffer[1] = 2;
59 buffer[2] = 0;
60 buffer[3] = 2;
61 }
62 await WriteAsync(stream, buffer.AsMemory(0, buffer[1] + 2), async).ConfigureAwait(continueOnCapturedContext: false);
63 await ReadToFillAsync(stream, buffer.AsMemory(0, 2), async).ConfigureAwait(continueOnCapturedContext: false);
65 switch (buffer[1])
66 {
67 case 2:
68 {
69 if (credentials == null)
70 {
71 throw new SocksException(System.SR.net_socks_auth_required);
72 }
73 buffer[0] = 1;
74 byte b = (buffer[1] = EncodeString(credentials.UserName, buffer.AsSpan(2), "UserName"));
75 await WriteAsync(stream, buffer.AsMemory(0, 3 + b + (buffer[2 + b] = EncodeString(credentials.Password, buffer.AsSpan(3 + b), "Password"))), async).ConfigureAwait(continueOnCapturedContext: false);
76 await ReadToFillAsync(stream, buffer.AsMemory(0, 2), async).ConfigureAwait(continueOnCapturedContext: false);
77 if (buffer[0] != 1 || buffer[1] != 0)
78 {
79 throw new SocksException(System.SR.net_socks_auth_failed);
80 }
81 break;
82 }
83 default:
84 throw new SocksException(System.SR.net_socks_no_auth_method);
85 case 0:
86 break;
87 }
88 buffer[0] = 5;
89 buffer[1] = 1;
90 buffer[2] = 0;
91 int num;
92 if (IPAddress.TryParse(host, out IPAddress address))
93 {
94 if (address.AddressFamily == AddressFamily.InterNetwork)
95 {
96 buffer[3] = 1;
97 address.TryWriteBytes(buffer.AsSpan(4), out var _);
98 num = 4;
99 }
100 else
101 {
102 buffer[3] = 4;
103 address.TryWriteBytes(buffer.AsSpan(4), out var _);
104 num = 16;
105 }
106 }
107 else
108 {
109 buffer[3] = 3;
110 num = (buffer[4] = EncodeString(host, buffer.AsSpan(5), "host")) + 1;
111 }
112 BinaryPrimitives.WriteUInt16BigEndian(buffer.AsSpan(num + 4), (ushort)port);
113 await WriteAsync(stream, buffer.AsMemory(0, num + 6), async).ConfigureAwait(continueOnCapturedContext: false);
114 await ReadToFillAsync(stream, buffer.AsMemory(0, 5), async).ConfigureAwait(continueOnCapturedContext: false);
116 if (buffer[1] != 0)
117 {
118 throw new SocksException(System.SR.net_socks_connection_failed);
119 }
120 await ReadToFillAsync(stream, buffer.AsMemory(0, buffer[3] switch
121 {
122 1 => 5,
123 4 => 17,
124 3 => buffer[4] + 2,
125 _ => throw new SocksException(System.SR.net_socks_bad_address_type),
126 }), async).ConfigureAwait(continueOnCapturedContext: false);
127 }
128 finally
129 {
131 }
132 }
static ArrayPool< T > Shared
Definition ArrayPool.cs:7
static void WriteUInt16BigEndian(Span< byte > destination, ushort value)
static async ValueTask ReadToFillAsync(Stream stream, Memory< byte > buffer, bool async)
static ValueTask WriteAsync(Stream stream, Memory< byte > buffer, bool async)
static byte EncodeString(ReadOnlySpan< char > chars, Span< byte > buffer, string parameterName)
static void VerifyProtocolVersion(byte expected, byte version)
static string net_socks_no_auth_method
Definition SR.cs:218
static string net_socks_auth_failed
Definition SR.cs:210
static string net_socks_connection_failed
Definition SR.cs:214
static string net_socks_auth_required
Definition SR.cs:226
Definition SR.cs:7

References System.buffer, System.Net.Http.SocksHelper.EncodeString(), System.SR.net_socks_auth_failed, System.SR.net_socks_auth_required, System.SR.net_socks_connection_failed, System.SR.net_socks_no_auth_method, System.Net.NetworkCredential.Password, System.Net.Http.SocksHelper.ReadToFillAsync(), System.Buffers.ArrayPool< T >.Shared, System.stream, System.Net.IPAddress.TryParse(), System.Net.NetworkCredential.UserName, System.Net.Http.SocksHelper.VerifyProtocolVersion(), System.Net.Http.SocksHelper.WriteAsync(), and System.Buffers.Binary.BinaryPrimitives.WriteUInt16BigEndian().

Referenced by System.Net.Http.SocksHelper.EstablishSocksTunnelAsync().