forked from krolinventions/cjdnsmap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcjdns.py
220 lines (192 loc) · 7.11 KB
/
cjdns.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# You may redistribute this program and/or modify it under the terms of
# the GNU General Public License as published by the Free Software Foundation,
# either version 3 of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import sys;
import os;
import socket;
import hashlib;
import json;
import threading;
import time;
import Queue;
import random;
import string;
from bencode import *;
BUFFER_SIZE = 69632;
KEEPALIVE_INTERVAL_SECONDS = 2;
def randStr():
return ''.join(random.choice(string.ascii_uppercase + string.digits) for x in range(10));
def callfunc(cjdns, funcName, password, args):
txid = randStr();
sock = cjdns.socket;
sock.send('d1:q6:cookie4:txid10:'+ txid +'e');
msg = _getMessage(cjdns, txid);
cookie = msg['cookie'];
txid = randStr();
req = {
'q': 'auth',
'aq': funcName,
'hash': hashlib.sha256(password + cookie).hexdigest(),
'cookie' : cookie,
'args': args,
'txid': txid
};
reqBenc = bencode(req);
req['hash'] = hashlib.sha256(reqBenc).hexdigest();
reqBenc = bencode(req);
sock.send(reqBenc);
return _getMessage(cjdns, txid);
def receiverThread(cjdns):
timeOfLastSend = time.time();
timeOfLastRecv = time.time();
try:
while True:
if (timeOfLastSend + KEEPALIVE_INTERVAL_SECONDS < time.time()):
if (timeOfLastRecv + 10 < time.time()):
raise Exception("ping timeout");
cjdns.socket.send('d1:q18:Admin_asyncEnabled4:txid8:keepalive');
timeOfLastSend = time.time();
try:
data = cjdns.socket.recv(BUFFER_SIZE);
except (socket.timeout): continue;
try:
benc = bdecode(data);
except (KeyError, ValueError):
print("error decoding [" + data + "]");
continue;
if benc['txid'] == 'keepaliv':
if benc['asyncEnabled'] == 0:
raise Exception("lost session");
timeOfLastRecv = time.time();
else:
#print "putting to queue " + str(benc);
cjdns.queue.put(benc);
except KeyboardError:
print("interrupted");
import thread
thread.interrupt_main();
def _getMessage(cjdns, txid):
while True:
if txid in cjdns.messages:
msg = cjdns.messages[txid];
del cjdns.messages[txid];
return msg;
else:
#print "getting from queue";
try:
# apparently any timeout at all allows the thread to be
# stopped but none make it unstoppable with ctrl+c
next = cjdns.queue.get(timeout=100);
except Queue.Empty: continue;
if 'txid' in next:
cjdns.messages[next['txid']] = next;
#print "adding message [" + str(next) + "]";
else:
print "message with no txid: " + str(next);
def cjdns_connect(ipAddr, port, password):
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM);
sock.connect((ipAddr, port));
sock.settimeout(2);
# Make sure it pongs.
sock.send('d1:q4:pinge');
data = sock.recv(BUFFER_SIZE);
if (data != 'd1:q4:ponge'):
raise Exception("Looks like " + ipAddr + ":" + str(port) + " is to a non-cjdns socket.");
# Get the functions and make the object
page = 0;
availableFunctions = {};
while True:
sock.send('d1:q24:Admin_availableFunctions4:argsd4:pagei' + str(page) + 'eee');
data = sock.recv(BUFFER_SIZE);
benc = bdecode(data);
for func in benc['availableFunctions']:
availableFunctions[func] = benc['availableFunctions'][func];
if (not 'more' in benc):
break;
page = page+1;
argLists = {};
cc = ("class Cjdns:\n"
+ " def __init__(self, socket):\n"
+ " self.socket = socket;\n"
+ " self.queue = Queue.Queue();\n"
+ " self.messages = {};\n"
+ " def disconnect(self):\n"
+ " self.socket.close();\n"
+ " def getMessage(self, txid):\n"
+ " return _getMessage(self, txid);\n");
for func in availableFunctions:
argList = [];
argLists[func] = argList;
funcDict = availableFunctions[func];
cc += " def " + func + "(self";
# If the arg is required, put it first,
# otherwise put it after and use a default
# value of a type which will be ignored by the core.
for arg in funcDict:
if (funcDict[arg]['required'] == 1):
argList.append(arg);
cc += ", " + arg;
for arg in funcDict:
argDict = funcDict[arg];
if (argDict['required'] == 0):
argList.append(arg);
cc += ", " + arg + "=";
if (argDict['type'] == 'Int'):
cc += "''";
else:
cc += "0";
cc += ("):\n"
+ " args = {");
for arg in argList:
cc += "\"" + arg + "\": " + arg + ", ";
cc += ("};\n"
+ " return callfunc(self, \"" + func + "\", \"" + password + "\", args);\n");
exec(cc);
cjdns = Cjdns(sock);
kat = threading.Thread(target=receiverThread, args=[cjdns]);
kat.setDaemon(True);
kat.start();
# Check our password.
ret = callfunc(cjdns, "ping", password, {});
if ('error' in ret):
raise Exception("Connect failed, incorrect admin password?\n" + str(ret))
cjdns.functions = "";
nl = "";
for func in availableFunctions:
funcDict = availableFunctions[func];
cjdns.functions += nl + func + "(";
nl = "\n";
sep = "";
for arg in argLists[func]:
cjdns.functions += sep;
sep = ", ";
argDict = funcDict[arg];
if (argDict['required'] == 1):
cjdns.functions += "required ";
cjdns.functions += argDict['type'] + " " + arg;
cjdns.functions += ")";
return cjdns;
def cjdns_connectWithAdminInfo():
try:
adminInfo = open(os.getenv("HOME") + '/.cjdnsadmin', 'r');
except IOError:
print('Please create a file named .cjdnsadmin in your home directory with');
print('ip, port, and password of your cjdns engine in json.');
print('for example:');
print('{');
print(' "addr": "127.0.0.1",');
print(' "port": 11234,');
print(' "password": "You tell me! (you\'ll find it in your cjdroute.conf)"');
print('}');
raise;
data = json.load(adminInfo);
adminInfo.close();
return cjdns_connect(data['addr'], data['port'], data['password']);