utils.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. from time import sleep
  2. from config import *
  3. from datetime import datetime
  4. from ecdsa import SigningKey, SECP256k1
  5. from eth_account.messages import encode_defunct
  6. from random import choice
  7. from requests import request
  8. from sha3 import keccak_256
  9. from sys import exit
  10. from time import sleep
  11. from string import ascii_lowercase, digits
  12. from user_agent import generate_user_agent
  13. from web3.auto import w3
  14. from names import get_first_name, get_last_name
  15. from config import (
  16. SECMAIL_DOMAINS,
  17. WAIT_MAIL_ROUNDS,
  18. WAIT_MAIL_TIME,
  19. CHANGE_PROXY_URL,
  20. PROXY_URL,
  21. ALLOWED_PROXY_SCHEMES,
  22. )
  23. from log import logger
  24. def proxy_url_to_proxies(url):
  25. try:
  26. schema, url = url.split('://')
  27. auth, url = url.split('@')
  28. username, password = auth.split(':')
  29. host, port = url.split(':')
  30. except Exception as e:
  31. logger.error(f'Zjef5mTM4J3NWmHP; invalid proxy format: {url}')
  32. return {'status': 'error'}
  33. if not schema in ALLOWED_PROXY_SCHEMES:
  34. logger.error(f'2Hsm4eUf3YjktahK; {schema}')
  35. return {
  36. 'status': 'success',
  37. 'data': {
  38. 'proxies': {
  39. 'http': f'{schema}://{username}:{password}@{host}:{port}',
  40. 'https': f'{schema}://{username}:{password}@{host}:{port}',
  41. }
  42. }
  43. }
  44. def proxy_validation(proxies):
  45. sleep(5)
  46. response = request('GET', 'http://myexternalip.com/raw', proxies=proxies)
  47. if response.status_code != 200:
  48. logger.error(f'code: kvbMTudHNS8nkt8d; {response.status_code} {response.text}')
  49. return {'status': 'error'}
  50. if not response.text:
  51. logger.error(f'code: CBTc5NEGE75vuwCU; {response.status_code} {response.text}')
  52. return {'status': 'error'}
  53. return {
  54. 'status': 'success',
  55. 'data': {
  56. 'external_ip': response.text
  57. }
  58. }
  59. def change_proxy_with_url(url):
  60. response = request('GET', url)
  61. if response.status_code != 200:
  62. logger.error(f'code: wzuz3FPKaJmJtEJb; {response.status_code} {response.text}')
  63. return {'status': 'error'}
  64. logger.debug(response.text)
  65. sleep(10)
  66. return {'status': 'success'}
  67. def get_random_string(length=10):
  68. return ''.join([choice(ascii_lowercase+digits) for _ in range(length)])
  69. def checksum_encode(addr_str): # Takes a hex (string) address as input
  70. keccak = keccak_256()
  71. out = ''
  72. addr = addr_str.lower().replace('0x', '')
  73. keccak.update(addr.encode('ascii'))
  74. hash_addr = keccak.hexdigest()
  75. for i, c in enumerate(addr):
  76. if int(hash_addr[i], 16) >= 8:
  77. out += c.upper()
  78. else:
  79. out += c
  80. return '0x' + out
  81. def parse_1secmail_mail(email, _from, subject, proxies, without_mail_id=None):
  82. mail_id = None
  83. split_email = email.split('@')
  84. url = f'https://www.1secmail.com/api/v1/?action=getMessages&login={split_email[0]}&domain={split_email[1]}'
  85. all_break = False
  86. for _ in range(WAIT_MAIL_ROUNDS):
  87. if all_break == True: break
  88. response = request('GET', url, proxies=proxies)
  89. if not response.status_code == 200:
  90. sleep(WAIT_MAIL_TIME+1)
  91. continue
  92. if not response.json():
  93. sleep(WAIT_MAIL_TIME+1)
  94. continue
  95. for mail in response.json():
  96. if all_break == True: break
  97. if not _from in mail['from'] or not subject in mail['subject']:
  98. logger.debug(f'unknown mail: {mail}')
  99. continue
  100. else:
  101. mail_id = mail['id']
  102. all_break = True
  103. sleep(WAIT_MAIL_TIME+1)
  104. if not mail_id:
  105. logger.error('code: wd75Kpbn,cannnot get mail')
  106. return {'status': 'error'}
  107. if mail_id == without_mail_id:
  108. logger.debug('repetition mail, start parser again')
  109. return parse_mail(email, _from, subject, without_mail_id, proxies)
  110. url = f'https://www.1secmail.com/api/v1/?action=readMessage&login={split_email[0]}&domain={split_email[1]}&id={mail_id}'
  111. sleep(5)
  112. response = request('GET', url)
  113. if not response.status_code == 200:
  114. logger.error(f'code: zK997XAv, {response.status_code} {response.text}')
  115. return {'status': 'error'}
  116. if not response.text or response.text == 'Message not found':
  117. logger.error(f'code: VaPXmY7f, {response.text}')
  118. return {'status': 'error'}
  119. return {
  120. 'status': 'success',
  121. 'data': {
  122. 'id': mail_id,
  123. 'text': response.text,
  124. 'json': response.json()
  125. }
  126. }
  127. def get_random_alliance_name():
  128. return choice(ALLIANCES)
  129. def get_random_1secmail_email():
  130. return get_random_string() + '@' + choice(SECMAIL_DOMAINS)
  131. def get_random_first_name():
  132. return get_first_name()
  133. def get_random_last_name():
  134. return get_last_name()
  135. def get_random_password():
  136. return '!1Q' + get_random_string()
  137. def get_current_datetime():
  138. return str(datetime.now())
  139. def get_random_username():
  140. return get_random_string()
  141. def get_random_user_agent():
  142. return generate_user_agent()
  143. def wallet_sign_message(message, private_key):
  144. encode_message = encode_defunct(text=message)
  145. signed_message = w3.eth.account.sign_message(encode_message, private_key=private_key)
  146. return signed_message.signature.hex()
  147. def get_random_wallet():
  148. keccak = keccak_256()
  149. private_key = SigningKey.generate(curve=SECP256k1)
  150. public_key = private_key.get_verifying_key().to_string()
  151. keccak.update(public_key)
  152. address = keccak.hexdigest()[24:]
  153. return {
  154. 'address': checksum_encode(address),
  155. 'public_key': public_key.hex(),
  156. 'private_key': private_key.to_string().hex()
  157. }
  158. def get_random_tor_proxy_url(username=get_random_string(), password=get_random_string()):
  159. return f'socks5://{username}:{password}@127.0.0.1:9052'
  160. def get_proxy():
  161. if not PROXY_URL:
  162. return {
  163. 'type': 'none',
  164. 'external_ip': '',
  165. 'proxies': {
  166. 'http': '',
  167. 'https': '',
  168. },
  169. }
  170. elif not CHANGE_PROXY_URL:
  171. proxy_url = PROXY_URL
  172. else:
  173. change_proxy_with_url_result = change_proxy_with_url(CHANGE_PROXY_URL)
  174. if change_proxy_with_url_result['status'] != 'success':
  175. assert False, f'dqcjheHVm75qk6fH; {proxies}'
  176. proxy_url = PROXY_URL
  177. proxies = proxy_url_to_proxies(proxy_url)
  178. if proxies['status'] != 'success':
  179. assert False, 'xsnYjY6BwbLw8X9q' + str(proxies)
  180. proxy_validation_result = proxy_validation(proxies['data']['proxies'])
  181. if proxy_validation_result['status'] != 'success':
  182. assert False, f'Pk8sv9wHkCuPWNYv; {proxy_validation_result}'
  183. external_ip = proxy_validation_result['data']['external_ip']
  184. return {
  185. 'external_ip': external_ip,
  186. 'proxies': proxies['data']['proxies'],
  187. }
  188. def get_email():
  189. return get_random_1secmail_email()