Skip to content

Commit

Permalink
feat: Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
bytexenon committed Oct 6, 2024
0 parents commit 4a38011
Show file tree
Hide file tree
Showing 11 changed files with 312 additions and 0 deletions.
11 changes: 11 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
*__pycache__*
*.pyc
*.tmp
*.log
*.o
*.so
*.vscode
*.history
*build*
*egg-info*
build
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 Bytexenon

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
51 changes: 51 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Undetected GeckoDriver v1.0.0

[![PyPI version](https://badge.fury.io/py/undetected_geckodriver.svg)](https://badge.fury.io/py/undetected_geckodriver)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)

## Overview

Undetected GeckoDriver is a wrapper around Selenium's `webdriver.Firefox` that patches the driver to bypass detection mechanisms used by various web services. This package is designed to be a drop-in replacement for Selenium's `webdriver.Firefox` and can be used in the same way.

## Installation

You can install the package via pip:

```bash
pip install undetected_geckodriver
```

## Usage

Here's a basic example of how to use Undetected GeckoDriver:

```python
from undetected_geckodriver import Firefox

driver = Firefox()
driver.get("https://www.google.com")

# Use it like a normal Selenium driver ...
```

## Requirements

- Python 3.6+
- Selenium 4.10.0+

## Contributing

Contributions are welcome! Please feel free to open an issue or submit a pull request if you encounter any problems or have any suggestions.

## License

This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.

## Author

- **ByteXenon** - [GitHub](https://github.com/ByteXenon)

## Acknowledgments

- Special thanks to the contributors of the Selenium project.
- Inspiration from the [undetected-chromedriver project](https://github.com/ultrafunkamsterdam/undetected-chromedriver).
7 changes: 7 additions & 0 deletions example/example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Imports #
from undetected_geckodriver import Firefox

# Code #
if __name__ == "__main__":
driver = Firefox()
driver.get("https://www.google.com")
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
selenium
56 changes: 56 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
"""
Undetected Geckodriver
======================
This package provides a sophisticated wrapper around the
webdriver.Firefox class from the Selenium package. It
attempts to avoid detection by web services by patching
certain parts of the Firefox browser.
Author: Bytexenon (https://github.com/Bytexenon)
"""

# Constants #
DIRNAME = os.path.dirname(__file__)
DESCRIPTION = (
"A custom Firefox WebDriver that attempts to avoid detection by web services.",
"Can bypass Cloudflare/hCaptcha/etc. detections."
)
LONG_DESC = open(os.path.join(DIRNAME, "README.md")).read()

# Imports #
import os

from setuptools import setup
from undetected_geckodriver import __version__

# Setup #
setup(
name='undetected-geckodriver',
version=__version__,
packages=['undetected_geckodriver'],
install_requires=[
"selenium>=4.10.0"
],
include_package_data=True,
description='A custom Firefox WebDriver that attempts to avoid detection by web services.',
author='ByteXenon',
author_email='[email protected]',
url='https://github.com/ByteXenon/undetected_geckodriver',
classifiers=[
'Development Status :: 3 - Alpha',
'Intended Audience :: Developers',
'Topic :: Software Development :: Testing',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: 3.7',
'Programming Language :: Python :: 3.8',
'Programming Language :: Python :: 3.9',
'Programming Language :: Python :: 3.10',
'License :: OSI Approved :: MIT License',
'Operating System :: OS Independent',
],
long_description=LONG_DESC,
long_description_content_type='text/markdown',
python_requires='>=3.6',
License='MIT',
)
16 changes: 16 additions & 0 deletions undetected_geckodriver/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""
Undetected Geckodriver
======================
This package provides a sophisticated wrapper around the
webdriver.Firefox class from the Selenium package. It
attempts to avoid detection by web services by patching
certain parts of the Firefox browser.
Author: Bytexenon (https://github.com/Bytexenon)
"""

# Version #
__version__ = "1.0.0"

# Imports #
from .driver import Firefox
9 changes: 9 additions & 0 deletions undetected_geckodriver/constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Constants #
TO_REPLACE_STRING = b'webdriver'
REPLACEMENT_STRING = b'1337river'

SELENIUM_CONFIG_PATHS = {
"linux": [
"/home/$USER/.cache/selenium/",
],
}
56 changes: 56 additions & 0 deletions undetected_geckodriver/driver.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# Imports #
import os

from selenium.webdriver.common.driver_finder import DriverFinder
from selenium.webdriver.remote.webdriver import WebDriver as RemoteWebDriver
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.firefox.remote_connection import FirefoxRemoteConnection
from selenium.webdriver.firefox.service import Service

from .mixins import WebDriverMixin
from .utils import (
_get_webdriver,
_get_firefox_path,
_get_selenium_config_path,
_create_undetected_firefox,
_patch_libxul,
)

# Main class #
class Firefox(RemoteWebDriver, WebDriverMixin):
"""
A custom Firefox WebDriver that attempts to avoid detection by web services.
"""

def __init__(
self, options: Options = None, service: Service = None, keep_alive: bool = True
) -> None:
self.webdriver = _get_webdriver()

self._firefox_path = _get_firefox_path()
self._selenium_config_path = _get_selenium_config_path()
self._undetected_path = _create_undetected_firefox(
self._firefox_path, self._selenium_config_path
)
_patch_libxul(self._undetected_path)

self.service = service if service else Service()
options = options if options else Options()
options.binary_location = os.path.join(self._undetected_path, "firefox")

finder = DriverFinder(self.service, options)

self.service.path = finder.get_driver_path()
self.service.start()

executor = FirefoxRemoteConnection(
remote_server_addr=self.service.service_url,
keep_alive=keep_alive,
ignore_proxy=options._ignore_local_proxy,
)
try:
super().__init__(command_executor=executor, options=options)
except Exception:
self.quit()
raise
self._is_remote = False
24 changes: 24 additions & 0 deletions undetected_geckodriver/mixins.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class WebDriverMixin:
def __getattr__(self, attr):
return getattr(self.webdriver, attr)

def __getitem__(self, key):
return self.webdriver[key]

def __setitem__(self, key, value):
self.webdriver[key] = value

def __delitem__(self, key):
del self.webdriver[key]

def __iter__(self):
return iter(self.webdriver)

def __len__(self):
return len(self.webdriver)

def __type__(self):
return type(self.webdriver)

def __str__(self):
return str(self.webdriver)
60 changes: 60 additions & 0 deletions undetected_geckodriver/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Imports #
import os
import sys
from selenium import webdriver

from .constants import REPLACEMENT_STRING, TO_REPLACE_STRING, SELENIUM_CONFIG_PATHS


# Functions #
def _get_webdriver() -> webdriver.Firefox:
return webdriver.Firefox.__new__(webdriver.Firefox)


def _get_firefox_path() -> str:
firefox_path = "/usr/lib/firefox"
if not os.path.exists(firefox_path):
raise FileNotFoundError("Could not find the Firefox path")
return firefox_path


def _get_selenium_config_path() -> str:
os_type = sys.platform
if os_type not in SELENIUM_CONFIG_PATHS:
raise FileNotFoundError(f"Unsupported platform: {os_type}")

for directory in SELENIUM_CONFIG_PATHS[os_type]:
directory = directory.replace("$USER", os.getlogin())
if os.path.exists(directory):
return directory

raise FileNotFoundError("Could not find the Selenium config path")


def _create_undetected_firefox(firefox_path: str, selenium_config_path: str) -> str:
undetected_path = os.path.join(selenium_config_path, "undetected_firefox")
#if os.path.exists(undetected_path):
# os.system(f"rm -rf {undetected_path}")

if not os.path.exists(undetected_path):
os.makedirs(undetected_path, exist_ok=True)
os.system(f"cp -r {firefox_path}/* {undetected_path}")
return undetected_path


def _patch_libxul(undetected_path: str) -> None:
libxul_path = os.path.join(undetected_path, "libxul.so")
if not os.path.exists(libxul_path):
raise FileNotFoundError("Could not find libxul.so (What the hell?!)")

if len(REPLACEMENT_STRING) != len(TO_REPLACE_STRING):
raise ValueError(
"The length of REPLACEMENT_STRING must be equal to the length of TO_REPLACE_STRING"
)

libxul_data = None
with open(libxul_path, "rb") as file:
libxul_data = file.read()
libxul_data = libxul_data.replace(TO_REPLACE_STRING, REPLACEMENT_STRING)
with open(libxul_path, "wb") as file:
file.write(libxul_data)

0 comments on commit 4a38011

Please sign in to comment.