Last active
April 25, 2019 14:31
-
-
Save kennethreitz/8952fe8043bbbe771344a831893c367f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Threading.Tasks; | |
using DNS.Client; | |
using DNS.Server; | |
namespace captivateme | |
{ | |
class DNSClientServer | |
{ | |
public const int PortDefault = 53535; | |
public const string ReferenceDNSDefault = "1.1.1.1"; | |
public const string RedirectIPDefault = "127.0.0.1"; | |
public static void Main(string[] args) | |
{ | |
MainAsync().Wait(); | |
} | |
public async static Task MainAsync() | |
{ | |
// Grab environment variables. | |
string PortEnviron = Environment.GetEnvironmentVariable("PORT"); | |
string ReferenceDNSEnviron = Environment.GetEnvironmentVariable("REFERENCE_DNS"); | |
string RedirectIPEnviron = Environment.GetEnvironmentVariable("REDIRECT_IP"); | |
// Set the PORT. | |
int Port; | |
int.TryParse(PortEnviron, out Port); | |
// Set Default port, if none was provided. | |
if (Port == 0) | |
{ | |
Port = PortDefault; | |
} | |
// Set variables for later use. | |
string ReferenceDNS = ReferenceDNSEnviron ?? ReferenceDNSDefault; | |
string RedirectIP = RedirectIPEnviron ?? RedirectIPDefault; | |
// Create Zone file and DNS server instance. | |
MasterFile masterFile = new MasterFile(); | |
DnsServer server = new DnsServer(masterFile, ReferenceDNS); | |
// Intercept IP addresses. | |
foreach (string domainPattern in new string[] { | |
// TLDs. | |
"*", | |
// Domains | |
"*.*", | |
// Subdomains. | |
"*.*.*" | |
}) | |
{ | |
// Write IP Redirect to Zone file. | |
masterFile.AddIPAddressResourceRecord(domainPattern, RedirectIP); | |
} | |
// Request hook: | |
// server.Requested += (sender, e) => Console.WriteLine("Requested: {0}", e.Request); | |
// Response hook: | |
server.Responded += (sender, e) => Console.WriteLine("Responded: {0} => {1}", e.Request, e.Response); | |
// Error hook: | |
server.Errored += (sender, e) => Console.WriteLine("Errored: {0}", e.Exception.Message); | |
// Listening (port open) hook: | |
server.Listening += (sender, e) => Console.WriteLine("Listening on port {0}:", Port); | |
// Test connection, to ensure things are working properly: | |
server.Listening += async (sender, e) => | |
{ | |
DnsClient client = new DnsClient("127.0.0.1", Port); | |
await client.Lookup("go.com"); | |
}; | |
await server.Listen(Port); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
oh out?