/// <summary> /// Creates code builder. /// </summary> /// <param name="options"> /// The options. /// </param> public static IWebHostBuilder CreateCodeBuilder(IPFilteringOptions options = null) { IWebHostBuilder builder = new WebHostBuilder() .ConfigureServices( s => s.AddIPFiltering( opts => { if (options != null) { opts.DefaultBlockLevel = options.DefaultBlockLevel; opts.HttpStatusCode = options.HttpStatusCode; opts.Blacklist = options.Blacklist; opts.Whitelist = options.Whitelist; } })) .Configure( app => { app.UseIPFiltering(); app.Run( async c => { await c.Response.WriteAsync("Hello World"); }); }); return(builder); }
public static IApplicationBuilder UseIPFiltering( this IApplicationBuilder app, IPFilteringOptions options) { if (app == null) { throw new ArgumentNullException(nameof(app)); } if (options == null) { throw new ArgumentNullException(nameof(options)); } return(app.UseMiddleware <IPFilteringMiddleware>(Options.Create(options))); }
/// <inheritdoc /> public virtual bool IsAllowed(IPAddress ipAddress, IPFilteringOptions options) { if (ipAddress != null) { var whitelist = options.Whitelist.Select(IPAddressRange.Parse).ToList(); var blacklist = options.Blacklist.Select(IPAddressRange.Parse).ToList(); switch (options.DefaultBlockLevel) { case DefaultBlockLevel.All: return(whitelist.Any(r => r.Contains(ipAddress)) && !blacklist.Any(r => r.Contains(ipAddress))); case DefaultBlockLevel.None: return(!blacklist.Any(r => r.Contains(ipAddress)) || whitelist.Any(r => r.Contains(ipAddress))); } } return(options.DefaultBlockLevel == DefaultBlockLevel.None); }
/// <summary> /// Creates code builder. /// </summary> /// <param name="options"> /// The options. /// </param> public static IWebHostBuilder CreateCodeBuilder(IPFilteringOptions options = null) { if (options == null) { options = new IPFilteringOptions(); } IWebHostBuilder builder = new WebHostBuilder() .ConfigureServices(s => s.AddIPFiltering()) .Configure( app => { app.UseIPFiltering(options); app.Run( async c => { await c.Response.WriteAsync("Hello World"); }); }); return(builder); }
/// <inheritdoc/> public bool IsIgnored(string verb, string path, IPFilteringOptions options) { if (options.IgnoredPaths != null) { string fullCheck = $"{verb}:{path}"; string check = $"*:{path}"; if (options.IgnoredPaths.Any(x => Contains(fullCheck, x)) || options.IgnoredPaths.Any(x => Contains(check, x))) { return(true); } } return(false); bool Contains(string source, string value) { return((source != null) && (value != null) && source.StartsWith(value, StringComparison.OrdinalIgnoreCase)); } }