Skip to content

Commit 1c298bf

Browse files
committed
Fix #233: "server_address" can hint the dcId in order to reuse existing AuthKey on last-ditch reconnection
1 parent 6a1114c commit 1c298bf

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

src/Client.cs

+13-4
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,9 @@ internal string Config(string what)
136136
Path.GetDirectoryName(Path.GetDirectoryName(AppDomain.CurrentDomain.BaseDirectory.TrimEnd(Path.DirectorySeparatorChar)))
137137
?? AppDomain.CurrentDomain.BaseDirectory, "WTelegram.session"),
138138
#if DEBUG
139-
"server_address" => "149.154.167.40:443", // Test DC 2
139+
"server_address" => "2>149.154.167.40:443", // Test DC 2
140140
#else
141-
"server_address" => "149.154.167.50:443", // DC 2
141+
"server_address" => "2>149.154.167.50:443", // DC 2
142142
#endif
143143
"device_model" => Environment.Is64BitOperatingSystem ? "PC 64bit" : "PC 32bit",
144144
"system_version" => Helpers.GetSystemVersion(),
@@ -772,6 +772,13 @@ static async Task<TcpClient> DefaultTcpHandler(string host, int port)
772772
return tcpClient;
773773
}
774774

775+
private IPEndPoint GetDefaultEndpoint(out int dcId)
776+
{
777+
string addr = Config("server_address");
778+
dcId = addr.Length > 2 && addr[1] == '>' && addr[0] is > '0' and <= '9' ? addr[0] - '0' : 0;
779+
return Compat.IPEndPoint_Parse(dcId == 0 ? addr : addr[2..]);
780+
}
781+
775782
/// <summary>Establish connection to Telegram servers without negociating a user session</summary>
776783
/// <param name="quickResume">Attempt to resume session immediately without issuing Layer/InitConnection/GetConfig <i>(not recommended by default)</i></param>
777784
/// <remarks>Usually you shouldn't need to call this method: Use <see cref="LoginUserIfNeeded">LoginUserIfNeeded</see> instead. <br/>Config callback is queried for: <b>server_address</b></remarks>
@@ -819,7 +826,7 @@ private async Task DoConnectAsync(bool quickResume)
819826
}
820827
else
821828
{
822-
endpoint = _dcSession?.EndPoint ?? Compat.IPEndPoint_Parse(Config("server_address"));
829+
endpoint = _dcSession?.EndPoint ?? GetDefaultEndpoint(out int defaultDc);
823830
Helpers.Log(2, $"Connecting to {endpoint}...");
824831
TcpClient tcpClient = null;
825832
try
@@ -855,11 +862,13 @@ private async Task DoConnectAsync(bool quickResume)
855862
}
856863
if (tcpClient == null)
857864
{
858-
endpoint = Compat.IPEndPoint_Parse(Config("server_address")); // re-ask callback for an address
865+
endpoint = GetDefaultEndpoint(out defaultDc); // re-ask callback for an address
859866
if (!triedEndpoints.Add(endpoint)) throw;
860867
_dcSession.Client = null;
861868
// is it address for a known DCSession?
862869
_dcSession = _session.DCSessions.Values.FirstOrDefault(dcs => dcs.EndPoint.Equals(endpoint));
870+
if (_dcSession == null && defaultDc != 0 && _session.DCSessions.TryGetValue(defaultDc, out _dcSession))
871+
_dcSession.DataCenter = null;
863872
_dcSession ??= new() { Id = Helpers.RandomLong() };
864873
_dcSession.Client = this;
865874
Helpers.Log(2, $"Connecting to {endpoint}...");

0 commit comments

Comments
 (0)