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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import base64
|
| 3 |
+
import json
|
| 4 |
+
import re
|
| 5 |
+
import html
|
| 6 |
+
from typing import Dict, List, Union, Any
|
| 7 |
+
|
| 8 |
+
def decode_proxy_entry(encoded_str: str) -> Dict[str, Any]:
|
| 9 |
+
"""Decode a single proxy entry with better error handling"""
|
| 10 |
+
try:
|
| 11 |
+
# Remove padding if needed and decode
|
| 12 |
+
padding = len(encoded_str) % 4
|
| 13 |
+
if padding:
|
| 14 |
+
encoded_str += '=' * (4 - padding)
|
| 15 |
+
|
| 16 |
+
decoded_bytes = base64.b64decode(encoded_str)
|
| 17 |
+
hex_string = decoded_bytes.decode('ascii')
|
| 18 |
+
json_string = bytes.fromhex(hex_string).decode('ascii')
|
| 19 |
+
return json.loads(json_string)
|
| 20 |
+
except Exception as e:
|
| 21 |
+
return {"error": f"Decoding failed: {str(e)}", "input_length": len(encoded_str)}
|
| 22 |
+
|
| 23 |
+
def extract_and_decode_html(html_content: str) -> Union[str, List[Dict]]:
|
| 24 |
+
"""Extract and decode all entries from HTML with improved parsing"""
|
| 25 |
+
try:
|
| 26 |
+
unescaped_html = html.unescape(html_content)
|
| 27 |
+
|
| 28 |
+
# Multiple patterns to catch different HTML formats
|
| 29 |
+
patterns = [
|
| 30 |
+
r'data-ss="\[(.*?)\]"',
|
| 31 |
+
r"data-ss='\[(.*?)\]'",
|
| 32 |
+
r'data-ss=\s*\[\s*(.*?)\s*\]'
|
| 33 |
+
]
|
| 34 |
+
|
| 35 |
+
data_ss_content = None
|
| 36 |
+
for pattern in patterns:
|
| 37 |
+
match = re.search(pattern, unescaped_html, re.DOTALL)
|
| 38 |
+
if match:
|
| 39 |
+
data_ss_content = match.group(1)
|
| 40 |
+
break
|
| 41 |
+
|
| 42 |
+
if not data_ss_content:
|
| 43 |
+
return "No data-ss attribute found in the HTML"
|
| 44 |
+
|
| 45 |
+
# Improved regex for base64 strings
|
| 46 |
+
base64_pattern = r'["\']([A-Za-z0-9+/=]+)["\']'
|
| 47 |
+
entries = re.findall(base64_pattern, data_ss_content)
|
| 48 |
+
|
| 49 |
+
if not entries:
|
| 50 |
+
return "No base64 encoded entries found in data-ss attribute"
|
| 51 |
+
|
| 52 |
+
results = []
|
| 53 |
+
for i, entry in enumerate(entries):
|
| 54 |
+
try:
|
| 55 |
+
result = decode_proxy_entry(entry)
|
| 56 |
+
results.append({
|
| 57 |
+
"entry": i + 1,
|
| 58 |
+
"encoded_string": entry[:30] + "..." + entry[-10:] if len(entry) > 50 else entry,
|
| 59 |
+
"decoded_data": result,
|
| 60 |
+
"status": "success"
|
| 61 |
+
})
|
| 62 |
+
except Exception as e:
|
| 63 |
+
results.append({
|
| 64 |
+
"entry": i + 1,
|
| 65 |
+
"encoded_string": entry[:30] + "..." + entry[-10:] if len(entry) > 50 else entry,
|
| 66 |
+
"error": f"Failed to decode: {str(e)}",
|
| 67 |
+
"status": "error"
|
| 68 |
+
})
|
| 69 |
+
|
| 70 |
+
return results
|
| 71 |
+
except Exception as e:
|
| 72 |
+
return f"Error processing HTML: {str(e)}"
|
| 73 |
+
|
| 74 |
+
def process_html_input(html_content: str) -> Dict[str, Any]:
|
| 75 |
+
"""Process HTML input with statistics"""
|
| 76 |
+
results = extract_and_decode_html(html_content)
|
| 77 |
+
|
| 78 |
+
if isinstance(results, str):
|
| 79 |
+
return {"error": results}
|
| 80 |
+
|
| 81 |
+
# Add statistics
|
| 82 |
+
success_count = sum(1 for r in results if r.get('status') == 'success')
|
| 83 |
+
error_count = sum(1 for r in results if r.get('status') == 'error')
|
| 84 |
+
|
| 85 |
+
formatted_results = {
|
| 86 |
+
"statistics": {
|
| 87 |
+
"total_entries": len(results),
|
| 88 |
+
"successful_decodes": success_count,
|
| 89 |
+
"failed_decodes": error_count
|
| 90 |
+
},
|
| 91 |
+
"entries": {}
|
| 92 |
+
}
|
| 93 |
+
|
| 94 |
+
for result in results:
|
| 95 |
+
formatted_results["entries"][f"Entry {result['entry']}"] = result
|
| 96 |
+
|
| 97 |
+
return formatted_results
|
| 98 |
+
|
| 99 |
+
# Your HTML data remains the same
|
| 100 |
+
your_html_data = '''<script
|
| 101 |
+
id="serverSelectorScript"
|
| 102 |
+
data-u=""https://adnade.net/ptp/?user=platformsincome&subid=131370""
|
| 103 |
+
data-d="0"
|
| 104 |
+
data-ss="["N2IyMjY5NjQyMjNhMzIzMDMxMmMyMjc1NzI2YzIyM2EyMjY4NzQ3NDcwNzMzYTVjMmY1YzJmMzEzMDM4MmUzMTM4MzEyZTMxMzEyZTMxMzczMTVjMmY1ZjVmNjM3MDMyMmU3MDY4NzAyMjJjMjI2ZTYxNmQ2NTIyM2EyMjcwNzM3OTYzNjg3YTRjNmY3MzQxNmU2NzY1NmM2NTczMzI0ZDIyN2Q=","N2IyMjY5NjQyMjNhMzEzODM1MmMyMjc1NzI2YzIyM2EyMjY4NzQ3NDcwNzMzYTVjMmY1YzJmMzkzNTJlMzIzMTM0MmUzNTMzMmUzNDM4NWMyZjVmNWY2MzcwMzIyZTcwNjg3MDIyMmMyMjZlNjE2ZDY1MjIzYTIyNmQ2NTc2NTM3MDYxNjM2NTU3NjE3MjczNjE3NzUwMjI3ZA==",...]"
|
| 105 |
+
></script>'''
|
| 106 |
+
|
| 107 |
+
# Enhanced Gradio interface
|
| 108 |
+
with gr.Blocks(title="Proxy Server Decoder", theme=gr.themes.Soft()) as demo:
|
| 109 |
+
gr.Markdown("""
|
| 110 |
+
# π Proxy Server Decoder
|
| 111 |
+
Decode base64-encoded proxy server information from HTML script tags.
|
| 112 |
+
""")
|
| 113 |
+
|
| 114 |
+
with gr.Tab("π§© Single Entry Decoder"):
|
| 115 |
+
gr.Markdown("Decode a single base64 encoded proxy entry")
|
| 116 |
+
with gr.Row():
|
| 117 |
+
with gr.Column():
|
| 118 |
+
single_input = gr.Textbox(
|
| 119 |
+
label="Base64 Encoded String",
|
| 120 |
+
placeholder="Paste your base64 string here...",
|
| 121 |
+
lines=3,
|
| 122 |
+
max_lines=5
|
| 123 |
+
)
|
| 124 |
+
single_decode_btn = gr.Button("π Decode Entry", variant="primary")
|
| 125 |
+
with gr.Column():
|
| 126 |
+
single_output = gr.JSON(label="Decoded Result")
|
| 127 |
+
|
| 128 |
+
with gr.Tab("π HTML Parser"):
|
| 129 |
+
gr.Markdown("Parse HTML and extract all proxy entries from data-ss attribute")
|
| 130 |
+
with gr.Row():
|
| 131 |
+
with gr.Column():
|
| 132 |
+
html_input = gr.Textbox(
|
| 133 |
+
label="HTML Script Tag",
|
| 134 |
+
value=your_html_data,
|
| 135 |
+
lines=15,
|
| 136 |
+
max_lines=20
|
| 137 |
+
)
|
| 138 |
+
html_parse_btn = gr.Button("π Parse HTML and Decode All Entries", variant="primary")
|
| 139 |
+
with gr.Column():
|
| 140 |
+
html_output = gr.JSON(label="Decoded Results")
|
| 141 |
+
|
| 142 |
+
with gr.Tab("π§ͺ Test Your Data"):
|
| 143 |
+
gr.Markdown("Test the decoder with the provided sample data")
|
| 144 |
+
with gr.Row():
|
| 145 |
+
test_btn = gr.Button("π§ͺ Test with Provided HTML Data", variant="secondary")
|
| 146 |
+
clear_btn = gr.Button("ποΈ Clear Results")
|
| 147 |
+
test_output = gr.JSON(label="Test Results")
|
| 148 |
+
|
| 149 |
+
with gr.Tab("π About"):
|
| 150 |
+
gr.Markdown("""
|
| 151 |
+
## About This Tool
|
| 152 |
+
|
| 153 |
+
This decoder processes proxy server information encoded in HTML script tags.
|
| 154 |
+
|
| 155 |
+
### The Decoding Process:
|
| 156 |
+
1. **Base64 Decode** - Convert the base64 string to bytes
|
| 157 |
+
2. **Hex to ASCII** - Convert hex-encoded bytes to ASCII text
|
| 158 |
+
3. **JSON Parse** - Parse the resulting JSON object
|
| 159 |
+
|
| 160 |
+
### Input Format:
|
| 161 |
+
The tool expects HTML like:
|
| 162 |
+
```html
|
| 163 |
+
<script
|
| 164 |
+
id="serverSelectorScript"
|
| 165 |
+
data-ss="["BASE64_STRING_1","BASE64_STRING_2",...]"
|
| 166 |
+
></script>
|
| 167 |
+
```
|
| 168 |
+
|
| 169 |
+
### Features:
|
| 170 |
+
- Single entry decoding
|
| 171 |
+
- Bulk HTML parsing
|
| 172 |
+
- Error handling and statistics
|
| 173 |
+
- Clean, user-friendly interface
|
| 174 |
+
""")
|
| 175 |
+
|
| 176 |
+
# Event handlers
|
| 177 |
+
single_decode_btn.click(
|
| 178 |
+
fn=decode_proxy_entry,
|
| 179 |
+
inputs=single_input,
|
| 180 |
+
outputs=single_output
|
| 181 |
+
)
|
| 182 |
+
|
| 183 |
+
html_parse_btn.click(
|
| 184 |
+
fn=process_html_input,
|
| 185 |
+
inputs=html_input,
|
| 186 |
+
outputs=html_output
|
| 187 |
+
)
|
| 188 |
+
|
| 189 |
+
def test_with_provided_data():
|
| 190 |
+
return process_html_input(your_html_data)
|
| 191 |
+
|
| 192 |
+
test_btn.click(
|
| 193 |
+
fn=test_with_provided_data,
|
| 194 |
+
outputs=test_output
|
| 195 |
+
)
|
| 196 |
+
|
| 197 |
+
def clear_all():
|
| 198 |
+
return None, None, None
|
| 199 |
+
|
| 200 |
+
clear_btn.click(
|
| 201 |
+
fn=clear_all,
|
| 202 |
+
outputs=[single_output, html_output, test_output]
|
| 203 |
+
)
|
| 204 |
+
|
| 205 |
+
if __name__ == "__main__":
|
| 206 |
+
demo.launch(
|
| 207 |
+
server_name="0.0.0.0",
|
| 208 |
+
server_port=7860,
|
| 209 |
+
share=False
|
| 210 |
+
)
|