-
Notifications
You must be signed in to change notification settings - Fork 5
/
Function.cs
250 lines (217 loc) · 9.89 KB
/
Function.cs
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
using Google.Cloud.Functions.Framework;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.WebUtilities;
using Microsoft.Extensions.Logging;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Net.Sockets;
using System.Net;
using System.Threading.Tasks;
using System;
namespace SimpleHttpFunction
{
public class Function : IHttpFunction
{
private readonly ILogger _logger;
public Function(ILogger<Function> logger) =>
_logger = logger;
public async Task HandleAsync(HttpContext context)
{
try
{
var datagram = extractDatagram(context);
var option = extractOption(context);
context.Response.ContentType = "application/dns-message";
var do53Address = pickDo53(option);
if (datagram.Result == null)
{
context.Response.StatusCode = 400;
return;
}
if (do53Address != null)
{
await sendDo53(context, datagram.Result, do53Address);
return;
}
var (dnsHost,dnsPath) = extractUrlDoH(option);
await sendDoH(context, datagram.Result, dnsHost, dnsPath);
}
catch (Exception ex)
{
_logger.LogError(ex, ex.Message + Environment.NewLine + ex.StackTrace);
}
}
private static (string dnsHost,string dnsPath) extractUrlDoH(string option)
{
if (dohProviders.ContainsKey(option))
{
option = dohProviders[option];
}
else if (option.StartsWith("nextdns"))
{
option = option.Replace("nextdns", "dns.nextdns.io");
}
else switch (option)
{
case "doh-unrestricted":
option = dohProviders["doh-"+unrestricted[rand.Next(unrestricted.Length)]];
break;
case "doh-malware":
option = dohProviders["doh-"+antiMalware[rand.Next(antiMalware.Length)]];
break;
case "doh-family":
option = dohProviders["doh-"+family[rand.Next(family.Length)]];
break;
}
var slashIndex = option.IndexOf("/");
string dnsHost, dnsPath;
if (slashIndex > 0)
{
dnsHost = option.Substring(0, slashIndex);
dnsPath = option[(slashIndex + 1)..];
}
else
{
dnsHost = option;
dnsPath= "dns-query";
}
return (dnsHost, dnsPath);
}
private static async Task sendDoH(HttpContext context, byte[] datagram, string dnsHost, string dnsPath)
{
var request = new HttpRequestMessage();
request.Content = new ByteArrayContent(datagram);
request.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/dns-message");
request.Method = HttpMethod.Post;
request.RequestUri = new Uri($"https://{dnsHost}/{dnsPath}");
request.Headers.Host = dnsHost;
//comment the line below to disable ECS using client's IP
request.Headers.Add("x-forwarded-for",context.Request.Headers["x-forwarded-for"].ToString());
using (var client = new HttpClient())
{
using (var responseMessage = await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, context.RequestAborted))
{
context.Response.StatusCode = (int)responseMessage.StatusCode;
await responseMessage.Content.CopyToAsync(context.Response.Body);
}
}
}
private async static Task sendDo53(HttpContext context, byte[] datagram, IPAddress do53Address)
{
var endpoint = new IPEndPoint(do53Address, 53);
var udpClient = new UdpClient(endpoint.AddressFamily);
udpClient.Connect(endpoint);
udpClient.Send(datagram, datagram.Length);
var result = udpClient.Receive(ref endpoint);
await context.Response.Body.WriteAsync(result, 0, result.Length);
}
private static readonly Dictionary<string, IPAddress> do53Providers = new Dictionary<string, IPAddress> ()
{
["google"] = IPAddress.Parse("8.8.8.8"),
["google6"] = IPAddress.Parse("2001:4860:4860::8888"),
["adguard"] = IPAddress.Parse("94.140.14.14"),
["adguard-family"] = IPAddress.Parse("94.140.14.15"),
["adguard-unrestricted"] = IPAddress.Parse("94.140.14.140"),
["cleanbrowsing-family"] = IPAddress.Parse("185.228.168.168"),
["cleanbrowsing-adult"] = IPAddress.Parse("185.228.168.10"),
["cleanbrowsing-security"] = IPAddress.Parse("185.228.168.9"),
["cloudflare"] = IPAddress.Parse("1.1.1.1"),
["cloudflare-malware"] = IPAddress.Parse("1.1.1.2"),
["cloudflare-adult"] = IPAddress.Parse("1.1.1.3"),
["opendns"] = IPAddress.Parse("208.67.222.222"),
["opendns-family"] = IPAddress.Parse("208.67.222.123"),
["quad9"] = IPAddress.Parse("9.9.9.9"),
["quad9-unrestricted"] = IPAddress.Parse("9.9.9.10"),
["quad9-ecs"] = IPAddress.Parse("9.9.9.11"),
};
private static readonly string[] unrestricted = new[] { "adguard-unrestricted", "cloudflare", "quad9-unrestricted", "google" };
private static readonly string[] antiMalware = new[] { "adguard", "cleanbrowsing-security", "cloudflare-malware", "quad9" };
private static readonly string[] family = new[] { "adguard-family", "cleanbrowsing-family", "cloudflare-adult", "opendns-family" };
private static readonly Dictionary<string, string> dohProviders = new Dictionary<string, string>()
{
["doh-google"] = "dns.google",
["doh-adguard"] = "dns.adguard.com",
["doh-adguard-family"] = "dns-family.adguard.com",
["doh-adguard-unrestricted"] = "dns-unfiltered.adguard.com",
["doh-cleanbrowsing-family"] = "doh.cleanbrowsing.org/doh/family-filter/",
["doh-cleanbrowsing-adult"] = "doh.cleanbrowsing.org/doh/adult-filter/",
["doh-cleanbrowsing-security"] = "doh.cleanbrowsing.org/doh/security-filter/",
["doh-cloudflare"] = "dns.cloudflare.com",
["doh-cloudflare-malware"] = "security.cloudflare-dns.com",
["doh-cloudflare-adult"] = "family.cloudflare-dns.com",
["doh-opendns"] = "doh.opendns.com",
["doh-opendns-family"] = "doh.familyshield.opendns.com",
["doh-quad9"] = "dns.quad9.net",
["doh-quad9-unrestricted"] = "dns10.quad9.net",
["doh-quad9-ecs"] = "dns11.quad9.net",
};
private static readonly Random rand = new Random();
private static IPAddress pickDo53(string option)
{
if (!option.Any())
{
return do53Providers["google"];
}
IPAddress do53Address;
if (IPAddress.TryParse(option,out do53Address))
{
return do53Address;
}
if (option.StartsWith("nextdns-"))
{
var configID = option.Replace("nextdns-", "");
return IPAddress.Parse($"2a07:a8c0::{configID[..^4]}:{configID[^4..]}");
}
if (do53Providers.ContainsKey(option))
{
return do53Providers[option];
}
switch (option)
{
case "unrestricted":
return do53Providers[unrestricted[rand.Next(unrestricted.Length)]];
case "malware":
return do53Providers[antiMalware[rand.Next(antiMalware.Length)]];
case "family":
return do53Providers[family[rand.Next(family.Length)]];
default:
return null;
}
}
private static string extractOption(HttpContext context)
{
var originPath = context.Request.Path.ToString();
var secondSlashIndex = originPath.IndexOf("/");
var option = "";
if (secondSlashIndex >= 0 && secondSlashIndex < originPath.Length - 1)
{
option = originPath[(secondSlashIndex + 1)..];
}
return option;
}
private static async Task<byte[]> extractDatagram(HttpContext context)
{
byte[] datagram;
if (context.Request.Method == "POST" && context.Request.ContentType == "application/dns-message")
{
using (var bodyStream = new MemoryStream())
{
await context.Request.Body.CopyToAsync(bodyStream);
datagram = bodyStream.ToArray();
}
}
else if (context.Request.Query["dns"].Any())
{
var base64 = context.Request.Query["dns"].ToString();
datagram = WebEncoders.Base64UrlDecode(base64);
}
else
{
datagram = null;
}
return datagram;
}
}
}