From 537379b670a8c5601a83c76d371dfac3c3bf741e Mon Sep 17 00:00:00 2001 From: Michael Marx Date: Fri, 1 Mar 2019 23:08:31 +0100 Subject: [PATCH 1/2] Allow to connect to Facebook through TOR --- fbchat/client.py | 10 +++++++++- fbchat/utils.py | 12 ++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/fbchat/client.py b/fbchat/client.py index 8726f683..7040306c 100644 --- a/fbchat/client.py +++ b/fbchat/client.py @@ -43,6 +43,7 @@ def __init__( email, password, user_agent=None, + use_tor=False, max_tries=5, session_cookies=None, logging_level=logging.INFO, @@ -52,12 +53,14 @@ def __init__( :param email: Facebook `email`, `id` or `phone number` :param password: Facebook account password :param user_agent: Custom user agent to use when sending requests. If `None`, user agent will be chosen from a premade list (see :any:`utils.USER_AGENTS`) + :param use_tor: Defines whether to use TOR to connect to Facebook. Requires the `requests[socks]` module. :param max_tries: Maximum number of times to try logging in :param session_cookies: Cookies from a previous session (Will default to login if these are invalid) :param logging_level: Configures the `logging level `_. Defaults to `INFO` :type max_tries: int :type session_cookies: dict :type logging_level: int + :type use_tor: bool :raises: FBchatException on failed login """ @@ -70,7 +73,7 @@ def __init__( self.client = "mercury" self.default_thread_id = None self.default_thread_type = None - self.req_url = ReqUrl() + self.req_url = ReqUrl(use_tor=use_tor) self._markAlive = True self._buddylist = dict() @@ -87,6 +90,11 @@ def __init__( handler.setLevel(logging_level) + if use_tor: + self._session.proxies = {} + self._session.proxies['http'] = 'socks5h://localhost:9050' + self._session.proxies['https'] = 'socks5h://localhost:9050' + # If session cookies aren't set, not properly loaded or gives us an invalid session, then do the login if ( not session_cookies diff --git a/fbchat/utils.py b/fbchat/utils.py index 87054426..da7431a6 100644 --- a/fbchat/utils.py +++ b/fbchat/utils.py @@ -10,6 +10,7 @@ from os.path import basename import warnings import logging +import inspect import requests import aenum from .models import * @@ -157,6 +158,17 @@ class ReqUrl(object): pull_channel = 0 + def __init__(self, use_tor=False): + super().__init__() + if use_tor: + urls = inspect.getmembers( + self, + lambda var: type(var) is str and "facebook" in var + ) + for name, url in urls: + new_url = url.replace("facebook.com", "facebookcorewwwi.onion") + setattr(self, name, new_url) + def change_pull_channel(self, channel=None): if channel is None: self.pull_channel = (self.pull_channel + 1) % 5 # Pull channel will be 0-4 From 2b57555a7e493b3a8496f39f533c87f6d6f0e97e Mon Sep 17 00:00:00 2001 From: Michael Marx Date: Sun, 17 Mar 2019 13:55:15 +0100 Subject: [PATCH 2/2] Sets the default TorBrowser user agent when 'use_tor' is True --- fbchat/client.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/fbchat/client.py b/fbchat/client.py index 7040306c..2ee9bf33 100644 --- a/fbchat/client.py +++ b/fbchat/client.py @@ -78,7 +78,13 @@ def __init__( self._buddylist = dict() if not user_agent: - user_agent = choice(USER_AGENTS) + if use_tor: + user_agent = ( + "Mozilla/5.0 (Windows NT 6.1; rv:60.0) Gecko/20100101 " + "Firefox/60.0" + ) + else: + user_agent = choice(USER_AGENTS) self._header = { "Content-Type": "application/x-www-form-urlencoded",