mirror of
https://github.com/sochix/TLSharp.git
synced 2025-12-06 08:02:00 +01:00
mereg from other branch
This commit is contained in:
parent
d86d7609e0
commit
7bca8302a3
|
|
@ -19,10 +19,15 @@ namespace ClientConsoleApp
|
||||||
|
|
||||||
static void Main(string[] args)
|
static void Main(string[] args)
|
||||||
{
|
{
|
||||||
System.Threading.Thread.Sleep(2000);
|
Thread.Sleep(2000);
|
||||||
Console.WriteLine("Hello World!");
|
Console.WriteLine("Hello World!");
|
||||||
|
|
||||||
var client = GetTlgClient().Result;
|
//var tcpClient = new TcpClient();
|
||||||
|
//tcpClient.Connect(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 5000));
|
||||||
|
//TestTcpClient(tcpClient);
|
||||||
|
//TestTcpClient(tcpClient);
|
||||||
|
|
||||||
|
TelegramClient client = GetTlgClient().Result;
|
||||||
var normalizedNumber = NumberToSendMessage.StartsWith("+") ?
|
var normalizedNumber = NumberToSendMessage.StartsWith("+") ?
|
||||||
NumberToSendMessage.Substring(1, NumberToSendMessage.Length - 1) :
|
NumberToSendMessage.Substring(1, NumberToSendMessage.Length - 1) :
|
||||||
NumberToSendMessage;
|
NumberToSendMessage;
|
||||||
|
|
@ -86,10 +91,8 @@ namespace ClientConsoleApp
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void TestTcpClient()
|
private static void TestTcpClient(TcpClient tcpClient)
|
||||||
{
|
{
|
||||||
var tcpClient = new TcpClient();
|
|
||||||
tcpClient.Connect(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 5000));
|
|
||||||
if (tcpClient.Connected)
|
if (tcpClient.Connected)
|
||||||
{
|
{
|
||||||
using (var memoryStream = new MemoryStream())
|
using (var memoryStream = new MemoryStream())
|
||||||
|
|
@ -108,8 +111,7 @@ namespace ClientConsoleApp
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
System.Threading.Thread.Sleep(20000);
|
Thread.Sleep(5000);
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -9,6 +9,7 @@ using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Ionic.Crc;
|
using Ionic.Crc;
|
||||||
using TLSharp.Core.Auth;
|
using TLSharp.Core.Auth;
|
||||||
|
using TLSharp.Core.MTProto;
|
||||||
using TLSharp.Core.MTProto.Crypto;
|
using TLSharp.Core.MTProto.Crypto;
|
||||||
using TLSharp.Core.Network;
|
using TLSharp.Core.Network;
|
||||||
using TLSharp.Core.Utils;
|
using TLSharp.Core.Utils;
|
||||||
|
|
@ -59,16 +60,141 @@ namespace TlgListenerApplication
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static void ProcessRequest(TcpListener tcpListener)
|
||||||
private static void ProcessRequestBySocket(TcpListener tcpListener)
|
|
||||||
{
|
{
|
||||||
Console.WriteLine("Processing socket...");
|
Console.WriteLine("Processing...");
|
||||||
var socketClient = tcpListener.AcceptSocket();
|
var tcpClient = tcpListener.AcceptTcpClient();
|
||||||
|
var netStream = tcpClient.GetStream();
|
||||||
|
|
||||||
|
//var getingCounter = 0;
|
||||||
|
//while (true)
|
||||||
|
//{
|
||||||
|
// if (!netStream.DataAvailable)
|
||||||
|
// continue;
|
||||||
|
// Console.WriteLine("Get data " + ++getingCounter);
|
||||||
|
//}
|
||||||
|
|
||||||
|
while (tcpClient.Connected)
|
||||||
|
{
|
||||||
|
System.Threading.Thread.Sleep(100);
|
||||||
|
if (!netStream.DataAvailable) continue;
|
||||||
|
|
||||||
|
byte[] nonceFromClient = new byte[16];
|
||||||
|
uint responseCode = 0;
|
||||||
|
const uint step1Constructor = 0x60469778;
|
||||||
|
const uint step2Constructor = 0xd712e4be;
|
||||||
|
|
||||||
|
if (netStream.CanRead)
|
||||||
|
{
|
||||||
|
var bytes = new byte[tcpClient.ReceiveBufferSize];
|
||||||
|
netStream.Read(bytes, 0, (int)tcpClient.ReceiveBufferSize);
|
||||||
|
var tcpMessage = TcpMessage.Decode(bytes);
|
||||||
|
var binaryReader = new BinaryReader(new MemoryStream(tcpMessage.Body, false));
|
||||||
|
var a = binaryReader.ReadInt64();
|
||||||
|
var msgId = binaryReader.ReadInt64();
|
||||||
|
var datalength = binaryReader.ReadInt32();
|
||||||
|
var data = binaryReader.ReadBytes(datalength);
|
||||||
|
|
||||||
|
var binaryReader2 = new BinaryReader(new MemoryStream(data, false));
|
||||||
|
|
||||||
|
responseCode = binaryReader2.ReadUInt32();
|
||||||
|
Console.WriteLine("Request code: " + responseCode);
|
||||||
|
if (responseCode == step1Constructor)//---Step1_PQRequest
|
||||||
|
{
|
||||||
|
nonceFromClient = binaryReader2.ReadBytes(16);
|
||||||
|
}
|
||||||
|
else if (responseCode == step2Constructor)//---Step1_PQRequest
|
||||||
|
{
|
||||||
|
nonceFromClient = binaryReader2.ReadBytes(16);
|
||||||
|
}
|
||||||
|
|
||||||
|
//var obj = new Step1_PQRequest().FromBytes(data);
|
||||||
|
//var rr = FromByteArray<Step1_PQRequest>(data);
|
||||||
|
|
||||||
|
//var binaryReader = new BinaryReader(netStream);
|
||||||
|
//var a = binaryReader.ReadInt64();
|
||||||
|
//var b = binaryReader.ReadInt32();
|
||||||
|
//var c = binaryReader.ReadInt32();
|
||||||
|
//var d = binaryReader.ReadInt32();
|
||||||
|
|
||||||
|
//Console.WriteLine("This is what the host returned to you: " + returndata);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (netStream.CanWrite)
|
||||||
|
{
|
||||||
|
|
||||||
|
var fingerprint = StringToByteArray("216be86c022bb4c3");
|
||||||
|
|
||||||
|
byte[] outputdata = null;
|
||||||
|
if (responseCode == step1Constructor)
|
||||||
|
{
|
||||||
|
var nonce = new byte[16];
|
||||||
|
new Random().NextBytes(nonce);
|
||||||
|
outputdata = new Step1_Response()
|
||||||
|
{
|
||||||
|
Pq = new BigInteger(1, BitConverter.GetBytes(880)),
|
||||||
|
ServerNonce = nonceFromClient,
|
||||||
|
Nonce = nonce,
|
||||||
|
Fingerprints = new List<byte[]>() { fingerprint }
|
||||||
|
}.ToBytes();
|
||||||
|
}
|
||||||
|
else if (responseCode == step2Constructor)
|
||||||
|
{
|
||||||
|
var newnonce = new byte[32];
|
||||||
|
new Random().NextBytes(newnonce);
|
||||||
|
|
||||||
|
byte[] answer;
|
||||||
|
var hashsum = Encoding.UTF8.GetBytes("asdfghjklmnbvcxzasdf");
|
||||||
|
const uint innerCode = 0xb5890dba;
|
||||||
|
AESKeyData key = AES.GenerateKeyDataFromNonces(nonceFromClient, newnonce);
|
||||||
|
using (var memoryStream = new MemoryStream())
|
||||||
|
{
|
||||||
|
using (var binaryWriter = new BinaryWriter(memoryStream))
|
||||||
|
{
|
||||||
|
binaryWriter.Write(hashsum);
|
||||||
|
binaryWriter.Write(innerCode);
|
||||||
|
binaryWriter.Write(nonceFromClient);
|
||||||
|
binaryWriter.Write(newnonce);
|
||||||
|
binaryWriter.Write(123456789);
|
||||||
|
Serializers.Bytes.write(binaryWriter, new BigInteger(1, BitConverter.GetBytes(777)).ToByteArrayUnsigned());
|
||||||
|
Serializers.Bytes.write(binaryWriter, new BigInteger(1, BitConverter.GetBytes(888)).ToByteArrayUnsigned());
|
||||||
|
answer = memoryStream.ToArray();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
outputdata = new Step2_Response()
|
||||||
|
{
|
||||||
|
ServerNonce = nonceFromClient,
|
||||||
|
Nonce = newnonce,
|
||||||
|
NewNonce = newnonce,
|
||||||
|
EncryptedAnswer = AES.EncryptAES(key, answer)
|
||||||
|
}.ToBytes();
|
||||||
|
}
|
||||||
|
|
||||||
|
var bytes = PrepareToSend(outputdata);
|
||||||
|
var datatosend = Encode(bytes, 11);
|
||||||
|
netStream.Write(datatosend, 0, datatosend.Length);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Console.WriteLine("You cannot write data to this stream.");
|
||||||
|
tcpClient.Close();
|
||||||
|
netStream.Close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void ProcessRequestSocket(TcpListener tcpListener)
|
||||||
|
{
|
||||||
|
Console.WriteLine("Processing...");
|
||||||
|
var tcpClient = tcpListener.AcceptSocket();
|
||||||
|
|
||||||
|
var bytes = new byte[tcpClient.ReceiveBufferSize];
|
||||||
|
var countbyte = tcpClient.Receive(bytes);
|
||||||
|
|
||||||
|
return;
|
||||||
|
|
||||||
byte[] nonceFromClient = new byte[16];
|
byte[] nonceFromClient = new byte[16];
|
||||||
|
|
||||||
var bytes = new byte[socketClient.ReceiveBufferSize];
|
|
||||||
socketClient.Receive(bytes);
|
|
||||||
var tcpMessage = TcpMessage.Decode(bytes);
|
var tcpMessage = TcpMessage.Decode(bytes);
|
||||||
var binaryReader = new BinaryReader(new MemoryStream(tcpMessage.Body, false));
|
var binaryReader = new BinaryReader(new MemoryStream(tcpMessage.Body, false));
|
||||||
var a = binaryReader.ReadInt64();
|
var a = binaryReader.ReadInt64();
|
||||||
|
|
@ -89,6 +215,7 @@ namespace TlgListenerApplication
|
||||||
new Random().NextBytes(nonce);
|
new Random().NextBytes(nonce);
|
||||||
|
|
||||||
var fingerprint = StringToByteArray("216be86c022bb4c3");
|
var fingerprint = StringToByteArray("216be86c022bb4c3");
|
||||||
|
//var rr = BitConverter.ToString(fingerprint).Replace("-", "");
|
||||||
|
|
||||||
var step1 = new Step1_Response()
|
var step1 = new Step1_Response()
|
||||||
{
|
{
|
||||||
|
|
@ -97,80 +224,12 @@ namespace TlgListenerApplication
|
||||||
Nonce = nonce,
|
Nonce = nonce,
|
||||||
Fingerprints = new List<byte[]>() { fingerprint }
|
Fingerprints = new List<byte[]>() { fingerprint }
|
||||||
};
|
};
|
||||||
var bytestosend = PrepareToSend(step1.ToBytes());
|
var bytes1 = PrepareToSend(step1.ToBytes());
|
||||||
var datatosend = Encode(bytestosend, 11);
|
var datatosend = Encode(bytes1, 11);
|
||||||
socketClient.Send(datatosend);
|
//Byte[] sendBytes = Encoding.UTF8.GetBytes("Is anybody there?");
|
||||||
}
|
tcpClient.Send(datatosend, SocketFlags.Truncated);
|
||||||
|
|
||||||
private static void ProcessRequest(TcpListener tcpListener)
|
//tcpClient.Close();
|
||||||
{
|
|
||||||
Console.WriteLine("Processing...");
|
|
||||||
var tcpClient = tcpListener.AcceptTcpClient();
|
|
||||||
|
|
||||||
var netStream = tcpClient.GetStream();
|
|
||||||
if (netStream.DataAvailable)
|
|
||||||
{
|
|
||||||
byte[] nonceFromClient = new byte[16];
|
|
||||||
if (netStream.CanRead)
|
|
||||||
{
|
|
||||||
var bytes = new byte[tcpClient.ReceiveBufferSize];
|
|
||||||
netStream.Read(bytes, 0, (int)tcpClient.ReceiveBufferSize);
|
|
||||||
var tcpMessage = TcpMessage.Decode(bytes);
|
|
||||||
var binaryReader = new BinaryReader(new MemoryStream(tcpMessage.Body, false));
|
|
||||||
//var a = binaryReader.ReadInt64();
|
|
||||||
var msgId = binaryReader.ReadInt64();
|
|
||||||
var datalength = binaryReader.ReadInt32();
|
|
||||||
var data = binaryReader.ReadBytes(datalength);
|
|
||||||
|
|
||||||
var binaryReader2 = new BinaryReader(new MemoryStream(data, false));
|
|
||||||
const int responseConstructorNumber = 0x60469778;
|
|
||||||
var responseCode = binaryReader2.ReadInt32();
|
|
||||||
Console.WriteLine("Request code: " + responseCode);
|
|
||||||
if (responseCode == responseConstructorNumber)//---Step1_PQRequest
|
|
||||||
{
|
|
||||||
nonceFromClient = binaryReader2.ReadBytes(16);
|
|
||||||
}
|
|
||||||
|
|
||||||
//var obj = new Step1_PQRequest().FromBytes(data);
|
|
||||||
//var rr = FromByteArray<Step1_PQRequest>(data);
|
|
||||||
|
|
||||||
//var binaryReader = new BinaryReader(netStream);
|
|
||||||
//var a = binaryReader.ReadInt64();
|
|
||||||
//var b = binaryReader.ReadInt32();
|
|
||||||
//var c = binaryReader.ReadInt32();
|
|
||||||
//var d = binaryReader.ReadInt32();
|
|
||||||
|
|
||||||
|
|
||||||
//Console.WriteLine("This is what the host returned to you: " + returndata);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (netStream.CanWrite)
|
|
||||||
{
|
|
||||||
var nonce = new byte[16];
|
|
||||||
new Random().NextBytes(nonce);
|
|
||||||
|
|
||||||
var fingerprint = StringToByteArray("216be86c022bb4c3");
|
|
||||||
//var rr = BitConverter.ToString(fingerprint).Replace("-", "");
|
|
||||||
|
|
||||||
var step1 = new Step1_Response()
|
|
||||||
{
|
|
||||||
Pq = new BigInteger(1, BitConverter.GetBytes(880)),
|
|
||||||
ServerNonce = nonceFromClient,
|
|
||||||
Nonce = nonce,
|
|
||||||
Fingerprints = new List<byte[]>() { fingerprint }
|
|
||||||
};
|
|
||||||
var bytes = PrepareToSend(step1.ToBytes());
|
|
||||||
var datatosend = Encode(bytes, 11);
|
|
||||||
//Byte[] sendBytes = Encoding.UTF8.GetBytes("Is anybody there?");
|
|
||||||
netStream.Write(datatosend, 0, datatosend.Length);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
Console.WriteLine("You cannot write data to this stream.");
|
|
||||||
netStream.Close();
|
|
||||||
tcpClient.Close();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static async Task<TcpMessage> Receieve(TcpClient tcpClient)
|
public static async Task<TcpMessage> Receieve(TcpClient tcpClient)
|
||||||
|
|
@ -247,7 +306,7 @@ namespace TlgListenerApplication
|
||||||
long newMessageId = ((time / 1000 + timeOffset) << 32) |
|
long newMessageId = ((time / 1000 + timeOffset) << 32) |
|
||||||
((time % 1000) << 22) |
|
((time % 1000) << 22) |
|
||||||
(random.Next(524288) << 2); // 2^19
|
(random.Next(524288) << 2); // 2^19
|
||||||
// [ unix timestamp : 32 bit] [ milliseconds : 10 bit ] [ buffer space : 1 bit ] [ random : 19 bit ] [ msg_id type : 2 bit ] = [ msg_id : 64 bit ]
|
// [ unix timestamp : 32 bit] [ milliseconds : 10 bit ] [ buffer space : 1 bit ] [ random : 19 bit ] [ msg_id type : 2 bit ] = [ msg_id : 64 bit ]
|
||||||
|
|
||||||
if (lastMessageId >= newMessageId)
|
if (lastMessageId >= newMessageId)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -12,6 +12,25 @@ namespace TLSharp.Core.Auth
|
||||||
public byte[] ServerNonce { get; set; }
|
public byte[] ServerNonce { get; set; }
|
||||||
public byte[] NewNonce { get; set; }
|
public byte[] NewNonce { get; set; }
|
||||||
public byte[] EncryptedAnswer { get; set; }
|
public byte[] EncryptedAnswer { get; set; }
|
||||||
|
|
||||||
|
public byte[] ToBytes()
|
||||||
|
{
|
||||||
|
new Random().NextBytes(Nonce);
|
||||||
|
const uint constructorNumber = 0xd0e8075c;
|
||||||
|
|
||||||
|
using (var memoryStream = new MemoryStream())
|
||||||
|
{
|
||||||
|
using (var binaryWriter = new BinaryWriter(memoryStream))
|
||||||
|
{
|
||||||
|
binaryWriter.Write(constructorNumber);
|
||||||
|
binaryWriter.Write(ServerNonce);
|
||||||
|
binaryWriter.Write(Nonce);
|
||||||
|
binaryWriter.Write(EncryptedAnswer);
|
||||||
|
|
||||||
|
return memoryStream.ToArray();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public class Step2_DHExchange
|
public class Step2_DHExchange
|
||||||
|
|
@ -48,7 +67,7 @@ namespace TLSharp.Core.Auth
|
||||||
foreach (byte[] fingerprint in fingerprints)
|
foreach (byte[] fingerprint in fingerprints)
|
||||||
{
|
{
|
||||||
ciphertext = RSA.Encrypt(BitConverter.ToString(fingerprint).Replace("-", string.Empty),
|
ciphertext = RSA.Encrypt(BitConverter.ToString(fingerprint).Replace("-", string.Empty),
|
||||||
pqInnerData.GetBuffer(), 0, (int)pqInnerData.Position);
|
pqInnerData.GetBuffer(), 0, (int)pqInnerData.Position);
|
||||||
if (ciphertext != null)
|
if (ciphertext != null)
|
||||||
{
|
{
|
||||||
targetFingerprint = fingerprint;
|
targetFingerprint = fingerprint;
|
||||||
|
|
|
||||||
|
|
@ -1,89 +1,91 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Net.Sockets;
|
using System.Net.Sockets;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace TLSharp.Core.Network
|
namespace TLSharp.Core.Network
|
||||||
{
|
{
|
||||||
public delegate TcpClient TcpClientConnectionHandler(string address, int port);
|
public delegate TcpClient TcpClientConnectionHandler(string address, int port);
|
||||||
|
|
||||||
public class TcpTransport : IDisposable
|
public class TcpTransport : IDisposable
|
||||||
{
|
{
|
||||||
private readonly TcpClient _tcpClient;
|
private readonly TcpClient _tcpClient;
|
||||||
private int sendCounter = 0;
|
private int sendCounter = 0;
|
||||||
|
|
||||||
public TcpTransport(string address, int port, TcpClientConnectionHandler handler = null)
|
public TcpTransport(string address, int port, TcpClientConnectionHandler handler = null)
|
||||||
{
|
{
|
||||||
if (handler == null)
|
if (handler == null)
|
||||||
{
|
{
|
||||||
_tcpClient = new TcpClient();
|
_tcpClient = new TcpClient();
|
||||||
|
|
||||||
var ipAddress = IPAddress.Parse(address);
|
_tcpClient.Connect(IPAddress.Parse(address), port);
|
||||||
_tcpClient.Connect(ipAddress, port);
|
}
|
||||||
}
|
else
|
||||||
else
|
_tcpClient = handler(address, port);
|
||||||
_tcpClient = handler(address, port);
|
}
|
||||||
}
|
|
||||||
|
public async Task Send(byte[] packet)
|
||||||
public async Task Send(byte[] packet)
|
{
|
||||||
{
|
if (!_tcpClient.Connected)
|
||||||
if (!_tcpClient.Connected)
|
throw new InvalidOperationException("Client not connected to server.");
|
||||||
throw new InvalidOperationException("Client not connected to server.");
|
|
||||||
|
var tcpMessage = new TcpMessage(sendCounter, packet);
|
||||||
var tcpMessage = new TcpMessage(sendCounter, packet);
|
|
||||||
|
await _tcpClient.GetStream().WriteAsync(tcpMessage.Encode(), 0, tcpMessage.Encode().Length);
|
||||||
await _tcpClient.GetStream().WriteAsync(tcpMessage.Encode(), 0, tcpMessage.Encode().Length);
|
sendCounter++;
|
||||||
sendCounter++;
|
}
|
||||||
}
|
|
||||||
|
public async Task<TcpMessage> Receieve()
|
||||||
public async Task<TcpMessage> Receieve()
|
{
|
||||||
{
|
if (!_tcpClient.Connected)
|
||||||
var stream = _tcpClient.GetStream();
|
throw new InvalidOperationException("Client not connected to server.");
|
||||||
|
|
||||||
var packetLengthBytes = new byte[4];
|
var stream = _tcpClient.GetStream();
|
||||||
if (await stream.ReadAsync(packetLengthBytes, 0, 4) != 4)
|
|
||||||
throw new InvalidOperationException("Couldn't read the packet length");
|
var packetLengthBytes = new byte[4];
|
||||||
int packetLength = BitConverter.ToInt32(packetLengthBytes, 0);
|
if (await stream.ReadAsync(packetLengthBytes, 0, 4) != 4)
|
||||||
|
throw new InvalidOperationException("Couldn't read the packet length");
|
||||||
var seqBytes = new byte[4];
|
int packetLength = BitConverter.ToInt32(packetLengthBytes, 0);
|
||||||
if (await stream.ReadAsync(seqBytes, 0, 4) != 4)
|
|
||||||
throw new InvalidOperationException("Couldn't read the sequence");
|
var seqBytes = new byte[4];
|
||||||
int seq = BitConverter.ToInt32(seqBytes, 0);
|
if (await stream.ReadAsync(seqBytes, 0, 4) != 4)
|
||||||
|
throw new InvalidOperationException("Couldn't read the sequence");
|
||||||
int readBytes = 0;
|
int seq = BitConverter.ToInt32(seqBytes, 0);
|
||||||
var body = new byte[packetLength - 12];
|
|
||||||
int neededToRead = packetLength - 12;
|
int readBytes = 0;
|
||||||
|
var body = new byte[packetLength - 12];
|
||||||
do
|
int neededToRead = packetLength - 12;
|
||||||
{
|
|
||||||
var bodyByte = new byte[packetLength - 12];
|
do
|
||||||
var availableBytes = await stream.ReadAsync(bodyByte, 0, neededToRead);
|
{
|
||||||
neededToRead -= availableBytes;
|
var bodyByte = new byte[packetLength - 12];
|
||||||
Buffer.BlockCopy(bodyByte, 0, body, readBytes, availableBytes);
|
var availableBytes = await stream.ReadAsync(bodyByte, 0, neededToRead);
|
||||||
readBytes += availableBytes;
|
neededToRead -= availableBytes;
|
||||||
}
|
Buffer.BlockCopy(bodyByte, 0, body, readBytes, availableBytes);
|
||||||
while (readBytes != packetLength - 12);
|
readBytes += availableBytes;
|
||||||
|
}
|
||||||
var crcBytes = new byte[4];
|
while (readBytes != packetLength - 12);
|
||||||
if (await stream.ReadAsync(crcBytes, 0, 4) != 4)
|
|
||||||
throw new InvalidOperationException("Couldn't read the crc");
|
var crcBytes = new byte[4];
|
||||||
int checksum = BitConverter.ToInt32(crcBytes, 0);
|
if (await stream.ReadAsync(crcBytes, 0, 4) != 4)
|
||||||
|
throw new InvalidOperationException("Couldn't read the crc");
|
||||||
byte[] rv = new byte[packetLengthBytes.Length + seqBytes.Length + body.Length];
|
int checksum = BitConverter.ToInt32(crcBytes, 0);
|
||||||
|
|
||||||
Buffer.BlockCopy(packetLengthBytes, 0, rv, 0, packetLengthBytes.Length);
|
byte[] rv = new byte[packetLengthBytes.Length + seqBytes.Length + body.Length];
|
||||||
Buffer.BlockCopy(seqBytes, 0, rv, packetLengthBytes.Length, seqBytes.Length);
|
|
||||||
Buffer.BlockCopy(body, 0, rv, packetLengthBytes.Length + seqBytes.Length, body.Length);
|
Buffer.BlockCopy(packetLengthBytes, 0, rv, 0, packetLengthBytes.Length);
|
||||||
var crc32 = new Ionic.Crc.CRC32();
|
Buffer.BlockCopy(seqBytes, 0, rv, packetLengthBytes.Length, seqBytes.Length);
|
||||||
crc32.SlurpBlock(rv, 0, rv.Length);
|
Buffer.BlockCopy(body, 0, rv, packetLengthBytes.Length + seqBytes.Length, body.Length);
|
||||||
var validChecksum = crc32.Crc32Result;
|
var crc32 = new Ionic.Crc.CRC32();
|
||||||
|
crc32.SlurpBlock(rv, 0, rv.Length);
|
||||||
if (checksum != validChecksum)
|
var validChecksum = crc32.Crc32Result;
|
||||||
{
|
|
||||||
throw new InvalidOperationException("invalid checksum! skip");
|
if (checksum != validChecksum)
|
||||||
}
|
{
|
||||||
|
throw new InvalidOperationException("invalid checksum! skip");
|
||||||
return new TcpMessage(seq, body);
|
}
|
||||||
|
|
||||||
|
return new TcpMessage(seq, body);
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool IsConnected
|
public bool IsConnected
|
||||||
|
|
@ -95,10 +97,10 @@ namespace TLSharp.Core.Network
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public void Dispose()
|
public void Dispose()
|
||||||
{
|
{
|
||||||
if (_tcpClient.Connected)
|
if (_tcpClient.Connected)
|
||||||
_tcpClient.Close();
|
_tcpClient.Close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -27,7 +27,7 @@ namespace TLSharp.Core
|
||||||
{
|
{
|
||||||
var sessionFileName = $"{sessionUserId}.dat";
|
var sessionFileName = $"{sessionUserId}.dat";
|
||||||
//if (!File.Exists(sessionFileName))
|
//if (!File.Exists(sessionFileName))
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
using (var stream = new FileStream(sessionFileName, FileMode.Open))
|
using (var stream = new FileStream(sessionFileName, FileMode.Open))
|
||||||
{
|
{
|
||||||
|
|
@ -54,9 +54,9 @@ namespace TLSharp.Core
|
||||||
|
|
||||||
public class Session
|
public class Session
|
||||||
{
|
{
|
||||||
private const string defaultConnectionAddress = "149.154.175.100";//"149.154.167.50";
|
private const string defaultConnectionAddress = "149.154.175.100";//"149.154.167.50";
|
||||||
|
|
||||||
private const int defaultConnectionPort = 443;
|
private const int defaultConnectionPort = 443;
|
||||||
|
|
||||||
public string SessionUserId { get; set; }
|
public string SessionUserId { get; set; }
|
||||||
public string ServerAddress { get; set; }
|
public string ServerAddress { get; set; }
|
||||||
|
|
@ -179,7 +179,7 @@ namespace TLSharp.Core
|
||||||
long newMessageId = ((time / 1000 + TimeOffset) << 32) |
|
long newMessageId = ((time / 1000 + TimeOffset) << 32) |
|
||||||
((time % 1000) << 22) |
|
((time % 1000) << 22) |
|
||||||
(random.Next(524288) << 2); // 2^19
|
(random.Next(524288) << 2); // 2^19
|
||||||
// [ unix timestamp : 32 bit] [ milliseconds : 10 bit ] [ buffer space : 1 bit ] [ random : 19 bit ] [ msg_id type : 2 bit ] = [ msg_id : 64 bit ]
|
// [ unix timestamp : 32 bit] [ milliseconds : 10 bit ] [ buffer space : 1 bit ] [ random : 19 bit ] [ msg_id type : 2 bit ] = [ msg_id : 64 bit ]
|
||||||
|
|
||||||
if (LastMessageId >= newMessageId)
|
if (LastMessageId >= newMessageId)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue