Beispiel #1
0
        public I2CPSession(SessionStatusMessage msg, I2PDestination mydest)
        {
            SessionId = msg.SessionId;

            //MyLeaseInfo = new I2PLeaseInfo( I2PSigningKey.SigningKeyTypes.DSA_SHA1 );
            MyDestination = mydest;
        }
Beispiel #2
0
 public TestSessionKeyOrigin(
     ClientDestination owner,
     I2PDestination mydest,
     I2PDestination remotedest)
     : base(owner, mydest, remotedest)
 {
 }
Beispiel #3
0
        private void SendMessageToDestination(
            I2PDestination dest,
            ushort sessid,
            BufLen payload,
            uint nonce)
        {
            var s3 = Session.SessionIds[sessid];

            var status    = MessageStatatuses.GuaranteedSuccess;
            var sendstaus = s3.MyDestination.Send(dest, payload);

            if (nonce != 0)
            {
                switch (sendstaus)
                {
                case ClientStates.NoTunnels:
                    status = MessageStatatuses.NoLocalTunnels;
                    break;

                case ClientStates.NoLeases:
                    status = MessageStatatuses.NoLeaseset;
                    break;
                }

                Session.Send(new MessageStatusMessage(
                                 s3.SessionId,
                                 s3.MessageId,
                                 status, 0, nonce));
            }
        }
Beispiel #4
0
        internal ClientDestination(ClientTunnelProvider tp, I2PDestinationInfo dest, bool publishdest)
        {
            ClientTunnelMgr    = tp;
            PublishDestination = publishdest;
            ThisDestination    = dest;
            MyDestination      = new I2PDestination(ThisDestination);

            LeaseSet = new I2PLeaseSet(MyDestination, null, new I2PLeaseInfo(ThisDestination));

            IncommingSessions = new ReceivedSessions(ThisDestination.PrivateKey);
            Destinations      = new DestinationSessions((ls, header, inf) =>
            {
                DebugUtils.LogDebug(string.Format("ClientDestination: Execute: Sending data. TrackingId: {0} ({1}) ack {2}, msg {3}.",
                                                  inf.TrackingId, inf.KeyType, inf.AckMessageId, header));

                var outtunnel = OutboundEstablishedPool.Random();
                if (outtunnel == null || ls == null || ls.Leases.Count == 0)
                {
                    throw new FailedToConnectException("No tunnels available");
                }
                var lease = ls.Leases.Random();

                outtunnel.Send(
                    new TunnelMessageTunnel(header, lease.TunnelGw, lease.TunnelId));
            },
                                                        () => InboundEstablishedPool.Random());

            NetDb.Inst.IdentHashLookup.LeaseSetReceived += new IdentResolver.IdentResolverResultLeaseSet(IdentHashLookup_LeaseSetReceived);
            NetDb.Inst.IdentHashLookup.LookupFailure    += new IdentResolver.IdentResolverResultFail(IdentHashLookup_LookupFailure);
        }
Beispiel #5
0
 public HostReplyMessage(ushort sessid, uint reqid, I2PDestination dest)
     : base(ProtocolMessageType.HostLookupReply)
 {
     SessionId   = sessid;
     RequestId   = reqid;
     ResultCode  = HostLookupResults.Success;
     Destination = dest;
 }
 public CreateLeaseSetMessage(
     I2PDestination dest,
     ushort sessionid,
     I2PLeaseInfo info,
     List <I2PLease> leases) : base(ProtocolMessageType.CreateLS)
 {
     SessionId = sessionid;
     Info      = info;
     Leases    = new I2PLeaseSet(dest, leases, info);
 }
Beispiel #7
0
        public SendMessageMessage(BufRefLen reader)
            : base(ProtocolMessageType.SendMessage)
        {
            SessionId   = reader.ReadFlip16();
            Destination = new I2PDestination(reader);
            var len = reader.ReadFlip32();

            Payload = reader.ReadBufLen((int)len);
            Nonce   = reader.ReadFlip32();
        }
Beispiel #8
0
        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;
        }
