tox dns update
This commit is contained in:
parent
5df82c6b3c
commit
326ebc155c
2 changed files with 45 additions and 21 deletions
|
@ -1,6 +1,7 @@
|
|||
import json
|
||||
import urllib.request
|
||||
from util import log
|
||||
import settings
|
||||
|
||||
|
||||
def tox_dns(email):
|
||||
|
@ -11,16 +12,42 @@ def tox_dns(email):
|
|||
"""
|
||||
site = email.split('@')[1]
|
||||
data = {"action": 3, "name": "{}".format(email)}
|
||||
for url in ('https://{}/api'.format(site), 'http://{}/api'.format(site)):
|
||||
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:
|
||||
return send_request(url, data)
|
||||
except Exception as ex: # try http
|
||||
log('TOX DNS ERROR: ' + str(ex))
|
||||
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
|
||||
|
||||
|
||||
def send_request(url, data):
|
||||
def send_request(url, data, proxy):
|
||||
req = urllib.request.Request(url)
|
||||
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'))
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue