public void TestAESBlock() { for (int runs = 0; runs < 10; ++runs) { var buf = new BufLen(new byte[30000]); var writer = new BufRefLen(buf); var data = BufUtils.RandomBytes(1 + BufUtils.RandomInt(45)); var datar = new BufRefLen(data); var tags = new List <I2PSessionTag>(); for (int i = 0; i < BufUtils.RandomInt(5); ++i) { tags.Add(new I2PSessionTag()); } var newsession = BufUtils.RandomDouble(1.0) < 0.3 ? new I2PSessionKey() : null; var b1 = new GarlicAESBlock(writer, tags, newsession, datar); var bldata = new BufLen(buf, 0, writer - buf).Clone(); var b2 = new GarlicAESBlock(new BufRefLen(bldata)); var b1ar = new BufLen(b1.ToByteArray()); var b2ar = new BufLen(b2.ToByteArray()); Assert.IsTrue(b1ar == b2ar); var bufs = new BufRefStream(); b1.Write(bufs); var b3 = new GarlicAESBlock(new BufRefLen(bufs.ToByteArray())); var b3ar = new BufLen(b3.ToByteArray()); Assert.IsTrue(b1ar == b3ar); } }
public void TestShuffle() { var ints = Enumerable.Range(0, 200); var bag = ints.Select(i => BufUtils.RandomDouble(1)).ToArray(); var shuffled = bag.Shuffle().ToArray(); Assert.IsTrue(bag.All(b => shuffled.Any(i => i == b))); }
public void TestRandom() { var values = Enumerable .Range(0, 21) .Select(i => BufUtils.RandomDouble(1.0)) .ToArray(); var bag = values.SelectMany(i1 => values.SelectMany(i2 => values)) .Select(i3 => values.Random()); Assert.IsTrue(bag.All(b => values.Any(i => i == b))); }
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); } } }