toxygen/toxygen/tox_dns.py

58 lines
1.8 KiB
Python
Raw Normal View History

import json
2016-06-21 11:58:11 +00:00
import urllib.request
from util import log
2016-07-04 09:45:51 +00:00
import settings
def tox_dns(email):
"""
TOX DNS 4
:param email: data like 'groupbot@toxme.io'
:return: tox id on success else None
"""
site = email.split('@')[1]
data = {"action": 3, "name": "{}".format(email)}
2016-07-04 09:45:51 +00:00
urls = ('https://{}/api'.format(site), 'http://{}/api'.format(site))
s = settings.Settings.get_instance()
if s['proxy_type'] != 2: # no proxy or http proxy
proxy = s['proxy_host'] + ':' + s['proxy_port'] if s['proxy_type'] else None
for url in urls:
try:
return send_request(url, data, proxy)
except Exception as ex:
log('TOX DNS ERROR: ' + str(ex))
else: # SOCKS5 proxy
try:
2016-07-04 09:45:51 +00:00
import socks
import socket
import requests
socks.set_default_proxy(socks.SOCKS5, s['proxy_host'], s['proxy_port'])
socket.socket = socks.socksocket
for url in urls:
try:
r = requests.get(url)
res = json.loads(r.text)
if not res['c']:
return res['tox_id']
else:
raise LookupError()
except Exception as ex:
log('TOX DNS ERROR: ' + str(ex))
except:
pass
return None # error
2016-07-04 09:45:51 +00:00
def send_request(url, data, proxy):
2016-06-21 11:58:11 +00:00
req = urllib.request.Request(url)
2016-07-04 09:45:51 +00:00
if proxy is not None:
req.set_proxy(proxy, 'http')
req.add_header('Content-Type', 'application/json')
response = urllib.request.urlopen(req, bytes(json.dumps(data), 'utf-8'))
res = json.loads(str(response.read(), 'utf-8'))
if not res['c']:
return res['tox_id']
else:
raise LookupError()