Esempio n. 1
0
        /// <summary>
        ///     Displays command-line help.
        /// </summary>
        /// <param name="masterNode">The masternode to extract values from.</param>
        public static void PrintHelp(MasterNodeBase masterNode)
        {
            Guard.NotNull(masterNode, nameof(masterNode));

            ServerSettings defaults   = Default(masterNode);
            string         daemonName = Path.GetFileName(Assembly.GetEntryAssembly().Location);

            StringBuilder builder = new StringBuilder();

            builder.AppendLine("Usage:");
            builder.AppendLine($" dotnet run {daemonName} [arguments]");
            builder.AppendLine();
            builder.AppendLine("Command line arguments:");
            builder.AppendLine();
            builder.AppendLine("-help/--help              Show this help.");
            builder.AppendLine(
                $"-conf=<Path>              Path to the configuration file. Defaults to {defaults.ConfigurationFile}.");
            builder.AppendLine(
                $"-datadir=<Path>           Path to the data directory. Defaults to {defaults.DataDir}.");
            builder.AppendLine(
                "-debug[=<string>]         Set 'Debug' logging level. Specify what to log via e.g. '-debug=X42.Protocol,X42.Server.Builder'.");
            builder.AppendLine(
                "-loglevel=<string>        Direct control over the logging level: '-loglevel=trace/debug/info/warn/error/fatal'.");

            defaults.Logger.LogInformation(builder.ToString());
        }
Esempio n. 2
0
 private FeatureController(
     IX42Server x42Server        = null,
     ServerSettings nodeSettings = null,
     MasterNodeBase network      = null)
 {
     X42Server = x42Server;
     Settings  = nodeSettings;
     Network   = network;
 }
Esempio n. 3
0
        /// <summary>
        ///     Displays database help information on the console.
        /// </summary>
        /// <param name="masterNode">Not used.</param>
        public static void PrintHelp(MasterNodeBase masterNode)
        {
            ServerSettings defaults = ServerSettings.Default(masterNode);
            StringBuilder  builder  = new StringBuilder();

            builder.AppendLine("-connectionstring=<string>                     Database host.");

            defaults.Logger.LogInformation(builder.ToString());
        }
Esempio n. 4
0
 public DatabaseFeatures(
     MasterNodeBase network,
     ILoggerFactory loggerFactory,
     DatabaseSettings databaseSettings
     )
 {
     logger = loggerFactory.CreateLogger(GetType().FullName);
     this.databaseSettings = databaseSettings;
 }
        /// <summary>
        ///     Displays database help information on the console.
        /// </summary>
        /// <param name="masterNode">Not used.</param>
        public static void PrintHelp(MasterNodeBase masterNode)
        {
            ServerSettings defaults = ServerSettings.Default(masterNode);
            StringBuilder  builder  = new StringBuilder();

            builder.AppendLine("-address=127.0.0.1                  Node address.");
            builder.AppendLine("-port=42221                         Node port.");

            defaults.Logger.LogInformation(builder.ToString());
        }
Esempio n. 6
0
        public X42ClientFeature(
            MasterNodeBase network,
            ILoggerFactory loggerFactory,
            X42ClientSettings x42ClientSettings,
            IServerStats nodeStats,
            IX42ServerLifetime serverLifetime,
            IAsyncLoopFactory asyncLoopFactory)
        {
            this.serverLifetime    = serverLifetime;
            this.asyncLoopFactory  = asyncLoopFactory;
            this.x42ClientSettings = x42ClientSettings;
            logger = loggerFactory.CreateLogger(GetType().FullName);

            nodeStats.RegisterStats(this.AddComponentStats, StatsType.Component, 1000);
        }
Esempio n. 7
0
 public NetworkFeatures(
     MasterNodeBase network,
     ILoggerFactory loggerFactory,
     DatabaseSettings databaseSettings,
     X42ClientSettings x42ClientSettings,
     IX42ServerLifetime serverLifetime,
     IAsyncLoopFactory asyncLoopFactory
     )
 {
     this.network           = network;
     logger                 = loggerFactory.CreateLogger(GetType().FullName);
     this.databaseSettings  = databaseSettings;
     this.x42ClientSettings = x42ClientSettings;
     this.serverLifetime    = serverLifetime;
     this.asyncLoopFactory  = asyncLoopFactory;
 }
