Search code examples
pythonpython-3.xtorddp

Using Tor and Meteor DDP


I am trying to use the a meteor ddp client to use the data from a meteor app in my python script. IT is a script that uses the Tor proxy API called stem. This is how my tor communicator looks like which works if ran separately:

Tor communicator (taken from the tor tutorial page with minor alterations):

import socket
import socks
import stem.process
import requests
from stem.util import term
from requestData import requestData

SOCKS_PORT = 7000

# Set socks proxy and wrap the urllib module

socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, '127.0.0.1', SOCKS_PORT)
socket.socket = socks.socksocket

# Perform DNS resolution through the socket

def getaddrinfo(*args):
    return [(socket.AF_INET, socket.SOCK_STREAM, 6, '', (args[0], args[1]))]

socket.getaddrinfo = getaddrinfo

def query(itemId):
    """
    Uses requests to fetch a site using SocksiPy for Tor over the SOCKS_PORT.
    """

    try:
        return requestData(itemId)
    except:
        return "Unable to get data"

# Start an instance of Tor configured to only exit through Russia. This prints
# Tor's bootstrap information as it starts. Note that this likely will not
# work if you have another Tor instance running.

def print_bootstrap_lines(line):
    if "Bootstrapped " in line:
        print(line)


print(term.format("Starting Tor:\n", term.Attr.BOLD))

tor_process = stem.process.launch_tor_with_config(
  config = {
    'SocksPort': str(SOCKS_PORT),
    'ExitNodes': '{ru}',
  },
  init_msg_handler = print_bootstrap_lines,
)

tor_process.kill()  # stops tor

The above script is being ran from this script:

import Communicator
from MeteorClient import MeteorClient

client = MeteorClient('ws://127.0.0.1:3000/websocket')
client.connect()

def subscription_callback(error):
    if error:
        print(error)

client.subscribe('accounts', callback=subscription_callback)

all_posts = client.find('accounts')
print(all_posts)

Communicator.query("190aqe41vbewh7367f2hf27521")

But it is then giving me this result:

[1mStarting Tor:
[0m
May 10 13:21:45.000 [notice] Bootstrapped 0%: Starting
May 10 13:21:45.000 [notice] Bootstrapped 80%: Connecting to the Tor network
May 10 13:21:46.000 [notice] Bootstrapped 85%: Finishing handshake with first hop
May 10 13:21:46.000 [notice] Bootstrapped 90%: Establishing a Tor circuit
May 10 13:21:47.000 [notice] Bootstrapped 100%: Done
Traceback (most recent call last):
  File "C:\Python34\lib\site-packages\socks.py", line 663, in connect
    _BaseSocket.connect(self, proxy_addr)
ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\gatsu\My Documents\LiClipse Workspace\TorCommunicator\MeteorDDP.py", line 5, in <module>
    client.connect()
  File "C:\Python34\lib\site-packages\python_meteor-0.1.6-py3.4.egg\MeteorClient.py", line 55, in connect
  File "C:\Python34\lib\site-packages\python_ddp-0.1.5-py3.4.egg\DDPClient.py", line 119, in connect
  File "C:\Python34\lib\site-packages\ws4py-0.3.4-py3.4.egg\ws4py\client\__init__.py", line 209, in connect
  File "C:\Python34\lib\site-packages\socks.py", line 674, in connect
    raise ProxyConnectionError(msg, error)
socks.ProxyConnectionError: Error connecting to SOCKS5 proxy 127.0.0.1:7000: [WinError 10061] No connection could be made because the target machine actively refused it

Solution

  • I solved this by importing the Communicator after I had done my Meteor stuff, right before I call a method in the Communicator.

    from MeteorClient import MeteorClient
    
    client = MeteorClient('ws://127.0.0.1:3000/websocket')
    client.connect()
    
    def subscription_callback(error):
        if error:
            print(error)
    
    client.subscribe('accounts', callback=subscription_callback)
    
    all_posts = client.find('accounts')
    print(all_posts)
    
    import Communicator
    Communicator.query("190aqe41vbewh7367f2hf27521")