forked from sochix/TLSharp
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Core,TL,Gen: Transport layer rewrite
- Upgrade .NET to 4.7.2 to use .NET Standard dependency Reason: https://docs.microsoft.com/en-us/dotnet/standard/net-standard - Uses obfuscated WebSocket transport instead of TCP - Adds asynchronous response handling
- Loading branch information
Showing
357 changed files
with
1,769 additions
and
1,671 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,60 @@ | ||
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)); | ||
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)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
using System; | ||
using System.IO; | ||
using System.Linq; | ||
using TgSharp.Core.MTProto.Crypto; | ||
|
||
namespace TgSharp.Core.Network | ||
{ | ||
public class Message | ||
{ | ||
public byte[] Body { get; private set; } | ||
|
||
public Message(byte[] body) | ||
{ | ||
if (body == null) | ||
throw new ArgumentNullException(nameof(body)); | ||
|
||
Body = body; | ||
} | ||
|
||
public static Message Decode(byte[] body) | ||
{ | ||
using (var memoryStream = new MemoryStream(body)) | ||
{ | ||
using (var binaryReader = new BinaryReader(memoryStream)) | ||
{ | ||
int length = binaryReader.ReadInt32(); | ||
return new Message(binaryReader.ReadBytes(length)); | ||
} | ||
} | ||
} | ||
|
||
public byte[] Encode() | ||
{ | ||
using (var memoryStream = new MemoryStream()) | ||
{ | ||
using (var binaryWriter = new BinaryWriter(memoryStream)) | ||
{ | ||
binaryWriter.Write(Body.Length); | ||
binaryWriter.Write(Body); | ||
return memoryStream.ToArray(); | ||
} | ||
} | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.