Esempio n. 8
0
        /// <summary>Prints the help information on how to configure the API settings to the logger.</summary>
        /// <param name="network">The network to use.</param>
        public static void PrintHelp(MasterNodeBase network)
        {
            StringBuilder builder = new StringBuilder();

            builder.AppendLine(
                $"-apiuri=<string>                  URI to node's API interface. Defaults to '{DefaultApiHost}'.");
            builder.AppendLine(
                $"-apiport=<0-65535>                Port of node's API interface. Defaults to {GetDefaultPort(network)}.");
            builder.AppendLine(
                "-keepalive=<seconds>              Keep Alive interval (set in seconds). Default: 0 (no keep alive).");
            builder.AppendLine("-usehttps=<bool>                  Use https protocol on the API. Defaults to false.");
            builder.AppendLine(
                "-certificatefilepath=<string>     Path to the certificate used for https traffic encryption. Defaults to <null>. Password protected files are not supported. On MacOs, only p12 certificates can be used without password.");
            builder.AppendLine("-enableswagger=<bool>             Enable swagger. Defaults to false.");

            ServerSettings.Default(network).Logger.LogInformation(builder.ToString());
        }
        public MasterNodeContoller(IX42Server x42Server, ILoggerFactory loggerFactory,
                                   IDateTimeProvider dateTimeProvider,
                                   ServerSettings serverSettings,
                                   MasterNodeBase masterNode)
        {
            Guard.NotNull(x42Server, nameof(x42Server));
            Guard.NotNull(masterNode, nameof(masterNode));
            Guard.NotNull(loggerFactory, nameof(loggerFactory));
            Guard.NotNull(serverSettings, nameof(serverSettings));
            Guard.NotNull(dateTimeProvider, nameof(dateTimeProvider));

            this.x42Server        = x42Server;
            logger                = loggerFactory.CreateLogger(GetType().FullName);
            this.dateTimeProvider = dateTimeProvider;
            nodeSettings          = serverSettings;
            this.masterNode       = masterNode;
        }
Esempio n. 10
0
 /// <summary>
 ///     Get the default configuration.
 /// </summary>
 /// <param name="builder">The string builder to add the settings to.</param>
 /// <param name="network">The network to base the defaults off.</param>
 public static void BuildDefaultConfigurationFile(StringBuilder builder, MasterNodeBase network)
 {
     builder.AppendLine("####API Settings####");
     builder.AppendLine($"#URI to node's API interface. Defaults to '{DefaultApiHost}'.");
     builder.AppendLine($"#apiuri={DefaultApiHost}");
     builder.AppendLine($"#Port of node's API interface. Defaults to {GetDefaultPort(network)}.");
     builder.AppendLine($"#apiport={GetDefaultPort(network)}");
     builder.AppendLine("#Keep Alive interval (set in seconds). Default: 0 (no keep alive).");
     builder.AppendLine("#keepalive=0");
     builder.AppendLine("#Use HTTPS protocol on the API. Default is false.");
     builder.AppendLine("#usehttps=false");
     builder.AppendLine(
         "#Path to the file containing the certificate to use for https traffic encryption. Password protected files are not supported. On MacOs, only p12 certificates can be used without password.");
     builder.AppendLine(
         @"#Please refer to .Net Core documentation for usage: 'https://docs.microsoft.com/en-us/dotnet/api/system.security.cryptography.x509certificates.x509certificate2.-ctor?view=netcore-2.1#System_Security_Cryptography_X509Certificates_X509Certificate2__ctor_System_Byte___'.");
     builder.AppendLine("#certificatefilepath=");
     builder.AppendLine("#Enable swagger. Defaults to false.");
     builder.AppendLine("#enableswagger=false");
 }
