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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
|
# -*- coding:utf-8 -*-
## src/common/proxy65_manager.py
##
## Copyright (C) 2006 Dimitur Kirov <dkirov AT gmail.com>
## Jean-Marie Traissard <jim AT lapin.org>
## Copyright (C) 2007 Yann Leboulanger <asterix AT lagaule.org>
##
## This file is part of Gajim.
##
## Gajim is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published
## by the Free Software Foundation; version 3 only.
##
## Gajim 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 Gajim. If not, see <http://www.gnu.org/licenses/>.
##
import socket
import struct
import errno
import common.xmpp
from common import gajim
from common import helpers
from socks5 import Socks5
from common.xmpp.idlequeue import IdleObject
S_INITIAL = 0
S_STARTED = 1
S_RESOLVED = 2
S_ACTIVATED = 3
S_FINISHED = 4
CONNECT_TIMEOUT = 20
class Proxy65Manager:
''' keep records for file transfer proxies. Each time account
establishes a connection to its server call proxy65manger.resolve(proxy)
for every proxy that is convigured within the account. The class takes
care to resolve and test each proxy only once.'''
def __init__(self, idlequeue):
# dict {proxy: proxy properties}
self.idlequeue = idlequeue
self.proxies = {}
# dict {account: proxy} default proxy for account
self.default_proxies = {}
def resolve(self, proxy, connection, sender_jid, default=None):
''' start '''
if proxy in self.proxies:
resolver = self.proxies[proxy]
else:
# proxy is being ressolved for the first time
resolver = ProxyResolver(proxy, sender_jid)
self.proxies[proxy] = resolver
resolver.add_connection(connection)
if default:
# add this proxy as default for account
self.default_proxies[default] = proxy
def disconnect(self, connection):
for resolver in self.proxies.values():
resolver.disconnect(connection)
def resolve_result(self, proxy, query):
if proxy not in self.proxies:
return
jid = None
for item in query.getChildren():
if item.getName() == 'streamhost':
host = item.getAttr('host')
port = item.getAttr('port')
jid = item.getAttr('jid')
self.proxies[proxy].resolve_result(host, port, jid)
# we can have only one streamhost
raise common.xmpp.NodeProcessed
def error_cb(self, proxy, query):
sid = query.getAttr('sid')
for resolver in self.proxies.values():
if resolver.sid == sid:
resolver.keep_conf()
break
def get_default_for_name(self, account):
if account in self.default_proxies:
return self.default_proxies[account]
def get_proxy(self, proxy, account):
if proxy in self.proxies:
resolver = self.proxies[proxy]
if resolver.state == S_FINISHED:
return (resolver.host, resolver.port, resolver.jid)
return (None, 0, None)
class ProxyResolver:
def resolve_result(self, host, port, jid):
''' test if host has a real proxy65 listening on port '''
self.host = str(host)
self.port = int(port)
self.jid = unicode(jid)
self.state = S_RESOLVED
#FIXME: re-enable proxy testing
self.state = S_FINISHED
#self.receiver_tester = ReceiverTester(self.host, self.port, self.jid,
# self.sid, self.sender_jid, self._on_receiver_success,
# self._on_connect_failure)
#self.receiver_tester.connect()
def _on_receiver_success(self):
self.host_tester = HostTester(self.host, self.port, self.jid,
self.sid, self.sender_jid, self._on_connect_success,
self._on_connect_failure)
self.host_tester.connect()
def _on_connect_success(self):
iq = common.xmpp.Protocol(name='iq', to=self.jid, typ='set')
query = iq.setTag('query')
query.setNamespace(common.xmpp.NS_BYTESTREAM)
query.setAttr('sid', self.sid)
activate = query.setTag('activate')
activate.setData('test@gajim.org/test2')
if self.active_connection:
self.active_connection.SendAndCallForResponse(iq, self.keep_conf)
self.state = S_ACTIVATED
else:
self.state = S_INITIAL
def keep_conf(self, data=None):
self.disconnect(self.active_connection)
self.state = S_FINISHED
def _on_connect_failure(self):
self.state = S_FINISHED
self.host = None
self.port = 0
self.jid = None
def disconnect(self, connection):
if self.host_tester:
self.host_tester.disconnect()
self.host_tester = None
if self.receiver_tester:
self.receiver_tester.disconnect()
self.receiver_tester = None
try:
self.connections.remove(connection)
except ValueError:
pass
if connection == self.active_connection:
self.active_connection = None
if self.state != S_FINISHED:
self.state = S_INITIAL
self.try_next_connection()
def try_next_connection(self):
''' try to resolve proxy with the next possible connection '''
if self.connections:
connection = self.connections.pop(0)
self.start_resolve(connection)
def add_connection(self, connection):
''' add a new connection in case the first fails '''
self.connections.append(connection)
if self.state == S_INITIAL:
self.start_resolve(connection)
def start_resolve(self, connection):
''' request network address from proxy '''
self.state = S_STARTED
self.active_connection = connection
iq = common.xmpp.Protocol(name='iq', to=self.proxy, typ='get')
query = iq.setTag('query')
query.setNamespace(common.xmpp.NS_BYTESTREAM)
connection.send(iq)
def __init__(self, proxy, sender_jid):
self.proxy = proxy
self.state = S_INITIAL
self.active_connection = None
self.connections = []
self.host_tester = None
self.receiver_tester = None
self.jid = None
self.host = None
self.port = None
self.sid = helpers.get_random_string_16()
self.sender_jid = sender_jid
class HostTester(Socks5, IdleObject):
''' fake proxy tester. '''
def __init__(self, host, port, jid, sid, sender_jid, on_success, on_failure):
''' try to establish and auth to proxy at (host, port)
call on_success, or on_failure according to the result'''
self.host = host
self.port = port
self.jid = jid
self.on_success = on_success
self.on_failure = on_failure
self._sock = None
self.file_props = {'is_a_proxy': True,
'proxy_sender': sender_jid,
'proxy_receiver': 'test@gajim.org/test2'}
Socks5.__init__(self, gajim.idlequeue, host, port, None, None, None)
self.sid = sid
def connect(self):
''' create the socket and plug it to the idlequeue '''
if self.host is None:
self.on_failure()
return None
self._sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self._sock.setblocking(False)
self.fd = self._sock.fileno()
self.state = 0 # about to be connected
gajim.idlequeue.plug_idle(self, True, False)
self.do_connect()
self.idlequeue.set_read_timeout(self.fd, CONNECT_TIMEOUT)
return None
def read_timeout(self):
self.idlequeue.remove_timeout(self.fd)
self.pollend()
def pollend(self):
self.disconnect()
self.on_failure()
def pollout(self):
self.idlequeue.remove_timeout(self.fd)
if self.state == 0:
self.do_connect()
return
elif self.state == 1: # send initially: version and auth types
data = self._get_auth_buff()
self.send_raw(data)
else:
return
self.state += 1
# unplug and plug for reading
gajim.idlequeue.plug_idle(self, False, True)
gajim.idlequeue.set_read_timeout(self.fd, CONNECT_TIMEOUT)
def pollin(self):
self.idlequeue.remove_timeout(self.fd)
if self.state == 2:
self.idlequeue.set_read_timeout(self.fd, CONNECT_TIMEOUT)
# begin negotiation. on success 'address' != 0
buff = self.receive()
if buff == '':
# end connection
self.pollend()
return
# read auth response
if buff is None or len(buff) != 2:
return None
version, method = struct.unpack('!BB', buff[:2])
if version != 0x05 or method == 0xff:
self.pollend()
return
data = self._get_request_buff(self._get_sha1_auth())
self.send_raw(data)
self.state += 1
elif self.state == 3:
self.on_success()
self.state += 1
def do_connect(self):
try:
self._sock.connect((self.host, self.port))
self._sock.setblocking(False)
self._send = self._sock.send
self._recv = self._sock.recv
except Exception, ee:
(errnum, errstr) = ee
# 56 is for freebsd
if errnum in (errno.EINPROGRESS, errno.EALREADY, errno.EWOULDBLOCK):
# still trying to connect
return
# win32 needs this
if errnum not in (0, 10056, errno.EISCONN):
# connection failed
self.on_failure()
return
# socket is already connected
self._sock.setblocking(False)
self._send = self._sock.send
self._recv = self._sock.recv
self.buff = ''
self.state = 1 # connected
self.idlequeue.plug_idle(self, True, False)
return
class ReceiverTester(Socks5, IdleObject):
''' fake proxy tester. '''
def __init__(self, host, port, jid, sid, sender_jid, on_success, on_failure):
''' try to establish and auth to proxy at (host, port)
call on_success, or on_failure according to the result'''
self.host = host
self.port = port
self.jid = jid
self.on_success = on_success
self.on_failure = on_failure
self._sock = None
self.file_props = {'is_a_proxy': True,
'proxy_sender': sender_jid,
'proxy_receiver': 'test@gajim.org/test2'}
Socks5.__init__(self, gajim.idlequeue, host, port, None, None, None)
self.sid = sid
def connect(self):
''' create the socket and plug it to the idlequeue '''
if self.host is None:
self.on_failure()
return None
self._sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self._sock.setblocking(False)
self.fd = self._sock.fileno()
self.state = 0 # about to be connected
gajim.idlequeue.plug_idle(self, True, False)
self.do_connect()
self.idlequeue.set_read_timeout(self.fd, CONNECT_TIMEOUT)
return None
def read_timeout(self):
self.idlequeue.remove_timeout(self.fd)
self.pollend()
def pollend(self):
self.disconnect()
self.on_failure()
def pollout(self):
self.idlequeue.remove_timeout(self.fd)
if self.state == 0:
self.do_connect()
return
elif self.state == 1: # send initially: version and auth types
data = self._get_auth_buff()
self.send_raw(data)
else:
return
self.state += 1
# unplug and plug for reading
gajim.idlequeue.plug_idle(self, False, True)
gajim.idlequeue.set_read_timeout(self.fd, CONNECT_TIMEOUT)
def pollin(self):
self.idlequeue.remove_timeout(self.fd)
if self.state in (2, 3):
self.idlequeue.set_read_timeout(self.fd, CONNECT_TIMEOUT)
# begin negotiation. on success 'address' != 0
buff = self.receive()
if buff == '':
# end connection
self.pollend()
return
if self.state == 2:
# read auth response
if buff is None or len(buff) != 2:
return None
version, method = struct.unpack('!BB', buff[:2])
if version != 0x05 or method == 0xff:
self.pollend()
return
data = self._get_request_buff(self._get_sha1_auth())
self.send_raw(data)
self.state += 1
elif self.state == 3:
# read connect response
if buff is None or len(buff) < 2:
return None
version, reply = struct.unpack('!BB', buff[:2])
if version != 0x05 or reply != 0x00:
self.pollend()
return
self.on_success()
self.state += 1
def do_connect(self):
try:
self._sock.connect((self.host, self.port))
self._sock.setblocking(False)
self._send = self._sock.send
self._recv = self._sock.recv
except Exception, ee:
(errnum, errstr) = ee
# 56 is for freebsd
if errnum in (errno.EINPROGRESS, errno.EALREADY, errno.EWOULDBLOCK):
# still trying to connect
return
# win32 needs this
if errnum not in (0, 10056, errno.EISCONN):
# connection failed
self.on_failure()
return
# socket is already connected
self._sock.setblocking(False)
self._send = self._sock.send
self._recv = self._sock.recv
self.buff = ''
self.state = 1 # connected
self.idlequeue.plug_idle(self, True, False)
return
# vim: se ts=3:
|