Last active
September 26, 2023 11:22
-
-
Save abhishekluv/8d02a4dcc112c0c17e4c86744187237e to your computer and use it in GitHub Desktop.
TLD Regex Generator for PiHole and AdGuard
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.Collections.Generic; | |
using System.IO; | |
using System.Threading.Tasks; | |
using System.Net.Http; | |
internal class Program | |
{ | |
private static async Task Main(string[] args) | |
{ | |
using (HttpClient client = new HttpClient()) | |
{ | |
var response = await client.GetAsync("https://data.iana.org/TLD/tlds-alpha-by-domain.txt"); | |
var data = await response.Content.ReadAsStreamAsync(); | |
StreamReader reader = new StreamReader(data); | |
var text = await reader.ReadLineAsync(); | |
List<string> tlds = new List<string>(); | |
while (text != null) | |
{ | |
tlds.Add(text.ToLower()); | |
text = await reader.ReadLineAsync(); | |
} | |
reader.Close(); | |
reader.Dispose(); | |
Console.WriteLine($"Total Number of TLDs: {tlds.Count}"); | |
Console.WriteLine("Removing WhiteListed TLDs..."); | |
//Use this to whitelist TLDs | |
string[] tldsToRemove = { "youtube", "us", "uk", "tv", "sbi", "org", "net", "ms", "kr", "jp", "io", "info", "in", "hk", "gov", "fr", "eu", "edu", "dk", "de", "com", "co", "ch", "ca", "biz", "azure", "aws", "au", "nz", "nl", "arpa", "dev","to","me" }; | |
foreach (var tld in tldsToRemove) | |
{ | |
tlds.Remove(tld); | |
} | |
tlds.RemoveAt(0); //Removing # Version | |
Console.WriteLine($"Total Number of TLDs after whitelisting: {tlds.Count}"); | |
while (true) | |
{ | |
Console.WriteLine("Menu:"); | |
Console.WriteLine("TLD Blocking Generator for PiHole and AdGuard Home:"); | |
Console.WriteLine("1. PiHole TLD List"); | |
Console.WriteLine("2. AdGuard TLD List"); | |
Console.WriteLine("3. Exit"); | |
Console.WriteLine("Please enter 1(PiHole) or 2(AdGuard Home): "); | |
//1. PiHole | |
//2. AdGuard home | |
int input = int.Parse(Console.ReadLine()); | |
Console.WriteLine("-----------------------------------"); | |
switch (input) | |
{ | |
case 1: | |
Console.WriteLine("Generating TLD List for PiHole..."); | |
StreamWriter writerPiHole = new StreamWriter($@"{Environment.GetFolderPath(Environment.SpecialFolder.Desktop)}\tldsForPihole.txt"); | |
foreach (var item in tlds) | |
{ | |
//using Pihole Regex Format | |
//(\.|^)ad$ | |
await writerPiHole.WriteLineAsync($"(\\.|^){item}$"); | |
} | |
writerPiHole.Close(); | |
writerPiHole.Dispose(); | |
Console.WriteLine("PiHole TLD List Generated Successfully.."); | |
Console.WriteLine($"Pihole TLD List Available at: {Environment.GetFolderPath(Environment.SpecialFolder.Desktop)}\\tldsForPihole.txt\""); | |
break; | |
case 2: | |
Console.WriteLine("Generating TLD List for AdGuard Home..."); | |
StreamWriter writerAdGuard = new StreamWriter($@"{Environment.GetFolderPath(Environment.SpecialFolder.Desktop)}\tldsForAdGuard.txt"); | |
foreach (var item in tlds) | |
{ | |
//using AdGuard Home Regex Format | |
//await writerAdGuard.WriteLineAsync($"||{item}^"); | |
await writerAdGuard.WriteLineAsync($"||*.{item}^"); | |
} | |
writerAdGuard.Close(); | |
writerAdGuard.Dispose(); | |
Console.WriteLine("AdGuard Home TLD List Generated Successfully.."); | |
Console.WriteLine($"AdGuard Home TLD List Available at: {Environment.GetFolderPath(Environment.SpecialFolder.Desktop)}\\tldsForAdGuard.txt\""); | |
break; | |
case 3: | |
Environment.Exit(0); | |
break; | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This TLD Regex Generator code is also available on .NET Fiddle.
https://dotnetfiddle.net/OaKKqD