-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
292 lines (229 loc) · 10.2 KB
/
main.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
import os
import json
import flask
import requests
import base64
from flask import Flask, request, send_from_directory
from urllib.parse import urlparse
from bs4 import BeautifulSoup
PORTNUMB = 80
ADDRNUMB = '127.0.0.1'
COMBINEPORTADDR = str(ADDRNUMB + ":" + str(PORTNUMB))
def clear_json(FILES:list):
for i in range(len(FILES)):
FILE = open(FILES[i], 'w')
FILE.write('')
FILE.close()
print(f"CLEARED JSON FILE : {FILE.name}")
def append_json(FILE, DICT:dict):
JSONOBJ = json.dumps(DICT, indent=4, sort_keys=True, separators=(',', ':'))
with open(FILE, '+a') as JSONFILE:
JSONFILE.write(f'{JSONOBJ}'[:-1][1:]+',')
#JSONFILE.write('}')
JSONFILE.close()
def write_favicon(data_to_write):
try:
with open('static/favicon.ico', 'wb') as favicon_file:
favicon_file.write(data_to_write.__bytes__())
except Exception as e:
print(f"Error writing favicon.ico: {e}")
def remove_occurences_in_list(LIST: list, STRING: str, WHERETOREMOVE: int):
# print("STRING = " + STRING)
COUNT = 0
for i in range(0, len(LIST) - 1):
if STRING == LIST[i]:
COUNT += 1
if COUNT == WHERETOREMOVE:
del LIST[i]
break
def remove_occurences_in_string(STRING: str, WHATTOREMOVE: str, SPLIT: str, WHERETOREMOVE: int):
LIST = STRING.split(SPLIT)
#print(LIST)
remove_occurences_in_list(LIST, WHATTOREMOVE, WHERETOREMOVE)
#print(LIST)
RETURNTHIS = reconstruct_list_to_string(LIST)
#print(RETURNTHIS)
return RETURNTHIS
def reconstruct_list_to_string(LIST):
# print("----- ----- ----- ----- -----")
TEMPSTRING = ''
for i in range(len(LIST)):
TEMPSTRING += LIST[i]
TEMPSTRING += "/"
# print(TEMPSTRING)
# print("----- ----- ----- ----- -----")
return TEMPSTRING
def remove_repeated_substring_rl(s, substring):
count = 0
countofsub = len(s)/ len(substring)
"""print(countofsub)
print(len(s)/ len(substring))
print(len(s))
print(len(substring))"""
while substring in s and count < countofsub - 1:
index = s.rfind(substring) # Find the last occurrence of the substring
s = s[:index] + s[index + len(substring):] # Remove the last occurrence
count += 1
return s
def remove_repeated_substring_lr(s, substring):
count = 0
countofsub = len(s)/ len(substring)
"""print(countofsub)
print(len(s)/ len(substring))
print(len(s))
print(len(substring))"""
while substring in s and count < countofsub - 1:
index = s.find(substring) # Find the last occurrence of the substring
s = s[:index] + s[index + len(substring):] # Remove the last occurrence
count += 1
return s
"""
# Example usage:
s = "abcabcabcabcabc"
substring = "4412"
S_WITHOUT_REPEATS = remove_and_count_repeated_substring("abcabcabcabcabc", "abc")
print("String with repeated substrings removed:", S_WITHOUT_REPEATS)
"""
def modify_html_content(content, full_url, method, url):
# print("Starting modification of HTML content...")
# print("Full URL:", full_url)
# print("Method:", method)
# print("Base URL:", url)
soup = BeautifulSoup(content)
"""if True:
# Get the Content-Type header from the response
content_type = soup.headers.get('Content-Type')
# Check if the content type indicates HTML
if content_type and 'text/html' in content_type:
print("The response contains HTML content.")
else:
print("The response does not contain HTML content.")"""
# Convert relative URLs starting with '/' to proxied URLs
for tag in soup.find_all():
for attr in tag.attrs:
if isinstance(tag[attr], str) and tag[attr].startswith('/') and not tag[attr].startswith('127.0.0.1:5000/'):
original_url = tag[attr]
if original_url.startswith("http:/"):
original_url = original_url[6:]
elif original_url.startswith("https:/"):
original_url = original_url[7:]
proxied_url = f'http://127.0.0.1:5000/{method}/{url}/{original_url}'
for X in range(len(['//','///','////'])):
proxied_url.replace(['//','///','////'][X], '/')
print('----- URLS STARTING WITH "/" -----\n')
print("URL:", url)
print("URL Split:", url.split('/'))
print("Original URL:", original_url)
print("Original URL Split:", original_url.split('/'))
print("Proxied URL:", proxied_url)
#print(proxied_url)
proxied_url = remove_repeated_substring_lr(proxied_url, '127.0.0.1:5000/')
#proxied_url = remove_repeated_substring_lr(proxied_url, f'http:/{url.split('/')[0]}/')
#proxied_url = proxied_url.replace('https://', '127.0.0.1:5000/http:/')
for i in range(5):
proxied_url = proxied_url.replace('//', '/')
#proxied_url = remove_repeated_substring_rl(proxied_url, 'http://')
print(proxied_url)
print('\n')
tag[attr] = proxied_url
# print("Current URL:", tag[attr], "\n")
#remove_occurences_in_string(tag[attr], '127.0.0.1:5000', '/', 2)
URLDICTATE = {str(tag[attr]): {
"URL": url,
"URL Split": url.split('/'),
"Original URL": original_url,
"Original URL Split": original_url.split('/'),
"Proxied URL": proxied_url,
"Proxied URL Split": proxied_url.split('/')
}
}
append_json('URLSLASH.json', URLDICTATE)
"""# Convert URLs starting with http:// or https:// to proxied URLs
for tag in soup.find_all():
for attr in tag.attrs:"""
if isinstance(tag[attr], str) and (tag[attr].startswith('http://') or tag[attr].startswith('https://')) and not tag[attr].startswith('127.0.0.1'):
original_url = tag[attr]
if original_url.startswith("http:/"):
original_url = original_url[6:]
elif original_url.startswith("https:/"):
original_url = original_url[7:]
proxied_url = f'http://127.0.0.1:5000/{method}/{url.split('/')[0]}/{original_url}'
#print('----- URLS STARTING WITH "http" -----\n')
#print("URL:", url)
#print("URL Split:", url.split('/'))
#print("Original URL:", original_url)
#print("Original URL Split:", original_url.split('/'))
#print("Proxied URL:", proxied_url)
proxied_url = remove_repeated_substring_rl(proxied_url, f'//{url.split('/')[0]}')
#proxied_url= proxied_url.replace(f'//http:/{url.split('/')[0]}/', '')
tag[attr] = proxied_url
URLDICTATE = {str(tag[attr]): {
"URL": url,
"URL Split": url.split('/'),
"Original URL": original_url,
"Original URL Split": original_url.split('/'),
"Proxied URL": proxied_url,
"Proxied URL Split": proxied_url.split('/')
}
}
append_json('URLHTTP.json', URLDICTATE)
# print("Current URL:", tag[attr], "\n")
# print("Modification completed.")
return str(soup)
app = Flask(__name__)
@app.route('/favicon.ico')
def favicon():
return send_from_directory(directory=os.path.join(app.root_path, 'static'), path='favicon.ico', max_age=0)
@app.route('/')
def root():
return open("static/Welcome.html")
@app.route('/<method>/<path:url>')
def proxy(url, method):
base_url = urlparse(request.base_url).hostname
# print("BASE URL = " + base_url)
# print(request.base_url.split('/', 600))
# print("BASE URL = " + base_url)
if "127.0.0.1" in base_url:
full_url_pre_split = f'{url}'
else:
full_url_pre_split = f'{method}//{base_url}//{url}'
full_url_split = full_url_pre_split.split('/', 500)
remove_occurences_in_list(full_url_split, url, 1)
full_url = method + "//" + reconstruct_list_to_string(full_url_split)
# print(full_url_split)
# print("FULL URL = " + full_url)
"""if request.method == 'GET':"""
if True:
try:
response = requests.request(method=request.method, url=full_url, verify=False)
if url.split('/')[0] == "127.0.0.1:5000":
favicon_url = f'{method}//' + url.split('/')[2] + "//favicon.ico"
else:
favicon_url = f'{method}//' + url.split('/')[0] + "//favicon.ico"
#print(favicon_url)
#print( url.split('/'))
favicon_load = requests.request(method='http', url=favicon_url)
# print("URL for favicon:", f'{method}//{url}/favicon.ico')
if favicon_load.status_code == 200:
# print("Favicon content received:")
write_favicon(favicon_load.content)
else:
# print("Failed to fetch favicon.ico:", favicon_load.status_code)
pass
#print(response.encoding)
if str(response.encoding) in ['UTF-8', 'utf-8', 'utf8', 'UTF8']:
modified_content = modify_html_content(response.content.decode('utf-8', errors='ignore'), full_url, method, url)
elif str(response.encoding) in ['ISO-8859-1', 'None']:
modified_content = modify_html_content(response.content.decode('ISO-8859-1', errors='ignore'), full_url, method, url)
else:
modified_content = modify_html_content(response.content, full_url, method, url)
# print(modified_content)
return modified_content
except requests.exceptions.RequestException as e:
return str(e), 500
jsonFiles = ['URLSLASH.json', 'URLHTTP.json']
if __name__ == '__main__':
os.system('cls' if os.name == 'nt' else 'clear')
clear_json(jsonFiles)
#append_json('URLSLASH.json', 'a', {'1': '2', '2': '3'})
app.run(debug=True, host='127.0.0.1', port=5000)