-
Notifications
You must be signed in to change notification settings - Fork 31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Using a proxy for tonClient #121
base: dev
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
}; | ||
Comment on lines
+27
to
+47
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Можно вынести в приватный метод по типу: private HttpClient CreateHttpClient(HttpApiParameters httpApiParameters) и там можно будет повысить читаемость кода (понизим вложенность и облегчим чуть конструктор) if (httpApiParameters.Proxy == null)
{
return new HttpClient();
}
var proxyAddress = BuildProxyAddress(httpApiParameters.Proxy);
var webProxy = new WebProxy {};
var httpClientHandler = new HttpClientHandler
{
Proxy = webProxy
};
return new HttpClient(httpClientHandler); +мб можно вынести и переиспользовать для других типов хттп клиентов There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Кстати да, факты, так было бы удобней |
||
|
||
_httpClient = new HttpClient(httpClientHandler); | ||
|
||
} | ||
else | ||
{ | ||
_httpClient = new HttpClient(); | ||
} | ||
_httpClient.Timeout = TimeSpan.FromMilliseconds(Convert.ToDouble(httpApiParameters.Timeout ?? 30000)); | ||
|
||
_httpClient.DefaultRequestHeaders.Accept.Clear(); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Как будто бы можно объединить повторяющийся код "protocol://" + httpApiParameters.Proxy.Ip + ":" + httpApiParameters.Proxy.Port.
Мб UriBuilder
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Partial, в данном случае, выглядит удобнее всего. В Models.cs у нас указываются только поля и их типы в разных классах. Реализация работы методов, на мой взгляд, там не к месту.
На счет логики сравнения прокси. А как по-другому указывать тип прокси? Как-то из enum доставать? Я просто не знаю)