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

◆ EstablishSocks4TunnelAsync()

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

Definition at line 134 of file SocksHelper.cs.

135 {
136 byte[] buffer = ArrayPool<byte>.Shared.Rent(513);
137 try
138 {
139 buffer[0] = 4;
140 buffer[1] = 1;
141 BinaryPrimitives.WriteUInt16BigEndian(buffer.AsSpan(2), (ushort)port);
142 IPAddress iPAddress = null;
143 if (IPAddress.TryParse(host, out IPAddress address))
144 {
145 if (address.AddressFamily == AddressFamily.InterNetwork)
146 {
147 iPAddress = address;
148 }
149 else
150 {
151 if (!address.IsIPv4MappedToIPv6)
152 {
153 throw new SocksException(System.SR.net_socks_ipv6_notsupported);
154 }
155 iPAddress = address.MapToIPv4();
156 }
157 }
158 else if (!isVersion4a)
159 {
160 IPAddress[] array2;
161 try
162 {
163 IPAddress[] array = ((!async) ? Dns.GetHostAddresses(host, AddressFamily.InterNetwork) : (await Dns.GetHostAddressesAsync(host, AddressFamily.InterNetwork, cancellationToken).ConfigureAwait(continueOnCapturedContext: false)));
164 array2 = array;
165 }
166 catch (Exception innerException)
167 {
168 throw new SocksException(System.SR.net_socks_no_ipv4_address, innerException);
169 }
170 if (array2.Length == 0)
171 {
172 throw new SocksException(System.SR.net_socks_no_ipv4_address);
173 }
174 iPAddress = array2[0];
175 }
176 if (iPAddress == null)
177 {
178 buffer[4] = 0;
179 buffer[5] = 0;
180 buffer[6] = 0;
181 buffer[7] = byte.MaxValue;
182 }
183 else
184 {
185 iPAddress.TryWriteBytes(buffer.AsSpan(4), out var _);
186 }
187 byte b = EncodeString(credentials?.UserName, buffer.AsSpan(8), "UserName");
188 buffer[8 + b] = 0;
189 int num = 9 + b;
190 if (iPAddress == null)
191 {
192 byte b2 = EncodeString(host, buffer.AsSpan(num), "host");
193 buffer[num + b2] = 0;
194 num += b2 + 1;
195 }
196 await WriteAsync(stream, buffer.AsMemory(0, num), async).ConfigureAwait(continueOnCapturedContext: false);
197 await ReadToFillAsync(stream, buffer.AsMemory(0, 8), async).ConfigureAwait(continueOnCapturedContext: false);
198 switch (buffer[1])
199 {
200 case 93:
201 throw new SocksException(System.SR.net_socks_auth_failed);
202 default:
203 throw new SocksException(System.SR.net_socks_connection_failed);
204 case 90:
205 break;
206 }
207 }
208 finally
209 {
211 }
212 }
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 string net_socks_ipv6_notsupported
Definition SR.cs:216
static string net_socks_auth_failed
Definition SR.cs:210
static string net_socks_no_ipv4_address
Definition SR.cs:220
static string net_socks_connection_failed
Definition SR.cs:214
Definition SR.cs:7

References System.array, System.buffer, System.cancellationToken, System.Net.Http.SocksHelper.EncodeString(), System.Net.Dns.GetHostAddresses(), System.Net.Dns.GetHostAddressesAsync(), System.Net.IPAddress.MapToIPv4(), System.SR.net_socks_auth_failed, System.SR.net_socks_connection_failed, System.SR.net_socks_ipv6_notsupported, System.SR.net_socks_no_ipv4_address, System.Net.Http.SocksHelper.ReadToFillAsync(), System.Buffers.ArrayPool< T >.Shared, System.stream, System.Net.IPAddress.TryParse(), System.Net.IPAddress.TryWriteBytes(), System.Net.Http.SocksHelper.WriteAsync(), and System.Buffers.Binary.BinaryPrimitives.WriteUInt16BigEndian().

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