Esempio n. 11
0
 /// <summary>
 ///     Get the default configuration.
 /// </summary>
 /// <param name="builder">The string builder to add the settings to.</param>
 /// <param name="network">The network to base the defaults off.</param>
 public static void BuildDefaultConfigurationFile(StringBuilder builder, MasterNodeBase network)
 {
     builder.AppendLine("####X42 Node Settings####");
     builder.AppendLine("#Node Address");
     builder.AppendLine("#address=127.0.0.1");
     builder.AppendLine("#Node Port");
     builder.AppendLine("#port=42220");
     builder.AppendLine("");
     builder.AppendLine("####SSH Settings####");
     builder.AppendLine("#SSH Server Address");
     builder.AppendLine("#sshserveraddress=127.0.0.1");
     builder.AppendLine("#SSH Server Port");
     builder.AppendLine("#sshport=22");
     builder.AppendLine("#SSH Server Username");
     builder.AppendLine("#sshusername=username");
     builder.AppendLine("#SSH Password");
     builder.AppendLine("#sshpassword=password");
     builder.AppendLine("#SSH Local Bound Address");
     builder.AppendLine("#sshlocalboundaddress=127.0.0.1");
     builder.AppendLine("#SSH Local Bound Port");
     builder.AppendLine("#sshlocalboundport=42220");
 }
Esempio n. 12
0
 /// <summary>
 ///     Initializes default configuration.
 /// </summary>
 /// <param name="masterNode">Specification of the master node the server runs on</param>
 /// <param name="protocolVersion">Supported protocol version for which to create the configuration.</param>
 /// <returns>Default server configuration.</returns>
 public static ServerSettings Default(MasterNodeBase masterNode,
                                      ProtocolVersion protocolVersion = SupportedProtocolVersion)
 {
     return(new ServerSettings(masterNode, protocolVersion));
 }
Esempio n. 13
0
 /// <summary>
 ///     Get the default configuration.
 /// </summary>
 /// <param name="builder">The string builder to add the settings to.</param>
 /// <param name="network">The network to base the defaults off.</param>
 public static void BuildDefaultConfigurationFile(StringBuilder builder, MasterNodeBase network)
 {
     DatabaseSettings.BuildDefaultConfigurationFile(builder, network);
 }
Esempio n. 14
0
 /// <summary>
 ///     Prints command-line help.
 /// </summary>
 /// <param name="masterNodeBase">The masternode to extract values from.</param>
 public static void PrintHelp(MasterNodeBase masterNodeBase)
 {
     DatabaseSettings.PrintHelp(masterNodeBase);
 }
Esempio n. 15
0
 /// <summary>
 ///     Prints command-line help.
 /// </summary>
 /// <param name="masterNodeBase">The masternode to extract values from.</param>
 public static void PrintHelp(MasterNodeBase masterNodeBase)
 {
     X42ClientSettings.PrintHelp(masterNodeBase);
 }
Esempio n. 16
0
 /// <summary>
 ///     Get the default configuration.
 /// </summary>
 /// <param name="builder">The string builder to add the settings to.</param>
 /// <param name="network">The network to base the defaults off.</param>
 public static void BuildDefaultConfigurationFile(StringBuilder builder, MasterNodeBase masterNodeBase)
 {
     builder.AppendLine("####Database Settings####");
     builder.AppendLine("#Connection string for database.");
     builder.AppendLine("#connectionstring=<string>");
 }
Esempio n. 17
0
 /// <summary>
 ///     Get the default configuration.
 /// </summary>
 /// <param name="builder">The string builder to add the settings to.</param>
 /// <param name="network">The network to base the defaults off.</param>
 public static void BuildDefaultConfigurationFile(StringBuilder builder, MasterNodeBase masterNodeBase)
 {
     builder.AppendLine("####Network Settings####");
     builder.AppendLine("#Manually connect to masternode.");
     builder.AppendLine("#connect=<string>");
 }
