forked from DzMahdi/SafeLink-URL-Decoder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSafelink_URL_Decoder.py
44 lines (35 loc) · 1.65 KB
/
Safelink_URL_Decoder.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#!/usr/bin/env python3
import urllib.parse
# Prompt the user to enter the SafeLink URL
safelink = input("Enter the SafeLink URL: ")
try:
# Parse the SafeLink URL to extract its components
parsed_url = urllib.parse.urlparse(safelink)
# Extract the query parameters from the SafeLink URL
query_params = urllib.parse.parse_qs(parsed_url.query)
# Check if the 'a' or 'url' parameter exists in the query parameters
if 'a' in query_params:
# Decode the 'a' parameter value
decoded_url = urllib.parse.unquote(query_params['a'][0])
elif 'url' in query_params:
# Decode the 'url' parameter value
decoded_url = urllib.parse.unquote(query_params['url'][0])
else:
raise ValueError("The provided SafeLink URL does not contain 'a' or 'url' parameter.")
# Parse the decoded URL to check for nested query parameters
nested_query_params = urllib.parse.parse_qs(urllib.parse.urlparse(decoded_url).query)
# If a nested 'u' parameter exists, decode it
if 'u' in nested_query_params:
nested_url = nested_query_params['u'][0]
final_url = urllib.parse.unquote(nested_url)
else:
# If no nested 'u' parameter, use the decoded URL as the final URL
final_url = decoded_url
# Parse the final URL to handle and remove any fragments
final_parsed_url = urllib.parse.urlparse(final_url)
final_url_without_fragment = final_parsed_url._replace(fragment='').geturl()
# Print the final URL without the fragment
print(f"Final URL: {final_url_without_fragment}")
except Exception as e:
# Print any error that occurs during the process
print(f"An error occurred: {e}")