| | from flask import Flask, request, jsonify |
| | import base64 |
| | import re |
| | from urllib.parse import urlparse, urlunparse, parse_qs, urlencode |
| | from concurrent.futures import ThreadPoolExecutor |
| |
|
| | app = Flask(__name__) |
| |
|
| | |
| | HARDCODED_IPS = [ |
| | "108.181.33.119", |
| | "108.181.34.151", |
| | "108.181.34.157", |
| | "108.181.90.163", |
| | "108.181.34.177", |
| | "208.87.241.1", |
| | "208.87.241.149", |
| | "208.87.242.125", |
| | "208.87.242.233", |
| | "108.181.11.171", |
| | "108.181.6.9", |
| | "108.181.33.135", |
| | "108.181.9.39", |
| | "108.181.11.193", |
| | "108.181.21.229", |
| | "108.181.5.31", |
| | "108.181.3.54", |
| | "108.181.5.51", |
| | "108.181.11.173" |
| | ] |
| |
|
| | |
| | def swap_ip_in_url(template_url, new_ip): |
| | if not template_url or not new_ip: |
| | return None |
| | |
| | try: |
| | |
| | base_url = template_url.split('?')[0] |
| | query_string = template_url.split('?')[1] if '?' in template_url else "" |
| | |
| | |
| | old_ip_match = re.search(r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}', base_url) |
| | if not old_ip_match: |
| | return None |
| | old_ip = old_ip_match.group(0) |
| | |
| | |
| | new_base_url = base_url.replace(old_ip, new_ip) |
| | |
| | if not query_string: |
| | return new_base_url |
| | |
| | |
| | new_query_string = "" |
| | params = query_string.split('&') |
| | |
| | for param in params: |
| | if '=' in param: |
| | key, value = param.split('=', 1) |
| | |
| | if key == "r": |
| | |
| | url_decoded_value = value |
| | replacements = [ |
| | ('%2F', '/'), ('%3D', '='), ('%3F', '?'), ('%5F', '_'), |
| | ('%20', ' '), ('%26', '&'), ('%25', '%'), ('%2B', '+') |
| | ] |
| | |
| | for old_char, new_char in replacements: |
| | url_decoded_value = url_decoded_value.replace(old_char, new_char) |
| | |
| | |
| | safe_value = url_decoded_value.replace('_', '/').replace('-', '+') |
| | |
| | try: |
| | |
| | decoded_r = base64.b64decode(safe_value).decode('utf-8') |
| | |
| | if decoded_r: |
| | |
| | new_decoded_r = decoded_r.replace(old_ip, new_ip) |
| | |
| | |
| | new_encoded = base64.b64encode(new_decoded_r.encode('utf-8')).decode('utf-8') |
| | |
| | |
| | new_value = new_encoded.replace('/', '_').replace('+', '-').replace('=', '') |
| | |
| | new_query_string += f"&{key}={new_value}" |
| | else: |
| | new_query_string += f"&{key}={value}" |
| | except: |
| | new_query_string += f"&{key}={value}" |
| | else: |
| | new_query_string += f"&{key}={value}" |
| | else: |
| | new_query_string += f"&{param}" |
| | |
| | |
| | new_query_string = new_query_string[1:] if new_query_string.startswith('&') else new_query_string |
| | |
| | return f"{new_base_url}?{new_query_string}" |
| | |
| | except Exception as e: |
| | print(f"Error processing URL: {str(e)}") |
| | return None |
| |
|
| | @app.route('/') |
| | def home(): |
| | return "IP Swapper API is running. Use POST /generate with JSON payload containing 'template_url'." |
| |
|
| | @app.route('/generate', methods=['POST']) |
| | def generate_urls(): |
| | data = request.get_json() |
| | |
| | if not data or 'template_url' not in data: |
| | return jsonify({"error": "template_url is required"}), 400 |
| | |
| | template_url = data['template_url'].strip() |
| | |
| | |
| | results = [] |
| | for ip in HARDCODED_IPS: |
| | new_url = swap_ip_in_url(template_url, ip) |
| | if new_url: |
| | results.append(new_url) |
| | |
| | return jsonify({ |
| | "generated_urls": results, |
| | "count": len(results) |
| | }) |
| |
|
| | @app.route('/health', methods=['GET']) |
| | def health(): |
| | return jsonify({"status": "healthy", "ip_count": len(HARDCODED_IPS)}) |
| |
|
| | if __name__ == '__main__': |
| | app.run(host='0.0.0.0', port=7860, debug=True, threaded=True) |
| |
|