This commit is contained in:
Federico Armellini 2019-01-28 20:37:49 +01:00
parent 558d58a950
commit 0b6c67b5aa
3 changed files with 39 additions and 14 deletions

View file

@ -101,11 +101,26 @@ namespace TLSharp.Core.Network
ulong remoteMessageId; ulong remoteMessageId;
int remoteSequence; int remoteSequence;
/*
if (body.Length<8)
{
List<byte> lb = new List<byte>();
foreach (var vb in body)
lb.Add(vb);
for (int i = lb.Count; i < (8+16); i++)
lb.Add(0);
body = lb.ToArray();
}
*/
using (var inputStream = new MemoryStream(body)) using (var inputStream = new MemoryStream(body))
using (var inputReader = new BinaryReader(inputStream)) using (var inputReader = new BinaryReader(inputStream))
{ {
if (inputReader.BaseStream.Length < 8) if (inputReader.BaseStream.Length < 8)
{
throw new InvalidOperationException($"Can't decode packet"); throw new InvalidOperationException($"Can't decode packet");
}
ulong remoteAuthKeyId = inputReader.ReadUInt64(); // TODO: check auth key id ulong remoteAuthKeyId = inputReader.ReadUInt64(); // TODO: check auth key id
byte[] msgKey = inputReader.ReadBytes(16); // TODO: check msg_key correctness byte[] msgKey = inputReader.ReadBytes(16); // TODO: check msg_key correctness
@ -342,7 +357,7 @@ namespace TLSharp.Core.Network
} }
catch (ZlibException ex) catch (ZlibException ex)
{ {
Console.WriteLine(ex.Message);
} }
} }
else else
@ -512,6 +527,7 @@ namespace TLSharp.Core.Network
{ {
// logger.error("failed to process message in contailer: {0}", e); // logger.error("failed to process message in contailer: {0}", e);
messageReader.BaseStream.Position = beginPosition + innerLength; messageReader.BaseStream.Position = beginPosition + innerLength;
Console.WriteLine(e.Message);
} }
} }

View file

