Block a cloudfare request
This commit is contained in:
parent
b67c362735
commit
9aee1064a3
2 changed files with 36 additions and 7 deletions
|
@ -1,19 +1,47 @@
|
||||||
|
import builtins
|
||||||
|
import io
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
|
|
||||||
from modules.logging_colors import logger
|
from modules.logging_colors import logger
|
||||||
|
|
||||||
|
original_open = open
|
||||||
|
original_get = requests.get
|
||||||
|
|
||||||
|
|
||||||
class RequestBlocker:
|
class RequestBlocker:
|
||||||
|
|
||||||
def __enter__(self):
|
def __enter__(self):
|
||||||
self.original_get = requests.get
|
|
||||||
requests.get = my_get
|
requests.get = my_get
|
||||||
|
|
||||||
def __exit__(self, exc_type, exc_value, traceback):
|
def __exit__(self, exc_type, exc_value, traceback):
|
||||||
requests.get = self.original_get
|
requests.get = original_get
|
||||||
|
|
||||||
|
|
||||||
|
class OpenMonkeyPatch:
|
||||||
|
|
||||||
|
def __enter__(self):
|
||||||
|
builtins.open = my_open
|
||||||
|
|
||||||
|
def __exit__(self, exc_type, exc_value, traceback):
|
||||||
|
builtins.open = original_open
|
||||||
|
|
||||||
|
|
||||||
def my_get(url, **kwargs):
|
def my_get(url, **kwargs):
|
||||||
logger.info('Unwanted HTTP request redirected to localhost :)')
|
logger.info('Unwanted HTTP request redirected to localhost :)')
|
||||||
kwargs.setdefault('allow_redirects', True)
|
kwargs.setdefault('allow_redirects', True)
|
||||||
return requests.api.request('get', 'http://127.0.0.1/', **kwargs)
|
return requests.api.request('get', 'http://127.0.0.1/', **kwargs)
|
||||||
|
|
||||||
|
|
||||||
|
# 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()
|
||||||
|
|
||||||
|
file_contents = file_contents.replace(b'<script src="https://cdnjs.cloudflare.com/ajax/libs/iframe-resizer/4.3.1/iframeResizer.contentWindow.min.js"></script>', b'')
|
||||||
|
file_contents = file_contents.replace(b'cdnjs.cloudflare.com', b'127.0.0.1')
|
||||||
|
return io.BytesIO(file_contents)
|
||||||
|
else:
|
||||||
|
return original_open(*args, **kwargs)
|
||||||
|
|
|
@ -2,7 +2,7 @@ import os
|
||||||
import warnings
|
import warnings
|
||||||
|
|
||||||
from modules.logging_colors import logger
|
from modules.logging_colors import logger
|
||||||
from modules.block_requests import RequestBlocker
|
from modules.block_requests import OpenMonkeyPatch, RequestBlocker
|
||||||
|
|
||||||
os.environ['GRADIO_ANALYTICS_ENABLED'] = 'False'
|
os.environ['GRADIO_ANALYTICS_ENABLED'] = 'False'
|
||||||
os.environ['BITSANDBYTES_NOWELCOME'] = '1'
|
os.environ['BITSANDBYTES_NOWELCOME'] = '1'
|
||||||
|
@ -1068,6 +1068,7 @@ def create_interface():
|
||||||
|
|
||||||
# Launch the interface
|
# Launch the interface
|
||||||
shared.gradio['interface'].queue()
|
shared.gradio['interface'].queue()
|
||||||
|
with OpenMonkeyPatch():
|
||||||
if shared.args.listen:
|
if shared.args.listen:
|
||||||
shared.gradio['interface'].launch(prevent_thread_lock=True, share=shared.args.share, server_name=shared.args.listen_host or '0.0.0.0', server_port=shared.args.listen_port, inbrowser=shared.args.auto_launch, auth=auth)
|
shared.gradio['interface'].launch(prevent_thread_lock=True, share=shared.args.share, server_name=shared.args.listen_host or '0.0.0.0', server_port=shared.args.listen_port, inbrowser=shared.args.auto_launch, auth=auth)
|
||||||
else:
|
else:
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue