Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from flask import Flask, request, jsonify
|
2 |
+
import os
|
3 |
+
import ipaddress
|
4 |
+
import hashlib
|
5 |
+
import urllib.request
|
6 |
+
|
7 |
+
app = Flask(__name__)
|
8 |
+
|
9 |
+
# VPN IP list URL
|
10 |
+
VPN_LIST_URL = "https://raw.githubusercontent.com/Dan-Duran/vpn-checker/main/known-vpn/vpn-list.txt"
|
11 |
+
VPN_LIST_PATH = "known-vpn/vpn-list.txt"
|
12 |
+
MANUAL_LIST_PATH = "known-vpn/vpn-manual.txt"
|
13 |
+
|
14 |
+
# Ensure known-vpn directory exists
|
15 |
+
os.makedirs("known-vpn", exist_ok=True)
|
16 |
+
|
17 |
+
|
18 |
+
def download_file(url, local_path):
|
19 |
+
"""Download the VPN IP list if updated."""
|
20 |
+
try:
|
21 |
+
response = urllib.request.urlopen(url)
|
22 |
+
new_content = response.read()
|
23 |
+
|
24 |
+
if os.path.exists(local_path):
|
25 |
+
with open(local_path, "rb") as local_file:
|
26 |
+
existing_content = local_file.read()
|
27 |
+
if hashlib.md5(existing_content).hexdigest() == hashlib.md5(new_content).hexdigest():
|
28 |
+
print(f"No changes in {local_path}. Using the existing file.")
|
29 |
+
return
|
30 |
+
|
31 |
+
with open(local_path, "wb") as local_file:
|
32 |
+
local_file.write(new_content)
|
33 |
+
print(f"Updated {local_path} with the latest version.")
|
34 |
+
except urllib.error.URLError as e:
|
35 |
+
print(f"Failed to download {url}: {e}")
|
36 |
+
|
37 |
+
|
38 |
+
def load_ip_list(file_path):
|
39 |
+
"""Load IP lists from a given file path."""
|
40 |
+
if os.path.exists(file_path):
|
41 |
+
with open(file_path, "r") as file:
|
42 |
+
return [line.strip() for line in file if line.strip()]
|
43 |
+
return []
|
44 |
+
|
45 |
+
|
46 |
+
def is_ip_in_list(ip, ip_list):
|
47 |
+
"""Check if an IP is in a VPN CIDR block."""
|
48 |
+
try:
|
49 |
+
ip_obj = ipaddress.IPv4Address(ip)
|
50 |
+
for cidr in ip_list:
|
51 |
+
if ip_obj in ipaddress.IPv4Network(cidr, strict=False):
|
52 |
+
return True
|
53 |
+
except ipaddress.AddressValueError:
|
54 |
+
return False
|
55 |
+
return False
|
56 |
+
|
57 |
+
|
58 |
+
@app.route("/check-vpn", methods=["GET"])
|
59 |
+
def check_vpn():
|
60 |
+
"""Check if an IP is in the VPN list."""
|
61 |
+
user_ip = request.args.get("ip")
|
62 |
+
if not user_ip:
|
63 |
+
return jsonify({"error": "No IP address provided"}), 400
|
64 |
+
|
65 |
+
# Ensure latest VPN list is downloaded
|
66 |
+
download_file(VPN_LIST_URL, VPN_LIST_PATH)
|
67 |
+
|
68 |
+
vpn_list = load_ip_list(VPN_LIST_PATH)
|
69 |
+
manual_list = load_ip_list(MANUAL_LIST_PATH)
|
70 |
+
|
71 |
+
is_vpn = is_ip_in_list(user_ip, vpn_list) or is_ip_in_list(user_ip, manual_list)
|
72 |
+
|
73 |
+
return jsonify({"ip": user_ip, "vpn_detected": is_vpn})
|
74 |
+
|
75 |
+
|
76 |
+
if __name__ == "__main__":
|
77 |
+
app.run(host="0.0.0.0", port=5000, debug=True)
|