From 76beb6fcb5856ebe387d41bfe5609030a96b815c Mon Sep 17 00:00:00 2001 From: tqk2811 Date: Wed, 24 Jul 2024 15:37:27 +0700 Subject: [PATCH] make public everything, add IPacketData --- src/TqkLibrary.Proxy/Enums/Socks4_CMD.cs | 2 +- src/TqkLibrary.Proxy/Enums/Socks4_REP.cs | 2 +- src/TqkLibrary.Proxy/Enums/Socks5_ATYP.cs | 2 +- src/TqkLibrary.Proxy/Enums/Socks5_CMD.cs | 2 +- src/TqkLibrary.Proxy/Enums/Socks5_STATUS.cs | 2 +- .../Helpers/HeaderRequestParse.cs | 8 +-- .../Helpers/HeaderResponseParse.cs | 8 +-- .../Helpers/Socks4_Request.cs | 12 ++-- .../Helpers/Socks4_RequestResponse.cs | 12 ++-- .../Helpers/Socks5_DSTADDR.cs | 22 +++--- .../Helpers/Socks5_Greeting.cs | 20 +++--- .../Helpers/Socks5_GreetingResponse.cs | 18 ++--- .../Helpers/Socks5_Request.cs | 24 +++---- .../Helpers/Socks5_RequestResponse.cs | 26 +++---- .../Helpers/Socks5_UsernamePassword.cs | 20 +++--- .../Socks5_UsernamePasswordResponse.cs | 20 +++--- .../Interfaces/IPacketData.cs | 7 ++ .../{Extensions.cs => ProxyExtensions.cs} | 14 ++-- .../LocalProxySource.BaseTunnel.cs | 4 +- .../LocalProxySource.BindTunnel.cs | 16 ++--- .../LocalProxySource.ConnectTunnel.cs | 14 ++-- .../LocalProxySource.UdpTunnel.cs | 13 +--- .../Socks4ProxySource.BaseTunnel.cs | 6 +- .../Socks4ProxySource.BindTunnel.cs | 10 +-- .../Socks4ProxySource.ConnectTunnel.cs | 8 +-- .../Socks5ProxySource.BaseTunnel.cs | 12 ++-- .../Socks5ProxySource.BindTunnel.cs | 8 +-- .../Socks5ProxySource.ConnectTunnel.cs | 8 +-- .../Socks5ProxySource.UdpTunnel.cs | 8 +-- .../StreamHeplers/StreamExtensions.cs | 70 ++++++++++++------- 30 files changed, 209 insertions(+), 189 deletions(-) create mode 100644 src/TqkLibrary.Proxy/Interfaces/IPacketData.cs rename src/TqkLibrary.Proxy/{Extensions.cs => ProxyExtensions.cs} (56%) diff --git a/src/TqkLibrary.Proxy/Enums/Socks4_CMD.cs b/src/TqkLibrary.Proxy/Enums/Socks4_CMD.cs index ee707e2..25a67d4 100644 --- a/src/TqkLibrary.Proxy/Enums/Socks4_CMD.cs +++ b/src/TqkLibrary.Proxy/Enums/Socks4_CMD.cs @@ -1,6 +1,6 @@ namespace TqkLibrary.Proxy.Enums { - internal enum Socks4_CMD : byte + public enum Socks4_CMD : byte { Connect = 0x01, Bind = 0x02, diff --git a/src/TqkLibrary.Proxy/Enums/Socks4_REP.cs b/src/TqkLibrary.Proxy/Enums/Socks4_REP.cs index 61ec1d8..34fabda 100644 --- a/src/TqkLibrary.Proxy/Enums/Socks4_REP.cs +++ b/src/TqkLibrary.Proxy/Enums/Socks4_REP.cs @@ -1,6 +1,6 @@ namespace TqkLibrary.Proxy.Enums { - internal enum Socks4_REP : byte + public enum Socks4_REP : byte { /// /// Request granted diff --git a/src/TqkLibrary.Proxy/Enums/Socks5_ATYP.cs b/src/TqkLibrary.Proxy/Enums/Socks5_ATYP.cs index 5bda800..c042af2 100644 --- a/src/TqkLibrary.Proxy/Enums/Socks5_ATYP.cs +++ b/src/TqkLibrary.Proxy/Enums/Socks5_ATYP.cs @@ -1,6 +1,6 @@ namespace TqkLibrary.Proxy.Enums { - internal enum Socks5_ATYP + public enum Socks5_ATYP : byte { IpV4 = 0x01, DomainName = 0x03, diff --git a/src/TqkLibrary.Proxy/Enums/Socks5_CMD.cs b/src/TqkLibrary.Proxy/Enums/Socks5_CMD.cs index 15204b8..3df04a7 100644 --- a/src/TqkLibrary.Proxy/Enums/Socks5_CMD.cs +++ b/src/TqkLibrary.Proxy/Enums/Socks5_CMD.cs @@ -1,6 +1,6 @@ namespace TqkLibrary.Proxy.Enums { - internal enum Socks5_CMD : byte + public enum Socks5_CMD : byte { EstablishStreamConnection = 0x01, EstablishPortBinding = 0x02, diff --git a/src/TqkLibrary.Proxy/Enums/Socks5_STATUS.cs b/src/TqkLibrary.Proxy/Enums/Socks5_STATUS.cs index 308c3e2..421ab02 100644 --- a/src/TqkLibrary.Proxy/Enums/Socks5_STATUS.cs +++ b/src/TqkLibrary.Proxy/Enums/Socks5_STATUS.cs @@ -1,6 +1,6 @@ namespace TqkLibrary.Proxy.Enums { - internal enum Socks5_STATUS : byte + public enum Socks5_STATUS : byte { RequestGranted = 0x00, GeneralFailure = 0x01, diff --git a/src/TqkLibrary.Proxy/Helpers/HeaderRequestParse.cs b/src/TqkLibrary.Proxy/Helpers/HeaderRequestParse.cs index 702abfe..9a77dfd 100644 --- a/src/TqkLibrary.Proxy/Helpers/HeaderRequestParse.cs +++ b/src/TqkLibrary.Proxy/Helpers/HeaderRequestParse.cs @@ -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)); @@ -57,9 +57,9 @@ private HeaderRequestParse(string? line) public AuthenticationHeaderValue? ProxyAuthorization { get; set; } - internal static HeaderRequestParse ParseRequest(IEnumerable lines) + public static HeaderRequestParse ParseRequest(IEnumerable lines) { - HeaderRequestParse headerRequestParse = new HeaderRequestParse(lines.FirstOrDefault()); + HeaderRequestParse headerRequestParse = new HeaderRequestParse(lines.FirstOrDefault()!); var dict = lines.Skip(1) .Select(x => x.Split(':')) diff --git a/src/TqkLibrary.Proxy/Helpers/HeaderResponseParse.cs b/src/TqkLibrary.Proxy/Helpers/HeaderResponseParse.cs index 252e1ed..5aacdb1 100644 --- a/src/TqkLibrary.Proxy/Helpers/HeaderResponseParse.cs +++ b/src/TqkLibrary.Proxy/Helpers/HeaderResponseParse.cs @@ -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)); @@ -39,9 +39,9 @@ private HeaderResponseParse(string? line) public int ContentLength { get; set; } = 0; - internal static HeaderResponseParse ParseResponse(IEnumerable lines) + public static HeaderResponseParse ParseResponse(IEnumerable lines) { - HeaderResponseParse responseStatusCode = new HeaderResponseParse(lines.FirstOrDefault()); + HeaderResponseParse responseStatusCode = new HeaderResponseParse(lines.FirstOrDefault()!); var dict = lines.Skip(1) .Select(x => x.Split(':')) diff --git a/src/TqkLibrary.Proxy/Helpers/Socks4_Request.cs b/src/TqkLibrary.Proxy/Helpers/Socks4_Request.cs index dd829f7..eca64b0 100644 --- a/src/TqkLibrary.Proxy/Helpers/Socks4_Request.cs +++ b/src/TqkLibrary.Proxy/Helpers/Socks4_Request.cs @@ -1,6 +1,7 @@ using System.Net; using System.Text; using TqkLibrary.Proxy.Enums; +using TqkLibrary.Proxy.Interfaces; using TqkLibrary.Proxy.StreamHeplers; namespace TqkLibrary.Proxy.Helpers @@ -8,7 +9,7 @@ namespace TqkLibrary.Proxy.Helpers /// /// /// - internal class Socks4_Request + public class Socks4_Request : IPacketData { const long socks4aDomain = 0x00000001; //0.0.0.x with x non-zero private Socks4_Request() @@ -79,7 +80,7 @@ public bool IsDomain } } - internal static async Task ReadAsync(Stream stream, CancellationToken cancellationToken = default) + public static async Task ReadAsync(Stream stream, CancellationToken cancellationToken = default) { Socks4_Request socks4_Request = new Socks4_Request(); byte[] buffer = await stream.ReadBytesAsync(8, cancellationToken); @@ -98,8 +99,7 @@ internal static async Task ReadAsync(Stream stream, Cancellation } - internal byte[] GetByteArray() => GetBytes().ToArray(); - internal IEnumerable GetBytes() + public IEnumerable GetBytes() { yield return VER; yield return (byte)CMD; @@ -132,9 +132,9 @@ internal IEnumerable GetBytes() } } } - internal static class Socks4_Request_Extension + public static class Socks4_Request_Extension { - internal static Task Read_Socks4_Request_Async(this Stream stream, CancellationToken cancellationToken = default) + public static Task Read_Socks4_Request_Async(this Stream stream, CancellationToken cancellationToken = default) => Socks4_Request.ReadAsync(stream, cancellationToken); } } diff --git a/src/TqkLibrary.Proxy/Helpers/Socks4_RequestResponse.cs b/src/TqkLibrary.Proxy/Helpers/Socks4_RequestResponse.cs index ebffa8f..a6cf716 100644 --- a/src/TqkLibrary.Proxy/Helpers/Socks4_RequestResponse.cs +++ b/src/TqkLibrary.Proxy/Helpers/Socks4_RequestResponse.cs @@ -1,6 +1,7 @@ using System.Net; using System.Net.Sockets; using TqkLibrary.Proxy.Enums; +using TqkLibrary.Proxy.Interfaces; using TqkLibrary.Proxy.StreamHeplers; namespace TqkLibrary.Proxy.Helpers @@ -8,7 +9,7 @@ namespace TqkLibrary.Proxy.Helpers /// /// /// - internal class Socks4_RequestResponse + public class Socks4_RequestResponse : IPacketData { public Socks4_RequestResponse(Socks4_REP socks4_REP, Uri uri) { @@ -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 ReadAsync(Stream stream, CancellationToken cancellationToken = default) + public static async Task ReadAsync(Stream stream, CancellationToken cancellationToken = default) { Socks4_RequestResponse socks4_RequestResponse = new Socks4_RequestResponse(); byte[] buffer = await stream.ReadBytesAsync(8, cancellationToken); @@ -67,8 +68,7 @@ internal static async Task ReadAsync(Stream stream, Canc } - internal byte[] GetByteArray() => GetBytes().ToArray(); - internal IEnumerable GetBytes() + public IEnumerable GetBytes() { yield return VN; yield return (byte)REP; @@ -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 Read_Socks4_RequestResponse_Async(this Stream stream, CancellationToken cancellationToken = default) + public static Task Read_Socks4_RequestResponse_Async(this Stream stream, CancellationToken cancellationToken = default) => Socks4_RequestResponse.ReadAsync(stream, cancellationToken); } } diff --git a/src/TqkLibrary.Proxy/Helpers/Socks5_DSTADDR.cs b/src/TqkLibrary.Proxy/Helpers/Socks5_DSTADDR.cs index 3115677..6e4b0a5 100644 --- a/src/TqkLibrary.Proxy/Helpers/Socks5_DSTADDR.cs +++ b/src/TqkLibrary.Proxy/Helpers/Socks5_DSTADDR.cs @@ -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)); @@ -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)); @@ -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 ReadAsync(Stream stream, CancellationToken cancellationToken = default) + public static async Task ReadAsync(Stream stream, CancellationToken cancellationToken = default) { Socks5_DSTADDR socks5_DSTADDR = new Socks5_DSTADDR(); socks5_DSTADDR.ATYP = (Socks5_ATYP)await stream.ReadByteAsync(cancellationToken); @@ -97,8 +98,7 @@ internal static async Task ReadAsync(Stream stream, Cancellation } - internal byte[] GetByteArray() => GetBytes().ToArray(); - internal IEnumerable GetBytes() + public IEnumerable GetBytes() { yield return (byte)ATYP; if (ATYP == Socks5_ATYP.DomainName) @@ -119,9 +119,9 @@ internal IEnumerable GetBytes() } } } - internal static class Socks5_DSTADDR_Extensions + public static class Socks5_DSTADDR_Extensions { - internal static Task Read_Socks5_DSTADDR_Async(this Stream stream, CancellationToken cancellationToken = default) + public static Task Read_Socks5_DSTADDR_Async(this Stream stream, CancellationToken cancellationToken = default) => Socks5_DSTADDR.ReadAsync(stream, cancellationToken); } } diff --git a/src/TqkLibrary.Proxy/Helpers/Socks5_Greeting.cs b/src/TqkLibrary.Proxy/Helpers/Socks5_Greeting.cs index bd5d66f..e6ad07a 100644 --- a/src/TqkLibrary.Proxy/Helpers/Socks5_Greeting.cs +++ b/src/TqkLibrary.Proxy/Helpers/Socks5_Greeting.cs @@ -1,4 +1,5 @@ using TqkLibrary.Proxy.Enums; +using TqkLibrary.Proxy.Interfaces; using TqkLibrary.Proxy.StreamHeplers; namespace TqkLibrary.Proxy.Helpers @@ -6,9 +7,9 @@ namespace TqkLibrary.Proxy.Helpers /// /// /// - internal class Socks5_Greeting + public class Socks5_Greeting : IPacketData { - internal Socks5_Greeting(IEnumerable socks5_Auths) + public Socks5_Greeting(IEnumerable socks5_Auths) { if (socks5_Auths is null) throw new ArgumentNullException(nameof(socks5_Auths)); Auths = socks5_Auths.ToArray(); @@ -19,12 +20,12 @@ private Socks5_Greeting() { } - internal byte VER { get; private set; } = 0x05; - internal int AuthCount { get { return Auths.Count(); } } - internal IEnumerable Auths { get; private set; } = Enumerable.Empty(); + public byte VER { get; private set; } = 0x05; + public int AuthCount { get { return Auths.Count(); } } + public IEnumerable Auths { get; private set; } = Enumerable.Empty(); - internal static async Task ReadAsync(Stream stream, CancellationToken cancellationToken = default) + public static async Task ReadAsync(Stream stream, CancellationToken cancellationToken = default) { Socks5_Greeting socks5_Greeting = new Socks5_Greeting(); byte[] buffer = await stream.ReadBytesAsync(2, cancellationToken); @@ -36,8 +37,7 @@ internal static async Task ReadAsync(Stream stream, Cancellatio } - internal byte[] GetByteArray() => GetBytes().ToArray(); - internal IEnumerable GetBytes() + public IEnumerable GetBytes() { yield return VER; yield return (byte)AuthCount; @@ -47,9 +47,9 @@ internal IEnumerable GetBytes() } } } - internal static class Socks5_Greeting_Extensions + public static class Socks5_Greeting_Extensions { - internal static Task Read_Socks5_Greeting_Async(this Stream stream, CancellationToken cancellationToken = default) + public static Task Read_Socks5_Greeting_Async(this Stream stream, CancellationToken cancellationToken = default) => Socks5_Greeting.ReadAsync(stream, cancellationToken); } } diff --git a/src/TqkLibrary.Proxy/Helpers/Socks5_GreetingResponse.cs b/src/TqkLibrary.Proxy/Helpers/Socks5_GreetingResponse.cs index 692aba8..f3d8934 100644 --- a/src/TqkLibrary.Proxy/Helpers/Socks5_GreetingResponse.cs +++ b/src/TqkLibrary.Proxy/Helpers/Socks5_GreetingResponse.cs @@ -1,4 +1,5 @@ using TqkLibrary.Proxy.Enums; +using TqkLibrary.Proxy.Interfaces; using TqkLibrary.Proxy.StreamHeplers; namespace TqkLibrary.Proxy.Helpers @@ -6,9 +7,9 @@ namespace TqkLibrary.Proxy.Helpers /// /// /// - 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; } @@ -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 ReadAsync(Stream stream, CancellationToken cancellationToken = default) + public static async Task ReadAsync(Stream stream, CancellationToken cancellationToken = default) { Socks5_GreetingResponse socks5_GreetingResponse = new Socks5_GreetingResponse(); byte[] buffer = await stream.ReadBytesAsync(2, cancellationToken); @@ -31,16 +32,15 @@ internal static async Task ReadAsync(Stream stream, Can } - internal byte[] GetByteArray() => GetBytes().ToArray(); - internal IEnumerable GetBytes() + public IEnumerable GetBytes() { yield return VER; yield return (byte)CAUTH; } } - internal static class Socks5_GreetingResponse_Extensions + public static class Socks5_GreetingResponse_Extensions { - internal static Task Read_Socks5_GreetingResponse_Async(this Stream stream, CancellationToken cancellationToken = default) + public static Task Read_Socks5_GreetingResponse_Async(this Stream stream, CancellationToken cancellationToken = default) => Socks5_GreetingResponse.ReadAsync(stream, cancellationToken); } } diff --git a/src/TqkLibrary.Proxy/Helpers/Socks5_Request.cs b/src/TqkLibrary.Proxy/Helpers/Socks5_Request.cs index f9655c5..e7ff54f 100644 --- a/src/TqkLibrary.Proxy/Helpers/Socks5_Request.cs +++ b/src/TqkLibrary.Proxy/Helpers/Socks5_Request.cs @@ -1,5 +1,6 @@ using System.Net; using TqkLibrary.Proxy.Enums; +using TqkLibrary.Proxy.Interfaces; using TqkLibrary.Proxy.StreamHeplers; namespace TqkLibrary.Proxy.Helpers @@ -7,7 +8,7 @@ namespace TqkLibrary.Proxy.Helpers /// /// /// - internal class Socks5_Request + public class Socks5_Request : IPacketData { #pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable. private Socks5_Request() @@ -44,12 +45,12 @@ public static Socks5_Request CreateUdp() } - internal byte VER { get; private set; } = 0x05; - internal Socks5_CMD CMD { get; private set; } - internal byte RSV { get; private set; } - internal Socks5_DSTADDR DSTADDR { get; private set; } - internal UInt16 DSTPORT { get; private set; } - internal Uri Uri + public byte VER { get; private set; } = 0x05; + public Socks5_CMD CMD { get; private set; } + public byte RSV { get; private set; } + public Socks5_DSTADDR DSTADDR { get; private set; } + public UInt16 DSTPORT { get; private set; } + public Uri Uri { get { @@ -65,7 +66,7 @@ internal Uri Uri } } - internal static async Task ReadAsync(Stream stream, CancellationToken cancellationToken = default) + public static async Task ReadAsync(Stream stream, CancellationToken cancellationToken = default) { Socks5_Request socks5_Connection = new Socks5_Request(); byte[] buffer = await stream.ReadBytesAsync(3, cancellationToken); @@ -78,8 +79,7 @@ internal static async Task ReadAsync(Stream stream, Cancellation } - internal byte[] GetByteArray() => GetBytes().ToArray(); - internal IEnumerable GetBytes() + public IEnumerable GetBytes() { yield return VER; yield return (byte)CMD; @@ -94,9 +94,9 @@ internal IEnumerable GetBytes() } } } - internal static class Socks5_Request_Extensions + public static class Socks5_Request_Extensions { - internal static Task Read_Socks5_Request_Async(this Stream stream, CancellationToken cancellationToken = default) + public static Task Read_Socks5_Request_Async(this Stream stream, CancellationToken cancellationToken = default) => Socks5_Request.ReadAsync(stream, cancellationToken); } } diff --git a/src/TqkLibrary.Proxy/Helpers/Socks5_RequestResponse.cs b/src/TqkLibrary.Proxy/Helpers/Socks5_RequestResponse.cs index b4e09e6..6af0447 100644 --- a/src/TqkLibrary.Proxy/Helpers/Socks5_RequestResponse.cs +++ b/src/TqkLibrary.Proxy/Helpers/Socks5_RequestResponse.cs @@ -1,5 +1,6 @@ using System.Net; using TqkLibrary.Proxy.Enums; +using TqkLibrary.Proxy.Interfaces; using TqkLibrary.Proxy.StreamHeplers; namespace TqkLibrary.Proxy.Helpers @@ -7,9 +8,9 @@ namespace TqkLibrary.Proxy.Helpers /// /// /// - internal class Socks5_RequestResponse + public class Socks5_RequestResponse : IPacketData { - internal Socks5_RequestResponse(Socks5_STATUS socks5_STATUS, IPEndPoint iPEndPoint) + public Socks5_RequestResponse(Socks5_STATUS socks5_STATUS, IPEndPoint iPEndPoint) { if (iPEndPoint is null) throw new ArgumentNullException(nameof(iPEndPoint)); @@ -23,14 +24,14 @@ private Socks5_RequestResponse() { } - internal byte VER { get; private set; } = 0x05; - internal Socks5_STATUS STATUS { get; private set; } - internal byte RSV { get; private set; } = 0x00; - internal Socks5_DSTADDR BNDADDR { get; private set; } - internal UInt16 BNDPORT { get; private set; } - internal IPEndPoint IPEndPoint { get { return new IPEndPoint(BNDADDR.IPAddress, BNDPORT); } } + public byte VER { get; private set; } = 0x05; + public Socks5_STATUS STATUS { get; private set; } + public byte RSV { get; private set; } = 0x00; + public Socks5_DSTADDR BNDADDR { get; private set; } + public UInt16 BNDPORT { get; private set; } + public IPEndPoint IPEndPoint { get { return new IPEndPoint(BNDADDR.IPAddress, BNDPORT); } } - internal static async Task ReadAsync(Stream stream, CancellationToken cancellationToken = default) + public static async Task ReadAsync(Stream stream, CancellationToken cancellationToken = default) { Socks5_RequestResponse socks5_ConnectionResponse = new Socks5_RequestResponse(); byte[] buffer = await stream.ReadBytesAsync(3, cancellationToken); @@ -43,8 +44,7 @@ internal static async Task ReadAsync(Stream stream, Canc } - internal byte[] GetByteArray() => GetBytes().ToArray(); - internal IEnumerable GetBytes() + public IEnumerable GetBytes() { yield return VER; yield return (byte)STATUS; @@ -59,9 +59,9 @@ internal IEnumerable GetBytes() } } } - internal static class Socks5_RequestResponse_Extensions + public static class Socks5_RequestResponse_Extensions { - internal static Task Read_Socks5_RequestResponse_Async(this Stream stream, CancellationToken cancellationToken = default) + public static Task Read_Socks5_RequestResponse_Async(this Stream stream, CancellationToken cancellationToken = default) => Socks5_RequestResponse.ReadAsync(stream, cancellationToken); } } diff --git a/src/TqkLibrary.Proxy/Helpers/Socks5_UsernamePassword.cs b/src/TqkLibrary.Proxy/Helpers/Socks5_UsernamePassword.cs index a9e58c9..8869b7c 100644 --- a/src/TqkLibrary.Proxy/Helpers/Socks5_UsernamePassword.cs +++ b/src/TqkLibrary.Proxy/Helpers/Socks5_UsernamePassword.cs @@ -1,4 +1,5 @@ using System.Text; +using TqkLibrary.Proxy.Interfaces; using TqkLibrary.Proxy.StreamHeplers; namespace TqkLibrary.Proxy.Helpers @@ -6,9 +7,9 @@ namespace TqkLibrary.Proxy.Helpers /// /// /// - internal class Socks5_UsernamePassword + public class Socks5_UsernamePassword: IPacketData { - internal Socks5_UsernamePassword(string userName, string password) + public Socks5_UsernamePassword(string userName, string password) { if (string.IsNullOrWhiteSpace(userName)) throw new ArgumentNullException(nameof(userName)); if (string.IsNullOrWhiteSpace(password)) throw new ArgumentNullException(nameof(password)); @@ -24,12 +25,12 @@ private Socks5_UsernamePassword() { } - internal byte VER { get; private set; } = 0x01; - internal string UserName { get; private set; } - internal string Password { get; private set; } + public byte VER { get; private set; } = 0x01; + public string UserName { get; private set; } + public string Password { get; private set; } - internal static async Task ReadAsync(Stream stream, CancellationToken cancellationToken = default) + public static async Task ReadAsync(Stream stream, CancellationToken cancellationToken = default) { Socks5_UsernamePassword socks5_UsernamePassword = new Socks5_UsernamePassword(); byte[] buffer = await stream.ReadBytesAsync(2, cancellationToken); @@ -46,8 +47,7 @@ internal static async Task ReadAsync(Stream stream, Can } - internal byte[] GetByteArray() => GetBytes().ToArray(); - internal IEnumerable GetBytes() + public IEnumerable GetBytes() { yield return VER; @@ -66,9 +66,9 @@ internal IEnumerable GetBytes() } } } - internal static class Socks5_UsernamePassword_Extensions + public static class Socks5_UsernamePassword_Extensions { - internal static Task Read_Socks5_UsernamePassword_Async(this Stream stream, CancellationToken cancellationToken = default) + public static Task Read_Socks5_UsernamePassword_Async(this Stream stream, CancellationToken cancellationToken = default) => Socks5_UsernamePassword.ReadAsync(stream, cancellationToken); } } diff --git a/src/TqkLibrary.Proxy/Helpers/Socks5_UsernamePasswordResponse.cs b/src/TqkLibrary.Proxy/Helpers/Socks5_UsernamePasswordResponse.cs index b3000f8..c323f25 100644 --- a/src/TqkLibrary.Proxy/Helpers/Socks5_UsernamePasswordResponse.cs +++ b/src/TqkLibrary.Proxy/Helpers/Socks5_UsernamePasswordResponse.cs @@ -1,13 +1,14 @@ -using TqkLibrary.Proxy.StreamHeplers; +using TqkLibrary.Proxy.Interfaces; +using TqkLibrary.Proxy.StreamHeplers; namespace TqkLibrary.Proxy.Helpers { /// /// /// - internal class Socks5_UsernamePasswordResponse + public class Socks5_UsernamePasswordResponse : IPacketData { - internal Socks5_UsernamePasswordResponse(byte status) + public Socks5_UsernamePasswordResponse(byte status) { STATUS = status; } @@ -16,11 +17,11 @@ private Socks5_UsernamePasswordResponse() { } - internal byte VER { get; private set; } = 0x01; - internal byte STATUS { get; private set; } + public byte VER { get; private set; } = 0x01; + public byte STATUS { get; private set; } - internal static async Task ReadAsync(Stream stream, CancellationToken cancellationToken = default) + public static async Task ReadAsync(Stream stream, CancellationToken cancellationToken = default) { Socks5_UsernamePasswordResponse socks5_UsernamePasswordResponse = new Socks5_UsernamePasswordResponse(); byte[] buffer = await stream.ReadBytesAsync(2, cancellationToken); @@ -30,16 +31,15 @@ internal static async Task ReadAsync(Stream str } - internal byte[] GetByteArray() => GetBytes().ToArray(); - internal IEnumerable GetBytes() + public IEnumerable GetBytes() { yield return VER; yield return STATUS; } } - internal static class Socks5_UsernamePasswordResponse_Extensions + public static class Socks5_UsernamePasswordResponse_Extensions { - internal static Task Read_Socks5_UsernamePasswordResponse_Async(this Stream stream, CancellationToken cancellationToken = default) + public static Task Read_Socks5_UsernamePasswordResponse_Async(this Stream stream, CancellationToken cancellationToken = default) => Socks5_UsernamePasswordResponse.ReadAsync(stream, cancellationToken); } } diff --git a/src/TqkLibrary.Proxy/Interfaces/IPacketData.cs b/src/TqkLibrary.Proxy/Interfaces/IPacketData.cs new file mode 100644 index 0000000..6437bf4 --- /dev/null +++ b/src/TqkLibrary.Proxy/Interfaces/IPacketData.cs @@ -0,0 +1,7 @@ +namespace TqkLibrary.Proxy.Interfaces +{ + public interface IPacketData + { + IEnumerable GetBytes(); + } +} diff --git a/src/TqkLibrary.Proxy/Extensions.cs b/src/TqkLibrary.Proxy/ProxyExtensions.cs similarity index 56% rename from src/TqkLibrary.Proxy/Extensions.cs rename to src/TqkLibrary.Proxy/ProxyExtensions.cs index 34c3df4..113f51d 100644 --- a/src/TqkLibrary.Proxy/Extensions.cs +++ b/src/TqkLibrary.Proxy/ProxyExtensions.cs @@ -1,6 +1,8 @@ -namespace TqkLibrary.Proxy +using TqkLibrary.Proxy.Interfaces; + +namespace TqkLibrary.Proxy { - internal static class Extensions + public static class ProxyExtensions { #if !NET5_0_OR_GREATER public static bool Contains(this string self, string value, StringComparison stringComparison) @@ -23,11 +25,7 @@ public static IEnumerable Append(this IEnumerable values, params T[]? i } } #endif - internal static IEnumerable Except(this IEnumerable source, params T[] values) - => source.Except(values.AsEnumerable()); - internal static IEnumerable Concat(this IEnumerable source, params T[] values) - => source.Concat(values.AsEnumerable()); - internal static IEnumerable ConcatIf(this IEnumerable source, bool val, params T[] values) - => val ? source.Concat(values.AsEnumerable()) : source; + public static byte[] GetByteArray(this IPacketData packetData) + => packetData.GetBytes().ToArray(); } } diff --git a/src/TqkLibrary.Proxy/ProxySources/LocalProxySource.BaseTunnel.cs b/src/TqkLibrary.Proxy/ProxySources/LocalProxySource.BaseTunnel.cs index 79284ef..266d5fa 100644 --- a/src/TqkLibrary.Proxy/ProxySources/LocalProxySource.BaseTunnel.cs +++ b/src/TqkLibrary.Proxy/ProxySources/LocalProxySource.BaseTunnel.cs @@ -2,9 +2,9 @@ { public partial class LocalProxySource { - class BaseTunnel : BaseProxySourceTunnel + public class BaseTunnel : BaseProxySourceTunnel { - internal BaseTunnel(LocalProxySource proxySource, Guid tunnelId) : base(proxySource, tunnelId) + internal protected BaseTunnel(LocalProxySource proxySource, Guid tunnelId) : base(proxySource, tunnelId) { } diff --git a/src/TqkLibrary.Proxy/ProxySources/LocalProxySource.BindTunnel.cs b/src/TqkLibrary.Proxy/ProxySources/LocalProxySource.BindTunnel.cs index 16ea096..cb728bf 100644 --- a/src/TqkLibrary.Proxy/ProxySources/LocalProxySource.BindTunnel.cs +++ b/src/TqkLibrary.Proxy/ProxySources/LocalProxySource.BindTunnel.cs @@ -9,11 +9,11 @@ namespace TqkLibrary.Proxy.ProxySources { public partial class LocalProxySource { - class BindTunnel : BaseTunnel, IBindSource + public class BindTunnel : BaseTunnel, IBindSource { - TcpListener? _tcpListener; - TcpClient? _tcpClient; - internal BindTunnel(LocalProxySource proxySource, Guid tunnelId) : base(proxySource, tunnelId) + protected TcpListener? _tcpListener; + protected TcpClient? _tcpClient; + internal protected BindTunnel(LocalProxySource proxySource, Guid tunnelId) : base(proxySource, tunnelId) { } @@ -25,7 +25,7 @@ protected override void Dispose(bool isDisposing) base.Dispose(isDisposing); } - public async Task BindAsync(CancellationToken cancellationToken = default) + public virtual async Task BindAsync(CancellationToken cancellationToken = default) { CheckIsDisposed(); if (_tcpListener is null) @@ -44,7 +44,7 @@ public async Task BindAsync(CancellationToken cancellationToken = de return new IPEndPoint(await _proxySource.GetResponseIPAddressAsync(), ((IPEndPoint)_tcpListener.LocalEndpoint).Port); } - public async Task GetStreamAsync(CancellationToken cancellationToken = default) + public virtual async Task GetStreamAsync(CancellationToken cancellationToken = default) { CheckIsDisposed(); if (_tcpListener is null) @@ -69,8 +69,8 @@ public async Task GetStreamAsync(CancellationToken cancellationToken = d } - private event Action? OnEndAcceptTcpClient; - void OnBeginAcceptTcpClient(IAsyncResult ar) + protected virtual event Action? OnEndAcceptTcpClient; + protected virtual void OnBeginAcceptTcpClient(IAsyncResult ar) { try { diff --git a/src/TqkLibrary.Proxy/ProxySources/LocalProxySource.ConnectTunnel.cs b/src/TqkLibrary.Proxy/ProxySources/LocalProxySource.ConnectTunnel.cs index 59049b7..afc462f 100644 --- a/src/TqkLibrary.Proxy/ProxySources/LocalProxySource.ConnectTunnel.cs +++ b/src/TqkLibrary.Proxy/ProxySources/LocalProxySource.ConnectTunnel.cs @@ -5,11 +5,11 @@ namespace TqkLibrary.Proxy.ProxySources { public partial class LocalProxySource { - class ConnectTunnel : BaseProxySourceTunnel, IConnectSource + public class ConnectTunnel : BaseProxySourceTunnel, IConnectSource { - readonly TcpClient _tcpClient = new TcpClient(); - Stream? _stream = null; - public ConnectTunnel(LocalProxySource localProxySource, Guid tunnelId) : base(localProxySource, tunnelId) + protected readonly TcpClient _tcpClient = new TcpClient(); + protected Stream? _stream = null; + internal protected ConnectTunnel(LocalProxySource localProxySource, Guid tunnelId) : base(localProxySource, tunnelId) { } @@ -21,7 +21,7 @@ protected override void Dispose(bool isDisposing) base.Dispose(isDisposing); } - static readonly IEnumerable _SupportUriSchemes = new string[] + static protected readonly IEnumerable _SupportUriSchemes = new string[] { Uri.UriSchemeHttp, Uri.UriSchemeHttps, @@ -53,7 +53,7 @@ protected override void Dispose(bool isDisposing) #endif }; - public async Task ConnectAsync(Uri address, CancellationToken cancellationToken = default) + public virtual async Task ConnectAsync(Uri address, CancellationToken cancellationToken = default) { if (address is null) throw new ArgumentNullException(nameof(address)); @@ -91,7 +91,7 @@ await _tcpClient.ConnectAsync( } } - public Task GetStreamAsync(CancellationToken cancellationToken = default) + public virtual Task GetStreamAsync(CancellationToken cancellationToken = default) { if (_stream is null) throw new InvalidOperationException($"Mustbe run {nameof(ConnectAsync)} first"); diff --git a/src/TqkLibrary.Proxy/ProxySources/LocalProxySource.UdpTunnel.cs b/src/TqkLibrary.Proxy/ProxySources/LocalProxySource.UdpTunnel.cs index 531974c..ae19a7e 100644 --- a/src/TqkLibrary.Proxy/ProxySources/LocalProxySource.UdpTunnel.cs +++ b/src/TqkLibrary.Proxy/ProxySources/LocalProxySource.UdpTunnel.cs @@ -2,21 +2,14 @@ { public partial class LocalProxySource { - //class UdpTunnel : BaseTunnel, IUdpAssociateSource + //public class UdpTunnel : BaseTunnel, IUdpAssociateSource //{ - // internal UdpTunnel( - // LocalProxySource proxySource, - // CancellationToken cancellationToken = default - // ) - // : base( - // proxySource, - // cancellationToken - // ) + // internal protected UdpTunnel(LocalProxySource proxySource, CancellationToken cancellationToken = default) : base(proxySource, cancellationToken) // { // } - // public Task InitAsync(Uri address, CancellationToken cancellationToken = default) + // public virtual Task InitAsync(Uri address, CancellationToken cancellationToken = default) // { // throw new NotImplementedException(); // } diff --git a/src/TqkLibrary.Proxy/ProxySources/Socks4ProxySource.BaseTunnel.cs b/src/TqkLibrary.Proxy/ProxySources/Socks4ProxySource.BaseTunnel.cs index 43c5321..ad9b752 100644 --- a/src/TqkLibrary.Proxy/ProxySources/Socks4ProxySource.BaseTunnel.cs +++ b/src/TqkLibrary.Proxy/ProxySources/Socks4ProxySource.BaseTunnel.cs @@ -4,12 +4,12 @@ namespace TqkLibrary.Proxy.ProxySources { public partial class Socks4ProxySource { - class BaseTunnel : BaseProxySourceTunnel + public class BaseTunnel : BaseProxySourceTunnel { protected readonly TcpClient _tcpClient = new TcpClient(); protected Stream? _stream; - internal BaseTunnel(Socks4ProxySource proxySource, Guid tunnelId) : base(proxySource, tunnelId) + internal protected BaseTunnel(Socks4ProxySource proxySource, Guid tunnelId) : base(proxySource, tunnelId) { } @@ -20,7 +20,7 @@ protected override void Dispose(bool isDisposing) base.Dispose(isDisposing); } - protected async Task _ConnectToSocksServerAsync(CancellationToken cancellationToken = default) + protected virtual async Task _ConnectToSocksServerAsync(CancellationToken cancellationToken = default) { #if NET5_0_OR_GREATER await _tcpClient.ConnectAsync(_proxySource.iPEndPoint.Address, _proxySource.iPEndPoint.Port, cancellationToken); diff --git a/src/TqkLibrary.Proxy/ProxySources/Socks4ProxySource.BindTunnel.cs b/src/TqkLibrary.Proxy/ProxySources/Socks4ProxySource.BindTunnel.cs index 4c161b2..05521bf 100644 --- a/src/TqkLibrary.Proxy/ProxySources/Socks4ProxySource.BindTunnel.cs +++ b/src/TqkLibrary.Proxy/ProxySources/Socks4ProxySource.BindTunnel.cs @@ -8,15 +8,15 @@ namespace TqkLibrary.Proxy.ProxySources { public partial class Socks4ProxySource { - class BindTunnel : BaseTunnel, IBindSource + public class BindTunnel : BaseTunnel, IBindSource { - internal BindTunnel(Socks4ProxySource proxySource, Guid tunnelId) : base(proxySource, tunnelId) + internal protected BindTunnel(Socks4ProxySource proxySource, Guid tunnelId) : base(proxySource, tunnelId) { } - Socks4_RequestResponse? _socks4_RequestResponse; - public async Task BindAsync(CancellationToken cancellationToken = default) + protected Socks4_RequestResponse? _socks4_RequestResponse; + public virtual async Task BindAsync(CancellationToken cancellationToken = default) { CheckIsDisposed(); await _ConnectToSocksServerAsync(cancellationToken); @@ -42,7 +42,7 @@ public async Task BindAsync(CancellationToken cancellationToken = de } } - public Task GetStreamAsync(CancellationToken cancellationToken = default) + public virtual Task GetStreamAsync(CancellationToken cancellationToken = default) { if (_stream is null) throw new InvalidOperationException($"Mustbe run {nameof(BindAsync)} first"); diff --git a/src/TqkLibrary.Proxy/ProxySources/Socks4ProxySource.ConnectTunnel.cs b/src/TqkLibrary.Proxy/ProxySources/Socks4ProxySource.ConnectTunnel.cs index f021f74..c8d6c4c 100644 --- a/src/TqkLibrary.Proxy/ProxySources/Socks4ProxySource.ConnectTunnel.cs +++ b/src/TqkLibrary.Proxy/ProxySources/Socks4ProxySource.ConnectTunnel.cs @@ -7,13 +7,13 @@ namespace TqkLibrary.Proxy.ProxySources { public partial class Socks4ProxySource { - class ConnectTunnel : BaseTunnel, IConnectSource + public class ConnectTunnel : BaseTunnel, IConnectSource { - internal ConnectTunnel(Socks4ProxySource proxySource, Guid tunnelId) : base(proxySource, tunnelId) + internal protected ConnectTunnel(Socks4ProxySource proxySource, Guid tunnelId) : base(proxySource, tunnelId) { } - public async Task ConnectAsync(Uri address, CancellationToken cancellationToken = default) + public virtual async Task ConnectAsync(Uri address, CancellationToken cancellationToken = default) { if (address is null) throw new ArgumentNullException(nameof(address)); @@ -33,7 +33,7 @@ public async Task ConnectAsync(Uri address, CancellationToken cancellationToken } - public Task GetStreamAsync(CancellationToken cancellationToken = default) + public virtual Task GetStreamAsync(CancellationToken cancellationToken = default) { if (_stream is null) throw new InvalidOperationException($"Mustbe run {nameof(ConnectTunnel)}.{nameof(ConnectAsync)} first"); diff --git a/src/TqkLibrary.Proxy/ProxySources/Socks5ProxySource.BaseTunnel.cs b/src/TqkLibrary.Proxy/ProxySources/Socks5ProxySource.BaseTunnel.cs index 181e40a..4e341ba 100644 --- a/src/TqkLibrary.Proxy/ProxySources/Socks5ProxySource.BaseTunnel.cs +++ b/src/TqkLibrary.Proxy/ProxySources/Socks5ProxySource.BaseTunnel.cs @@ -7,7 +7,7 @@ namespace TqkLibrary.Proxy.ProxySources { public partial class Socks5ProxySource { - class BaseTunnel : BaseProxySourceTunnel + public class BaseTunnel : BaseProxySourceTunnel { protected const byte SOCKS5_VER = 0x05; protected const byte UsernamePassword_Ver = 0x01; @@ -15,7 +15,7 @@ class BaseTunnel : BaseProxySourceTunnel protected readonly TcpClient _tcpClient = new TcpClient(); protected Stream? _stream; - internal BaseTunnel(Socks5ProxySource proxySource, Guid tunnelId) : base(proxySource, tunnelId) + internal protected BaseTunnel(Socks5ProxySource proxySource, Guid tunnelId) : base(proxySource, tunnelId) { } @@ -36,7 +36,7 @@ protected override void Dispose(bool isDisposing) /// /// /// - protected async Task ConnectAndAuthAsync(CancellationToken cancellationToken = default) + protected virtual async Task ConnectAndAuthAsync(CancellationToken cancellationToken = default) { #if NET5_0_OR_GREATER await _tcpClient.ConnectAsync(_proxySource.IPEndPoint.Address, _proxySource.IPEndPoint.Port, cancellationToken); @@ -53,13 +53,13 @@ protected async Task ConnectAndAuthAsync(CancellationToken cancellationToken = d } - IEnumerable _GetSupportAuth() + protected virtual IEnumerable _GetSupportAuth() { if (_proxySource.HttpProxyAuthentication != null) yield return Socks5_Auth.UsernamePassword; yield return Socks5_Auth.NoAuthentication; } - async Task _ClientGreetingAsync(IEnumerable auths, CancellationToken cancellationToken = default) + protected virtual async Task _ClientGreetingAsync(IEnumerable auths, CancellationToken cancellationToken = default) { if (auths == null || !auths.Any()) throw new InvalidDataException($"{nameof(auths)} is null or empty"); @@ -78,7 +78,7 @@ async Task _ClientGreetingAsync(IEnumerable auths, Can } - async Task _AuthAsync(Socks5_Auth socks5_Auth, CancellationToken cancellationToken = default) + protected virtual async Task _AuthAsync(Socks5_Auth socks5_Auth, CancellationToken cancellationToken = default) { if (_stream is null) throw new InvalidOperationException(); diff --git a/src/TqkLibrary.Proxy/ProxySources/Socks5ProxySource.BindTunnel.cs b/src/TqkLibrary.Proxy/ProxySources/Socks5ProxySource.BindTunnel.cs index ae31d66..b9df38a 100644 --- a/src/TqkLibrary.Proxy/ProxySources/Socks5ProxySource.BindTunnel.cs +++ b/src/TqkLibrary.Proxy/ProxySources/Socks5ProxySource.BindTunnel.cs @@ -9,14 +9,14 @@ namespace TqkLibrary.Proxy.ProxySources { public partial class Socks5ProxySource { - class BindTunnel : BaseTunnel, IBindSource + public class BindTunnel : BaseTunnel, IBindSource { - internal BindTunnel(Socks5ProxySource proxySource, Guid tunnelId) : base(proxySource, tunnelId) + internal protected BindTunnel(Socks5ProxySource proxySource, Guid tunnelId) : base(proxySource, tunnelId) { } - public async Task BindAsync(CancellationToken cancellationToken = default) + public virtual async Task BindAsync(CancellationToken cancellationToken = default) { CheckIsDisposed(); await ConnectAndAuthAsync(); @@ -47,7 +47,7 @@ public async Task BindAsync(CancellationToken cancellationToken = de } } - public Task GetStreamAsync(CancellationToken cancellationToken = default) + public virtual Task GetStreamAsync(CancellationToken cancellationToken = default) { if (_stream is null) throw new InvalidOperationException($"Mustbe run {nameof(BindAsync)} first"); diff --git a/src/TqkLibrary.Proxy/ProxySources/Socks5ProxySource.ConnectTunnel.cs b/src/TqkLibrary.Proxy/ProxySources/Socks5ProxySource.ConnectTunnel.cs index 9ddae5b..4ec55c9 100644 --- a/src/TqkLibrary.Proxy/ProxySources/Socks5ProxySource.ConnectTunnel.cs +++ b/src/TqkLibrary.Proxy/ProxySources/Socks5ProxySource.ConnectTunnel.cs @@ -8,14 +8,14 @@ namespace TqkLibrary.Proxy.ProxySources { public partial class Socks5ProxySource { - class ConnectTunnel : BaseTunnel, IConnectSource + public class ConnectTunnel : BaseTunnel, IConnectSource { - internal ConnectTunnel(Socks5ProxySource proxySource, Guid tunnelId) : base(proxySource, tunnelId) + internal protected ConnectTunnel(Socks5ProxySource proxySource, Guid tunnelId) : base(proxySource, tunnelId) { } - public async Task ConnectAsync(Uri address, CancellationToken cancellationToken = default) + public virtual async Task ConnectAsync(Uri address, CancellationToken cancellationToken = default) { if (address is null) throw new ArgumentNullException(nameof(address)); @@ -32,7 +32,7 @@ public async Task ConnectAsync(Uri address, CancellationToken cancellationToken throw new InitConnectSourceFailedException($"{nameof(Socks5_STATUS)}: {socks5_RequestResponse.STATUS}"); } } - public Task GetStreamAsync(CancellationToken cancellationToken = default) + public virtual Task GetStreamAsync(CancellationToken cancellationToken = default) { if (_stream is null) throw new InvalidOperationException($"Mustbe run {nameof(ConnectTunnel)}.{nameof(ConnectAsync)} first"); diff --git a/src/TqkLibrary.Proxy/ProxySources/Socks5ProxySource.UdpTunnel.cs b/src/TqkLibrary.Proxy/ProxySources/Socks5ProxySource.UdpTunnel.cs index 8eb09ca..ddfaa98 100644 --- a/src/TqkLibrary.Proxy/ProxySources/Socks5ProxySource.UdpTunnel.cs +++ b/src/TqkLibrary.Proxy/ProxySources/Socks5ProxySource.UdpTunnel.cs @@ -2,19 +2,19 @@ { public partial class Socks5ProxySource { - //class UdpTunnel : BaseTunnel, IUdpAssociateSource + //public class UdpTunnel : BaseTunnel, IUdpAssociateSource //{ - // internal UdpTunnel(Socks5ProxySource proxySource) : base(proxySource) + // internal protected UdpTunnel(Socks5ProxySource proxySource, Guid tunnelId) : base(proxySource, tunnelId) // { // } - // public Task InitAsync(Uri address, CancellationToken cancellationToken = default) + // public virtual Task InitAsync(Uri address, CancellationToken cancellationToken = default) // { // throw new NotImplementedException(); // } - // public async Task InitUdpAssociateAsync(Uri address) + // public virtual async Task InitUdpAssociateAsync(Uri address) // { // } diff --git a/src/TqkLibrary.Proxy/StreamHeplers/StreamExtensions.cs b/src/TqkLibrary.Proxy/StreamHeplers/StreamExtensions.cs index 1bd0e5f..ee3660e 100644 --- a/src/TqkLibrary.Proxy/StreamHeplers/StreamExtensions.cs +++ b/src/TqkLibrary.Proxy/StreamHeplers/StreamExtensions.cs @@ -2,13 +2,15 @@ namespace TqkLibrary.Proxy.StreamHeplers { - internal static class StreamExtensions + public static class StreamExtensions { - internal static async Task TransferAsync(this Stream from, + public static async Task TransferAsync( + this Stream from, Stream to, long size, int bufferSize = 4096, - CancellationToken cancellationToken = default) + CancellationToken cancellationToken = default + ) { if (size <= 0) return; if (from == null) throw new ArgumentNullException(nameof(from)); @@ -27,10 +29,11 @@ internal static async Task TransferAsync(this Stream from, while (totalRead < size); } - internal static async Task ReadBytesAsync( + public static async Task ReadBytesAsync( this Stream from, int size, - CancellationToken cancellationToken = default) + CancellationToken cancellationToken = default + ) { if (size <= 0) return new byte[0]; if (from == null) throw new ArgumentNullException(nameof(from)); @@ -49,22 +52,31 @@ internal static async Task ReadBytesAsync( return buffer; } - internal static async Task ReadByteAsync( + public static async Task ReadByteAsync( this Stream from, - CancellationToken cancellationToken = default) + CancellationToken cancellationToken = default + ) { byte[] bytes = await ReadBytesAsync(from, 1, cancellationToken); return bytes.First(); } - internal static Task WriteAsync(this Stream stream, string text, CancellationToken cancellationToken = default) + public static Task WriteAsync( + this Stream stream, + string text, + CancellationToken cancellationToken = default + ) { if (string.IsNullOrWhiteSpace(text)) throw new ArgumentNullException(nameof(text)); byte[] buffer = Encoding.ASCII.GetBytes(text); return stream.WriteAsync(buffer, 0, buffer.Length, cancellationToken); } - internal static Task WriteLineAsync(this Stream stream, string text, CancellationToken cancellationToken = default) + public static Task WriteLineAsync( + this Stream stream, + string text, + CancellationToken cancellationToken = default + ) { if (string.IsNullOrWhiteSpace(text)) throw new ArgumentNullException(nameof(text)); byte[] buffer = new byte[text.Length + 2]; @@ -74,19 +86,29 @@ internal static Task WriteLineAsync(this Stream stream, string text, Cancellatio return stream.WriteAsync(buffer, 0, buffer.Length, cancellationToken); } - static readonly byte[] line_break = new byte[] { 13, 10 }; - internal static Task WriteLineAsync(this Stream stream, CancellationToken cancellationToken = default) + public static byte[] LineBreak => new byte[] { 13, 10 }; + public static async Task WriteLineAsync( + this Stream stream, + CancellationToken cancellationToken = default + ) { - return stream.WriteAsync(line_break, 0, line_break.Length, cancellationToken); + await stream.WriteAsync(LineBreak, cancellationToken); } - internal static Task WriteHeadersAsync(this Stream stream, IEnumerable headers, CancellationToken cancellationToken = default) + public static Task WriteHeadersAsync( + this Stream stream, + IEnumerable headers, + CancellationToken cancellationToken = default + ) { return stream.WriteLineAsync(string.Join("\r\n", headers) + "\r\n", cancellationToken); } - internal static async Task ReadLineAsync(this Stream stream, CancellationToken cancellationToken = default) + public static async Task ReadLineAsync( + this Stream stream, + CancellationToken cancellationToken = default + ) { using MemoryStream memoryStream = new MemoryStream(); byte[] buffer = new byte[1]; @@ -120,7 +142,11 @@ internal static async Task ReadLineAsync(this Stream stream, Cancellatio } } - internal static async Task ReadUntilNullTerminated(this Stream stream, int maxLength = 256 * 1024, CancellationToken cancellationToken = default) + public static async Task ReadUntilNullTerminated( + this Stream stream, + int maxLength = 256 * 1024, + CancellationToken cancellationToken = default + ) { using MemoryStream memoryStream = new MemoryStream(); byte[] buffer = new byte[1]; @@ -145,17 +171,13 @@ internal static async Task ReadUntilNullTerminated(this Stream stream, i } - -#if !NET5_0_OR_GREATER - public static Task WriteAsync(this Stream stream, byte[] buffer, CancellationToken cancellationToken = default) + public static Task WriteAsync( + this Stream stream, + byte[] buffer, + CancellationToken cancellationToken = default + ) { return stream.WriteAsync(buffer, 0, buffer.Length, cancellationToken); } - - public static void Write(this Stream stream, byte[] buffer) - { - stream.Write(buffer, 0, buffer.Length); - } -#endif } }