-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathProxy.cs
More file actions
68 lines (62 loc) · 2.87 KB
/
Proxy.cs
File metadata and controls
68 lines (62 loc) · 2.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
using System;
using System.Linq;
using System.Net.Http;
using System.Text;
using RT.Json;
using RT.Servers;
using RT.Util.ExtensionMethods;
namespace KtaneWeb
{
public sealed partial class KtanePropellerModule
{
private static readonly string[] _proxyAllowedUrlPrefixes = ["https://cdn.discordapp.com/", "https://hastebin.com/raw/", "https://ktane.w00ty.com/raw/", "https://ktane.onpointcoding.net/", "https://bombs.samfun.dev/"];
private HttpResponse proxy(HttpRequest req)
{
if (req.Url.Path.Length == 0)
throw new HttpException(HttpStatusCode._404_NotFound);
var url = req.Url.Path[1..];
if (!_proxyAllowedUrlPrefixes.Any(url.StartsWith))
throw new HttpException(HttpStatusCode._403_Forbidden);
url = refreshDiscordAttachment(req.Url) ?? url;
try
{
return HttpResponse.PlainText(new HttpClient().GetAsync(url).Result.Content.ReadAsStringAsync().Result);
}
catch (Exception e)
{
var sb = new StringBuilder();
var exc = e;
while (exc != null)
{
sb.AppendLine($"{e.Message} ({e.GetType().FullName})");
sb.AppendLine(exc.StackTrace.Indent(8));
sb.AppendLine();
exc = exc.InnerException;
}
return HttpResponse.PlainText(sb.ToString(), HttpStatusCode._503_ServiceUnavailable);
}
}
private string refreshDiscordAttachment(IHttpUrl fullUrl)
{
var url = $"{fullUrl.Path[1..]}{fullUrl.QueryString}";
if (!url.StartsWith("https://cdn.discordapp.com/attachments/") || _config.DiscordBotToken == null)
return null;
var parsed = new HttpUrl("cdn.discordapp.com", url["https://cdn.discordapp.com".Length..]);
var expired = !int.TryParse(parsed.QueryValues("ex").FirstOrDefault("0"), System.Globalization.NumberStyles.HexNumber, null, out var expirationSeconds) || expirationSeconds < DateTimeOffset.UtcNow.ToUnixTimeSeconds();
if (!expired)
return url;
var client = new HttpClient();
client.DefaultRequestHeaders.Authorization = new("Bot", _config.DiscordBotToken);
var response = client
.PostAsync(
"https://discord.com/api/v10/attachments/refresh-urls",
new StringContent(
new JsonDict { ["attachment_urls"] = new JsonList { url } }.ToString(),
Encoding.UTF8,
"application/json"
)
).Result.Content.ReadAsStringAsync().Result;
return JsonDict.Parse(response)["refreshed_urls"].GetList().First()["refreshed"].GetString();
}
}
}