示例#1
0
文件: Program.cs 项目: itfenom/i2p-cs
        static void MyDestination_DataReceived(ClientDestination dest, BufLen data)
        {
            Logging.LogInformation($"Program {PublishedDestination}: data received {data:15}");

            var reader = new BufRefLen(data);
            var unzip  = LZUtils.BCGZipDecompressNew((BufLen)reader);
            var packet = new StreamingPacket((BufRefLen)unzip);

            Logging.LogInformation($"Program {PublishedDestination}: {packet} {packet.Payload}");

            PublishedDestination.LookupDestination(packet?.From.IdentHash, (hash, ls, tag) =>
            {
                if (ls is null)
                {
                    Logging.LogInformation($"Program {PublishedDestination}: Failed to lookup {hash?.Id32Short}.");
                    return;
                }

                var s  = new BufRefStream();
                var sh = new StreamingPacket(
                    PacketFlags.SYNCHRONIZE
                    | PacketFlags.FROM_INCLUDED
                    | PacketFlags.SIGNATURE_INCLUDED
                    | PacketFlags.MAX_PACKET_SIZE_INCLUDED
                    | PacketFlags.NO_ACK)
                {
                    From            = PublishedDestination.Destination,
                    SigningKey      = MyDestinationInfo.PrivateSigningKey,
                    ReceiveStreamId = packet.ReceiveStreamId,
                    SendStreamId    = SendId,
                    NACKs           = new List <uint>(),
                    Payload         = new BufLen(BufUtils.RandomBytes(30)),
                };

                sh.Write(s);
                var buf    = s.ToByteArray();
                var zipped = LZUtils.BCGZipCompressNew(new BufLen(buf));
                zipped.PokeFlip16(4353, 4);                // source port
                zipped.PokeFlip16(25, 6);                  // dest port
                zipped[9] = (byte)PayloadFormat.Streaming; // streaming

                Logging.LogInformation($"Program {PublishedDestination}: Sending {zipped:20}.");
                PublishedDestination.Send(ls.Destination, zipped);
            });
        }
示例#2
0
        public DatabaseStoreMessage(
            I2PRouterInfo info,
            uint replytoken,
            I2PIdentHash replygw,
            I2PTunnelId replytunnelid)
        {
            BufLen msb;

#if USE_BC_GZIP
            msb = LZUtils.BCGZipCompressNew(new BufLen(info.ToByteArray()));
#else
            using (var ms = new MemoryStream())
            {
                var buf = info.ToByteArray();

                using (var gzs = new GZipStream(ms, CompressionMode.Compress))
                {
                    gzs.Write(buf, 0, buf.Length);
                    gzs.Flush();
                }

                msb = new BufLen(ms.ToArray());
            }
#endif

            var len = 32 + 1 + 4 + 2 + msb.Length + (replytoken != 0 ? 4 + 32 : 0);
            AllocateBuffer(len);
            var writer = new BufRefLen(Payload);

            writer.Write(info.Identity.IdentHash.Hash);
            writer.Write8((byte)MessageContent.RouterInfo);
            writer.Write(BitConverter.GetBytes(replytoken));
            if (replytoken != 0)
            {
                writer.Write(BitConverter.GetBytes(replytunnelid));
                if (replygw == null || replygw.Hash.Length != 32)
                {
                    throw new FormatException("ReplyGateway has to be 32 bytes long!");
                }
                writer.Write(replygw.Hash);
            }

            writer.WriteFlip16((ushort)msb.Length);
            writer.Write(msb);
        }
示例#3
0
        public void TestGZip()
        {
            var smalldata = BufUtils.RandomBytes(200);
            var bigdata   = BufUtils.RandomBytes(2 * 1024 * 1024);

            var smalldata_zero = new byte[200];
            var bigdata_zero   = new byte[2 * 1024 * 1024];

            var b1 = LZUtils.BCGZipCompressNew(new BufLen(smalldata));
            var b2 = LZUtils.BCGZipDecompressNew(b1);

            Assert.IsTrue(b2 == new BufLen(smalldata));

            b1 = LZUtils.BCGZipCompressNew(new BufLen(bigdata));
            b2 = LZUtils.BCGZipDecompressNew(b1);
            Assert.IsTrue(b2 == new BufLen(bigdata));

            b1 = LZUtils.BCGZipCompressNew(new BufLen(smalldata_zero));
            b2 = LZUtils.BCGZipDecompressNew(b1);
            Assert.IsTrue(b2 == new BufLen(smalldata_zero));

            b1 = LZUtils.BCGZipCompressNew(new BufLen(bigdata_zero));
            b2 = LZUtils.BCGZipDecompressNew(b1);
            Assert.IsTrue(b2 == new BufLen(bigdata_zero));

            var ba1 = LZUtils.BCGZipCompress(bigdata);

            b2 = LZUtils.BCGZipDecompressNew(new BufLen(ba1));
            Assert.IsTrue(b2 == new BufLen(bigdata));

            b1 = LZUtils.BCGZipCompressNew(new BufLen(bigdata_zero));
            var ba2 = LZUtils.BCGZipDecompress(b1);

            Assert.IsTrue(new BufLen(ba2) == new BufLen(bigdata_zero));

            for (int i = bigdata.Length / 10; i < bigdata.Length - bigdata.Length / 10; ++i)
            {
                bigdata[i] = 42;
            }
            b1 = LZUtils.BCGZipCompressNew(new BufLen(bigdata));
            b2 = LZUtils.BCGZipDecompressNew(b1);
            Assert.IsTrue(b2 == new BufLen(bigdata));
        }
