2023-07-07 01:24:52 -04:00
|
|
|
import builtins
|
|
|
|
import io
|
|
|
|
|
2023-06-18 15:31:14 -04:00
|
|
|
import requests
|
|
|
|
|
|
|
|
from modules.logging_colors import logger
|
|
|
|
|
2023-07-07 01:24:52 -04:00
|
|
|
original_open = open
|
|
|
|
original_get = requests.get
|
2024-02-25 18:06:08 -05:00
|
|
|
original_print = print
|
2023-07-07 01:24:52 -04:00
|
|
|
|
2023-06-18 15:31:14 -04:00
|
|
|
|
|
|
|
class RequestBlocker:
|
|
|
|
|
|
|
|
def __enter__(self):
|
|
|
|
requests.get = my_get
|
|
|
|
|
|
|
|
def __exit__(self, exc_type, exc_value, traceback):
|
2023-07-07 01:24:52 -04:00
|
|
|
requests.get = original_get
|
|
|
|
|
|
|
|
|
|
|
|
class OpenMonkeyPatch:
|
|
|
|
|
|
|
|
def __enter__(self):
|
|
|
|
builtins.open = my_open
|
2024-02-25 18:06:08 -05:00
|
|
|
builtins.print = my_print
|
2023-07-07 01:24:52 -04:00
|
|
|
|
|
|
|
def __exit__(self, exc_type, exc_value, traceback):
|
|
|
|
builtins.open = original_open
|
2024-02-25 18:06:08 -05:00
|
|
|
builtins.print = original_print
|
2023-06-18 15:31:14 -04:00
|
|
|
|
|
|
|
|
|
|
|
def my_get(url, **kwargs):
|
|
|
|
logger.info('Unwanted HTTP request redirected to localhost :)')
|
|
|
|
kwargs.setdefault('allow_redirects', True)
|
|
|
|
return requests.api.request('get', 'http://127.0.0.1/', **kwargs)
|
2023-07-07 01:24:52 -04:00
|
|
|
|
|
|
|
|
|
|
|
# Kindly provided by our friend WizardLM-30B
|
|
|
|
def my_open(*args, **kwargs):
|
|
|
|
filename = str(args[0])
|
|
|
|
if filename.endswith('index.html'):
|
|
|
|
with original_open(*args, **kwargs) as f:
|
|
|
|
file_contents = f.read()
|
|
|
|
|
2024-06-24 18:50:45 -04:00
|
|
|
if len(args) > 1 and args[1] == 'rb':
|
|
|
|
file_contents = file_contents.decode('utf-8')
|
|
|
|
|
2024-06-24 00:56:51 -04:00
|
|
|
file_contents = file_contents.replace('\t\t<script\n\t\t\tsrc="https://cdnjs.cloudflare.com/ajax/libs/iframe-resizer/4.3.9/iframeResizer.contentWindow.min.js"\n\t\t\tasync\n\t\t></script>', '')
|
2024-06-24 00:58:28 -04:00
|
|
|
file_contents = file_contents.replace('cdnjs.cloudflare.com', '127.0.0.1')
|
2024-04-26 09:13:11 -04:00
|
|
|
file_contents = file_contents.replace(
|
2024-06-24 00:56:51 -04:00
|
|
|
'</head>',
|
|
|
|
'\n <script src="file/js/katex/katex.min.js"></script>'
|
|
|
|
'\n <script src="file/js/katex/auto-render.min.js"></script>'
|
|
|
|
'\n <script src="file/js/highlightjs/highlight.min.js"></script>'
|
|
|
|
'\n <script src="file/js/highlightjs/highlightjs-copy.min.js"></script>'
|
|
|
|
'\n <script>hljs.addPlugin(new CopyButtonPlugin());</script>'
|
|
|
|
'\n </head>'
|
2024-04-26 09:13:11 -04:00
|
|
|
)
|
2024-03-26 15:32:20 -04:00
|
|
|
|
2024-06-24 18:50:45 -04:00
|
|
|
if len(args) > 1 and args[1] == 'rb':
|
|
|
|
file_contents = file_contents.encode('utf-8')
|
|
|
|
return io.BytesIO(file_contents)
|
|
|
|
else:
|
|
|
|
return io.StringIO(file_contents)
|
|
|
|
|
2023-07-07 01:24:52 -04:00
|
|
|
else:
|
|
|
|
return original_open(*args, **kwargs)
|
2024-02-25 18:06:08 -05:00
|
|
|
|
|
|
|
|
|
|
|
def my_print(*args, **kwargs):
|
|
|
|
if len(args) > 0 and 'To create a public link, set `share=True`' in args[0]:
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
if len(args) > 0 and 'Running on local URL' in args[0]:
|
|
|
|
args = list(args)
|
|
|
|
args[0] = f"\n{args[0].strip()}\n"
|
|
|
|
args = tuple(args)
|
|
|
|
|
|
|
|
original_print(*args, **kwargs)
|