Beispiel #9
0
        public void TestI2PDestination()
        {
            var certificate = new I2PCertificate(I2PSigningKey.SigningKeyTypes.EdDSA_SHA512_Ed25519);

            var keys = I2PPrivateKey.GetNewKeyPair();

            var privkey  = keys.PrivateKey;
            var privskey = new I2PSigningPrivateKey(certificate);

            var dest = new I2PDestination(
                keys.PublicKey,
                new I2PSigningPublicKey(privskey));

            var d2 = new I2PDestination(new BufRefLen(dest.ToByteArray()));

            Assert.IsTrue(BufUtils.Equal(dest.ToByteArray(), d2.ToByteArray()));
        }
Beispiel #10
0
        public static ClientDestination CreateDestination(I2PDestination dest, I2PPrivateKey privkey, bool publish, out bool alreadyrunning)
        {
            lock ( RunningDestinations )
            {
                if (RunningDestinations.TryGetValue(dest, out var runninginst))
                {
                    alreadyrunning = true;
                    return(runninginst);
                }

                var newclient = new ClientDestination(dest, privkey, publish);
                RunningDestinations[dest] = newclient;
                ClientMgr.AttachClient(newclient);
                alreadyrunning = false;
                return(newclient);
            }
        }
Beispiel #11
0
        private void NewIdentity()
        {
            ThisDestination = new I2PDestinationInfo(I2PSigningKey.SigningKeyTypes.EdDSA_SHA512_Ed25519);
            MyDestination   = new I2PDestination(ThisDestination);

            LeaseSet = new I2PLeaseSet(MyDestination, null, new I2PLeaseInfo(ThisDestination));

            IncommingSessions = new ReceivedSessions(ThisDestination.PrivateKey);
            Destinations      = new DestinationSessions((dest, header, inf) =>
            {
                DebugUtils.LogDebug(string.Format("ClientDestination: Execute: Sending data. TrackingId: {0} ({1}) ack {2}, msg {3}.",
                                                  inf.TrackingId, inf.KeyType, inf.AckMessageId, header));

                var outtunnel = OutboundEstablishedPool.Random();
                outtunnel.Send(
                    new TunnelMessageTunnel(header, TestRemoteDest.InboundEstablishedPool.Random()));
            },
                                                        () => InboundEstablishedPool.Random());
        }
Beispiel #12
0
        public DestReplyMessage(BufRefLen reader)
            : base(ProtocolMessageType.DestReply)
        {
            Destination = null;
            Ident       = null;

            if (reader.Length == 0)
            {
                return;
            }
            if (reader.Length == 32)
            {
                Ident = new I2PIdentHash(reader);
            }
            else
            {
                Destination = new I2PDestination(reader);
            }
        }
Beispiel #13
0
        public StreamingPacket(BufRefLen reader)
        {
            SendStreamId    = reader.ReadFlip32();
            ReceiveStreamId = reader.ReadFlip32();
            SequenceNumber  = reader.ReadFlip32();
            AckTrhough      = reader.ReadFlip32();

            NACKs = new List <uint>();
            var nackcount = reader.Read8();

            for (int i = 0; i < nackcount; ++i)
            {
                NACKs.Add(reader.ReadFlip32());
            }

            ResendDelay = reader.Read8();

            Flags = (PacketFlags)reader.ReadFlip16();
            var optionsize = reader.ReadFlip16();

            // Options order
            // DELAY_REQUESTED
            // FROM_INCLUDED
            if ((Flags & PacketFlags.FROM_INCLUDED) != 0)
            {
                From = new I2PDestination(reader);
            }
            // MAX_PACKET_SIZE_INCLUDED
            if ((Flags & PacketFlags.MAX_PACKET_SIZE_INCLUDED) != 0)
            {
                var mtu = reader.ReadFlip16();
            }
            // OFFLINE_SIGNATURE
            // SIGNATURE_INCLUDED
            if ((Flags & PacketFlags.SIGNATURE_INCLUDED) != 0)
            {
                Signature = new I2PSignature(reader, From.Certificate);
            }

            Payload = reader.ReadBufLen(reader.Length);
        }
Beispiel #14
0
        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);
                }
            }
        }
Beispiel #15
0
 // Success
 public DestReplyMessage(I2PDestination dest)
     : base(ProtocolMessageType.DestReply)
 {
     Destination = dest;
 }