diff --git a/.github/ci.yml b/.github/ci.yml
index 9f08f4f..e27eb3e 100644
--- a/.github/ci.yml
+++ b/.github/ci.yml
@@ -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
diff --git a/.github/release.yml b/.github/release.yml
index dfc606c..a497ffd 100644
--- a/.github/release.yml
+++ b/.github/release.yml
@@ -1,7 +1,7 @@
pr: none
trigger: none
-name: 1.4.$(Rev:r)
+name: 1.5.$(Rev:r)
pool:
vmImage: ubuntu-latest
diff --git a/EXAMPLES.md b/EXAMPLES.md
index 4694d10..2e8c877 100644
--- a/EXAMPLES.md
+++ b/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.
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}");
+```
diff --git a/src/Client.cs b/src/Client.cs
index 16bb40a..c947fda 100644
--- a/src/Client.cs
+++ b/src/Client.cs
@@ -25,6 +25,8 @@ namespace WTelegram
/// This event will be called when an unsollicited update/message is sent by Telegram servers
/// See Examples/Program_ListenUpdate.cs for how to use this
public event Action Update;
+ public delegate Task 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 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;
diff --git a/src/Generator.cs b/src/Generator.cs
index e825ce0..869108b 100644
--- a/src/Generator.cs
+++ b/src/Generator.cs
@@ -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();