My-Free-VPN / app.py
Akwbw's picture
Update app.py
20d9a66 verified
from flask import Flask, render_template_string, jsonify, Response, request
from flask_cors import CORS
import requests
import base64
import csv
app = Flask(__name__)
CORS(app) # Yeh zaroori hai taake Mobile App API ko access kar sake
# --- Data Fetching Logic ---
def get_vpn_data():
VPNGATE_API_URL = "http://www.vpngate.net/api/iphone/"
servers = []
try:
headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64)"}
response = requests.get(VPNGATE_API_URL, headers=headers, timeout=6)
text_data = response.text
lines = [line for line in text_data.split('\n') if not line.startswith('*')]
reader = csv.DictReader(lines)
for row in reader:
if row and 'OpenVPN_ConfigData_Base64' in row:
try:
speed_mbps = round(int(row.get('Speed', 0)) / 1000000, 2)
# Sirf wo servers jo 1mbps se tez hon
if speed_mbps > 1:
servers.append({
'ip': row['IP'],
'country': row['CountryLong'],
'speed': speed_mbps,
'ping': row['Ping'],
'base64_config': row['OpenVPN_ConfigData_Base64']
})
except:
pass
return servers[:40] # Top 40 servers
except Exception as e:
print(f"Error: {e}")
return []
# --- 1. API Route (Mobile App ke liye) ---
@app.route('/api/servers', methods=['GET'])
def api_servers():
servers = get_vpn_data()
return jsonify({
"status": "success",
"message": "Data fetched successfully",
"total_servers": len(servers),
"data": servers
})
# --- 2. Web UI Route (Browser ke liye) ---
HTML_TEMPLATE = """
<!DOCTYPE html>
<html>
<head>
<title>VPN Master Hub</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body { font-family: 'Segoe UI', sans-serif; background: #1a1a1a; color: white; padding: 20px; text-align: center; }
.card { background: #2d2d2d; padding: 15px; margin: 10px auto; max-width: 500px; border-radius: 10px; display: flex; justify-content: space-between; align-items: center; box-shadow: 0 4px 6px rgba(0,0,0,0.3); }
.btn { background: #00d2ff; color: #000; padding: 10px 20px; text-decoration: none; border-radius: 20px; font-weight: bold; }
.flag { font-size: 24px; margin-right: 10px; }
</style>
</head>
<body>
<h1>🔥 VPN Master Hub</h1>
<p>Mobile App API Endpoint: <code style="background:#333; padding:5px;">/api/servers</code></p>
{% for server in servers %}
<div class="card">
<div style="text-align:left;">
<span class="flag">🌍</span> <strong>{{ server.country }}</strong><br>
<small style="color:#aaa;">IP: {{ server.ip }} | Speed: {{ server.speed }} Mbps</small>
</div>
<a href="/download/{{ server.ip }}/{{ server.base64_config }}" class="btn">Connect</a>
</div>
{% endfor %}
</body>
</html>
"""
@app.route('/')
def web_ui():
servers = get_vpn_data()
return render_template_string(HTML_TEMPLATE, servers=servers)
# --- File Download Route ---
@app.route('/download/<ip>/<config_base64>')
def download_file(ip, config_base64):
try:
decoded = base64.b64decode(config_base64)
return Response(decoded, mimetype="application/x-openvpn-profile",
headers={"Content-disposition": f"attachment; filename=VPN_{ip}.ovpn"})
except:
return "Error downloading file"
if __name__ == '__main__':
app.run(host='0.0.0.0', port=7860)