urlapi2 / app.py
huijio's picture
Update app.py
109c5a8 verified
from flask import Flask, request, jsonify
import base64
import re
from urllib.parse import unquote
app = Flask(__name__)
# High Performing IPs Only (Credit > 0.00)
HARDCODED_IPS = [
# 🥇 EXCELLENT PERFORMERS (0.35+ credits)
"108.181.32.57", # URL #75: 0.570 credits ⭐
"108.181.32.71", # URL #89: 0.420 credits ⭐
"185.16.39.166", # URL #74: 0.350 credits ⭐
# 🥈 GREAT PERFORMERS (0.25-0.34 credits)
"108.181.32.67", # URL #85: 0.330 credits
"108.181.34.42", # URL #60: 0.320 credits
"108.181.34.69", # URL #87: 0.320 credits
"108.181.32.61", # URL #79: 0.290 credits
"108.181.32.64", # URL #82: 0.250 credits
"108.181.32.65", # URL #83: 0.240 credits
"185.16.39.164", # URL #72: 0.240 credits
# 🥉 GOOD PERFORMERS (0.20-0.24 credits)
"108.181.33.119", # New IP - GOOD PERFORMER
"108.181.34.151", # New IP - GOOD PERFORMER
"108.181.34.157", # New IP - GOOD PERFORMER
"108.181.90.163", # New IP - GOOD PERFORMER
"108.181.34.177", # New IP - GOOD PERFORMER
"208.87.241.1", # New IP - GOOD PERFORMER
"208.87.241.149", # New IP - GOOD PERFORMER
"208.87.242.125", # New IP - GOOD PERFORMER
"208.87.242.233", # New IP - GOOD PERFORMER
"108.181.11.171", # New IP - GOOD PERFORMER
"108.181.6.9", # New IP - GOOD PERFORMER
"108.181.33.135", # New IP - GOOD PERFORMER
"108.181.9.39", # New IP - GOOD PERFORMER
"108.181.11.193", # New IP - GOOD PERFORMER
"108.181.21.229", # New IP - GOOD PERFORMER
"108.181.5.31", # New IP - GOOD PERFORMER
"108.181.3.54", # New IP - GOOD PERFORMER
"108.181.5.51", # New IP - GOOD PERFORMER
"108.181.11.173", # New IP - GOOD PERFORMER
]
def swap_ip_in_url_working(template_url, new_ip):
"""
Working version that uses standard base64 with padding for r parameter
"""
if not template_url or not new_ip:
return None
try:
# Extract old IP from template
old_ip_match = re.search(r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}', template_url)
if not old_ip_match:
return None
old_ip = old_ip_match.group(0)
# Replace IP in main URL
new_url = template_url.replace(old_ip, new_ip)
# Handle r parameter with proper base64 encoding
r_match = re.search(r'[&?]r=([^&]+)', new_url)
if r_match:
original_r = r_match.group(1)
# URL decode the original r parameter
url_decoded_r = unquote(original_r)
# Add padding if needed and decode base64
padding = 4 - len(url_decoded_r) % 4
if padding != 4:
url_decoded_r += '=' * padding
# Convert to standard base64 and decode
base64_fixed = url_decoded_r.replace('_', '/').replace('-', '+')
decoded_r = base64.b64decode(base64_fixed).decode('utf-8')
# Replace IP in decoded URL
new_decoded_r = decoded_r.replace(old_ip, new_ip)
# Re-encode with STANDARD base64 (not URL-safe) and keep padding
new_encoded_r = base64.b64encode(new_decoded_r.encode()).decode()
# Replace in the URL - NO URL encoding of the base64 string
new_url = new_url.replace(f"r={original_r}", f"r={new_encoded_r}")
return new_url
except Exception as e:
print(f"Error processing URL for IP {new_ip}: {str(e)}")
return None
@app.route('/')
def home():
return """
<h1>IP Swapper API</h1>
<p>Use POST /generate with JSON payload containing 'template_url'</p>
<p>Example:</p>
<pre>
curl -X POST https://your-app.hf.space/generate \\
-H "Content-Type: application/json" \\
-d '{"template_url": "https://108.181.8.179/__cpi.php?s=...&r=...&__cpo=1"}'
</pre>
"""
@app.route('/generate', methods=['POST'])
def generate_urls():
"""
Generate URLs for all IPs in the hardcoded list
"""
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()
# Use custom IP list if provided, otherwise use hardcoded IPs
ip_list = data.get('ip_list', HARDCODED_IPS)
# Generate URLs using the working method
results = []
for ip in ip_list:
new_url = swap_ip_in_url_working(template_url, ip)
if new_url:
results.append(new_url)
return jsonify({
"generated_urls": results,
"count": len(results),
"method": "standard_base64_with_padding"
})
@app.route('/generate_single', methods=['POST'])
def generate_single():
"""
Generate URL for a specific IP
"""
data = request.get_json()
if not data or 'template_url' not in data or 'ip' not in data:
return jsonify({"error": "template_url and ip are required"}), 400
template_url = data['template_url'].strip()
ip = data['ip'].strip()
new_url = swap_ip_in_url_working(template_url, ip)
if new_url:
return jsonify({
"original_url": template_url,
"generated_url": new_url,
"ip": ip,
"success": True
})
else:
return jsonify({
"error": "Failed to generate URL",
"ip": ip,
"success": False
}), 400
@app.route('/test_r_param', methods=['POST'])
def test_r_param():
"""
Test if the r parameter decodes correctly for a generated URL
"""
data = request.get_json()
if not data or 'url' not in data:
return jsonify({"error": "url is required"}), 400
url = data['url'].strip()
# Extract r parameter
r_match = re.search(r'[&?]r=([^&]+)', url)
if not r_match:
return jsonify({"error": "No r parameter found in URL"}), 400
r_value = r_match.group(1)
try:
# Try to decode the r parameter
decoded = base64.b64decode(r_value).decode('utf-8')
return jsonify({
"r_parameter": r_value,
"decoded_successfully": True,
"decoded_value": decoded,
"contains_corruption": "\\" in decoded or "%" in decoded
})
except Exception as e:
return jsonify({
"r_parameter": r_value,
"decoded_successfully": False,
"error": str(e)
})
@app.route('/health', methods=['GET'])
def health():
return jsonify({
"status": "healthy",
"ip_count": len(HARDCODED_IPS),
"method": "standard_base64_with_padding"
})
@app.route('/ips', methods=['GET'])
def list_ips():
"""
List all available IPs
"""
return jsonify({
"ip_count": len(HARDCODED_IPS),
"ips": HARDCODED_IPS
})
if __name__ == '__main__':
app.run(host='0.0.0.0', port=7860, debug=True)