Skip to content

Commit

Permalink
Core,TL,Gen: Transport layer rewrite and bugfixes
Browse files Browse the repository at this point in the history
This commit consists of the following changes:
  - Fixes the typo in generator that caused all ComputeFlags()
  functions to be empty.
  - Adds the missing call to ComputeFlags() inside
  the serialize function
  - Uses obfuscated websocket transport instead of TCP
  - Adds asynchronous response handling
  • Loading branch information
aarani committed May 13, 2021
1 parent 0948ab8 commit 92b700e
Show file tree
Hide file tree
Showing 1,152 changed files with 4,040 additions and 1,899 deletions.
54 changes: 35 additions & 19 deletions src/TgSharp.Core/Auth/Authenticator.cs
Original file line number Diff line number Diff line change
@@ -1,45 +1,61 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using TgSharp.Core.Network;

namespace TgSharp.Core.Auth
{
public static class Authenticator
public class Authenticator
{
public static async Task<Step3_Response> DoAuthentication(TcpTransport transport, CancellationToken token = default(CancellationToken))
private readonly MtProtoPlainSender sender;
private TaskCompletionSource<Step3_Response> completionSource;
public Authenticator(WSTransport transport)
{
token.ThrowIfCancellationRequested();
sender = new MtProtoPlainSender(transport);
completionSource = new TaskCompletionSource<Step3_Response>();
}

var sender = new MtProtoPlainSender(transport);
public Task<Step3_Response> DoAuthentication()
{
var step1 = new Step1_PQRequest();

await sender.Send(step1.ToBytes(), token).ConfigureAwait(false);
var step1Response = step1.FromBytes(await sender.Receive(token)
.ConfigureAwait(false));
//race condition ? move randomization in consturctor
sender.OnResponseReceived = (step1Message) => Sender_OnStep1ResponseReceived(step1, step1Message);
sender.Send(step1.ToBytes());

return completionSource.Task;
}

private void Sender_OnStep1ResponseReceived(Step1_PQRequest step1, byte[] step1Message)
{
var step1Response = step1.FromBytes(step1Message);

var step2 = new Step2_DHExchange();
await sender.Send(step2.ToBytes(
sender.OnResponseReceived = (step2message) => Sender_OnStep2ResponseReceived(step2, step2message);
sender.Send(step2.ToBytes(
step1Response.Nonce,
step1Response.ServerNonce,
step1Response.Fingerprints,
step1Response.Pq), token)
.ConfigureAwait(false);
step1Response.Pq));
}

private void Sender_OnStep2ResponseReceived(Step2_DHExchange step2, byte[] step2message)
{
var step2Response = step2.FromBytes(step2message);

var step2Response = step2.FromBytes(await sender.Receive(token)
.ConfigureAwait(false));

var step3 = new Step3_CompleteDHExchange();
await sender.Send(step3.ToBytes(
sender.OnResponseReceived = (step3Message) => Sender_OnStep3ResponseReceived(step3, step3Message);
sender.Send(step3.ToBytes(
step2Response.Nonce,
step2Response.ServerNonce,
step2Response.NewNonce,
step2Response.EncryptedAnswer), token)
.ConfigureAwait(false);

var step3Response = step3.FromBytes(await sender.Receive(token)
.ConfigureAwait(false));
step2Response.EncryptedAnswer));
}

return step3Response;
private void Sender_OnStep3ResponseReceived(Step3_CompleteDHExchange step3, byte[] response)
{
completionSource.SetResult(step3.FromBytes(response));
}
}
}
48 changes: 22 additions & 26 deletions src/TgSharp.Core/Network/MtProtoPlainSender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,38 @@ namespace TgSharp.Core.Network
{
public class MtProtoPlainSender
{
public delegate void ResponseHandler(byte[] response);
public ResponseHandler OnResponseReceived;

private int timeOffset;
private long lastMessageId;
private Random random;
private TcpTransport transport;
private WSTransport transport;

public MtProtoPlainSender(TcpTransport transport)
public MtProtoPlainSender(WSTransport transport)
{
this.transport = transport;
transport.OnUnencryptedMessage += Transport_OnUnencryptedMessage;
random = new Random();
}

public async Task Send(byte[] data, CancellationToken token = default(CancellationToken))
private void Transport_OnUnencryptedMessage(TgMessage message)
{
token.ThrowIfCancellationRequested();
using (MemoryStream memoryStream = new MemoryStream(message.Body))
using (BinaryReader binaryReader = new BinaryReader(memoryStream))
{
long authKeyid = binaryReader.ReadInt64();
long messageId = binaryReader.ReadInt64();
int messageLength = binaryReader.ReadInt32();

byte[] response = binaryReader.ReadBytes(messageLength);

OnResponseReceived?.Invoke(response);
}
}

public void Send(byte[] data)
{
using (var memoryStream = new MemoryStream())
{
using (var binaryWriter = new BinaryWriter(memoryStream))
Expand All @@ -33,28 +50,7 @@ public MtProtoPlainSender(TcpTransport transport)

byte[] packet = memoryStream.ToArray();

await transport.Send(packet, token).ConfigureAwait(false);
}
}
}

public async Task<byte[]> Receive(CancellationToken token = default(CancellationToken))
{
token.ThrowIfCancellationRequested();

var result = await transport.Receive(token).ConfigureAwait(false);

using (var memoryStream = new MemoryStream(result.Body))
{
using (BinaryReader binaryReader = new BinaryReader(memoryStream))
{
long authKeyid = binaryReader.ReadInt64();
long messageId = binaryReader.ReadInt64();
int messageLength = binaryReader.ReadInt32();

byte[] response = binaryReader.ReadBytes(messageLength);

return response;
transport.Send(packet);
}
}
}
Expand Down
Loading

0 comments on commit 92b700e

Please sign in to comment.