-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtox_dig.py
executable file
·101 lines (85 loc) · 2.97 KB
/
tox_dig.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/usr/bin/env python3
# ToxDNS lookup script, requires python3, and python-dns(
# http://www.dnspython.org/)
# Use as tox_dig [email protected]
# Written by GI_Jack <[email protected]>
# Licensed under the GPLv3 or any later version
# https://www.gnu.org/copyleft/gpl.html
# Except the TOX development team, which is explicitly and exclusively given the
# "WTFPL – Do What the Fuck You Want to Public License" http://www.wtfpl.net/
#exits 0 for success, 1 for error, 2 for help
import sys
import dns.resolver
def get_ToxID(address):
'''input a ToxDNS address, get back a ToxID'''
try:
user,domain = address.split('@')
except:
return(-1,address+" is not a valid ToxDNS name, format is [email protected]")
try:
rawdata = str(dns.resolver.query(user+'._tox.'+domain,'txt')[0])
except dns.resolver.NXDOMAIN:
return(-1,address+" does not have a ToxID associated with it")
except:
return(-1,"python dns gave a non-specific failure")
rawdata = rawdata.strip('"')
# splitting the dns return into segments, version comes first[0], in all
# versions
toxver = rawdata.split(';')[0]
if toxver == "v=tox1":
outdata = _tox1(rawdata)
elif toxver == "v=toxv":
outdata = _tox2(rawdata)
elif toxver == "v=tox3":
outdata = _tox3(rawdata)
else:
return(-1,"DNS returned invalid data: "+address)
return outdata
def _tox1(record):
'''Lookup a ToxDNS version1 Record '''
# ToxID is the second entry on version one records, it should be identified
# by id=
try:
toxid = record.split(';')[1]
except:
return(-1,"Cannot get address from record: "+address)
if toxid.find("id=") == 0 :
toxid = toxid.rstrip('\\')
toxid = toxid.lstrip('id=')
else:
return(-1,"DNS returned invalid data: "+address)
return toxid
def _tox2(record):
return(-2,"not implemented")
def _tox3(record):
return(-2,"not implemented")
def tox_dig_help():
print('''tox_dig: small Python script that returns a ToxID from ToxDNS using the standard unix dig command
USAGE: tox_dig userid@toxdomain, EXAMPLE: tox_dig [email protected]'''
)
sys.exit(2)
#main program is now a function
def main():
#get input, if there run with no arguments display help
script_name = sys.argv[0].lower()
try:
address = sys.argv[1].lower()
except:
tox_dig_help()
#HALP! if someone requests help, display help intead lookup
if address == "help":
tox_dig_help()
output = get_ToxID(address)
# If there is an error from the function it will return a tupple with the
# first element[0] being -1 and the second element [1] being the error
# message
if output[0] == -1:
print("tox_dig: Error",output[1])
sys.exit(1)
elif output[0] == -2:
tox_dig_help()
else:
#we have data and no errors, output to user
print(address+" "+output)
if __name__ == "__main__":
main()