Advertisement
Guest User

Untitled

a guest
Feb 10th, 2017
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.24 KB | None | 0 0
  1. import subprocess
  2. import os
  3. import socket
  4. from time import sleep, time
  5. from datetime import datetime
  6. import mysql.connector
  7. import stem.process
  8.  
  9. tors = {}
  10.  
  11. class DB:
  12. def __init__(self):
  13. self.connect()
  14.  
  15. def connect(self):
  16. try:
  17. self.cnn = mysql.connector.connect(user="root", password="lucas_pau_mole_broxa_do_caralho", host="127.0.0.1", database="wyd2bot")
  18. self.cursor = self.cnn.cursor()
  19. except :
  20. time.sleep(5)
  21. print("Nao foi possivel conectar ao banco! Tentando conectar novamente")
  22. time.sleep(3)
  23. connect()
  24.  
  25. def disconnect(self):
  26. self.cursor.close()
  27. self.cnn.close()
  28.  
  29. def select(self, query, query_data):
  30. while True:
  31. try:
  32. self.cursor.execute(query, query_data)
  33. return self.cursor.fetchall()
  34. except:
  35. self.connect()
  36.  
  37. def insert(self, query, query_data):
  38. while True:
  39. try:
  40. self.cursor.execute(query, query_data)
  41. return self.cursor.fetchall()
  42. except:
  43. self.connect()
  44.  
  45. def execute(self, query, query_data):
  46. while True:
  47. try:
  48. self.cursor.execute(query, query_data)
  49. self.cnn.commit()
  50. break
  51. except:
  52. self.connect()
  53.  
  54.  
  55. class TOR:
  56. def __init__(self, port):
  57. self.port = port
  58. self.horario = datetime.now()
  59.  
  60. def start(self):
  61. raise NotImplementedError("Nao implementado")
  62.  
  63. def close(self):
  64. if self.is_open():
  65. self.p.kill()
  66.  
  67. def is_open(self):
  68. if self.p.poll() == None:
  69. return True
  70. return False
  71.  
  72. def register(self):
  73. db = DB()
  74. 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);",
  75. {'proxy_info': '127.0.0.1:'+str(self.port), 'dta_cadastro': datetime.now()})
  76. db.close()
  77.  
  78. class ManualTOR(TOR):
  79. def __init__(self, port):
  80. super().__init__(port)
  81. self.dir = "/var/lib/tor"+str(port)
  82. self.file = "/etc/tor/torrc."+str(port)
  83. self.start()
  84.  
  85. def start(self):
  86. if os.path.exists(self.dir) == False:
  87. os.makedirs(self.dir)
  88. f = open(self.file, 'w')
  89. f.write('SocksPort {0}\nControlPort {1}\nDataDirectory{2]}'.format(port, port + 10000, self.dir))
  90. f.close()
  91. self.p = subprocess.Popen(["tor", "-f", "/etc/tor/torrc."+str(port)])
  92. self.register()
  93.  
  94. class AutomaticTOR(TOR):
  95. def __init__(self, port):
  96. super().__init__(port)
  97. self.start()
  98.  
  99. def start(self):
  100. self.p = stem.process.launch_tor_with_config(
  101. config = {
  102. 'SocksPort': str(port),
  103. 'ControlPort': str(port+10000),
  104. },
  105. )
  106. self.register()
  107.  
  108. torino = AutomaticTOR
  109. db = DB()
  110. db.execute("TRUNCATE proxys", {})
  111. while True:
  112. 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", {})
  113. row = rows[0]
  114. n = row[0]
  115. _max = max(row[1:])
  116. #Se nao tiver 5 tors disponiveis, abre ate ficar com 5 sobrando
  117. while (n - _max) < 5:
  118. for port in range(40000, 50000):
  119. if port not in tors:
  120. sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  121. result = sock.connect_ex(('127.0.0.1', port))
  122. if result != 0:
  123. tors[port] = torino(port)
  124. n = n + 1
  125. #Se tiver mais de 5 remove ate ficar com 5
  126. if (n - _max) > 5:
  127. 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")
  128. for row in rows:
  129. if (n - _max) > 5:
  130. if row[0] in tors:
  131. tors[port].close()
  132. del tors[port]
  133. n = n - 1
  134. sleep(0.25)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement