Pass bytes via constructors.
MODIFY
WalletWasabi/Tor/Socks5/Models/Messages/MethodSelectionResponse.cs
WalletWasabi/Tor/Socks5/Models/Messages/MethodSelectionResponse.cs
+using System;
using WalletWasabi.Helpers;
using WalletWasabi.Tor.Socks5.Models.Bases;
using WalletWasabi.Tor.Socks5.Models.Fields.OctetFields;
@@ -6,21 +7,22 @@ namespace WalletWasabi.Tor.Socks5.Models.Messages
{
public class MethodSelectionResponse : ByteArraySerializableBase
{
- public MethodSelectionResponse()
+ /// <param name="bytes">2 bytes are required to be passed in.</param>
+ public MethodSelectionResponse(byte[] bytes)
{
+ Guard.Same($"{nameof(bytes)}.{nameof(bytes.Length)}", 2, bytes.Length);
+
+ Ver = new VerField(bytes[0]);
+ Method = new MethodField(bytes[1]);
}
- public VerField Ver { get; set; }
+ public VerField Ver { get; }
- public MethodField Method { get; set; }
+ public MethodField Method { get; }
public override void FromBytes(byte[] bytes)
{
- Guard.NotNullOrEmpty(nameof(bytes), bytes);
- Guard.Same($"{nameof(bytes)}.{nameof(bytes.Length)}", 2, bytes.Length);
-
- Ver = new VerField(bytes[0]);
- Method = new MethodField(bytes[1]);
+ throw new NotSupportedException("This method is no longer supported.");
}
public override byte[] ToBytes() => new byte[]

MODIFY
WalletWasabi/Tor/Socks5/Models/Messages/UsernamePasswordResponse.cs
WalletWasabi/Tor/Socks5/Models/Messages/UsernamePasswordResponse.cs
+using System;
using WalletWasabi.Helpers;
using WalletWasabi.Tor.Socks5.Models.Bases;
using WalletWasabi.Tor.Socks5.Models.Fields.OctetFields;
@@ -6,22 +7,22 @@ namespace WalletWasabi.Tor.Socks5.Models.Messages
{
public class UsernamePasswordResponse : ByteArraySerializableBase
{
- public UsernamePasswordResponse()
+ /// <param name="bytes">2 bytes are required to be passed in.</param>
+ public UsernamePasswordResponse(byte[] bytes)
{
+ Guard.Same($"{nameof(bytes)}.{nameof(bytes.Length)}", 2, bytes.Length);
+
+ Ver = new AuthVerField(bytes[0]);
+ Status = new AuthStatusField(bytes[1]);
}
- public AuthVerField Ver { get; set; }
+ public AuthVerField Ver { get; }
- public AuthStatusField Status { get; set; }
+ public AuthStatusField Status { get; }
public override void FromBytes(byte[] bytes)
{
- Guard.NotNullOrEmpty(nameof(bytes), bytes);
- Guard.Same($"{nameof(bytes)}.{nameof(bytes.Length)}", 2, bytes.Length);
-
- Ver = new AuthVerField(bytes[0]);
-
- Status = new AuthStatusField(bytes[1]);
+ throw new NotSupportedException("This method is no longer supported.");
}
public override byte[] ToBytes() => new byte[]

MODIFY
WalletWasabi/Tor/Socks5/TorSocks5Client.cs
WalletWasabi/Tor/Socks5/TorSocks5Client.cs
@@ -121,15 +121,13 @@ namespace WalletWasabi.Tor.Socks5
byte[] sendBuffer = new VersionMethodRequest(methods).ToBytes();
byte[] receiveBuffer = await SendAsync(sendBuffer, receiveBufferSize: 2, cancellationToken).ConfigureAwait(false);
- var methodSelection = new MethodSelectionResponse();
- methodSelection.FromBytes(receiveBuffer);
+ var methodSelection = new MethodSelectionResponse(receiveBuffer);
if (methodSelection.Ver != VerField.Socks5)
{
throw new NotSupportedException($"SOCKS{methodSelection.Ver.Value} not supported. Only SOCKS5 is supported.");
}
-
- if (methodSelection.Method == MethodField.NoAcceptableMethods)
+ else if (methodSelection.Method == MethodField.NoAcceptableMethods)
{
// https://www.ietf.org/rfc/rfc1928.txt
// If the selected METHOD is X'FF', none of the methods listed by the
@@ -137,8 +135,7 @@ namespace WalletWasabi.Tor.Socks5
DisposeTcpClient();
throw new NotSupportedException("Tor's SOCKS5 proxy does not support any of the client's authentication methods.");
}
-
- if (methodSelection.Method == MethodField.UsernamePassword)
+ else if (methodSelection.Method == MethodField.UsernamePassword)
{
// https://tools.ietf.org/html/rfc1929#section-2
// Once the SOCKS V5 server has started, and the client has selected the
@@ -153,8 +150,8 @@ namespace WalletWasabi.Tor.Socks5
Array.Clear(receiveBuffer, 0, receiveBuffer.Length);
receiveBuffer = await SendAsync(sendBuffer, receiveBufferSize: 2, cancellationToken).ConfigureAwait(false);
- var userNamePasswordResponse = new UsernamePasswordResponse();
- userNamePasswordResponse.FromBytes(receiveBuffer);
+ var userNamePasswordResponse = new UsernamePasswordResponse(receiveBuffer);
+
if (userNamePasswordResponse.Ver != usernamePasswordRequest.Ver)
{
throw new NotSupportedException($"Authentication version {userNamePasswordResponse.Ver.Value} not supported. Only version {usernamePasswordRequest.Ver} is supported.");

Pass bytes via constructors.