generated from nathanwoodburn/python-webserver-template
parent
edbfa66249
commit
003e14343b
@ -93,7 +93,7 @@ def proxy(url: str):
|
|||||||
|
|
||||||
|
|
||||||
content: requests.Response = tools.proxy(url)
|
content: requests.Response = tools.proxy(url)
|
||||||
if not content.ok:
|
if not content.status_code < 500:
|
||||||
print(content.text)
|
print(content.text)
|
||||||
return render_template("500.html"), 500
|
return render_template("500.html"), 500
|
||||||
|
|
||||||
@ -102,17 +102,17 @@ def proxy(url: str):
|
|||||||
if "text/html" in contentType:
|
if "text/html" in contentType:
|
||||||
response = make_response(tools.cleanProxyContent(content.text,url,request.host_url))
|
response = make_response(tools.cleanProxyContent(content.text,url,request.host_url))
|
||||||
response.headers["Content-Type"] = contentType
|
response.headers["Content-Type"] = contentType
|
||||||
return response
|
return response, content.status_code
|
||||||
|
|
||||||
# Clean JS
|
# Clean JS
|
||||||
if "text/javascript" in contentType or 'application/javascript' in contentType:
|
if "text/javascript" in contentType or 'application/javascript' in contentType:
|
||||||
response = make_response(tools.proxyCleanJS(content.text,url,request.host_url))
|
response = make_response(tools.proxyCleanJS(content.text,url,request.host_url))
|
||||||
response.headers["Content-Type"] = contentType
|
response.headers["Content-Type"] = contentType
|
||||||
return response
|
return response, content.status_code
|
||||||
|
|
||||||
response = make_response(content.content)
|
response = make_response(content.content)
|
||||||
response.headers["Content-Type"] = contentType
|
response.headers["Content-Type"] = contentType
|
||||||
return response
|
return response, content.status_code
|
||||||
|
|
||||||
|
|
||||||
@app.route("/<path:path>")
|
@app.route("/<path:path>")
|
||||||
|
22
tools.py
22
tools.py
@ -190,7 +190,7 @@ def curl(url: str):
|
|||||||
try:
|
try:
|
||||||
# curl --doh-url https://hnsdoh.com/dns-query {url} --insecure
|
# curl --doh-url https://hnsdoh.com/dns-query {url} --insecure
|
||||||
command = f"curl --doh-url https://hnsdoh.com/dns-query {url} --insecure --silent"
|
command = f"curl --doh-url https://hnsdoh.com/dns-query {url} --insecure --silent"
|
||||||
response = subprocess.run(command, shell=True, capture_output=True, text=True)
|
response = subprocess.run(command, shell=True, capture_output=True, text=True, timeout=10)
|
||||||
if response.returncode != 0:
|
if response.returncode != 0:
|
||||||
return {"success": False, "error": response.stderr}
|
return {"success": False, "error": response.stderr}
|
||||||
else:
|
else:
|
||||||
@ -212,10 +212,6 @@ class ProxyError(Exception):
|
|||||||
def proxy(url: str) -> requests.Response:
|
def proxy(url: str) -> requests.Response:
|
||||||
try:
|
try:
|
||||||
session = requests_doh.DNSOverHTTPSSession("HNSDoH")
|
session = requests_doh.DNSOverHTTPSSession("HNSDoH")
|
||||||
ip = socket.gethostbyname("hnsdoh.com")
|
|
||||||
print(f"Resolved IP: {ip}")
|
|
||||||
ip = requests_doh.resolve_dns("dso.dprofile")
|
|
||||||
print(f"Resolved IP: {ip}")
|
|
||||||
r = session.get(url,verify=False,timeout=30)
|
r = session.get(url,verify=False,timeout=30)
|
||||||
return r
|
return r
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
@ -253,12 +249,22 @@ def cleanProxyContent(htmlContent: str,url:str, proxyHost: str):
|
|||||||
ignored = True
|
ignored = True
|
||||||
break
|
break
|
||||||
if not ignored:
|
if not ignored:
|
||||||
link.attrs[attrib] = f"{proxyUrl}/{link[attrib]}"
|
# link.attrs[attrib] = f"{proxyUrl}/{link[attrib]}"
|
||||||
|
# Add path also
|
||||||
|
link.attrs[attrib] = f"{proxyUrl}/{urlparse(link[attrib]).path}/{link[attrib]}"
|
||||||
|
|
||||||
scripts = soup.find_all('script')
|
scripts = soup.find_all('script')
|
||||||
for script in scripts:
|
for script in scripts:
|
||||||
if len(script.text) > 0:
|
if script.has_attr("text"):
|
||||||
script.text = proxyCleanJS(script.text,url,proxyHost)
|
script.attrs["text"] = proxyCleanJS(script.text,url,proxyHost)
|
||||||
|
continue
|
||||||
|
if not script.has_attr("contents"):
|
||||||
|
continue
|
||||||
|
if len(script.contents) > 0:
|
||||||
|
newScript = soup.new_tag("script")
|
||||||
|
for content in script.contents:
|
||||||
|
newScript.append(proxyCleanJS(content,url,proxyHost))
|
||||||
|
script.replace_with(newScript)
|
||||||
|
|
||||||
return soup.prettify()
|
return soup.prettify()
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user