Update app.py
Browse files
app.py
CHANGED
|
@@ -6,10 +6,10 @@ from concurrent.futures import ThreadPoolExecutor
|
|
| 6 |
|
| 7 |
app = Flask(__name__)
|
| 8 |
|
| 9 |
-
#
|
| 10 |
HARDCODED_IPS = [
|
| 11 |
"108.181.33.119",
|
| 12 |
-
"108.181.34.151",
|
| 13 |
"108.181.34.157",
|
| 14 |
"108.181.90.163",
|
| 15 |
"108.181.34.177",
|
|
@@ -29,23 +29,17 @@ HARDCODED_IPS = [
|
|
| 29 |
"108.181.11.173"
|
| 30 |
]
|
| 31 |
|
| 32 |
-
|
| 33 |
-
s = s.replace('_', '/').replace('-', '+')
|
| 34 |
-
pad = len(s) % 4
|
| 35 |
-
if pad:
|
| 36 |
-
s += '=' * (4 - pad)
|
| 37 |
-
return base64.b64decode(s).decode('utf-8')
|
| 38 |
-
|
| 39 |
-
def base64_url_encode(s):
|
| 40 |
-
return base64.b64encode(s.encode('utf-8')).decode('utf-8').replace('/', '_').replace('+', '-').rstrip('=')
|
| 41 |
-
|
| 42 |
def swap_ip_in_url(template_url, new_ip):
|
|
|
|
|
|
|
|
|
|
| 43 |
try:
|
| 44 |
-
# Split URL into base and query string
|
| 45 |
base_url = template_url.split('?')[0]
|
| 46 |
-
query_string =
|
| 47 |
|
| 48 |
-
# Extract
|
| 49 |
old_ip_match = re.search(r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}', base_url)
|
| 50 |
if not old_ip_match:
|
| 51 |
return None
|
|
@@ -54,45 +48,59 @@ def swap_ip_in_url(template_url, new_ip):
|
|
| 54 |
# Replace IP in base URL
|
| 55 |
new_base_url = base_url.replace(old_ip, new_ip)
|
| 56 |
|
| 57 |
-
# Process query parameters
|
| 58 |
if not query_string:
|
| 59 |
return new_base_url
|
| 60 |
-
|
| 61 |
-
params = parse_qs(query_string, keep_blank_values=True)
|
| 62 |
-
new_params = {}
|
| 63 |
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
try:
|
| 70 |
-
#
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
# Base64 URL decode
|
| 74 |
-
safe_value = url_decoded.replace('_', '/').replace('-', '+')
|
| 75 |
-
decoded_r = base64_url_decode(safe_value)
|
| 76 |
|
| 77 |
if decoded_r:
|
| 78 |
-
# Replace IP in decoded
|
| 79 |
new_decoded_r = decoded_r.replace(old_ip, new_ip)
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
else:
|
| 84 |
-
|
| 85 |
except:
|
| 86 |
-
|
| 87 |
else:
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
|
| 92 |
-
#
|
| 93 |
-
|
| 94 |
|
| 95 |
-
return f"{new_base_url}?{
|
| 96 |
|
| 97 |
except Exception as e:
|
| 98 |
print(f"Error processing URL: {str(e)}")
|
|
@@ -100,7 +108,7 @@ def swap_ip_in_url(template_url, new_ip):
|
|
| 100 |
|
| 101 |
@app.route('/')
|
| 102 |
def home():
|
| 103 |
-
return "IP Swapper API is running. Use POST /generate with JSON payload."
|
| 104 |
|
| 105 |
@app.route('/generate', methods=['POST'])
|
| 106 |
def generate_urls():
|
|
@@ -111,14 +119,21 @@ def generate_urls():
|
|
| 111 |
|
| 112 |
template_url = data['template_url'].strip()
|
| 113 |
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
|
| 121 |
-
return jsonify({
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
|
| 123 |
if __name__ == '__main__':
|
| 124 |
-
app.run(host='0.0.0.0', port=7860, threaded=True)
|
|
|
|
| 6 |
|
| 7 |
app = Flask(__name__)
|
| 8 |
|
| 9 |
+
# Exact same IP list from your script
|
| 10 |
HARDCODED_IPS = [
|
| 11 |
"108.181.33.119",
|
| 12 |
+
"108.181.34.151",
|
| 13 |
"108.181.34.157",
|
| 14 |
"108.181.90.163",
|
| 15 |
"108.181.34.177",
|
|
|
|
| 29 |
"108.181.11.173"
|
| 30 |
]
|
| 31 |
|
| 32 |
+
# EXACT REPLICA of the swap_ip_in_url function from your script
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
def swap_ip_in_url(template_url, new_ip):
|
| 34 |
+
if not template_url or not new_ip:
|
| 35 |
+
return None
|
| 36 |
+
|
| 37 |
try:
|
| 38 |
+
# Split URL into base and query string (exact logic)
|
| 39 |
base_url = template_url.split('?')[0]
|
| 40 |
+
query_string = template_url.split('?')[1] if '?' in template_url else ""
|
| 41 |
|
| 42 |
+
# Extract old IP using regex (same as script)
|
| 43 |
old_ip_match = re.search(r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}', base_url)
|
| 44 |
if not old_ip_match:
|
| 45 |
return None
|
|
|
|
| 48 |
# Replace IP in base URL
|
| 49 |
new_base_url = base_url.replace(old_ip, new_ip)
|
| 50 |
|
|
|
|
| 51 |
if not query_string:
|
| 52 |
return new_base_url
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
+
# Process query parameters (EXACT same logic)
|
| 55 |
+
new_query_string = ""
|
| 56 |
+
params = query_string.split('&')
|
| 57 |
+
|
| 58 |
+
for param in params:
|
| 59 |
+
if '=' in param:
|
| 60 |
+
key, value = param.split('=', 1)
|
| 61 |
+
|
| 62 |
+
if key == "r":
|
| 63 |
+
# URL decode the value (exact same replacements)
|
| 64 |
+
url_decoded_value = value
|
| 65 |
+
replacements = [
|
| 66 |
+
('%2F', '/'), ('%3D', '='), ('%3F', '?'), ('%5F', '_'),
|
| 67 |
+
('%20', ' '), ('%26', '&'), ('%25', '%'), ('%2B', '+')
|
| 68 |
+
]
|
| 69 |
+
|
| 70 |
+
for old_char, new_char in replacements:
|
| 71 |
+
url_decoded_value = url_decoded_value.replace(old_char, new_char)
|
| 72 |
+
|
| 73 |
+
# Make it base64 safe (exact same logic)
|
| 74 |
+
safe_value = url_decoded_value.replace('_', '/').replace('-', '+')
|
| 75 |
+
|
| 76 |
try:
|
| 77 |
+
# Base64 decode
|
| 78 |
+
decoded_r = base64.b64decode(safe_value).decode('utf-8')
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
|
| 80 |
if decoded_r:
|
| 81 |
+
# Replace IP in decoded URL
|
| 82 |
new_decoded_r = decoded_r.replace(old_ip, new_ip)
|
| 83 |
+
|
| 84 |
+
# Base64 encode (exact same logic)
|
| 85 |
+
new_encoded = base64.b64encode(new_decoded_r.encode('utf-8')).decode('utf-8')
|
| 86 |
+
|
| 87 |
+
# Make URL safe
|
| 88 |
+
new_value = new_encoded.replace('/', '_').replace('+', '-').replace('=', '')
|
| 89 |
+
|
| 90 |
+
new_query_string += f"&{key}={new_value}"
|
| 91 |
else:
|
| 92 |
+
new_query_string += f"&{key}={value}"
|
| 93 |
except:
|
| 94 |
+
new_query_string += f"&{key}={value}"
|
| 95 |
else:
|
| 96 |
+
new_query_string += f"&{key}={value}"
|
| 97 |
+
else:
|
| 98 |
+
new_query_string += f"&{param}"
|
| 99 |
|
| 100 |
+
# Remove leading '&'
|
| 101 |
+
new_query_string = new_query_string[1:] if new_query_string.startswith('&') else new_query_string
|
| 102 |
|
| 103 |
+
return f"{new_base_url}?{new_query_string}"
|
| 104 |
|
| 105 |
except Exception as e:
|
| 106 |
print(f"Error processing URL: {str(e)}")
|
|
|
|
| 108 |
|
| 109 |
@app.route('/')
|
| 110 |
def home():
|
| 111 |
+
return "IP Swapper API is running. Use POST /generate with JSON payload containing 'template_url'."
|
| 112 |
|
| 113 |
@app.route('/generate', methods=['POST'])
|
| 114 |
def generate_urls():
|
|
|
|
| 119 |
|
| 120 |
template_url = data['template_url'].strip()
|
| 121 |
|
| 122 |
+
# Generate URLs using the exact same method as your script
|
| 123 |
+
results = []
|
| 124 |
+
for ip in HARDCODED_IPS:
|
| 125 |
+
new_url = swap_ip_in_url(template_url, ip)
|
| 126 |
+
if new_url:
|
| 127 |
+
results.append(new_url)
|
| 128 |
|
| 129 |
+
return jsonify({
|
| 130 |
+
"generated_urls": results,
|
| 131 |
+
"count": len(results)
|
| 132 |
+
})
|
| 133 |
+
|
| 134 |
+
@app.route('/health', methods=['GET'])
|
| 135 |
+
def health():
|
| 136 |
+
return jsonify({"status": "healthy", "ip_count": len(HARDCODED_IPS)})
|
| 137 |
|
| 138 |
if __name__ == '__main__':
|
| 139 |
+
app.run(host='0.0.0.0', port=7860, debug=True, threaded=True)
|