Advertisement
Guest User

Untitled

a guest
Mar 30th, 2020
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.12 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net;
  5. using System.Net.Http;
  6. using System.Threading.Tasks;
  7. using AspNetCoreRateLimit;
  8. using Conduit.Models;
  9. using Microsoft.AspNetCore.Builder;
  10. using Microsoft.AspNetCore.Hosting;
  11. using Microsoft.AspNetCore.Http;
  12. using Microsoft.AspNetCore.HttpOverrides;
  13. using Microsoft.AspNetCore.HttpsPolicy;
  14. using Microsoft.AspNetCore.Server.Kestrel.Core;
  15. using Microsoft.Extensions.Configuration;
  16. using Microsoft.Extensions.DependencyInjection;
  17. using Microsoft.Extensions.Hosting;
  18. using Microsoft.EntityFrameworkCore;
  19. using ProxyKit;
  20.  
  21. namespace Conduit
  22. {
  23. public class Startup
  24. {
  25. public Startup(IConfiguration configuration)
  26. {
  27. Configuration = configuration;
  28. }
  29.  
  30. public IConfiguration Configuration { get; }
  31.  
  32. // This method gets called by the runtime. Use this method to add services to the container.
  33. public void ConfigureServices(IServiceCollection services)
  34. {
  35. services.AddControllersWithViews();
  36. services.AddDbContext<ApplicationDbContext>(options => { options.UseSqlite("Data Source=application.db"); });
  37. services.Configure<IpRateLimitOptions>(Configuration.GetSection("IpRateLimiting"));
  38. services.AddSingleton<IIpPolicyStore, MemoryCacheIpPolicyStore>();
  39. services.AddSingleton<IRateLimitCounterStore, MemoryCacheRateLimitCounterStore>();
  40. services.AddSingleton<IRateLimitConfiguration, RateLimitConfiguration>();
  41. services.AddHttpContextAccessor();
  42. services.AddProxy();
  43.  
  44. }
  45.  
  46. // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
  47. public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
  48. {
  49. app.UseForwardedHeaders(new ForwardedHeadersOptions
  50. {
  51. ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
  52. });
  53.  
  54. if (env.IsDevelopment())
  55. {
  56. app.UseDeveloperExceptionPage();
  57. }
  58. else
  59. {
  60. app.UseExceptionHandler("/Home/Error");
  61. // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
  62. app.UseHsts();
  63. }
  64.  
  65. app.UseStaticFiles();
  66. app.UseRouting();
  67. app.UseAuthorization();
  68.  
  69. app.UseStatusCodePages(
  70. "text/plain", "Status code page, status code: {0}");
  71.  
  72. app.UseIpRateLimiting();
  73.  
  74. app.UseEndpoints(endpoints =>
  75. {
  76. endpoints.MapControllerRoute(
  77. name: "default",
  78. pattern: "{controller=Home}/{action=Index}/{id?}");
  79. });
  80.  
  81. app.Map("/api/discord/v6/oauth2/token", app =>
  82. {
  83.  
  84. app.RunProxy(context => context
  85. .ForwardTo("https://discordapp.com/api/v6/oauth2/token")
  86. .AddXForwardedHeaders()
  87. .Send());
  88. });
  89.  
  90. app.Map("/api/discord/v6/users/@me", app =>
  91. {
  92.  
  93. app.RunProxy(context => context
  94. .ForwardTo("https://discordapp.com/api/v6/users/@me")
  95. .AddXForwardedHeaders()
  96. .Send());
  97. });
  98.  
  99. app.Map("/api/discord/v6/guilds", app =>
  100. {
  101. app.RunProxy(context =>
  102. {
  103. string forwardTo = "https://discordapp.com/api/v6/guilds/";
  104.  
  105. if (string.IsNullOrEmpty(context.Request.Query["guild_id"]))
  106. {
  107. var response = new HttpResponseMessage(HttpStatusCode.Forbidden);
  108. return Task.FromResult(response);
  109. }
  110. else
  111. {
  112. forwardTo += context.Request.Query["guild_id"];
  113. }
  114.  
  115.  
  116. if (string.IsNullOrEmpty(context.Request.Query["members"]))
  117. {
  118. var response = new HttpResponseMessage(HttpStatusCode.Forbidden);
  119. return Task.FromResult(response);
  120. }
  121. else
  122. {
  123. forwardTo += "/members/";
  124. }
  125.  
  126. if (string.IsNullOrEmpty(context.Request.Query["member_id"]))
  127. {
  128. var response = new HttpResponseMessage(HttpStatusCode.Forbidden);
  129. return Task.FromResult(response);
  130. }
  131. else
  132. {
  133. forwardTo += context.Request.Query["member_id"];
  134. }
  135.  
  136.  
  137.  
  138. return context
  139. .ForwardTo(forwardTo)
  140. .AddXForwardedHeaders()
  141. .Send();
  142. });
  143. });
  144. }
  145. }
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement