Пример #1
0
        async void HandShakeClientStart(object sender, RoutedEventArgs e)
        {
            var port    = int.Parse(PortTextBox.Text);
            var options = new MiraiHttpSessionOptions("localhost", port, "PinkPill");
            var selfQQ  = long.Parse(SelfIDTextBox.Text);   // 自己
            var otherQQ = long.Parse(TargetIDTextBox.Text); // 对方

            TargetQQ = otherQQ;
            session  = new MiraiHttpSession();

            await session.ConnectAsync(options, selfQQ);

            var superStream  = new DelayedBufferStream((_, e) => { SendMessage(new ImageObfuscator().Obfuscate(e)); });
            var serverStream = new SslStream(superStream, false,
                                             (sender, certificate, chain, errors) =>
            {
                var subject = certificate.Subject;
                AddMessage($"握手完成: 证书名 {subject}");
                return(true);
            }, null);

            stream = serverStream;
            var cert = X509Certificate2.CreateFromPemFile(@"cert.pem");

            cert = new X509Certificate2(cert.Export(X509ContentType.Pkcs12));


            session.FriendMessageEvt += (_, args) =>
            {
                if (args.Sender.Id == otherQQ)
                {
                    var obfs = new ImageObfuscator();
                    var msg  = args.Chain;
                    if (msg.FirstOrDefault(c => c is ImageMessage) is not ImageMessage imageMessage)
                    {
                        return(Task.FromResult(true));
                    }
                    var imageBytes = new HttpClient().GetByteArrayAsync(imageMessage.Url).Result;

                    superStream.SendMessage(obfs.Deobfuscate(new MagickImage(imageBytes, MagickFormat.Png)));
                }

                return(Task.FromResult(true));
            };
            await serverStream.AuthenticateAsClientAsync("", new X509Certificate2Collection(new[] { cert }), SslProtocols.Tls12, false);

            _ = Task.Factory.StartNew(() =>
            {
                while (true)
                {
                    var buffer = new Span <byte>(new byte[1024]);
                    var i      = serverStream.Read(buffer);
                    if (i > 0)
                    {
                        AddMessage($"接收到了: {Encoding.UTF8.GetString(buffer.Slice(0, i))}");
                    }
                }
            }, TaskCreationOptions.LongRunning);
        }
Пример #2
0
        static async Task ModeClient()
        {
            var options = new MiraiHttpSessionOptions("localhost", 8080, "INITKEYc15Hx0xs");
            var selfQQ  = 456;
            var otherQQ = 123;

            TargetQQ = otherQQ;
            Console.WriteLine("Act as B");
            await session.ConnectAsync(options, selfQQ);

            /*
             * var ramdom = new Random();
             * var bytes = new byte[1000000];
             * ramdom.NextBytes(bytes);
             * SendMessage(new ImageObfuscator().Obfuscate(bytes));
             */
            var superStream  = new DelayedBufferStream((_, e) => { SendMessage(new ImageObfuscator().Obfuscate(e)); });
            var serverStream = new SslStream(superStream, false,
                                             (sender, certificate, chain, errors) =>
            {
                var subject = certificate.Subject;
                Console.WriteLine($"Cert subject: {subject}");
                return(true);
            }, null);
            var cert = X509Certificate2.CreateFromEncryptedPemFile(@"C:\test\cert2.pem", "1234", @"C:\test\key2.pem");

            cert = new X509Certificate2(cert.Export(X509ContentType.Pkcs12));


            session.FriendMessageEvt += (_, args) =>
            {
                if (args.Sender.Id == otherQQ)
                {
                    var obfs = new ImageObfuscator();
                    var msg  = args.Chain;
                    if (msg.FirstOrDefault(c => c is ImageMessage) is not ImageMessage imageMessage)
                    {
                        return(Task.FromResult(true));
                    }
                    var imageBytes = new HttpClient().GetByteArrayAsync(imageMessage.Url).Result;

                    superStream.SendMessage(obfs.Deobfuscate(new MagickImage(imageBytes, MagickFormat.Png)));
                }

                return(Task.FromResult(true));
            };

            /*
             * session.FriendMessageEvt += (sender, args) =>
             * {
             *  if (args.Sender.Id == otherQQ)
             *  {
             *      var obfs = new Base64Obfuscator();
             *
             *      superStream.pipeServer.Write(obfs.Deobfuscate(args.Chain.GetPlain()));
             *  }
             *
             *  return Task.FromResult(true);
             * };*/
            serverStream.AuthenticateAsClient("", new X509Certificate2Collection(new[] { cert }), SslProtocols.Tls12, false);

            _ = Task.Factory.StartNew(() =>
            {
                while (true)
                {
                    var buffer = new Span <byte>(new byte[1024]);
                    var i      = serverStream.Read(buffer);
                    if (i > 0)
                    {
                        Console.WriteLine(Encoding.UTF8.GetString(buffer.Slice(0, i)));
                    }
                }
            }, TaskCreationOptions.LongRunning);

            while (true)
            {
                var line = Console.ReadLine();
                serverStream.Write(Encoding.UTF8.GetBytes(line));
            }
        }