Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import subprocess
- import os
- import socket
- from time import sleep, time
- from datetime import datetime
- import mysql.connector
- import stem.process
- tors = {}
- class DB:
- def __init__(self):
- self.connect()
- def connect(self):
- try:
- self.cnn = mysql.connector.connect(user="root", password="lucas_pau_mole_broxa_do_caralho", host="127.0.0.1", database="wyd2bot")
- self.cursor = self.cnn.cursor()
- except :
- time.sleep(5)
- print("Nao foi possivel conectar ao banco! Tentando conectar novamente")
- time.sleep(3)
- connect()
- def disconnect(self):
- self.cursor.close()
- self.cnn.close()
- def select(self, query, query_data):
- while True:
- try:
- self.cursor.execute(query, query_data)
- return self.cursor.fetchall()
- except:
- self.connect()
- def insert(self, query, query_data):
- while True:
- try:
- self.cursor.execute(query, query_data)
- return self.cursor.fetchall()
- except:
- self.connect()
- def execute(self, query, query_data):
- while True:
- try:
- self.cursor.execute(query, query_data)
- self.cnn.commit()
- break
- except:
- self.connect()
- class TOR:
- def __init__(self, port):
- self.port = port
- self.horario = datetime.now()
- def start(self):
- raise NotImplementedError("Nao implementado")
- def close(self):
- if self.is_open():
- self.p.kill()
- def is_open(self):
- if self.p.poll() == None:
- return True
- return False
- def register(self):
- db = DB()
- db.insert("INSERT INTO proxys (`proxy`, `don`, `don2`, `kofd`, `myd`, `wyd2`, `aon`, `data_cadastro`) VALUES ( %(proxy_info)s,0,0,0,0,0,0,%(dta_cadastro)s);",
- {'proxy_info': '127.0.0.1:'+str(self.port), 'dta_cadastro': datetime.now()})
- db.close()
- class ManualTOR(TOR):
- def __init__(self, port):
- super().__init__(port)
- self.dir = "/var/lib/tor"+str(port)
- self.file = "/etc/tor/torrc."+str(port)
- self.start()
- def start(self):
- if os.path.exists(self.dir) == False:
- os.makedirs(self.dir)
- f = open(self.file, 'w')
- f.write('SocksPort {0}\nControlPort {1}\nDataDirectory{2]}'.format(port, port + 10000, self.dir))
- f.close()
- self.p = subprocess.Popen(["tor", "-f", "/etc/tor/torrc."+str(port)])
- self.register()
- class AutomaticTOR(TOR):
- def __init__(self, port):
- super().__init__(port)
- self.start()
- def start(self):
- self.p = stem.process.launch_tor_with_config(
- config = {
- 'SocksPort': str(port),
- 'ControlPort': str(port+10000),
- },
- )
- self.register()
- torino = AutomaticTOR
- db = DB()
- db.execute("TRUNCATE proxys", {})
- while True:
- rows = db.select("SELECT count(*) AS total, COALESCE(SUM(don), 0) AS cdon, COALESCE(SUM(don2), 0) AS cdon2, COALESCE(sum(kofd), 0) AS ckofd, COALESCE(SUM(myd), 0) AS cmyd, COALESCE(SUM(wyd2), 0) AS cwyd2, COALESCE(SUM(aon), 0) AS caon FROM proxys", {})
- row = rows[0]
- n = row[0]
- _max = max(row[1:])
- #Se nao tiver 5 tors disponiveis, abre ate ficar com 5 sobrando
- while (n - _max) < 5:
- for port in range(40000, 50000):
- if port not in tors:
- sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- result = sock.connect_ex(('127.0.0.1', port))
- if result != 0:
- tors[port] = torino(port)
- n = n + 1
- #Se tiver mais de 5 remove ate ficar com 5
- if (n - _max) > 5:
- rows = db.select("SELECT proxy FROM proxys WHERE don = 0 AND don2 = 0 AND kofd = 0 AND myd = 0 AND wyd2 = 0 AND aon = 0 ORDER BY data_cadastro")
- for row in rows:
- if (n - _max) > 5:
- if row[0] in tors:
- tors[port].close()
- del tors[port]
- n = n - 1
- sleep(0.25)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement