From 505ac6f7b1ea56f8284bf4c9f84149cbcb5174d2 Mon Sep 17 00:00:00 2001
From: Fenixs8973 <61942811+Fenixs8973@users.noreply.github.com>
Date: Sat, 27 Jul 2024 04:49:10 +0600
Subject: [PATCH 1/3] Using proxy for tonClient
---
TonSdk.Client/src/HttpApi/HttpsApi.cs | 34 +++++++++++++++++++++++-
TonSdk.Client/src/HttpApi/HttpsApiV3.cs | 30 ++++++++++++++++++++-
TonSdk.Client/src/HttpApi/HttpsWhales.cs | 30 ++++++++++++++++++++-
TonSdk.Client/src/Models/Models.cs | 18 +++++++++++++
4 files changed, 109 insertions(+), 3 deletions(-)
diff --git a/TonSdk.Client/src/HttpApi/HttpsApi.cs b/TonSdk.Client/src/HttpApi/HttpsApi.cs
index fda9b9e..25377dc 100644
--- a/TonSdk.Client/src/HttpApi/HttpsApi.cs
+++ b/TonSdk.Client/src/HttpApi/HttpsApi.cs
@@ -1,6 +1,8 @@
using Newtonsoft.Json;
using System;
+using System.Net;
using System.Net.Http;
+using System.Net.Http.Headers;
using System.Threading.Tasks;
using TonSdk.Core;
using TonSdk.Core.Block;
@@ -16,6 +18,8 @@ public class HttpApiParameters
public string ApiKey { get; set; }
}
+
+
public class HttpApi : IDisposable
{
private readonly HttpClient _httpClient;
@@ -27,7 +31,35 @@ internal HttpApi(HttpParameters httpApiParameters)
throw new ArgumentNullException("Endpoint field in Http options cannot be null.");
}
- _httpClient = new HttpClient();
+ if (httpApiParameters.Proxy != null)
+ {
+ WebProxy webProxy = new WebProxy
+ {
+ Address = new Uri( httpApiParameters.Proxy.ProxyType switch
+ {
+ ProxyType.HTTP => "http://" + httpApiParameters.Proxy.Ip + ":" + httpApiParameters.Proxy.Port,
+ ProxyType.HTTPS => "https://" + httpApiParameters.Proxy.Ip + ":" + httpApiParameters.Proxy.Port,
+ ProxyType.Socks4 => "socks4://" + httpApiParameters.Proxy.Ip + ":" + httpApiParameters.Proxy.Port,
+ ProxyType.Socks5 => "socks5://" + httpApiParameters.Proxy.Ip + ":" + httpApiParameters.Proxy.Port,
+ _ => throw new ArgumentOutOfRangeException()
+ }),
+ Credentials = new NetworkCredential(
+ userName: httpApiParameters.Proxy.UserName,
+ password: httpApiParameters.Proxy.Password
+ )
+ };
+ HttpClientHandler httpClientHandler = new HttpClientHandler
+ {
+ Proxy = webProxy
+ };
+
+ _httpClient = new HttpClient(httpClientHandler);
+
+ }
+ else
+ {
+ _httpClient = new HttpClient();
+ }
_httpClient.Timeout = TimeSpan.FromMilliseconds(Convert.ToDouble(httpApiParameters.Timeout ?? 30000));
//httpClient.DefaultRequestHeaders.Add("Content-Type", "application/json");
diff --git a/TonSdk.Client/src/HttpApi/HttpsApiV3.cs b/TonSdk.Client/src/HttpApi/HttpsApiV3.cs
index 40d1758..3c2bd1a 100644
--- a/TonSdk.Client/src/HttpApi/HttpsApiV3.cs
+++ b/TonSdk.Client/src/HttpApi/HttpsApiV3.cs
@@ -2,6 +2,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
+using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using TonSdk.Client.Stack;
@@ -23,8 +24,35 @@ public HttpApiV3(HttpParameters httpApiParameters)
throw new ArgumentNullException("Endpoint field in Http options cannot be null.");
}
- _httpClient = new HttpClient();
+ if (httpApiParameters.Proxy != null)
+ {
+ WebProxy webProxy = new WebProxy
+ {
+ Address = new Uri( httpApiParameters.Proxy.ProxyType switch
+ {
+ ProxyType.HTTP => "http://" + httpApiParameters.Proxy.Ip + ":" + httpApiParameters.Proxy.Port,
+ ProxyType.HTTPS => "https://" + httpApiParameters.Proxy.Ip + ":" + httpApiParameters.Proxy.Port,
+ ProxyType.Socks4 => "socks4://" + httpApiParameters.Proxy.Ip + ":" + httpApiParameters.Proxy.Port,
+ ProxyType.Socks5 => "socks5://" + httpApiParameters.Proxy.Ip + ":" + httpApiParameters.Proxy.Port,
+ _ => throw new ArgumentOutOfRangeException()
+ }),
+ Credentials = new NetworkCredential(
+ userName: httpApiParameters.Proxy.UserName,
+ password: httpApiParameters.Proxy.Password
+ )
+ };
+ HttpClientHandler httpClientHandler = new HttpClientHandler
+ {
+ Proxy = webProxy
+ };
+ _httpClient = new HttpClient(httpClientHandler);
+
+ }
+ else
+ {
+ _httpClient = new HttpClient();
+ }
_httpClient.Timeout = TimeSpan.FromMilliseconds(Convert.ToDouble(httpApiParameters.Timeout ?? 30000));
_httpClient.DefaultRequestHeaders.Accept.Clear();
diff --git a/TonSdk.Client/src/HttpApi/HttpsWhales.cs b/TonSdk.Client/src/HttpApi/HttpsWhales.cs
index 41bc985..271400c 100644
--- a/TonSdk.Client/src/HttpApi/HttpsWhales.cs
+++ b/TonSdk.Client/src/HttpApi/HttpsWhales.cs
@@ -1,5 +1,6 @@
using Newtonsoft.Json;
using System;
+using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using TonSdk.Core;
@@ -21,8 +22,35 @@ internal HttpWhales(HttpParameters httpApiParameters)
throw new ArgumentNullException("Endpoint field in Http options cannot be null.");
}
- _httpClient = new HttpClient();
+ if (httpApiParameters.Proxy != null)
+ {
+ WebProxy webProxy = new WebProxy
+ {
+ Address = new Uri( httpApiParameters.Proxy.ProxyType switch
+ {
+ ProxyType.HTTP => "http://" + httpApiParameters.Proxy.Ip + ":" + httpApiParameters.Proxy.Port,
+ ProxyType.HTTPS => "https://" + httpApiParameters.Proxy.Ip + ":" + httpApiParameters.Proxy.Port,
+ ProxyType.Socks4 => "socks4://" + httpApiParameters.Proxy.Ip + ":" + httpApiParameters.Proxy.Port,
+ ProxyType.Socks5 => "socks5://" + httpApiParameters.Proxy.Ip + ":" + httpApiParameters.Proxy.Port,
+ _ => throw new ArgumentOutOfRangeException()
+ }),
+ Credentials = new NetworkCredential(
+ userName: httpApiParameters.Proxy.UserName,
+ password: httpApiParameters.Proxy.Password
+ )
+ };
+ HttpClientHandler httpClientHandler = new HttpClientHandler
+ {
+ Proxy = webProxy
+ };
+ _httpClient = new HttpClient(httpClientHandler);
+
+ }
+ else
+ {
+ _httpClient = new HttpClient();
+ }
_httpClient.Timeout = TimeSpan.FromMilliseconds(Convert.ToDouble(httpApiParameters.Timeout ?? 30000));
//httpClient.DefaultRequestHeaders.Add("Content-Type", "application/json");
diff --git a/TonSdk.Client/src/Models/Models.cs b/TonSdk.Client/src/Models/Models.cs
index bad3d40..91d32e6 100644
--- a/TonSdk.Client/src/Models/Models.cs
+++ b/TonSdk.Client/src/Models/Models.cs
@@ -16,6 +16,14 @@ public enum TonClientType
}
+ public enum ProxyType
+ {
+ Socks5,
+ Socks4,
+ HTTP,
+ HTTPS
+ }
+
public interface ITonClientOptions {}
public class HttpParameters : ITonClientOptions
@@ -23,6 +31,7 @@ public class HttpParameters : ITonClientOptions
public string Endpoint { get; set; }
public int? Timeout { get; set; }
public string ApiKey { get; set; }
+ public Proxy? Proxy { get; set; }
}
public class LiteClientParameters : ITonClientOptions
@@ -38,4 +47,13 @@ public LiteClientParameters(string host, int port, string peerPublicKey)
PeerPublicKey = peerPublicKey;
}
}
+
+ public class Proxy : ITonClientOptions
+ {
+ public string Ip { get; set; }
+ public string Port { get; set; }
+ public string UserName { get; set; }
+ public string Password { get; set; }
+ public ProxyType ProxyType { get; set; }
+ }
}
\ No newline at end of file
From dfdcbbf2683be23bd00a2200aed9fdb7467b4968 Mon Sep 17 00:00:00 2001
From: Fenixs8973 <61942811+Fenixs8973@users.noreply.github.com>
Date: Mon, 29 Jul 2024 16:15:21 +0600
Subject: [PATCH 2/3] Overriding Proxy.Equals & GetHashCode in Proxy
---
TonSdk.Client/src/HttpApi/HttpsApi.cs | 2 --
TonSdk.Client/src/Models/Models.cs | 2 +-
TonSdk.Client/src/Models/Proxy.cs | 30 +++++++++++++++++++++++
TonSdk.Client/test/Client.test.cs | 35 +++++++++++++++++++++++++++
4 files changed, 66 insertions(+), 3 deletions(-)
create mode 100644 TonSdk.Client/src/Models/Proxy.cs
diff --git a/TonSdk.Client/src/HttpApi/HttpsApi.cs b/TonSdk.Client/src/HttpApi/HttpsApi.cs
index 25377dc..c16382a 100644
--- a/TonSdk.Client/src/HttpApi/HttpsApi.cs
+++ b/TonSdk.Client/src/HttpApi/HttpsApi.cs
@@ -17,9 +17,7 @@ public class HttpApiParameters
public int? Timeout { get; set; }
public string ApiKey { get; set; }
}
-
-
public class HttpApi : IDisposable
{
private readonly HttpClient _httpClient;
diff --git a/TonSdk.Client/src/Models/Models.cs b/TonSdk.Client/src/Models/Models.cs
index 91d32e6..910ab49 100644
--- a/TonSdk.Client/src/Models/Models.cs
+++ b/TonSdk.Client/src/Models/Models.cs
@@ -48,7 +48,7 @@ public LiteClientParameters(string host, int port, string peerPublicKey)
}
}
- public class Proxy : ITonClientOptions
+ public partial class Proxy : ITonClientOptions
{
public string Ip { get; set; }
public string Port { get; set; }
diff --git a/TonSdk.Client/src/Models/Proxy.cs b/TonSdk.Client/src/Models/Proxy.cs
new file mode 100644
index 0000000..1b6547d
--- /dev/null
+++ b/TonSdk.Client/src/Models/Proxy.cs
@@ -0,0 +1,30 @@
+using Org.BouncyCastle.Crypto.Parameters;
+
+namespace TonSdk.Client
+{
+ public partial class Proxy
+ {
+ public override bool Equals(object obj)
+ {
+ Proxy tmpProxy = obj as Proxy;
+ if (tmpProxy == null) return false;
+
+ if (Ip != tmpProxy.Ip) return false;
+ if (Port != tmpProxy.Port) return false;
+ if (UserName != tmpProxy.UserName) return false;
+ if (Password != tmpProxy.Password) return false;
+ if (ProxyType != tmpProxy.ProxyType) return false;
+ return true;
+ }
+
+ public override int GetHashCode()
+ {
+ int hashCode = Ip.GetHashCode();
+ hashCode = 31 * hashCode + Port.GetHashCode();
+ hashCode = 31 * hashCode + UserName.GetHashCode();
+ hashCode = 31 * hashCode + Password.GetHashCode();
+ hashCode = 31 * hashCode + ProxyType.GetHashCode();
+ return hashCode;
+ }
+ }
+}
\ No newline at end of file
diff --git a/TonSdk.Client/test/Client.test.cs b/TonSdk.Client/test/Client.test.cs
index 23b9b4d..593a765 100644
--- a/TonSdk.Client/test/Client.test.cs
+++ b/TonSdk.Client/test/Client.test.cs
@@ -220,4 +220,39 @@ public async Task Test_JettonGetWalletAddress()
Assert.That((await client_lite.Jetton.GetWalletAddress(new Address("EQBlqsm144Dq6SjbPI4jjZvA1hqTIP3CvHovbIfW_t-SCALE"), new Address("EQAEnqomwC3dg323OcdgUsvk3T38VvYawX8q6x38ulfnCn7b"))).Equals(new Address("EQA_d9IqxSQCSuwZIvH0RRSUMvWK4qrvl5ZH_nOHFH7Gxifq")), Is.EqualTo(true));
Assert.That((await client_lite.Jetton.GetWalletAddress(new Address("EQBlqsm144Dq6SjbPI4jjZvA1hqTIP3CvHovbIfW_t-SCALE"), new Address("EQAEnqomwC3dg323OcdgUsvk3T38VvYawX8q6x38ulfnCn7b"))).Equals(new Address("EQAEnqomwC3dg323OcdgUsvk3T38VvYawX8q6x38ulfnCn7b")), Is.EqualTo(false));
}
+
+ [Test]
+ public void Test_ProxyClass()
+ {
+ Proxy proxy1 = new Proxy
+ {
+ Ip = "1.1.1.1",
+ Port = "22222",
+ UserName = "WhoIsIt?",
+ Password = "54321"
+ };
+
+ Proxy proxy2 = new Proxy
+ {
+ Ip = "1.1.1.1",
+ Port = "22222",
+ UserName = "WhoIsIt?",
+ Password = "54321"
+ };
+
+ Proxy proxy3 = new Proxy
+ {
+ Ip = "123.123.123.123",
+ Port = "33333",
+ UserName = "ItsMe,Mario",
+ Password = "51423"
+ };
+
+ Assert.That(proxy1.GetHashCode(), Is.EqualTo(1272649444));
+ Assert.That(proxy2.GetHashCode(), Is.EqualTo(1730258271));
+ Assert.That(proxy3.GetHashCode(), Is.EqualTo(581039570));
+
+ Assert.That(proxy1.Equals(proxy2), Is.True);
+ Assert.That(proxy1.Equals(proxy3), Is.False);
+ }
}
From ab053b34945c099955b0e3af7051127cad1b2851 Mon Sep 17 00:00:00 2001
From: Fenixs8973 <61942811+Fenixs8973@users.noreply.github.com>
Date: Tue, 30 Jul 2024 18:25:43 +0600
Subject: [PATCH 3/3] Changing the proxy & add summary
Changing the data type of an Ip address variable;
Adding a value constraint for the port variable
Adding a description to the fields of the Proxy class
---
TonSdk.Client/src/Models/Models.cs | 40 ++++++++++++++++++++++++++++--
TonSdk.Client/test/Client.test.cs | 19 +++++++-------
2 files changed, 48 insertions(+), 11 deletions(-)
diff --git a/TonSdk.Client/src/Models/Models.cs b/TonSdk.Client/src/Models/Models.cs
index 910ab49..4c26fa0 100644
--- a/TonSdk.Client/src/Models/Models.cs
+++ b/TonSdk.Client/src/Models/Models.cs
@@ -1,5 +1,7 @@
using System;
using System.Collections.Generic;
+using System.Net;
+using System.Reflection.Metadata;
using System.Threading.Tasks;
using TonSdk.Client.Stack;
using TonSdk.Core;
@@ -48,12 +50,46 @@ public LiteClientParameters(string host, int port, string peerPublicKey)
}
}
+ ///
+ /// Credentials for connecting to the proxy
+ ///
public partial class Proxy : ITonClientOptions
{
- public string Ip { get; set; }
- public string Port { get; set; }
+ ///
+ /// Ip address of the proxy server
+ ///
+ public IPAddress Ip { get; set; }
+
+ private int _port;
+
+ ///
+ /// Port number of the proxy server
+ ///
+ /// The proxy port number goes beyond 1-65536
+ public int Port
+ {
+ set
+ {
+ if (value < 1 || value > 65536)
+ throw new Exception("The proxy port number goes beyond 1-65536");
+ _port = value;
+ }
+ get => _port;
+ }
+
+ ///
+ /// The user's name of the credentials
+ ///
public string UserName { get; set; }
+
+ ///
+ /// The password's of the credentials
+ ///
public string Password { get; set; }
+
+ ///
+ /// Type of proxy server protocol
+ ///
public ProxyType ProxyType { get; set; }
}
}
\ No newline at end of file
diff --git a/TonSdk.Client/test/Client.test.cs b/TonSdk.Client/test/Client.test.cs
index 593a765..accc128 100644
--- a/TonSdk.Client/test/Client.test.cs
+++ b/TonSdk.Client/test/Client.test.cs
@@ -1,4 +1,5 @@
using System;
+using System.Net;
using System.Numerics;
using System.Threading.Tasks;
using NUnit.Framework;
@@ -226,31 +227,31 @@ public void Test_ProxyClass()
{
Proxy proxy1 = new Proxy
{
- Ip = "1.1.1.1",
- Port = "22222",
+ Ip = IPAddress.Parse("1.1.1.1"),
+ Port = 22222,
UserName = "WhoIsIt?",
Password = "54321"
};
Proxy proxy2 = new Proxy
{
- Ip = "1.1.1.1",
- Port = "22222",
+ Ip = IPAddress.Parse("1.1.1.1"),
+ Port = 22222,
UserName = "WhoIsIt?",
Password = "54321"
};
Proxy proxy3 = new Proxy
{
- Ip = "123.123.123.123",
- Port = "33333",
+ Ip = IPAddress.Parse("123.123.123.123"),
+ Port = 33333,
UserName = "ItsMe,Mario",
Password = "51423"
};
- Assert.That(proxy1.GetHashCode(), Is.EqualTo(1272649444));
- Assert.That(proxy2.GetHashCode(), Is.EqualTo(1730258271));
- Assert.That(proxy3.GetHashCode(), Is.EqualTo(581039570));
+ Assert.That(proxy1.GetHashCode(), Is.EqualTo(-1512725739));
+ Assert.That(proxy2.GetHashCode(), Is.EqualTo(-1512725739));
+ Assert.That(proxy3.GetHashCode(), Is.EqualTo(168523034));
Assert.That(proxy1.Equals(proxy2), Is.True);
Assert.That(proxy1.Equals(proxy3), Is.False);