2016-03-12 11:09:58 +01:00
|
|
|
import json
|
2016-06-21 13:58:11 +02:00
|
|
|
import urllib.request
|
2016-03-12 11:09:58 +01:00
|
|
|
from util import log
|
2016-07-04 11:45:51 +02:00
|
|
|
import settings
|
2016-03-12 11:09:58 +01:00
|
|
|
|
|
|
|
|
|
|
|
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 11:45:51 +02: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
|
2016-03-12 11:09:58 +01:00
|
|
|
try:
|
2016-07-04 11:45:51 +02: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
|
2016-03-12 11:09:58 +01:00
|
|
|
return None # error
|
|
|
|
|
|
|
|
|
2016-07-04 11:45:51 +02:00
|
|
|
def send_request(url, data, proxy):
|
2016-06-21 13:58:11 +02:00
|
|
|
req = urllib.request.Request(url)
|
2016-07-04 11:45:51 +02:00
|
|
|
if proxy is not None:
|
|
|
|
req.set_proxy(proxy, 'http')
|
2016-03-12 11:09:58 +01:00
|
|
|
req.add_header('Content-Type', 'application/json')
|
2016-06-23 14:04:36 +02:00
|
|
|
response = urllib.request.urlopen(req, bytes(json.dumps(data), 'utf-8'))
|
|
|
|
res = json.loads(str(response.read(), 'utf-8'))
|
2016-03-12 11:09:58 +01:00
|
|
|
if not res['c']:
|
|
|
|
return res['tox_id']
|
|
|
|
else:
|
|
|
|
raise LookupError()
|