Add browser headless mode for Linux

This commit is contained in:
ngosang 2022-09-24 16:18:36 +02:00
parent cf7e4f8749
commit 9ab7ab1371
5 changed files with 23 additions and 11 deletions

View File

@ -31,12 +31,11 @@ RUN pip install -r requirements.txt \
USER flaresolverr USER flaresolverr
COPY src . COPY src .
COPY docker/entrypoint.sh .
COPY package.json ../ COPY package.json ../
EXPOSE 8191 EXPOSE 8191
CMD ["/bin/sh", "/app/entrypoint.sh"] CMD ["/usr/local/bin/python", "-u", "/app/flaresolverr.py"]
# docker build -t flaresolverr:3.0.0.beta1 . # docker build -t flaresolverr:3.0.0.beta1 .
# docker run -p 8191:8191 flaresolverr:3.0.0.beta1 # docker run -p 8191:8191 flaresolverr:3.0.0.beta1

View File

@ -1,6 +0,0 @@
#!/bin/sh -
export DISPLAY=99
#echo "Listening on :$DISPLAY"
xvfb-run -s "-screen 0 1920x1080x24" python -u /app/flaresolverr.py

View File

@ -3,3 +3,4 @@ waitress==2.1.2
selenium==4.4.3 selenium==4.4.3
undetected-chromedriver==3.1.5.post4 undetected-chromedriver==3.1.5.post4
func-timeout==4.3.5 func-timeout==4.3.5
xvfbwrapper==0.2.9

View File

@ -63,6 +63,7 @@ if __name__ == "__main__":
# validate configuration # validate configuration
log_level = os.environ.get('LOG_LEVEL', 'info').upper() log_level = os.environ.get('LOG_LEVEL', 'info').upper()
log_html = utils.get_config_log_html() log_html = utils.get_config_log_html()
headless = utils.get_config_headless()
server_host = os.environ.get('HOST', '0.0.0.0') server_host = os.environ.get('HOST', '0.0.0.0')
server_port = int(os.environ.get('PORT', 8191)) server_port = int(os.environ.get('PORT', 8191))

View File

@ -8,12 +8,17 @@ import undetected_chromedriver as uc
FLARESOLVERR_VERSION = None FLARESOLVERR_VERSION = None
CHROME_MAJOR_VERSION = None CHROME_MAJOR_VERSION = None
USER_AGENT = None USER_AGENT = None
XVFB_DISPLAY = None
def get_config_log_html() -> bool: def get_config_log_html() -> bool:
return os.environ.get('LOG_HTML', 'false').lower() == 'true' return os.environ.get('LOG_HTML', 'false').lower() == 'true'
def get_config_headless() -> bool:
return os.environ.get('HEADLESS', 'true').lower() == 'true'
def get_flaresolverr_version() -> str: def get_flaresolverr_version() -> str:
global FLARESOLVERR_VERSION global FLARESOLVERR_VERSION
if FLARESOLVERR_VERSION is not None: if FLARESOLVERR_VERSION is not None:
@ -32,11 +37,15 @@ def get_webdriver() -> WebDriver:
options = uc.ChromeOptions() options = uc.ChromeOptions()
options.add_argument('--no-sandbox') options.add_argument('--no-sandbox')
options.add_argument('--window-size=1920,1080') options.add_argument('--window-size=1920,1080')
# todo: this param shows a warning in chrome headfull # todo: this param shows a warning in chrome head-full
options.add_argument('--disable-setuid-sandbox') options.add_argument('--disable-setuid-sandbox')
options.add_argument('--disable-dev-shm-usage') options.add_argument('--disable-dev-shm-usage')
# note: headless mode is detected
# options.headless = True # note: headless mode is detected (options.headless = True)
# we launch the browser in head-full mode with the window hidden
if get_config_headless():
if os.name != 'nt': # not in windows
start_xvfb_display()
# if we are inside the Docker container, we avoid downloading the driver # if we are inside the Docker container, we avoid downloading the driver
driver_exe_path = None driver_exe_path = None
@ -93,6 +102,14 @@ def get_user_agent(driver=None) -> str:
driver.quit() driver.quit()
def start_xvfb_display():
global XVFB_DISPLAY
if XVFB_DISPLAY is None:
from xvfbwrapper import Xvfb
XVFB_DISPLAY = Xvfb()
XVFB_DISPLAY.start()
def object_to_dict(_object): def object_to_dict(_object):
json_dict = json.loads(json.dumps(_object, default=lambda o: o.__dict__)) json_dict = json.loads(json.dumps(_object, default=lambda o: o.__dict__))
# remove hidden fields # remove hidden fields