Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net;
- using System.Net.Http;
- using System.Threading.Tasks;
- using AspNetCoreRateLimit;
- using Conduit.Models;
- using Microsoft.AspNetCore.Builder;
- using Microsoft.AspNetCore.Hosting;
- using Microsoft.AspNetCore.Http;
- using Microsoft.AspNetCore.HttpOverrides;
- using Microsoft.AspNetCore.HttpsPolicy;
- using Microsoft.AspNetCore.Server.Kestrel.Core;
- using Microsoft.Extensions.Configuration;
- using Microsoft.Extensions.DependencyInjection;
- using Microsoft.Extensions.Hosting;
- using Microsoft.EntityFrameworkCore;
- using ProxyKit;
- namespace Conduit
- {
- public class Startup
- {
- public Startup(IConfiguration configuration)
- {
- Configuration = configuration;
- }
- public IConfiguration Configuration { get; }
- // This method gets called by the runtime. Use this method to add services to the container.
- public void ConfigureServices(IServiceCollection services)
- {
- services.AddControllersWithViews();
- services.AddDbContext<ApplicationDbContext>(options => { options.UseSqlite("Data Source=application.db"); });
- services.Configure<IpRateLimitOptions>(Configuration.GetSection("IpRateLimiting"));
- services.AddSingleton<IIpPolicyStore, MemoryCacheIpPolicyStore>();
- services.AddSingleton<IRateLimitCounterStore, MemoryCacheRateLimitCounterStore>();
- services.AddSingleton<IRateLimitConfiguration, RateLimitConfiguration>();
- services.AddHttpContextAccessor();
- services.AddProxy();
- }
- // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
- public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
- {
- app.UseForwardedHeaders(new ForwardedHeadersOptions
- {
- ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
- });
- if (env.IsDevelopment())
- {
- app.UseDeveloperExceptionPage();
- }
- else
- {
- app.UseExceptionHandler("/Home/Error");
- // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
- app.UseHsts();
- }
- app.UseStaticFiles();
- app.UseRouting();
- app.UseAuthorization();
- app.UseStatusCodePages(
- "text/plain", "Status code page, status code: {0}");
- app.UseIpRateLimiting();
- app.UseEndpoints(endpoints =>
- {
- endpoints.MapControllerRoute(
- name: "default",
- pattern: "{controller=Home}/{action=Index}/{id?}");
- });
- app.Map("/api/discord/v6/oauth2/token", app =>
- {
- app.RunProxy(context => context
- .ForwardTo("https://discordapp.com/api/v6/oauth2/token")
- .AddXForwardedHeaders()
- .Send());
- });
- app.Map("/api/discord/v6/users/@me", app =>
- {
- app.RunProxy(context => context
- .ForwardTo("https://discordapp.com/api/v6/users/@me")
- .AddXForwardedHeaders()
- .Send());
- });
- app.Map("/api/discord/v6/guilds", app =>
- {
- app.RunProxy(context =>
- {
- string forwardTo = "https://discordapp.com/api/v6/guilds/";
- if (string.IsNullOrEmpty(context.Request.Query["guild_id"]))
- {
- var response = new HttpResponseMessage(HttpStatusCode.Forbidden);
- return Task.FromResult(response);
- }
- else
- {
- forwardTo += context.Request.Query["guild_id"];
- }
- if (string.IsNullOrEmpty(context.Request.Query["members"]))
- {
- var response = new HttpResponseMessage(HttpStatusCode.Forbidden);
- return Task.FromResult(response);
- }
- else
- {
- forwardTo += "/members/";
- }
- if (string.IsNullOrEmpty(context.Request.Query["member_id"]))
- {
- var response = new HttpResponseMessage(HttpStatusCode.Forbidden);
- return Task.FromResult(response);
- }
- else
- {
- forwardTo += context.Request.Query["member_id"];
- }
- return context
- .ForwardTo(forwardTo)
- .AddXForwardedHeaders()
- .Send();
- });
- });
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement