public static IServiceCollection AddNBXplorer(this IServiceCollection services) { services.AddSingleton <IObjectModelValidator, NoObjectModelValidator>(); services.Configure <MvcOptions>(mvc => { mvc.Filters.Add(new NBXplorerExceptionFilter()); }); services.AddSingleton <IConfigureOptions <MvcJsonOptions>, MVCConfigureOptions>(); services.TryAddSingleton <ConcurrentChain>(o => new ConcurrentChain(o.GetRequiredService <Network>())); services.TryAddSingleton <NetworkInformation>(o => o.GetRequiredService <IOptions <ExplorerConfiguration> >().Value.Network); services.TryAddSingleton <Network>(o => o.GetRequiredService <IOptions <NetworkInformation> >().Value.Network); services.TryAddSingleton <CallbackInvoker>(); services.TryAddSingleton <Repository>(o => { var configuration = o.GetRequiredService <ExplorerConfiguration>(); var dbPath = Path.Combine(configuration.DataDir, "db"); var repo = new Repository(configuration.CreateSerializer(), dbPath); if (configuration.Rescan) { Logs.Configuration.LogInformation("Rescanning..."); repo.SetIndexProgress(null); } return(repo); }); services.TryAddSingleton <Serializer>(); services.TryAddSingleton <ChainEvents>(); services.TryAddSingleton <NBxplorerInitializer>(); services.AddSingleton <ExplorerConfiguration>(o => o.GetRequiredService <IOptions <ExplorerConfiguration> >().Value); services.AddSingleton <Network>(o => { var c = o.GetRequiredService <ExplorerConfiguration>(); return(c.Network.Network); }); services.TryAddSingleton <RPCClient>(o => { var configuration = o.GetRequiredService <ExplorerConfiguration>(); configuration.RPC.NoTest = true; return(configuration.RPC.ConfigureRPCClient(configuration.Network.Network)); }); services.TryAddSingleton <RPCAuthorization>(o => { var configuration = o.GetRequiredService <ExplorerConfiguration>(); var cookieFile = Path.Combine(configuration.DataDir, ".cookie"); var cookieStr = "__cookie__:" + new uint256(RandomUtils.GetBytes(32)); File.WriteAllText(cookieFile, cookieStr); RPCAuthorization auth = new RPCAuthorization(); if (!configuration.NoAuthentication) { auth.AllowIp.Add(IPAddress.Parse("127.0.0.1")); auth.AllowIp.Add(IPAddress.Parse("::1")); auth.Authorized.Add(cookieStr); } return(auth); }); return(services); }
public NBXplorerMiddleware(RequestDelegate next, RPCAuthorization authorization) { if (authorization == null) { throw new ArgumentNullException(nameof(authorization)); } if (next == null) { throw new ArgumentNullException(nameof(next)); } this.authorization = authorization; _Next = next; }
public RPCAuthorizationTest() { this.authorization = new RPCAuthorization(); }