From ff5470a0a941a0b11562b21dd1db70c4b165fe92 Mon Sep 17 00:00:00 2001 From: b3nw Date: Sat, 14 Sep 2024 17:38:50 -0500 Subject: [PATCH] first commit --- README.md | 0 proton-checker.py | 60 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 README.md create mode 100644 proton-checker.py diff --git a/README.md b/README.md new file mode 100644 index 0000000..e69de29 diff --git a/proton-checker.py b/proton-checker.py new file mode 100644 index 0000000..95ca56f --- /dev/null +++ b/proton-checker.py @@ -0,0 +1,60 @@ +import requests +from collections import defaultdict +from collections import Counter +from datetime import datetime + +def get_vpn_list(): + """Fetches and parses the VPN list from the ProtonVPN API. + + Returns: + A list of dictionaries, where each dictionary represents a VPN server. + """ + + url = "https://api.protonmail.ch/vpn/logicals" + response = requests.get(url) + response.raise_for_status() # Raise an exception for bad status codes + + data = response.json() + return data["LogicalServers"] + +def group_by_exit_ip_range(servers): + """Groups servers by their /24 exit IP range and associated country. + + Args: + servers: A list of server dictionaries. + + Returns: + A dictionary where keys are /24 exit IP ranges and values are tuples of + (country_code, list of server names) within that range. + """ + + ip_range_to_servers = defaultdict(lambda: (None, [])) # Default: (None, []) + for server in servers: + for entry in server['Servers']: + exit_ip = entry['ExitIP'] + ip_range = ".".join(exit_ip.split(".")[:3]) + ".0/24" + country_code, server_names = ip_range_to_servers[ip_range] + if country_code is None: # First time seeing this IP range + country_code = server['ExitCountry'] + server_names.append(server['Name']) + ip_range_to_servers[ip_range] = (country_code, server_names) + return ip_range_to_servers + +if __name__ == "__main__": + vpn_list = get_vpn_list() + ip_range_groups = group_by_exit_ip_range(vpn_list) + + # Write to rbldnsd-compliant file + with open("/etc/rbldnsd-proton.db", "w") as f: + f.write(":127.0.0.4:Listed, see https://protonvpn.com\n") # Header + for ip_range, (country_code, server_names) in ip_range_groups.items(): + f.write(f"{ip_range} ; ProtonVPN {country_code}\n") + +# Calculate and print stats + total_ranges = len(ip_range_groups) + ranges_per_country = Counter(country_code for country_code, _ in ip_range_groups.values()) + + print(f"Total IP Ranges: {total_ranges}") + print("IP Ranges per Country:") + for country_code, count in ranges_per_country.items(): + print(f" {country_code}: {count}") \ No newline at end of file