@ -83,14 +83,23 @@ namespace TLSharp.Core
if (dcOptions == null || !dcOptions.Any()) if (dcOptions == null || !dcOptions.Any())
throw new InvalidOperationException($"Can't reconnect. Establish initial connection first."); throw new InvalidOperationException($"Can't reconnect. Establish initial connection first.");
TLDcOption dc = null;
foreach (var d2 in dcOptions)
{
if (d2.Id == dcId && d2.Ipv6 == false)
{
dc = d2;
break;
}
}
TLExportedAuthorization exported = null; TLExportedAuthorization exported = null;
if (_session.TLUser != null) if (_session.TLUser != null)
{ {
TLRequestExportAuthorization exportAuthorization = new TLRequestExportAuthorization() { DcId = dcId }; TLRequestExportAuthorization exportAuthorization = new TLRequestExportAuthorization() { DcId = dcId };
exported = await SendRequestAsync<TLExportedAuthorization>(exportAuthorization,times); exported = await SendRequestAsync<TLExportedAuthorization>(exportAuthorization, times);
} }
var dc = dcOptions.First(d => d.Id == dcId);
_transport = new TcpTransport(dc.IpAddress, dc.Port, _handler); _transport = new TcpTransport(dc.IpAddress, dc.Port, _handler);
_session.ServerAddress = dc.IpAddress; _session.ServerAddress = dc.IpAddress;
@ -124,7 +133,7 @@ namespace TLSharp.Core
} }
catch(DataCenterMigrationException e) catch(DataCenterMigrationException e)
{ {
if (times <= 1) if (times <= 150)
{ {
await ReconnectToDcAsync(e.DC, times + 1); await ReconnectToDcAsync(e.DC, times + 1);
// prepare the request for another try // prepare the request for another try

View file

@ -34,20 +34,20 @@ namespace TLSharp.Core.Utils
Array.Copy(msgKey, 0, buffer, 0, 16); // buffer[0:16] = msgKey Array.Copy(msgKey, 0, buffer, 0, 16); // buffer[0:16] = msgKey
Array.Copy(sharedKey, x, buffer, 16, 32); // buffer[16:48] = authKey[x:x+32] Array.Copy(sharedKey, x, buffer, 16, 32); // buffer[16:48] = authKey[x:x+32]
byte[] sha1a = sha1(buffer); // sha1a = sha1(buffer) byte[] sha1a = Sha1(buffer); // sha1a = sha1(buffer)
Array.Copy(sharedKey, 32 + x, buffer, 0, 16); // buffer[0:16] = authKey[x+32:x+48] Array.Copy(sharedKey, 32 + x, buffer, 0, 16); // buffer[0:16] = authKey[x+32:x+48]
Array.Copy(msgKey, 0, buffer, 16, 16); // buffer[16:32] = msgKey Array.Copy(msgKey, 0, buffer, 16, 16); // buffer[16:32] = msgKey
Array.Copy(sharedKey, 48 + x, buffer, 32, 16); // buffer[32:48] = authKey[x+48:x+64] Array.Copy(sharedKey, 48 + x, buffer, 32, 16); // buffer[32:48] = authKey[x+48:x+64]
byte[] sha1b = sha1(buffer); // sha1b = sha1(buffer) byte[] sha1b = Sha1(buffer); // sha1b = sha1(buffer)
Array.Copy(sharedKey, 64 + x, buffer, 0, 32); // buffer[0:32] = authKey[x+64:x+96] Array.Copy(sharedKey, 64 + x, buffer, 0, 32); // buffer[0:32] = authKey[x+64:x+96]
Array.Copy(msgKey, 0, buffer, 32, 16); // buffer[32:48] = msgKey Array.Copy(msgKey, 0, buffer, 32, 16); // buffer[32:48] = msgKey
byte[] sha1c = sha1(buffer); // sha1c = sha1(buffer) byte[] sha1c = Sha1(buffer); // sha1c = sha1(buffer)
Array.Copy(msgKey, 0, buffer, 0, 16); // buffer[0:16] = msgKey Array.Copy(msgKey, 0, buffer, 0, 16); // buffer[0:16] = msgKey
Array.Copy(sharedKey, 96 + x, buffer, 16, 32); // buffer[16:48] = authKey[x+96:x+128] Array.Copy(sharedKey, 96 + x, buffer, 16, 32); // buffer[16:48] = authKey[x+96:x+128]
byte[] sha1d = sha1(buffer); // sha1d = sha1(buffer) byte[] sha1d = Sha1(buffer); // sha1d = sha1(buffer)
byte[] key = new byte[32]; // key = sha1a[0:8] + sha1b[8:20] + sha1c[4:16] byte[] key = new byte[32]; // key = sha1a[0:8] + sha1b[8:20] + sha1c[4:16]
Array.Copy(sha1a, 0, key, 0, 8); Array.Copy(sha1a, 0, key, 0, 8);
@ -66,18 +66,18 @@ namespace TLSharp.Core.Utils
public static byte[] CalcMsgKey(byte[] data) public static byte[] CalcMsgKey(byte[] data)
{ {
byte[] msgKey = new byte[16]; byte[] msgKey = new byte[16];
Array.Copy(sha1(data), 4, msgKey, 0, 16); Array.Copy(Sha1(data), 4, msgKey, 0, 16);
return msgKey; return msgKey;
} }
public static byte[] CalcMsgKey(byte[] data, int offset, int limit) public static byte[] CalcMsgKey(byte[] data, int offset, int limit)
{ {
byte[] msgKey = new byte[16]; byte[] msgKey = new byte[16];
Array.Copy(sha1(data, offset, limit), 4, msgKey, 0, 16); Array.Copy(Sha1(data, offset, limit), 4, msgKey, 0, 16);
return msgKey; return msgKey;
} }
public static byte[] sha1(byte[] data) public static byte[] Sha1(byte[] data)
{ {
using (SHA1 sha1 = new SHA1Managed()) using (SHA1 sha1 = new SHA1Managed())
{ {
@ -85,7 +85,7 @@ namespace TLSharp.Core.Utils
} }
} }
public static byte[] sha1(byte[] data, int offset, int limit) public static byte[] Sha1(byte[] data, int offset, int limit)
{ {
using (SHA1 sha1 = new SHA1Managed()) using (SHA1 sha1 = new SHA1Managed())
{ {