示例#4
0
文件: Program.cs 项目: itfenom/i2p-cs
        static void Main(string[] args)
        {
            Logging.ReadAppConfig();
            Logging.LogToDebug   = false;
            Logging.LogToConsole = true;

            RouterContext.RouterSettingsFile = "EchoClientRouter.bin";
            RouterContext.Inst = new RouterContext(
                new I2PCertificate(
                    I2PSigningKey.SigningKeyTypes.DSA_SHA1));

            for (int i = 0; i < args.Length; ++i)
            {
                switch (args[i])
                {
                case "--addr":
                case "--address":
                    if (args.Length > i + 1)
                    {
                        RouterContext.Inst.DefaultExtAddress = IPAddress.Parse(args[++i]);
                        Console.WriteLine($"addr {RouterContext.Inst.DefaultExtAddress}");
                    }
                    else
                    {
                        Console.WriteLine("--addr require ip number");
                        return;
                    }
                    break;

                case "--port":
                    if (args.Length > i + 1)
                    {
                        var port = int.Parse(args[++i]);
                        RouterContext.Inst.DefaultTCPPort = port;
                        RouterContext.Inst.DefaultUDPPort = port;
                        Console.WriteLine($"port {port}");
                    }
                    else
                    {
                        Console.WriteLine("--port require port number");
                        return;
                    }
                    break;

                case "--nofw":
                    RouterContext.Inst.IsFirewalled = false;
                    Console.WriteLine($"Firewalled {RouterContext.Inst.IsFirewalled}");
                    break;

                default:
                    Console.WriteLine(args[i]);
                    Console.WriteLine("Usage: I2P.exe --addr 12.34.56.78 --port 8081 --nofw");
                    break;
                }
            }

            RouterContext.Inst.ApplyNewSettings();
            Router.Start();

            var destb32    = AppSettings["RemoteDestination"];
            var remotedest = new I2PIdentHash(destb32);

            MyDestinationInfo      = new I2PDestinationInfo(I2PSigningKey.SigningKeyTypes.EdDSA_SHA512_Ed25519);
            UnpublishedDestination = Router.CreateDestination(MyDestinationInfo, false, out _);
            UnpublishedDestination.DataReceived += MyDestination_DataReceived;
            UnpublishedDestination.Name          = "UnpublishedDestination";

            Logging.LogInformation($"MyDestination: {UnpublishedDestination.Destination.IdentHash} {MyDestinationInfo.Destination.Certificate}");

            var interval = new PeriodicAction(TickSpan.Seconds(40));

            while (true)
            {
                try
                {
                    Connected = true;

                    while (Connected)
                    {
                        Thread.Sleep(2000);

                        uint recvid = BufUtils.RandomUintNZ();

                        interval.Do(() =>
                        {
                            Logging.LogInformation($"Program {UnpublishedDestination}: Looking for {remotedest}.");
                            UnpublishedDestination.LookupDestination(remotedest, (hash, ls, tag) =>
                            {
                                if (ls is null)
                                {
                                    Logging.LogInformation($"Program {UnpublishedDestination}: Failed to lookup {hash.Id32Short}.");
                                    return;
                                }

                                var test = new I2PIdentHash(ls.Destination);
                                Logging.LogInformation($"Program {UnpublishedDestination}: Found {remotedest}, test: {test.Id32Short}.");

                                var s  = new BufRefStream();
                                var sh = new StreamingPacket(
                                    PacketFlags.SYNCHRONIZE
                                    | PacketFlags.FROM_INCLUDED
                                    | PacketFlags.SIGNATURE_INCLUDED
                                    | PacketFlags.MAX_PACKET_SIZE_INCLUDED
                                    | PacketFlags.NO_ACK)
                                {
                                    From            = UnpublishedDestination.Destination,
                                    SigningKey      = MyDestinationInfo.PrivateSigningKey,
                                    ReceiveStreamId = recvid,
                                    NACKs           = new List <uint>(),
                                    Payload         = new BufLen(new byte[0]),
                                };

                                sh.Write(s);
                                var buf    = s.ToByteArray();
                                var zipped = LZUtils.BCGZipCompressNew(new BufLen(buf));
                                zipped.PokeFlip16(4353, 4);                // source port
                                zipped.PokeFlip16(25, 6);                  // dest port
                                zipped[9] = (byte)PayloadFormat.Streaming; // streaming

                                Logging.LogInformation($"Program {UnpublishedDestination}: Sending {zipped:20}.");

                                UnpublishedDestination.Send(ls.Destination, zipped);
                            });
                        });
                    }
                }
                catch (SocketException ex)
                {
                    Logging.Log(ex);
                }
                catch (IOException ex)
                {
                    Logging.Log(ex);
                }
                catch (Exception ex)
                {
                    Logging.Log(ex);
                }
            }
        }