예제 #1
0
파일: Program.cs 프로젝트: itfenom/i2p-cs
        static void LookupResult(I2PIdentHash hash, I2PLeaseSet ls, object o)
        {
            Logging.LogInformation($"Program {MyOrigin}: LookupResult {hash.Id32Short} {ls}");

            if (ls is null)
            {
                // Try again
                MyOrigin.LookupDestination(PublishedDestination.Destination.IdentHash, LookupResult);
                return;
            }

            LookedUpDestination = ls.Destination;
        }
예제 #2
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);
            });
        }
예제 #3
0
파일: Program.cs 프로젝트: itfenom/i2p-cs
        static void Main(string[] args)
        {
            PeriodicAction SendInterval = new PeriodicAction(TickSpan.Seconds(20));

            Logging.ReadAppConfig();
            Logging.LogToDebug   = false;
            Logging.LogToConsole = true;

            RouterContext.RouterSettingsFile = "I2PDemo.bin";

            MyDestinationInfo = new I2PDestinationInfo(I2PSigningKey.SigningKeyTypes.EdDSA_SHA512_Ed25519);

            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;

                case "--mkdest":
                case "--create-destination":
                    var certtype = 0;
                    if (args.Length > i + 1)
                    {
                        certtype = int.Parse(args[++i]);
                    }

                    I2PSigningKey.SigningKeyTypes ct;
                    I2PDestinationInfo            d;

                    switch (certtype)
                    {
                    default:
                    case 0:
                        ct = I2PSigningKey.SigningKeyTypes.EdDSA_SHA512_Ed25519;
                        d  = new I2PDestinationInfo(ct);
                        break;

                    case 1:
                        ct = I2PSigningKey.SigningKeyTypes.DSA_SHA1;
                        d  = new I2PDestinationInfo(ct);
                        break;

                    case 2:
                        ct = I2PSigningKey.SigningKeyTypes.ECDSA_SHA256_P256;
                        d  = new I2PDestinationInfo(ct);
                        break;

                    case 3:
                        ct = I2PSigningKey.SigningKeyTypes.ECDSA_SHA384_P384;
                        d  = new I2PDestinationInfo(ct);
                        break;
                    }

                    Console.WriteLine($"New destination {ct}: {d.ToBase64()}");
                    return;

                case "--destination":
                    if (args.Length > i + 1)
                    {
                        MyDestinationInfo = new I2PDestinationInfo(args[++i]);
                        Console.WriteLine($"Destination {MyDestinationInfo}");
                    }
                    else
                    {
                        Console.WriteLine("Base64 encoded Destination required");
                        return;
                    }
                    break;

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

            RouterContext.Inst.ApplyNewSettings();

            var pnp = new UPnp();

            Thread.Sleep(5000);   // Give UPnp a chance

            Router.Start();

            // Create new identities for this run

            MyDestination = MyDestinationInfo.Destination;

#if MANUAL_SIGN
            PublishedDestination = Router.CreateDestination(
                MyDestination,
                MyDestinationInfo.PrivateKey,
                true,
                out _);      // Publish our destinaiton
            PublishedDestination.SignLeasesRequest += MyDestination_SignLeasesRequest;
#else
            PublishedDestination = Router.CreateDestination(MyDestinationInfo, true, out _);   // Publish our destinaiton
#endif
            PublishedDestination.DataReceived += MyDestination_DataReceived;
            PublishedDestination.Name          = "PublishedDestination";

            MyOriginInfo = new I2PDestinationInfo(I2PSigningKey.SigningKeyTypes.DSA_SHA1);
            MyOrigin     = Router.CreateDestination(MyOriginInfo, false, out _);
            MyOrigin.ClientStateChanged += MyOrigin_ClientStateChanged;
            MyOrigin.DataReceived       += MyOrigin_DataReceived;
            MyOrigin.Name = "MyOrigin";

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

            while (true)
            {
                try
                {
                    Connected = true;

                    MyOrigin.LookupDestination(PublishedDestination.Destination.IdentHash, LookupResult);

                    var sendevents = 0;

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

                        if (LookedUpDestination != null &&
                            MyOrigin.ClientState == ClientDestination.ClientStates.Established)
                        {
                            SendInterval.Do(() =>
                            {
                                if (sendevents++ < 10)
                                {
                                    // Send some data to the MyDestination
                                    DataSent = new BufLen(
                                        BufUtils.RandomBytes(
                                            (int)(1 + BufUtils.RandomDouble(25) * 1024)));

                                    var ok = MyOrigin.Send(LookedUpDestination, DataSent);
                                    Logging.LogInformation($"Program {MyOrigin}: Send[{sendevents}] {ok}, {DataSent:15}");
                                }

                                if (sendevents > 100)
                                {
                                    sendevents = 0;
                                }
                            });
                        }
                    }
                }
                catch (SocketException ex)
                {
                    Logging.Log(ex);
                }
                catch (IOException ex)
                {
                    Logging.Log(ex);
                }
                catch (Exception ex)
                {
                    Logging.Log(ex);
                }
            }
        }
예제 #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);
                }
            }
        }