sochix / TLSharp

Telegram client library implemented in C#
1k stars 380 forks source link

New Project #744

Closed aarani closed 6 years ago

aarani commented 6 years ago

As I already talked to @sochix I'm working on Rebranded Copy of TLSharp HERE: Network Part is Working and Ready but there is lots of things to rewrite so I invite all contributors of TLSharp to work on this new version

P.S 1 : Network part is capable of handling updates and all responses will now deliver using callbacks that's why we need to rewrite things P.S 2 : MTProto 2.0 Support implemented !

aarani commented 6 years ago

@knocte @MasihAkbari @Grepsy

Marjani commented 6 years ago

https://github.com/OpenTl/OpenTl.ClientApi

aarani commented 6 years ago

I couldn’t see your point here but I don’t care if any other similar Library(ies) exists and it will not stop me (at least) from developing TLSharp

P.S : thanks for letting me know about that project btw

Marjani commented 6 years ago

I did not intend to dissuade you Probably able to help If I can, I help in development

knocte commented 6 years ago

P.S 1 : Network part is capable of handling updates

What do you mean with "handling updates". You mean receiving messages without polling?

aarani commented 6 years ago

@knocte It's working with separate thread always listening on socket and if any packet arrive it will call appropriate callback for it

aarani commented 6 years ago

but i will consider using DotNetty soon

aarani commented 6 years ago

Sample Code :


using System.IO;
using System.Linq;
using System.Net;
using System.Net.Security;
using System.Numerics;
using System.Runtime.InteropServices;
using System.Runtime.Remoting.Channels;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Threading.Tasks;
using TeleDotNet.MTProto;
using TeleDotNet.MTProto.Crypto;
using TeleDotNet.MTProto.TCP;
using TeleDotNet.TL;
using TeleDotNet.TL.ML;
using BigInteger = System.Numerics.BigInteger;
using FakeSessionStore = TeleDotNet.MTProto.FakeSessionStore;

namespace Debug
{
    internal class Program
    {

        static void Main()
        {
            byte[] newNonce = new byte[32]; 
            TLContext.Init();
            TcpTransport tcp = new TcpTransport("149.154.175.100",443);
            MtProtoSender sender = new MtProtoSender(tcp,new Session(new FakeSessionStore()));
            MLRequestReqPQ requestReqPqMulti =new MLRequestReqPQ();

            sender.AddCallback(new MLResPq().Constructor, incomingObject =>
                {
                    new Random().NextBytes(newNonce);
                    var pqPair = Factorizator.Factorize(new TeleDotNet.MTProto.Crypto.BigInteger(((MLResPq)incomingObject).Pq));
                    var reqDHPrimes = new MLRequestReqDHParams()
                    {
                        P =  pqPair.Min.ToByteArray(),
                        Q = pqPair.Max.ToByteArray(),
                        Nonce = ((MLResPq)incomingObject).Nonce,
                        ServerNonce = ((MLResPq)incomingObject).ServerNonce,
                        PublicKeyFingerprint = ((MLResPq)incomingObject).ServerPublicKeyFingerprints[0]
                    };
                    var innerData = new MLPQInnerData()
                    {
                        PQ = ((MLResPq)incomingObject).Pq,
                        P =  pqPair.Min.ToByteArray(),
                        Q = pqPair.Max.ToByteArray(),
                        Nonce = ((MLResPq)incomingObject).Nonce,
                        ServerNonce = ((MLResPq)incomingObject).ServerNonce,
                        NewNonce = new System.Numerics.BigInteger(newNonce)
                    };
                    byte[] innerDataBytes; 
                    using (var memory = new MemoryStream())
                    using (var writer = new BinaryWriter(memory))
                    {
                        innerData.SerializeBody(writer);
                        innerDataBytes = memory.ToArray();
                    }

                    var ciphertext = TeleDotNet.MTProto.Crypto.RSA.Encrypt(BitConverter.ToString(BitConverter.GetBytes(reqDHPrimes.PublicKeyFingerprint)).Replace("-", string.Empty),
                        innerDataBytes.ToArray(), 0, innerDataBytes.Count());

                    reqDHPrimes.EncryptedData = ciphertext;
                    sender.Send(reqDHPrimes);
                });
            sender.AddCallback(new MLServerDhParamsOk().Constructor,(incomingObject =>
            {
                var obj = ((MLServerDhParamsOk) incomingObject);
                AESKeyData key = AES.GenerateKeyDataFromNonces(obj.ServerNonce.ToByteArray(), newNonce);
                byte[] plaintextAnswer = AES.DecryptAES(key, obj.EncryptedAnswer);
                MLServerDhInnerData dhInnerDataobj;
                using (MemoryStream dhInnerData = new MemoryStream(plaintextAnswer))
                {
                    using (BinaryReader dhInnerDataReader = new BinaryReader(dhInnerData))
                    {
                        byte[] hashsum = dhInnerDataReader.ReadBytes(20);
                        dhInnerDataobj = (MLServerDhInnerData) ObjectUtils.DeserializeObject(dhInnerDataReader);

                    }
                }
                byte[] b = new byte[256];
                new Random().NextBytes(b);
                BigInteger bb = new BigInteger(b);
                BigInteger gb = BigInteger.ModPow(new BigInteger(dhInnerDataobj.G),bb,new BigInteger(dhInnerDataobj.DhPrime));
                var gab = BigInteger.ModPow(new BigInteger(BigInteger.ModPow),bb, dhInnerDataobj.DhPrime);
            }));
            byte[] nonce = new byte[16];
            new Random().NextBytes(nonce);
            requestReqPqMulti.Nonce = new System.Numerics.BigInteger(nonce);
            sender.Send(requestReqPqMulti);

        }

    }
}```
sochix commented 6 years ago

@aarani please, do not advertie your project here,