Skip to content
This repository was archived by the owner on Aug 14, 2020. It is now read-only.

Commit 81a3a62

Browse files
committed
Add ToxDNS core functions
1 parent 21b0421 commit 81a3a62

File tree

9 files changed

+395
-5
lines changed

9 files changed

+395
-5
lines changed

examples/toxdns.py

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
__title__ = "toxdns"
5+
__version__ = "0.0.17"
6+
__author__ = "Anton Batenev"
7+
__license__ = "BSD"
8+
9+
import dns.resolver
10+
11+
from pytoxcore import ToxDNS, ToxDNSException
12+
13+
14+
def last_txt_record(host):
15+
response = dns.resolver.query(host, "TXT")
16+
if response:
17+
result = None
18+
for record in response:
19+
result = record
20+
return str(result).replace('"', "")
21+
return None
22+
23+
24+
def tox_dns3_id(record):
25+
parts = record.split(";")
26+
for part in parts:
27+
kv = part.split("=")
28+
if len(kv) != 2:
29+
return None
30+
31+
k = kv[0]
32+
v = kv[1]
33+
if k == "v" and v != "tox3":
34+
return None
35+
elif k == "id":
36+
return v
37+
38+
return None
39+
40+
41+
if __name__ == "__main__":
42+
request = "[email protected]"
43+
44+
print("Request: {0}".format(request))
45+
46+
parts = request.split("@")
47+
if len(parts) != 2:
48+
exit("User name must be user@domain")
49+
50+
user = parts[0]
51+
domain = parts[1]
52+
53+
print("User: {0}".format(user))
54+
print("Domain: {0}".format(domain))
55+
56+
host = "_tox.{0}".format(domain)
57+
key = last_txt_record(host)
58+
if key == None:
59+
exit("No TXT record found for {0}".format(host))
60+
else:
61+
print("Public key: {0}".format(key))
62+
63+
if len(key) != ToxDNS.TOX_CLIENT_ID_SIZE * 2:
64+
exit("Public key length = {0}, but {1} required".format(len(key), ToxDNS.TOX_CLIENT_ID_SIZE * 2))
65+
66+
tox_dns = ToxDNS(key)
67+
68+
id_record, request_id = tox_dns.tox_generate_dns3_string(user)
69+
70+
print("Request record: {0}".format(id_record))
71+
print("Request Id: {0}".format(request_id))
72+
73+
host = "_{0}._tox.{1}".format(id_record, domain)
74+
record = last_txt_record(host)
75+
if record == None:
76+
exit("No TXT record found for {0}".format(host))
77+
else:
78+
print("Record: {0}".format(record))
79+
80+
record = tox_dns3_id(record)
81+
if record == None:
82+
exit("Can not parse record")
83+
84+
address = tox_dns.tox_decrypt_dns3_TXT(record, request_id)
85+
86+
print("Address: {0}".format(address))

pytox.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
*/
1919
//----------------------------------------------------------------------------------------------
2020
#include "pytoxav.h"
21+
#include "pytoxdns.h"
2122
//----------------------------------------------------------------------------------------------
2223

2324
void bytes_to_hex_string(const uint8_t* digest, int length, uint8_t* hex_digest)
@@ -139,6 +140,23 @@ PyMODINIT_FUNC initpytoxcore(void)
139140
ToxAVException = PyErr_NewException("pytoxcore.ToxAVException", NULL, NULL);
140141
PyModule_AddObject(module, "ToxAVException", (PyObject*)ToxAVException);
141142

143+
//
144+
// initialize pytoxdns
145+
//
146+
147+
ToxDNS_install_dict();
148+
149+
if (PyType_Ready(&ToxDNSType) < 0) {
150+
fprintf(stderr, "Invalid PyTypeObject 'ToxDNSType'\n");
151+
goto error;
152+
}
153+
154+
Py_INCREF(&ToxDNSType);
155+
PyModule_AddObject(module, "ToxDNS", (PyObject*)&ToxDNSType);
156+
157+
ToxDNSException = PyErr_NewException("pytoxcore.ToxDNSException", NULL, NULL);
158+
PyModule_AddObject(module, "ToxDNSException", (PyObject*)ToxDNSException);
159+
142160
#if PY_MAJOR_VERSION >= 3
143161
return module;
144162
#endif

pytox.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include <pthread.h>
2929
#include <tox/tox.h>
3030
#include <tox/toxav.h>
31+
#include <tox/toxdns.h>
3132
#include <sys/param.h>
3233
#include <arpa/inet.h>
3334
#include <vpx/vpx_image.h>

pytoxav.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1187,7 +1187,7 @@ PyTypeObject ToxAVType = {
11871187
0, /* tp_dictoffset */
11881188
(initproc)ToxAV_init, /* tp_init */
11891189
0, /* tp_alloc */
1190-
ToxAV_new, /* tp_new */
1190+
ToxAV_new /* tp_new */
11911191
};
11921192
//----------------------------------------------------------------------------------------------
11931193

pytoxav.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,5 +46,5 @@ extern PyObject* ToxAVException;
4646
//----------------------------------------------------------------------------------------------
4747
void ToxAV_install_dict(void);
4848
//----------------------------------------------------------------------------------------------
49-
#endif // _pytoxavh_
49+
#endif // _pytoxav_h_
5050
//----------------------------------------------------------------------------------------------

pytoxcore.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2296,7 +2296,7 @@ PyTypeObject ToxCoreType = {
22962296
0, /* tp_dictoffset */
22972297
(initproc)ToxCore_init, /* tp_init */
22982298
0, /* tp_alloc */
2299-
ToxCore_new, /* tp_new */
2299+
ToxCore_new /* tp_new */
23002300
};
23012301
//----------------------------------------------------------------------------------------------
23022302

0 commit comments

Comments
 (0)