Esempio n. 18
0
 /// <summary>
 ///     Prints command-line help.
 /// </summary>
 /// <param name="network">The network to extract values from.</param>
 public static void PrintHelp(MasterNodeBase network)
 {
     ApiSettings.PrintHelp(network);
 }
Esempio n. 19
0
 /// <summary>
 ///     Prints command-line help.
 /// </summary>
 /// <param name="masterNodeBase">The masternode to extract values from.</param>
 public static void PrintHelp(MasterNodeBase masterNodeBase)
 {
     NetworkSettings.PrintHelp(masterNodeBase);
 }
Esempio n. 20
0
 /// <summary>
 ///     Get the default configuration.
 /// </summary>
 /// <param name="builder">The string builder to add the settings to.</param>
 /// <param name="masterNode">The masternode to base the defaults off.</param>
 public static void BuildDefaultConfigurationFile(StringBuilder builder, MasterNodeBase masterNode)
 {
     builder.AppendLine("####Server Settings####");
     builder.AppendLine();
 }
Esempio n. 21
0
        /// <inheritdoc />
        public IX42Server Build()
        {
            if (serverBuilt)
            {
                throw new InvalidOperationException("x42 server already built");
            }
            serverBuilt = true;

            Services = BuildServices();

            // Print command - line help
            if (ServerSettings?.PrintHelpAndExit ?? false)
            {
                foreach (IFeatureRegistration featureRegistration in Features.FeatureRegistrations)
                {
                    MethodInfo printHelp =
                        featureRegistration.FeatureType.GetMethod("PrintHelp",
                                                                  BindingFlags.Public | BindingFlags.Static);

                    printHelp?.Invoke(null, new object[] { ServerSettings.MasterNode });
                }

                // Signal server not built
                return(null);
            }

            // Create configuration file if required
            ServerSettings?.CreateDefaultConfigurationFile(Features.FeatureRegistrations);

            ServiceProvider serverServiceProvider = Services.BuildServiceProvider();

            ConfigureServices(serverServiceProvider);

            // Obtain the serverSettings from the service (it's set used ServerBuilder.UseServerSettings)
            ServerSettings serverSettings = serverServiceProvider.GetService <ServerSettings>();

            if (serverSettings == null)
            {
                throw new ServerBuilderException("ServerSettings not specified");
            }

            MasterNodeBase masterNode = serverServiceProvider.GetService <MasterNodeBase>();

            if (masterNode == null)
            {
                throw new ServerBuilderException("MasterNode not specified");
            }

            X42Server server = serverServiceProvider.GetService <X42Server>();

            if (server == null)
            {
                throw new InvalidOperationException("X42Server not registered with provider");
            }

            server.Initialize(
                new ServerServiceProvider(
                    serverServiceProvider,
                    Features.FeatureRegistrations.Select(s => s.FeatureType).ToList()
                    )
                );

            return(server);
        }
Esempio n. 22
0
        /// <summary>
        ///     Initializes a new instance of the object.
        /// </summary>
        /// <param name="masterNode">The masternode the server runs on</param>
        /// <param name="protocolVersion">Supported protocol version for which to create the configuration.</param>
        /// <param name="agent">The servers user agent that will be shared with peers.</param>
        /// <param name="args">The command-line arguments.</param>
        /// <exception cref="ConfigurationException">
        ///     Thrown in case of any problems with the configuration file or command line
        ///     arguments.
        /// </exception>
        /// <remarks>
        ///     Processing depends on whether a configuration file is passed via the command line.
        ///     There are two main scenarios here:
        ///     - The configuration file is passed via the command line. In this case we need
        ///     to read it earlier so that it can provide defaults for "testnet" and "regtest".
        ///     - Alternatively, if the file name is not supplied then a masternode-specific file
        ///     name would be determined. In this case we first need to determine the masternode.
        /// </remarks>
        public ServerSettings(MasterNodeBase masterNode,
                              ProtocolVersion protocolVersion = ProtocolVersion.PROTOCOL_VERSION, string agent = "x42",
                              string[] args = null)
        {
            MasterNode = masterNode;
            // Create the default logger factory and logger.
            ExtendedLoggerFactory loggerFactory = new ExtendedLoggerFactory();

            LoggerFactory = loggerFactory;
            LoggerFactory.AddConsoleWithFilters();
            LoggerFactory.AddNLog();
            Logger = LoggerFactory.CreateLogger(typeof(ServerSettings).FullName);

            ProtocolVersion = protocolVersion;
            Agent           = agent;
            ConfigReader    = new TextFileConfiguration(args ?? new string[] { });

            // Log arguments.
            Logger.LogDebug("Arguments: masternode='{0}', protocolVersion='{1}', agent='{2}', args='{3}'.",
                            string.IsNullOrEmpty(ServerName) ? "(None)" : ServerName,
                            ProtocolVersion,
                            Agent,
                            args == null ? "(None)" : string.Join(" ", args));

            // By default, we look for a file named '<masternode>.conf' in the masternode's data directory,
            // but both the data directory and the configuration file path may be changed using the -datadir and -conf command-line arguments.
            ConfigurationFile = ConfigReader.GetOrDefault <string>("conf", null, Logger)?.NormalizeDirectorySeparator();
            DataDir           = ConfigReader.GetOrDefault <string>("datadir", null, Logger)?.NormalizeDirectorySeparator();
            DataDirRoot       = ConfigReader.GetOrDefault("datadirroot", "x42Server", Logger);

            // If the configuration file is relative then assume it is relative to the data folder and combine the paths.
            if (DataDir != null && ConfigurationFile != null)
            {
                bool isRelativePath = Path.GetFullPath(ConfigurationFile).Length > ConfigurationFile.Length;
                if (isRelativePath)
                {
                    ConfigurationFile = Path.Combine(DataDir, ConfigurationFile);
                }
            }

            // If the configuration file has been specified on the command line then read it now
            // so that it can provide the defaults for testnet and regtest.
            if (ConfigurationFile != null)
            {
                // If the configuration file was specified on the command line then it must exist.
                if (!File.Exists(ConfigurationFile))
                {
                    throw new ConfigurationException($"Configuration file does not exist at {ConfigurationFile}.");
                }

                // Sets the ConfigReader based on the arguments and the configuration file if it exists.
                ReadConfigurationFile();
            }

            // Set the full data directory path.
            if (DataDir == null)
            {
                // Create the data directories if they don't exist.
                DataDir = CreateDefaultDataDirectories(ServerName);
            }
            else
            {
                // Combine the data directory with the masternode's root folder and name.
                string directoryPath = Path.Combine(DataDir, ServerName);
                DataDir = Directory.CreateDirectory(directoryPath).FullName;
                Logger.LogDebug("Data directory initialized with path {0}.", DataDir);
            }

            // Set the data folder.
            DataFolder = new DataFolder(DataDir);

            // Attempt to load NLog configuration from the DataFolder.
            loggerFactory.LoadNLogConfiguration(DataFolder);

            // Get the configuration file name for the masternode if it was not specified on the command line.
            if (ConfigurationFile == null)
            {
                ConfigurationFile = Path.Combine(DataDir, MasterNode.DefaultConfigFilename);
                Logger.LogDebug("Configuration file set to '{0}'.", ConfigurationFile);

                if (File.Exists(ConfigurationFile))
                {
                    ReadConfigurationFile();
                }
            }

            // Create the custom logger factory.
            Log = new LogSettings();
            Log.Load(ConfigReader);
            LoggerFactory.AddFilters(Log, DataFolder);
            LoggerFactory.ConfigureConsoleFilters(LoggerFactory.GetConsoleSettings(), Log);

            // Load the configuration.
            LoadConfiguration();
        }
Esempio n. 23
0
 /// <summary>
 ///     Determines the default API port.
 /// </summary>
 /// <param name="network">The network to use.</param>
 /// <returns>The default API port.</returns>
 private static int GetDefaultPort(MasterNodeBase network)
 {
     return(network.DefaultPort);
 }