File size: 2,576 Bytes
7e5a1e8
02bdb59
7e5a1e8
 
 
 
 
 
02bdb59
 
7e5a1e8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9726c82
 
 
 
 
 
 
 
 
7e5a1e8
 
 
 
9726c82
 
7e5a1e8
9726c82
 
 
7e5a1e8
 
 
 
 
 
 
 
 
 
 
 
9726c82
 
7e5a1e8
319feef
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
from flask import Flask, request, jsonify
from flask_cors import CORS
import os
import ipaddress
import hashlib
import urllib.request

app = Flask(__name__)
CORS(app)  # Enable CORS for all routes


def download_file(url, local_path):
    """Download the VPN IP list if updated."""
    try:
        response = urllib.request.urlopen(url)
        new_content = response.read()

        if os.path.exists(local_path):
            with open(local_path, "rb") as local_file:
                existing_content = local_file.read()
                if hashlib.md5(existing_content).hexdigest() == hashlib.md5(new_content).hexdigest():
                    print(f"No changes in {local_path}. Using the existing file.")
                    return

        with open(local_path, "wb") as local_file:
            local_file.write(new_content)
        print(f"Updated {local_path} with the latest version.")
    except urllib.error.URLError as e:
        print(f"Failed to download {url}: {e}")


def load_ip_list(file_path):
    """Load IP lists from a given file path."""
    if os.path.exists(file_path):
        with open(file_path, "r") as file:
            return [line.strip() for line in file if line.strip()]
    return []


def is_ip_in_list(ip, ip_list):
    """Check if an IP is in a VPN CIDR block."""
    try:
        ip_obj = ipaddress.IPv4Address(ip)
        for cidr in ip_list:
            if ip_obj in ipaddress.IPv4Network(cidr, strict=False):
                return True
    except ipaddress.AddressValueError:
        return False
    return False


def get_public_ip():
    """Fetch the user's public IP from api64.ipify.org"""
    try:
        response = requests.get("https://api64.ipify.org?format=json")
        return response.json().get("ip")
    except requests.RequestException:
        return None


@app.route("/check-vpn", methods=["GET"])
def check_vpn():
    """Check if an IP is in the VPN list."""
    user_ip = request.args.get("ip")

    # If no IP is provided, fetch the public IP automatically
    if not user_ip:
        user_ip = get_public_ip()
        if not user_ip:
            return jsonify({"error": "Could not fetch public IP"}), 500

    # Ensure latest VPN list is downloaded
    download_file(VPN_LIST_URL, VPN_LIST_PATH)

    vpn_list = load_ip_list(VPN_LIST_PATH)
    manual_list = load_ip_list(MANUAL_LIST_PATH)

    is_vpn = is_ip_in_list(user_ip, vpn_list) or is_ip_in_list(user_ip, manual_list)
    
    return jsonify({"ip": user_ip, "vpn_detected": is_vpn})




if __name__ == "__main__":
    app.run(host='0.0.0.0', port=7860)