huijio commited on
Commit
7ef9917
·
verified ·
1 Parent(s): 8a72bbc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +78 -159
app.py CHANGED
@@ -3,51 +3,44 @@ 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):
@@ -55,156 +48,82 @@ def extract_and_decode_html(html_content: str) -> Union[str, List[Dict]]:
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="&quot;https://adnade.net/ptp/?user=platformsincome&amp;subid=131370&quot;"
103
- data-d="0"
104
- data-ss="[&quot;N2IyMjY5NjQyMjNhMzIzMDMxMmMyMjc1NzI2YzIyM2EyMjY4NzQ3NDcwNzMzYTVjMmY1YzJmMzEzMDM4MmUzMTM4MzEyZTMxMzEyZTMxMzczMTVjMmY1ZjVmNjM3MDMyMmU3MDY4NzAyMjJjMjI2ZTYxNmQ2NTIyM2EyMjcwNzM3OTYzNjg3YTRjNmY3MzQxNmU2NzY1NmM2NTczMzI0ZDIyN2Q=&quot;,&quot;N2IyMjY5NjQyMjNhMzEzODM1MmMyMjc1NzI2YzIyM2EyMjY4NzQ3NDcwNzMzYTVjMmY1YzJmMzkzNTJlMzIzMTM0MmUzNTMzMmUzNDM4NWMyZjVmNWY2MzcwMzIyZTcwNjg3MDIyMmMyMjZlNjE2ZDY1MjIzYTIyNmQ2NTc2NTM3MDYxNjM2NTU3NjE3MjczNjE3NzUwMjI3ZA==&quot;,...]"
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="[&quot;BASE64_STRING_1&quot;,&quot;BASE64_STRING_2&quot;,...]"
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
  )
 
3
  import json
4
  import re
5
  import html
 
6
 
7
+ def decode_proxy_entry(encoded_str):
 
8
  try:
9
+ # Step 1: Base64 decode
 
 
 
 
10
  decoded_bytes = base64.b64decode(encoded_str)
11
+
12
+ # Step 2: Convert hex to ASCII
13
  hex_string = decoded_bytes.decode('ascii')
14
  json_string = bytes.fromhex(hex_string).decode('ascii')
15
+
16
+ # Step 3: Parse JSON
17
  return json.loads(json_string)
18
  except Exception as e:
19
+ return {"error": f"Decoding failed: {str(e)}"}
20
 
21
+ def process_html_input(html_content):
 
22
  try:
23
+ # First, unescape HTML entities
24
  unescaped_html = html.unescape(html_content)
25
 
26
+ # Extract data-ss array using regex
27
+ pattern = r'data-ss="\[(.*?)\]"'
28
+ match = re.search(pattern, unescaped_html, re.DOTALL)
 
 
 
29
 
30
+ if not match:
31
+ return {"error": "No data-ss attribute found in the HTML"}
 
 
 
 
32
 
33
+ data_ss_content = match.group(1)
 
34
 
35
+ # Extract individual base64 strings
36
+ entries = re.findall(r'&quot;([A-Za-z0-9+/=]+)&quot;', data_ss_content)
 
37
 
38
+ # If no matches with &quot;, try regular quotes
39
  if not entries:
40
+ entries = re.findall(r'"([A-Za-z0-9+/=]+)"', data_ss_content)
41
+
42
+ if not entries:
43
+ return {"error": "No base64 encoded entries found in data-ss attribute"}
44
 
45
  results = []
46
  for i, entry in enumerate(entries):
 
48
  result = decode_proxy_entry(entry)
49
  results.append({
50
  "entry": i + 1,
51
+ "decoded_data": result
 
 
52
  })
53
  except Exception as e:
54
  results.append({
55
  "entry": i + 1,
56
+ "error": f"Failed to decode: {str(e)}"
 
 
57
  })
58
 
59
+ return {"results": results, "total_entries": len(results)}
60
  except Exception as e:
61
+ return {"error": f"Error processing HTML: {str(e)}"}
62
 
63
+ def decode_single_entry(encoded_string):
64
+ try:
65
+ result = decode_proxy_entry(encoded_string)
66
+ return result
67
+ except Exception as e:
68
+ return {"error": str(e)}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69
 
70
+ # Test HTML data (shortened for performance)
71
+ test_html = '''<script
72
  id="serverSelectorScript"
73
+ data-ss="[&quot;N2IyMjY5NjQyMjNhMzIzMDMxMmMyMjc1NzI2YzIyM2EyMjY4NzQ3NDcwNzMzYTVjMmY1YzJmMzEzMDM4MmUzMTM4MzEyZTMxMzEyZTMxMzczMTVjMmY1ZjVmNjM3MDMyMmU3MDY4NzAyMjJjMjI2ZTYxNmQ2NTIyM2EyMjcwNzM3OTYzNjg3YTRjNmY3MzQxNmU2NzY1NmM2NTczMzI0ZDIyN2Q=&quot;]"
 
 
74
  ></script>'''
75
 
76
+ # Create a much simpler interface
77
+ def create_simple_interface():
78
+ with gr.Blocks(title="Proxy Decoder", css=".gradio-container {max-width: 1200px !important}") as demo:
79
+ gr.Markdown("# Proxy Server Decoder")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
80
 
81
+ with gr.Tab("Single Entry"):
82
+ with gr.Row():
83
+ with gr.Column():
84
+ single_input = gr.Textbox(
85
+ label="Base64 String",
86
+ placeholder="Paste base64 string here...",
87
+ lines=3
88
+ )
89
+ single_btn = gr.Button("Decode", variant="primary")
90
+ with gr.Column():
91
+ single_output = gr.JSON(label="Result")
92
 
93
+ with gr.Tab("HTML Parser"):
94
+ with gr.Row():
95
+ with gr.Column():
96
+ html_input = gr.Textbox(
97
+ label="HTML Content",
98
+ value=test_html,
99
+ lines=10
100
+ )
101
+ html_btn = gr.Button("Parse HTML", variant="primary")
102
+ with gr.Column():
103
+ html_output = gr.JSON(label="Results")
104
 
105
+ # Connect functions
106
+ single_btn.click(
107
+ decode_single_entry,
108
+ inputs=single_input,
109
+ outputs=single_output
110
+ )
 
 
111
 
112
+ html_btn.click(
113
+ process_html_input,
114
+ inputs=html_input,
115
+ outputs=html_output
116
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
117
 
118
+ return demo
 
 
 
119
 
120
+ # Launch directly without complex setup
121
  if __name__ == "__main__":
122
+ demo = create_simple_interface()
123
  demo.launch(
124
  server_name="0.0.0.0",
125
  server_port=7860,
126
+ share=False,
127
+ inbrowser=True,
128
+ show_error=True
129
  )