public ClassicTumblerRepository(TumblerConfiguration config, IRepository repository)
 {
     if (config == null)
     {
         throw new ArgumentNullException(nameof(config));
     }
     _Configuration = config;
     _Repository    = repository;
 }
Esempio n. 2
0
        public static void Main(string[] args)
        {
            Logs.Configure(new FuncLoggerFactory(i => new ConsoleLogger(i, (a, b) => true, false)));
            var configuration = new TumblerConfiguration();

            configuration.LoadArgs(args);
            try
            {
                IWebHost         host     = null;
                ExternalServices services = null;
                if (!configuration.OnlyMonitor)
                {
                    host = new WebHostBuilder()
                           .UseKestrel()
                           .UseAppConfiguration(configuration)
                           .UseContentRoot(Directory.GetCurrentDirectory())
                           .UseIISIntegration()
                           .UseStartup <Startup>()
                           .Build();
                    services = (ExternalServices)host.Services.GetService(typeof(ExternalServices));
                }
                else
                {
                    services = ExternalServices.CreateFromRPCClient(configuration.RPC.ConfigureRPCClient(configuration.Network), new DBreezeRepository(Path.Combine(configuration.DataDir, "db")));
                }

                CancellationTokenSource cts = new CancellationTokenSource();
                var job = new BroadcasterJob(services, Logs.Main);
                job.Start(cts.Token);
                Logs.Main.LogInformation("BroadcasterJob started");

                if (!configuration.OnlyMonitor)
                {
                    host.Run();
                }
                else
                {
                    Console.ReadLine();
                }
                cts.Cancel();
            }
            catch (ConfigException ex)
            {
                if (!string.IsNullOrEmpty(ex.Message))
                {
                    Logs.Main.LogError(ex.Message);
                }
            }
            catch (Exception exception)
            {
                Logs.Main.LogError("Exception thrown while running the server " + exception.Message);
            }
        }
Esempio n. 3
0
        public static void Main(string[] args)
        {
            Logs.Configure(new FuncLoggerFactory(i => new ConsoleLogger("Configuration", (a, b) => true, false)));
            var logger        = new ConsoleLogger("Main", (a, b) => true, false);
            var configuration = new TumblerConfiguration();

            configuration.LoadArgs(args);
            try
            {
                var host = new WebHostBuilder()
                           .UseKestrel()
                           .UseAppConfiguration(configuration)
                           .UseContentRoot(Directory.GetCurrentDirectory())
                           .UseIISIntegration()
                           .UseStartup <Startup>()
                           .Build();

                var services = (ExternalServices)host.Services.GetService(typeof(ExternalServices));
                CancellationTokenSource cts = new CancellationTokenSource();
                var job = new BroadcasterJob(services, logger);
                job.Start(cts.Token);
                host.Run();
                cts.Cancel();
            }
            catch (ConfigException ex)
            {
                if (!string.IsNullOrEmpty(ex.Message))
                {
                    logger.LogError(ex.Message);
                }
            }
            catch (Exception exception)
            {
                logger.LogError("Exception thrown while running the server " + exception.Message);
            }
        }
Esempio n. 4
0
        public static IWebHostBuilder UseAppConfiguration(this IWebHostBuilder builder, TumblerConfiguration configuration)
        {
            builder.ConfigureServices(services =>
            {
                services.AddSingleton(provider =>
                {
                    var conf = provider.GetRequiredService <TumblerConfiguration>();
                    var repo = provider.GetRequiredService <IRepository>();
                    return(new ClassicTumblerRepository(conf, repo));
                });

                services.AddSingleton <IRepository>(provider =>
                {
                    var conf    = provider.GetRequiredService <TumblerConfiguration>();
                    var dbreeze = new DBreezeRepository(Path.Combine(conf.DataDir, "db"));
                    return(dbreeze);
                });

                services.AddSingleton((provider) =>
                {
                    var conf = provider.GetRequiredService <TumblerConfiguration>();
                    var repo = provider.GetRequiredService <IRepository>();
                    return(ExternalServices.CreateFromRPCClient(conf.RPCClient, repo));
                });
                services.AddSingleton((provider) =>
                {
                    var conf = provider.GetRequiredService <TumblerConfiguration>();
                    return(conf.CreateClassicTumblerParameters());
                });
                services.AddSingleton((provider) =>
                {
                    var conf = configuration ?? new TumblerConfiguration().LoadArgs(new string[0]);

                    var rsaFile = Path.Combine(conf.DataDir, "Tumbler.pem");

                    if (conf.TumblerKey == null)
                    {
                        if (!File.Exists(rsaFile))
                        {
                            Logs.Configuration.LogWarning("RSA private key not found, please backup it. Creating...");
                            conf.TumblerKey = new RsaKey();
                            File.WriteAllBytes(rsaFile, conf.TumblerKey.ToBytes());
                            Logs.Configuration.LogInformation("RSA key saved (" + rsaFile + ")");
                        }
                        else
                        {
                            Logs.Configuration.LogInformation("RSA private key found (" + rsaFile + ")");
                            conf.TumblerKey = new RsaKey(File.ReadAllBytes(rsaFile));
                        }
                    }

                    if (conf.VoucherKey == null)
                    {
                        var voucherFile = Path.Combine(conf.DataDir, "Voucher.pem");
                        if (!File.Exists(voucherFile))
                        {
                            Logs.Configuration.LogWarning("Creation of Voucher Key");
                            conf.VoucherKey = new RsaKey();
                            File.WriteAllBytes(voucherFile, conf.VoucherKey.ToBytes());
                            Logs.Configuration.LogInformation("RSA key saved (" + voucherFile + ")");
                        }
                        else
                        {
                            Logs.Configuration.LogInformation("Voucher key found (" + voucherFile + ")");
                            conf.VoucherKey = new RsaKey(File.ReadAllBytes(voucherFile));
                        }
                    }

                    try
                    {
                        conf.RPCClient = conf.RPCClient ?? conf.RPC.ConfigureRPCClient(conf.Network);
                    }
                    catch
                    {
                        throw new ConfigException("Please, fix rpc settings in " + conf.ConfigurationFile);
                    }
                    return(configuration);
                });
            });

            builder.UseUrls(configuration.GetUrls());

            return(builder);
        }