Multiple TOR Exit Nodes - Controller Not Working?

梦想的初衷 提交于 2019-12-03 21:25:38

I have re-written the code, as it seems my code was just awful. I'm not sure what the problem was, however, here's the update for anyone trying to do the same:

assert str is not bytes

from lib_socks_proxy_2013_10_03 import monkey_patch as socks_proxy_monkey_patch

socks_proxy_monkey_patch.monkey_patch()

import io
import re

import time

import stem.connection
import stem.socket
import stem.process 
from stem import Signal
from stem.control import Controller
from stem.util import term

from urllib import request as req
from lib_socks_proxy_2013_10_03 import socks_proxy_context

import config

tor_process = []
_ = config.variables

def startTor(i):
    print(" - Starting TOR Process...")    

    tor_process.extend([stem.process.launch_tor_with_config(
        config={
            'SocksPort':str(9050+i),
            'ControlPort':str(9070+i),
            'CookieAuthentication':str(1),
            'HashedControlPassword':str(_.TOR_HASHED_PASS_),
            'DataDirectory':'data/tor'+str(i),
            'PidFile':'tor'+str(i)+'.pid',
                        },
        init_msg_handler = print_bootstrap_lines,
        )])

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

def checkIP(i):
    print("[+] Checking IP Address...")
    try:
        opener = req.build_opener()

        with socks_proxy_context.socks_proxy_context(proxy_address=('127.0.0.1', 9050+i)):
           res = opener.open('https://internet.yandex.com/get_full_info/', timeout=20.0)

        data = res.read(10000).decode()
        print(data)
    except:
        print(" - Failed.")


def changeIP(i):
    print("[+] Changing IP Address... ["+str(i)+"]")
    with Controller.from_port(port = 9070+i) as controller:
        controller.authenticate(_.TOR_PASS_)
        controller.signal(Signal.NEWNYM)

## Test 3 nodes.

if(__name__=="__main__"):
    for x in range(0,3):
        startTor(x)
        checkIP(x)
    for x in range(0,3):
        changeIP(x)
    for x in range(0,3):
        checkIP(x)
        tor_process[x].kill() 

For anyone planning on using this: don't cycle the TOR servers too fast/too much as it puts strain on the network.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!