Support for proxy

This commit is contained in:
Wizou 2021-10-25 02:40:15 +02:00
parent 391c7f96f0
commit 984c241a09
5 changed files with 38 additions and 9 deletions

2
.github/ci.yml vendored
View file

@ -2,7 +2,7 @@ pr: none
trigger:
- master
name: 1.4.2-ci.$(Rev:r)
name: 1.5.1-ci.$(Rev:r)
pool:
vmImage: ubuntu-latest

2
.github/release.yml vendored
View file

@ -1,7 +1,7 @@
pr: none
trigger: none
name: 1.4.$(Rev:r)
name: 1.5.$(Rev:r)
pool:
vmImage: ubuntu-latest

View file

@ -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.
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}");
```

View file

@ -25,6 +25,8 @@ namespace WTelegram
/// <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>
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 int MaxAutoReconnects { get; set; } = 5; // number of automatic reconnections on connection/reactor failure
public bool IsMainDC => (_dcSession?.DataCenter?.id ?? 0) == _session.MainDC;
@ -163,16 +165,31 @@ namespace WTelegram
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()
{
var endpoint = _dcSession?.EndPoint ?? Compat.IPEndPoint_Parse(Config("server_address"));
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
{
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
{
@ -192,14 +209,14 @@ namespace WTelegram
Helpers.Log(2, $"Connecting to {endpoint}...");
try
{
await tcpClient.ConnectAsync(endpoint.Address, endpoint.Port);
tcpClient = await TcpHandler(endpoint.Address.ToString(), endpoint.Port);
_dcSession.DataCenter = dcOption;
break;
}
catch (SocketException) { }
}
}
if (!tcpClient.Connected)
if (tcpClient == null)
{
endpoint = Compat.IPEndPoint_Parse(Config("server_address")); // re-ask callback for an address
if (!triedEndpoints.Add(endpoint)) throw;
@ -209,13 +226,13 @@ namespace WTelegram
_dcSession ??= new() { Id = Helpers.RandomLong() };
_dcSession.Client = this;
Helpers.Log(2, $"Connecting to {endpoint}...");
await tcpClient.ConnectAsync(endpoint.Address, endpoint.Port);
tcpClient = await TcpHandler(endpoint.Address.ToString(), endpoint.Port);
}
}
}
catch (Exception)
{
tcpClient.Dispose();
tcpClient?.Dispose();
throw;
}
_tcpClient = tcpClient;

View file

@ -201,7 +201,6 @@ namespace WTelegram
if (autoProps.Count > 0 && autoPropsCount > 1)
typeInfo.AutoProps = autoProps;
}
}
}
var layers = schema.constructors.Select(c => c.layer).Distinct().ToList();