Skip to content

Commit

Permalink
make public everything, add IPacketData
Browse files Browse the repository at this point in the history
  • Loading branch information
tqk2811 committed Jul 24, 2024
1 parent 2f794ab commit 76beb6f
Show file tree
Hide file tree
Showing 30 changed files with 209 additions and 189 deletions.
2 changes: 1 addition & 1 deletion src/TqkLibrary.Proxy/Enums/Socks4_CMD.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace TqkLibrary.Proxy.Enums
{
internal enum Socks4_CMD : byte
public enum Socks4_CMD : byte
{
Connect = 0x01,
Bind = 0x02,
Expand Down
2 changes: 1 addition & 1 deletion src/TqkLibrary.Proxy/Enums/Socks4_REP.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace TqkLibrary.Proxy.Enums
{
internal enum Socks4_REP : byte
public enum Socks4_REP : byte
{
/// <summary>
/// Request granted
Expand Down
2 changes: 1 addition & 1 deletion src/TqkLibrary.Proxy/Enums/Socks5_ATYP.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace TqkLibrary.Proxy.Enums
{
internal enum Socks5_ATYP
public enum Socks5_ATYP : byte
{
IpV4 = 0x01,
DomainName = 0x03,
Expand Down
2 changes: 1 addition & 1 deletion src/TqkLibrary.Proxy/Enums/Socks5_CMD.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace TqkLibrary.Proxy.Enums
{
internal enum Socks5_CMD : byte
public enum Socks5_CMD : byte
{
EstablishStreamConnection = 0x01,
EstablishPortBinding = 0x02,
Expand Down
2 changes: 1 addition & 1 deletion src/TqkLibrary.Proxy/Enums/Socks5_STATUS.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace TqkLibrary.Proxy.Enums
{
internal enum Socks5_STATUS : byte
public enum Socks5_STATUS : byte
{
RequestGranted = 0x00,
GeneralFailure = 0x01,
Expand Down
8 changes: 4 additions & 4 deletions src/TqkLibrary.Proxy/Helpers/HeaderRequestParse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@

namespace TqkLibrary.Proxy.Helpers
{
internal class HeaderRequestParse
public class HeaderRequestParse
{
static readonly Regex _regex_httpRequestMethod = new Regex("^([A-z]+) ([A-z]+:\\/\\/|)(.*?) HTTP\\/([0-9\\.]{3})$");
private HeaderRequestParse(string? line)
private HeaderRequestParse(string line)
{
if (string.IsNullOrWhiteSpace(line))
throw new ArgumentNullException(nameof(line));
Expand Down Expand Up @@ -57,9 +57,9 @@ private HeaderRequestParse(string? line)
public AuthenticationHeaderValue? ProxyAuthorization { get; set; }


internal static HeaderRequestParse ParseRequest(IEnumerable<string> lines)
public static HeaderRequestParse ParseRequest(IEnumerable<string> lines)
{
HeaderRequestParse headerRequestParse = new HeaderRequestParse(lines.FirstOrDefault());
HeaderRequestParse headerRequestParse = new HeaderRequestParse(lines.FirstOrDefault()!);

var dict = lines.Skip(1)
.Select(x => x.Split(':'))
Expand Down
8 changes: 4 additions & 4 deletions src/TqkLibrary.Proxy/Helpers/HeaderResponseParse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@

namespace TqkLibrary.Proxy.Helpers
{
internal class HeaderResponseParse
public class HeaderResponseParse
{
static readonly Regex regex_httpResponseStatus = new Regex("^HTTP\\/([0-9\\.]{3}) (\\d{3}) (.*?)$");
private HeaderResponseParse(string? line)
private HeaderResponseParse(string line)
{
if (string.IsNullOrWhiteSpace(line))
throw new ArgumentNullException(nameof(line));
Expand All @@ -39,9 +39,9 @@ private HeaderResponseParse(string? line)
public int ContentLength { get; set; } = 0;


internal static HeaderResponseParse ParseResponse(IEnumerable<string> lines)
public static HeaderResponseParse ParseResponse(IEnumerable<string> lines)
{
HeaderResponseParse responseStatusCode = new HeaderResponseParse(lines.FirstOrDefault());
HeaderResponseParse responseStatusCode = new HeaderResponseParse(lines.FirstOrDefault()!);

var dict = lines.Skip(1)
.Select(x => x.Split(':'))
Expand Down
12 changes: 6 additions & 6 deletions src/TqkLibrary.Proxy/Helpers/Socks4_Request.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
using System.Net;
using System.Text;
using TqkLibrary.Proxy.Enums;
using TqkLibrary.Proxy.Interfaces;
using TqkLibrary.Proxy.StreamHeplers;

namespace TqkLibrary.Proxy.Helpers
{
/// <summary>
/// <see href="https://www.openssh.com/txt/socks4.protocol"/>
/// </summary>
internal class Socks4_Request
public class Socks4_Request : IPacketData
{
const long socks4aDomain = 0x00000001; //0.0.0.x with x non-zero
private Socks4_Request()
Expand Down Expand Up @@ -79,7 +80,7 @@ public bool IsDomain
}
}

internal static async Task<Socks4_Request> ReadAsync(Stream stream, CancellationToken cancellationToken = default)
public static async Task<Socks4_Request> ReadAsync(Stream stream, CancellationToken cancellationToken = default)
{
Socks4_Request socks4_Request = new Socks4_Request();
byte[] buffer = await stream.ReadBytesAsync(8, cancellationToken);
Expand All @@ -98,8 +99,7 @@ internal static async Task<Socks4_Request> ReadAsync(Stream stream, Cancellation
}


internal byte[] GetByteArray() => GetBytes().ToArray();
internal IEnumerable<byte> GetBytes()
public IEnumerable<byte> GetBytes()
{
yield return VER;
yield return (byte)CMD;
Expand Down Expand Up @@ -132,9 +132,9 @@ internal IEnumerable<byte> GetBytes()
}
}
}
internal static class Socks4_Request_Extension
public static class Socks4_Request_Extension
{
internal static Task<Socks4_Request> Read_Socks4_Request_Async(this Stream stream, CancellationToken cancellationToken = default)
public static Task<Socks4_Request> Read_Socks4_Request_Async(this Stream stream, CancellationToken cancellationToken = default)
=> Socks4_Request.ReadAsync(stream, cancellationToken);
}
}
12 changes: 6 additions & 6 deletions src/TqkLibrary.Proxy/Helpers/Socks4_RequestResponse.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
using System.Net;
using System.Net.Sockets;
using TqkLibrary.Proxy.Enums;
using TqkLibrary.Proxy.Interfaces;
using TqkLibrary.Proxy.StreamHeplers;

namespace TqkLibrary.Proxy.Helpers
{
/// <summary>
/// <see href="https://www.openssh.com/txt/socks4.protocol"/>
/// </summary>
internal class Socks4_RequestResponse
public class Socks4_RequestResponse : IPacketData
{
public Socks4_RequestResponse(Socks4_REP socks4_REP, Uri uri)
{
Expand Down Expand Up @@ -55,7 +56,7 @@ private Socks4_RequestResponse()
public IPAddress DSTIP { get; private set; } = IPAddress.None;
public IPEndPoint IPEndPoint { get { return new IPEndPoint(DSTIP, DSTPORT); } }

internal static async Task<Socks4_RequestResponse> ReadAsync(Stream stream, CancellationToken cancellationToken = default)
public static async Task<Socks4_RequestResponse> ReadAsync(Stream stream, CancellationToken cancellationToken = default)
{
Socks4_RequestResponse socks4_RequestResponse = new Socks4_RequestResponse();
byte[] buffer = await stream.ReadBytesAsync(8, cancellationToken);
Expand All @@ -67,8 +68,7 @@ internal static async Task<Socks4_RequestResponse> ReadAsync(Stream stream, Canc
}


internal byte[] GetByteArray() => GetBytes().ToArray();
internal IEnumerable<byte> GetBytes()
public IEnumerable<byte> GetBytes()
{
yield return VN;
yield return (byte)REP;
Expand All @@ -87,9 +87,9 @@ public override string ToString()
return $"{REP}";
}
}
internal static class Socks4_RequestResponse_Extension
public static class Socks4_RequestResponse_Extension
{
internal static Task<Socks4_RequestResponse> Read_Socks4_RequestResponse_Async(this Stream stream, CancellationToken cancellationToken = default)
public static Task<Socks4_RequestResponse> Read_Socks4_RequestResponse_Async(this Stream stream, CancellationToken cancellationToken = default)
=> Socks4_RequestResponse.ReadAsync(stream, cancellationToken);
}
}
22 changes: 11 additions & 11 deletions src/TqkLibrary.Proxy/Helpers/Socks5_DSTADDR.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
using System.Net.Sockets;
using System.Text;
using TqkLibrary.Proxy.Enums;
using TqkLibrary.Proxy.Interfaces;
using TqkLibrary.Proxy.StreamHeplers;

namespace TqkLibrary.Proxy.Helpers
{
internal class Socks5_DSTADDR
public class Socks5_DSTADDR : IPacketData
{
internal Socks5_DSTADDR(Uri uri)
public Socks5_DSTADDR(Uri uri)
{
if (uri is null) throw new ArgumentNullException(nameof(uri));

Expand All @@ -34,7 +35,7 @@ internal Socks5_DSTADDR(Uri uri)
throw new InvalidDataException($"Invalid type uri input: {uri.HostNameType}");
}
}
internal Socks5_DSTADDR(IPAddress iPAddress)
public Socks5_DSTADDR(IPAddress iPAddress)
{
if (iPAddress is null)
throw new ArgumentNullException(nameof(iPAddress));
Expand All @@ -61,11 +62,11 @@ private Socks5_DSTADDR()

}

internal string Domain { get; private set; } = string.Empty;
internal IPAddress IPAddress { get; private set; } = IPAddress.None;
internal Socks5_ATYP ATYP { get; private set; }
public string Domain { get; private set; } = string.Empty;
public IPAddress IPAddress { get; private set; } = IPAddress.None;
public Socks5_ATYP ATYP { get; private set; }

internal static async Task<Socks5_DSTADDR> ReadAsync(Stream stream, CancellationToken cancellationToken = default)
public static async Task<Socks5_DSTADDR> ReadAsync(Stream stream, CancellationToken cancellationToken = default)
{
Socks5_DSTADDR socks5_DSTADDR = new Socks5_DSTADDR();
socks5_DSTADDR.ATYP = (Socks5_ATYP)await stream.ReadByteAsync(cancellationToken);
Expand Down Expand Up @@ -97,8 +98,7 @@ internal static async Task<Socks5_DSTADDR> ReadAsync(Stream stream, Cancellation
}


internal byte[] GetByteArray() => GetBytes().ToArray();
internal IEnumerable<byte> GetBytes()
public IEnumerable<byte> GetBytes()
{
yield return (byte)ATYP;
if (ATYP == Socks5_ATYP.DomainName)
Expand All @@ -119,9 +119,9 @@ internal IEnumerable<byte> GetBytes()
}
}
}
internal static class Socks5_DSTADDR_Extensions
public static class Socks5_DSTADDR_Extensions
{
internal static Task<Socks5_DSTADDR> Read_Socks5_DSTADDR_Async(this Stream stream, CancellationToken cancellationToken = default)
public static Task<Socks5_DSTADDR> Read_Socks5_DSTADDR_Async(this Stream stream, CancellationToken cancellationToken = default)
=> Socks5_DSTADDR.ReadAsync(stream, cancellationToken);
}
}
20 changes: 10 additions & 10 deletions src/TqkLibrary.Proxy/Helpers/Socks5_Greeting.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
using TqkLibrary.Proxy.Enums;
using TqkLibrary.Proxy.Interfaces;
using TqkLibrary.Proxy.StreamHeplers;

namespace TqkLibrary.Proxy.Helpers
{
/// <summary>
/// <see href="https://www.rfc-editor.org/rfc/rfc1928"/>
/// </summary>
internal class Socks5_Greeting
public class Socks5_Greeting : IPacketData
{
internal Socks5_Greeting(IEnumerable<Socks5_Auth> socks5_Auths)
public Socks5_Greeting(IEnumerable<Socks5_Auth> socks5_Auths)
{
if (socks5_Auths is null) throw new ArgumentNullException(nameof(socks5_Auths));
Auths = socks5_Auths.ToArray();
Expand All @@ -19,12 +20,12 @@ private Socks5_Greeting()
{
}

internal byte VER { get; private set; } = 0x05;
internal int AuthCount { get { return Auths.Count(); } }
internal IEnumerable<Socks5_Auth> Auths { get; private set; } = Enumerable.Empty<Socks5_Auth>();
public byte VER { get; private set; } = 0x05;
public int AuthCount { get { return Auths.Count(); } }
public IEnumerable<Socks5_Auth> Auths { get; private set; } = Enumerable.Empty<Socks5_Auth>();


internal static async Task<Socks5_Greeting> ReadAsync(Stream stream, CancellationToken cancellationToken = default)
public static async Task<Socks5_Greeting> ReadAsync(Stream stream, CancellationToken cancellationToken = default)
{
Socks5_Greeting socks5_Greeting = new Socks5_Greeting();
byte[] buffer = await stream.ReadBytesAsync(2, cancellationToken);
Expand All @@ -36,8 +37,7 @@ internal static async Task<Socks5_Greeting> ReadAsync(Stream stream, Cancellatio
}


internal byte[] GetByteArray() => GetBytes().ToArray();
internal IEnumerable<byte> GetBytes()
public IEnumerable<byte> GetBytes()
{
yield return VER;
yield return (byte)AuthCount;
Expand All @@ -47,9 +47,9 @@ internal IEnumerable<byte> GetBytes()
}
}
}
internal static class Socks5_Greeting_Extensions
public static class Socks5_Greeting_Extensions
{
internal static Task<Socks5_Greeting> Read_Socks5_Greeting_Async(this Stream stream, CancellationToken cancellationToken = default)
public static Task<Socks5_Greeting> Read_Socks5_Greeting_Async(this Stream stream, CancellationToken cancellationToken = default)
=> Socks5_Greeting.ReadAsync(stream, cancellationToken);
}
}
18 changes: 9 additions & 9 deletions src/TqkLibrary.Proxy/Helpers/Socks5_GreetingResponse.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
using TqkLibrary.Proxy.Enums;
using TqkLibrary.Proxy.Interfaces;
using TqkLibrary.Proxy.StreamHeplers;

namespace TqkLibrary.Proxy.Helpers
{
/// <summary>
/// <see href="https://www.rfc-editor.org/rfc/rfc1928"/>
/// </summary>
internal class Socks5_GreetingResponse
public class Socks5_GreetingResponse : IPacketData
{
internal Socks5_GreetingResponse(Socks5_Auth socks5_Auth)
public Socks5_GreetingResponse(Socks5_Auth socks5_Auth)
{
CAUTH = socks5_Auth;
}
Expand All @@ -17,11 +18,11 @@ private Socks5_GreetingResponse()
{
}

internal byte VER { get; private set; } = 0x05;
internal Socks5_Auth CAUTH { get; private set; }
public byte VER { get; private set; } = 0x05;
public Socks5_Auth CAUTH { get; private set; }


internal static async Task<Socks5_GreetingResponse> ReadAsync(Stream stream, CancellationToken cancellationToken = default)
public static async Task<Socks5_GreetingResponse> ReadAsync(Stream stream, CancellationToken cancellationToken = default)
{
Socks5_GreetingResponse socks5_GreetingResponse = new Socks5_GreetingResponse();
byte[] buffer = await stream.ReadBytesAsync(2, cancellationToken);
Expand All @@ -31,16 +32,15 @@ internal static async Task<Socks5_GreetingResponse> ReadAsync(Stream stream, Can
}


internal byte[] GetByteArray() => GetBytes().ToArray();
internal IEnumerable<byte> GetBytes()
public IEnumerable<byte> GetBytes()
{
yield return VER;
yield return (byte)CAUTH;
}
}
internal static class Socks5_GreetingResponse_Extensions
public static class Socks5_GreetingResponse_Extensions
{
internal static Task<Socks5_GreetingResponse> Read_Socks5_GreetingResponse_Async(this Stream stream, CancellationToken cancellationToken = default)
public static Task<Socks5_GreetingResponse> Read_Socks5_GreetingResponse_Async(this Stream stream, CancellationToken cancellationToken = default)
=> Socks5_GreetingResponse.ReadAsync(stream, cancellationToken);
}
}
Loading

0 comments on commit 76beb6f

Please sign in to comment.