mirror of
https://github.com/wiz0u/WTelegramClient.git
synced 2025-12-06 06:52:01 +01:00
Support for proxy
This commit is contained in:
parent
391c7f96f0
commit
984c241a09
2
.github/ci.yml
vendored
2
.github/ci.yml
vendored
|
|
@ -2,7 +2,7 @@ pr: none
|
||||||
trigger:
|
trigger:
|
||||||
- master
|
- master
|
||||||
|
|
||||||
name: 1.4.2-ci.$(Rev:r)
|
name: 1.5.1-ci.$(Rev:r)
|
||||||
|
|
||||||
pool:
|
pool:
|
||||||
vmImage: ubuntu-latest
|
vmImage: ubuntu-latest
|
||||||
|
|
|
||||||
2
.github/release.yml
vendored
2
.github/release.yml
vendored
|
|
@ -1,7 +1,7 @@
|
||||||
pr: none
|
pr: none
|
||||||
trigger: none
|
trigger: none
|
||||||
|
|
||||||
name: 1.4.$(Rev:r)
|
name: 1.5.$(Rev:r)
|
||||||
|
|
||||||
pool:
|
pool:
|
||||||
vmImage: ubuntu-latest
|
vmImage: ubuntu-latest
|
||||||
|
|
|
||||||
13
EXAMPLES.md
13
EXAMPLES.md
|
|
@ -160,3 +160,16 @@ You can automate the collection of `access_hash` for the various resources obtai
|
||||||
|
|
||||||
This is done by activating the experimental `client.CollectAccessHash` system.
|
This is done by activating the experimental `client.CollectAccessHash` system.
|
||||||
See [Examples/Program_CollectAccessHash.cs](Examples/Program_CollectAccessHash.cs) for how to enable it, and save/restore them for later use.
|
See [Examples/Program_CollectAccessHash.cs](Examples/Program_CollectAccessHash.cs) for how to enable it, and save/restore them for later use.
|
||||||
|
|
||||||
|
### Use a proxy to connect to Telegram
|
||||||
|
This can be done using the client.TcpHandler delegate and a proxy library like [StarkSoftProxy](https://www.nuget.org/packages/StarkSoftProxy/):
|
||||||
|
```csharp
|
||||||
|
using var client = new WTelegram.Client(Environment.GetEnvironmentVariable);
|
||||||
|
client.TcpHandler = async (address, port) =>
|
||||||
|
{
|
||||||
|
var proxy = new Socks5ProxyClient(ProxyHost, ProxyPort, ProxyUsername, ProxyPassword);
|
||||||
|
return proxy.CreateConnection(address, port);
|
||||||
|
};
|
||||||
|
var user = await client.LoginUserIfNeeded();
|
||||||
|
Console.WriteLine($"We are logged-in as {user.username ?? user.first_name + " " + user.last_name}");
|
||||||
|
```
|
||||||
|
|
|
||||||
|
|
@ -25,6 +25,8 @@ namespace WTelegram
|
||||||
/// <summary>This event will be called when an unsollicited update/message is sent by Telegram servers</summary>
|
/// <summary>This event will be called when an unsollicited update/message is sent by Telegram servers</summary>
|
||||||
/// <remarks>See <see href="https://github.com/wiz0u/WTelegramClient/tree/master/Examples/Program_ListenUpdate.cs">Examples/Program_ListenUpdate.cs</see> for how to use this</remarks>
|
/// <remarks>See <see href="https://github.com/wiz0u/WTelegramClient/tree/master/Examples/Program_ListenUpdate.cs">Examples/Program_ListenUpdate.cs</see> for how to use this</remarks>
|
||||||
public event Action<ITLObject> Update;
|
public event Action<ITLObject> Update;
|
||||||
|
public delegate Task<TcpClient> TcpFactory(string address, int port);
|
||||||
|
public TcpFactory TcpHandler = DefaultTcpHandler; // return a connected TcpClient or throw an exception
|
||||||
public Config TLConfig { get; private set; }
|
public Config TLConfig { get; private set; }
|
||||||
public int MaxAutoReconnects { get; set; } = 5; // number of automatic reconnections on connection/reactor failure
|
public int MaxAutoReconnects { get; set; } = 5; // number of automatic reconnections on connection/reactor failure
|
||||||
public bool IsMainDC => (_dcSession?.DataCenter?.id ?? 0) == _session.MainDC;
|
public bool IsMainDC => (_dcSession?.DataCenter?.id ?? 0) == _session.MainDC;
|
||||||
|
|
@ -163,16 +165,31 @@ namespace WTelegram
|
||||||
await _connecting;
|
await _connecting;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static async Task<TcpClient> DefaultTcpHandler(string host, int port)
|
||||||
|
{
|
||||||
|
var tcpClient = new TcpClient();
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await tcpClient.ConnectAsync(host, port);
|
||||||
|
}
|
||||||
|
catch (Exception)
|
||||||
|
{
|
||||||
|
tcpClient.Dispose();
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
return tcpClient;
|
||||||
|
}
|
||||||
|
|
||||||
private async Task DoConnectAsync()
|
private async Task DoConnectAsync()
|
||||||
{
|
{
|
||||||
var endpoint = _dcSession?.EndPoint ?? Compat.IPEndPoint_Parse(Config("server_address"));
|
var endpoint = _dcSession?.EndPoint ?? Compat.IPEndPoint_Parse(Config("server_address"));
|
||||||
Helpers.Log(2, $"Connecting to {endpoint}...");
|
Helpers.Log(2, $"Connecting to {endpoint}...");
|
||||||
var tcpClient = new TcpClient(AddressFamily.InterNetworkV6) { Client = { DualMode = true } }; // this allows both IPv4 & IPv6
|
TcpClient tcpClient = null;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
await tcpClient.ConnectAsync(endpoint.Address, endpoint.Port);
|
tcpClient = await TcpHandler(endpoint.Address.ToString(), endpoint.Port);
|
||||||
}
|
}
|
||||||
catch (SocketException ex) // cannot connect to target endpoint, try to find an alternate
|
catch (SocketException ex) // cannot connect to target endpoint, try to find an alternate
|
||||||
{
|
{
|
||||||
|
|
@ -192,14 +209,14 @@ namespace WTelegram
|
||||||
Helpers.Log(2, $"Connecting to {endpoint}...");
|
Helpers.Log(2, $"Connecting to {endpoint}...");
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
await tcpClient.ConnectAsync(endpoint.Address, endpoint.Port);
|
tcpClient = await TcpHandler(endpoint.Address.ToString(), endpoint.Port);
|
||||||
_dcSession.DataCenter = dcOption;
|
_dcSession.DataCenter = dcOption;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
catch (SocketException) { }
|
catch (SocketException) { }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!tcpClient.Connected)
|
if (tcpClient == null)
|
||||||
{
|
{
|
||||||
endpoint = Compat.IPEndPoint_Parse(Config("server_address")); // re-ask callback for an address
|
endpoint = Compat.IPEndPoint_Parse(Config("server_address")); // re-ask callback for an address
|
||||||
if (!triedEndpoints.Add(endpoint)) throw;
|
if (!triedEndpoints.Add(endpoint)) throw;
|
||||||
|
|
@ -209,13 +226,13 @@ namespace WTelegram
|
||||||
_dcSession ??= new() { Id = Helpers.RandomLong() };
|
_dcSession ??= new() { Id = Helpers.RandomLong() };
|
||||||
_dcSession.Client = this;
|
_dcSession.Client = this;
|
||||||
Helpers.Log(2, $"Connecting to {endpoint}...");
|
Helpers.Log(2, $"Connecting to {endpoint}...");
|
||||||
await tcpClient.ConnectAsync(endpoint.Address, endpoint.Port);
|
tcpClient = await TcpHandler(endpoint.Address.ToString(), endpoint.Port);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (Exception)
|
catch (Exception)
|
||||||
{
|
{
|
||||||
tcpClient.Dispose();
|
tcpClient?.Dispose();
|
||||||
throw;
|
throw;
|
||||||
}
|
}
|
||||||
_tcpClient = tcpClient;
|
_tcpClient = tcpClient;
|
||||||
|
|
|
||||||
|
|
@ -201,7 +201,6 @@ namespace WTelegram
|
||||||
if (autoProps.Count > 0 && autoPropsCount > 1)
|
if (autoProps.Count > 0 && autoPropsCount > 1)
|
||||||
typeInfo.AutoProps = autoProps;
|
typeInfo.AutoProps = autoProps;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
var layers = schema.constructors.Select(c => c.layer).Distinct().ToList();
|
var layers = schema.constructors.Select(c => c.layer).Distinct().ToList();
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue