Example #1
0
        public static async Task <int> Main(string[] args)
        {
            Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
            CultureInfo.DefaultThreadCurrentCulture   = CultureInfo.InvariantCulture;
            CultureInfo.DefaultThreadCurrentUICulture = CultureInfo.InvariantCulture;
            CultureInfo.CurrentCulture = CultureInfo.InvariantCulture;

            // check interactivity
            var isWindowsService = args.Contains("--non-interactive");

            // paths
            var appdataFolderPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Nexus", "Explorer");

            Directory.CreateDirectory(appdataFolderPath);

            // configure logging
            _loggerFactory = LoggerFactory.Create(builder =>
            {
                builder.AddConsole();
            });

            // load configuration
            Program.OptionsFilePath = Path.Combine(appdataFolderPath, "settings.json");

            if (File.Exists(Program.OptionsFilePath))
            {
                Program.Options = NexusOptions.Load(Program.OptionsFilePath);
            }
            else
            {
                Program.Options = new NexusOptions();
                Program.Options.Save(Program.OptionsFilePath);
            }

            // service vs. interactive
            if (isWindowsService)
            {
                await Program
                .CreateHostBuilder(Environment.CurrentDirectory)
                .UseWindowsService()
                .Build()
                .RunAsync();
            }
            else
            {
                await Program
                .CreateHostBuilder(Environment.CurrentDirectory)
                .Build()
                .RunAsync();
            }

            return(0);
        }
Example #2
0
        public DataService(DatabaseManager databaseManager,
                           UserIdService userIdService,
                           ILoggerFactory loggerFactory,
                           NexusOptions options)
        {
            _databaseManager = databaseManager;
            _userIdService   = userIdService;
            _logger          = loggerFactory.CreateLogger("Nexus Explorer");
            _options         = options;
            _blockSizeLimit  = 5 * 1000 * 1000UL;

            this.Progress = new Progress <ProgressUpdatedEventArgs>();
        }
Example #3
0
        public AggregationService(
            FileAccessManager fileAccessManager,
            SignInManager <IdentityUser> signInManager,
            DatabaseManager databaseManager,
            NexusOptions options,
            ILoggerFactory loggerFactory)
        {
            _fileAccessManager      = fileAccessManager;
            _signInManager          = signInManager;
            _databaseManager        = databaseManager;
            _aggregationChunkSizeMb = options.AggregationChunkSizeMB;
            _logger = loggerFactory.CreateLogger("Nexus Explorer");

            this.Progress = new Progress <ProgressUpdatedEventArgs>();
        }
Example #4
0
 public JobsController(
     DatabaseManager databaseManager,
     NexusOptions options,
     JobService <ExportJob> exportJobService,
     JobService <AggregationJob> aggregationJobService,
     IServiceProvider serviceProvider,
     ILoggerFactory loggerFactory)
 {
     _databaseManager       = databaseManager;
     _options               = options;
     _serviceProvider       = serviceProvider;
     _exportJobService      = exportJobService;
     _aggregationJobService = aggregationJobService;
     _logger = loggerFactory.CreateLogger("Nexus Explorer");
 }
Example #5
0
        public void Configure(IApplicationBuilder app,
                              IWebHostEnvironment env,
                              AppState appState, // needs to be called to initialize the database
                              NexusOptions options)
        {
            // https://docs.microsoft.com/en-us/aspnet/core/fundamentals/middleware/?view=aspnetcore-5.0

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
                app.UseForwardedHeaders();
            }
            else
            {
                app.UseExceptionHandler("/Error");
                app.UseForwardedHeaders();
            }

            // static files
            app.UseStaticFiles();
            app.UseStaticFiles(new StaticFileOptions
            {
                FileProvider          = new LazyPhysicalFileProvider(options, "ATTACHMENTS"),
                RequestPath           = "/attachments",
                ServeUnknownFileTypes = true
            });
            app.UseStaticFiles(new StaticFileOptions
            {
                FileProvider = new LazyPhysicalFileProvider(options, "PRESETS"),
                RequestPath  = "/presets"
            });
            app.UseStaticFiles(new StaticFileOptions
            {
                FileProvider = new LazyPhysicalFileProvider(options, "EXPORT"),
                RequestPath  = "/export"
            });

            // swagger
            app.UseOpenApi();
            app.UseSwaggerUi3();

            // graphql
            app.UseGraphQL <ProjectSchema>("/graphql");
            app.UseGraphQLPlayground();

            // routing (for REST API)
            app.UseRouting();

            // default authentication
            app.UseAuthentication();

            // custom authentication (to also authenticate via JWT bearer)
            app.Use(async(context, next) =>
            {
                bool terminate = false;

                if (!context.User.Identity.IsAuthenticated)
                {
                    var authorizationHeader = context.Request.Headers["Authorization"];

                    if (authorizationHeader.Any(header => header.StartsWith("Bearer")))
                    {
                        var result = await context.AuthenticateAsync(JwtBearerDefaults.AuthenticationScheme);

                        if (result.Succeeded)
                        {
                            context.User = result.Principal;
                        }
                        else
                        {
                            context.Response.StatusCode = StatusCodes.Status401Unauthorized;

                            var errorCode = result.Failure.Message.Split(':', count: 2).FirstOrDefault();

                            var message = errorCode switch
                            {
                                "IDX10230" => "Lifetime validation failed.",
                                "IDX10503" => "Signature validation failed.",
                                _ => "The bearer token could not be validated."
                            };

                            var bytes = Encoding.UTF8.GetBytes(message);
                            await context.Response.Body.WriteAsync(bytes);
                            terminate = true;
                        }
                    }
                }

                if (!terminate)
                {
                    await next();
                }
            });

            // authorization
            app.UseAuthorization();

            // endpoints
            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
                endpoints.MapBlazorHub();
                endpoints.MapFallbackToPage("/_Host");
            });
        }