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__) # Exact same IP list from your script 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" ] # EXACT REPLICA of the swap_ip_in_url function from your script def swap_ip_in_url(template_url, new_ip): if not template_url or not new_ip: return None try: # Split URL into base and query string (exact logic) base_url = template_url.split('?')[0] query_string = template_url.split('?')[1] if '?' in template_url else "" # Extract old IP using regex (same as script) 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) # Replace IP in base URL new_base_url = base_url.replace(old_ip, new_ip) if not query_string: return new_base_url # Process query parameters (EXACT same logic) new_query_string = "" params = query_string.split('&') for param in params: if '=' in param: key, value = param.split('=', 1) if key == "r": # URL decode the value (exact same replacements) 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) # Make it base64 safe (exact same logic) safe_value = url_decoded_value.replace('_', '/').replace('-', '+') try: # Base64 decode decoded_r = base64.b64decode(safe_value).decode('utf-8') if decoded_r: # Replace IP in decoded URL new_decoded_r = decoded_r.replace(old_ip, new_ip) # Base64 encode (exact same logic) new_encoded = base64.b64encode(new_decoded_r.encode('utf-8')).decode('utf-8') # Make URL safe 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}" # Remove leading '&' 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() # Generate URLs using the exact same method as your script 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)