Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to connect to Facebook through TOR #403

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions fbchat/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ def __init__(
email,
password,
user_agent=None,
use_tor=False,
max_tries=5,
session_cookies=None,
logging_level=logging.INFO,
Expand All @@ -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 <https://docs.python.org/3/library/logging.html#logging-levels>`_. Defaults to `INFO`
:type max_tries: int
:type session_cookies: dict
:type logging_level: int
:type use_tor: bool
:raises: FBchatException on failed login
"""

Expand All @@ -70,12 +73,18 @@ 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()

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",
Expand All @@ -87,6 +96,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
Expand Down
12 changes: 12 additions & 0 deletions fbchat/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from os.path import basename
import warnings
import logging
import inspect
import requests
import aenum
from .models import *
Expand Down Expand Up @@ -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
Expand Down