Skip to content

Commit

Permalink
Support for proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
Wizou authored and Wizou committed Oct 25, 2021
1 parent 391c7f9 commit 984c241
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .github/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion .github/release.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
pr: none
trigger: none

name: 1.4.$(Rev:r)
name: 1.5.$(Rev:r)

pool:
vmImage: ubuntu-latest
Expand Down
13 changes: 13 additions & 0 deletions EXAMPLES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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}");
```
29 changes: 23 additions & 6 deletions src/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public sealed class Client : IDisposable
/// <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;
Expand Down Expand Up @@ -163,16 +165,31 @@ public async Task ConnectAsync()
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
{
Expand All @@ -192,14 +209,14 @@ private async Task DoConnectAsync()
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;
Expand All @@ -209,13 +226,13 @@ private async Task DoConnectAsync()
_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;
Expand Down
1 change: 0 additions & 1 deletion src/Generator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,6 @@ public void FromSchema(SchemaJson schema, string outputCs)
if (autoProps.Count > 0 && autoPropsCount > 1)
typeInfo.AutoProps = autoProps;
}

}
}
var layers = schema.constructors.Select(c => c.layer).Distinct().ToList();
Expand Down

0 comments on commit 984c241

Please sign in to comment.