Use signed DcId values (improved logs)

This commit is contained in:
Wizou 2025-06-26 22:02:26 +02:00
parent 04e043222e
commit 3ff1200068
3 changed files with 16 additions and 17 deletions

View file

@ -325,7 +325,7 @@ namespace WTelegram
try try
{ {
Auth_ExportedAuthorization exported = null; Auth_ExportedAuthorization exported = null;
if (_session.UserId != 0 && IsMainDC && altSession.UserId != _session.UserId && altSession.DcID != _dcSession.DcID) if (_session.UserId != 0 && IsMainDC && altSession.UserId != _session.UserId && Math.Abs(altSession.DcID) != Math.Abs(_dcSession.DcID))
exported = await this.Auth_ExportAuthorization(Math.Abs(dcId)); exported = await this.Auth_ExportAuthorization(Math.Abs(dcId));
await altSession.Client.ConnectAsync(); await altSession.Client.ConnectAsync();
if (exported != null) if (exported != null)
@ -345,16 +345,16 @@ namespace WTelegram
return altSession.Client; return altSession.Client;
} }
private async Task Reactor(Stream stream, CancellationTokenSource cts) private async Task Reactor(Stream stream, CancellationToken ct)
{ {
const int MinBufferSize = 1024; const int MinBufferSize = 1024;
var data = new byte[MinBufferSize]; var data = new byte[MinBufferSize];
while (!cts.IsCancellationRequested) while (!ct.IsCancellationRequested)
{ {
IObject obj = null; IObject obj = null;
try try
{ {
if (await stream.FullReadAsync(data, 4, cts.Token) != 4) if (await stream.FullReadAsync(data, 4, ct) != 4)
throw new WTException(ConnectionShutDown); throw new WTException(ConnectionShutDown);
#if OBFUSCATION #if OBFUSCATION
_recvCtr.EncryptDecrypt(data, 4); _recvCtr.EncryptDecrypt(data, 4);
@ -366,7 +366,7 @@ namespace WTelegram
data = new byte[payloadLen]; data = new byte[payloadLen];
else if (Math.Max(payloadLen, MinBufferSize) < data.Length / 4) else if (Math.Max(payloadLen, MinBufferSize) < data.Length / 4)
data = new byte[Math.Max(payloadLen, MinBufferSize)]; data = new byte[Math.Max(payloadLen, MinBufferSize)];
if (await stream.FullReadAsync(data, payloadLen, cts.Token) != payloadLen) if (await stream.FullReadAsync(data, payloadLen, ct) != payloadLen)
throw new WTException("Could not read frame data : Connection shut down"); throw new WTException("Could not read frame data : Connection shut down");
#if OBFUSCATION #if OBFUSCATION
_recvCtr.EncryptDecrypt(data, payloadLen); _recvCtr.EncryptDecrypt(data, payloadLen);
@ -375,14 +375,14 @@ namespace WTelegram
} }
catch (Exception ex) // an exception in RecvAsync is always fatal catch (Exception ex) // an exception in RecvAsync is always fatal
{ {
if (cts.IsCancellationRequested) return; if (ct.IsCancellationRequested) return;
bool disconnectedAltDC = !IsMainDC && ex is WTException { Message: ConnectionShutDown } or IOException { InnerException: SocketException }; bool disconnectedAltDC = !IsMainDC && ex is WTException { Message: ConnectionShutDown } or IOException { InnerException: SocketException };
if (disconnectedAltDC) if (disconnectedAltDC)
Helpers.Log(3, $"{_dcSession.DcID}>Alt DC disconnected: {ex.Message}"); Helpers.Log(3, $"{_dcSession.DcID}>Alt DC disconnected: {ex.Message}");
else else
Helpers.Log(5, $"{_dcSession.DcID}>An exception occured in the reactor: {ex}"); Helpers.Log(5, $"{_dcSession.DcID}>An exception occured in the reactor: {ex}");
var oldSemaphore = _sendSemaphore; var oldSemaphore = _sendSemaphore;
await oldSemaphore.WaitAsync(cts.Token); // prevent any sending while we reconnect await oldSemaphore.WaitAsync(ct); // prevent any sending while we reconnect
var reactorError = new ReactorError { Exception = ex }; var reactorError = new ReactorError { Exception = ex };
try try
{ {
@ -889,8 +889,7 @@ namespace WTelegram
if (MTProxyUrl != null) if (MTProxyUrl != null)
{ {
#if OBFUSCATION #if OBFUSCATION
if (TLConfig?.test_mode == true) dcId += 10000; if (TLConfig?.test_mode == true) dcId += dcId < 0 ? -10000 : 10000;
if (_dcSession.DataCenter?.flags.HasFlag(DcOption.Flags.media_only) == true) dcId = -dcId;
var parms = HttpUtility.ParseQueryString(MTProxyUrl[MTProxyUrl.IndexOf('?')..]); var parms = HttpUtility.ParseQueryString(MTProxyUrl[MTProxyUrl.IndexOf('?')..]);
var server = parms["server"]; var server = parms["server"];
int port = int.Parse(parms["port"]); int port = int.Parse(parms["port"]);
@ -988,7 +987,7 @@ namespace WTelegram
#endif #endif
await _networkStream.WriteAsync(preamble, 0, preamble.Length, _cts.Token); await _networkStream.WriteAsync(preamble, 0, preamble.Length, _cts.Token);
_reactorTask = Reactor(_networkStream, _cts); _reactorTask = Reactor(_networkStream, _cts.Token);
} }
_sendSemaphore.Release(); _sendSemaphore.Release();

View file

@ -36,7 +36,7 @@ namespace WTelegram
internal long lastSentMsgId; internal long lastSentMsgId;
internal bool withoutUpdates; internal bool withoutUpdates;
internal Client Client; internal Client Client;
internal int DcID => DataCenter?.id ?? 0; internal int DcID => DataCenter == null ? 0 : DataCenter.flags.HasFlag(TL.DcOption.Flags.media_only) ? -DataCenter.id : DataCenter.id;
internal IPEndPoint EndPoint => DataCenter == null ? null : new(IPAddress.Parse(DataCenter.ip_address), DataCenter.port); internal IPEndPoint EndPoint => DataCenter == null ? null : new(IPAddress.Parse(DataCenter.ip_address), DataCenter.port);
internal void Renew() { Helpers.Log(3, $"Renewing session on DC {DcID}..."); id = Helpers.RandomLong(); seqno = 0; lastSentMsgId = 0; } internal void Renew() { Helpers.Log(3, $"Renewing session on DC {DcID}..."); id = Helpers.RandomLong(); seqno = 0; lastSentMsgId = 0; }
public void DisableUpdates(bool disable = true) { if (withoutUpdates != disable) { withoutUpdates = disable; Renew(); } } public void DisableUpdates(bool disable = true) { if (withoutUpdates != disable) { withoutUpdates = disable; Renew(); } }