From 212f33cafa9cf2d65ea659a5cc09710934cf8fae Mon Sep 17 00:00:00 2001 From: Jefry Lagrange Date: Sat, 29 Oct 2011 00:09:45 -0400 Subject: [PATCH] socks5 proxy fixed --- src/common/jingle_content.py | 10 ++-- src/common/jingle_ft.py | 75 ++++++++++++++++++---------- src/common/jingle_transport.py | 82 +++++++++++++++++++------------ src/common/protocol/bytestream.py | 4 +- src/common/socks5.py | 40 ++++++++------- 5 files changed, 127 insertions(+), 84 deletions(-) diff --git a/src/common/jingle_content.py b/src/common/jingle_content.py index dcc7e5bf8..079000f85 100644 --- a/src/common/jingle_content.py +++ b/src/common/jingle_content.py @@ -101,7 +101,6 @@ class JingleContent(object): Add a list of candidates to the list of remote candidates """ self.transport.remote_candidates = candidates - pass def on_stanza(self, stanza, content, error, action): """ @@ -112,15 +111,14 @@ class JingleContent(object): callback(stanza, content, error, action) def __on_transport_replace(self, stanza, content, error, action): - content.addChild(node=self.transport.make_transport()) - + def __on_transport_info(self, stanza, content, error, action): """ Got a new transport candidate """ candidates = self.transport.parse_transport_stanza( - content.getTag('transport')) + content.getTag('transport')) if candidates: self.add_remote_candidates(candidates) @@ -139,7 +137,7 @@ class JingleContent(object): content = self.__content() content.addChild(node=self.transport.make_transport([candidate])) self.session.send_transport_info(content) - + def send_error_candidate(self): """ Sends a candidate-error when we can't connect to a candidate. @@ -149,7 +147,7 @@ class JingleContent(object): tp.addChild(name='candidate-error') content.addChild(node=tp) self.session.send_transport_info(content) - + def send_description_info(self): content = self.__content() diff --git a/src/common/jingle_ft.py b/src/common/jingle_ft.py index 6b9d5bcda..99d76831c 100644 --- a/src/common/jingle_ft.py +++ b/src/common/jingle_ft.py @@ -24,7 +24,7 @@ import xmpp from jingle_content import contents, JingleContent from jingle_transport import JingleTransportICEUDP, JingleTransportSocks5 from common import helpers -from common.socks5 import Socks5Receiver +from common.socks5 import Socks5Receiver, Socks5Sender from common.connection_handlers_events import FileRequestReceivedEvent import logging @@ -181,6 +181,14 @@ class JingleFileTransfer(JingleContent): self.state = STATE_CAND_RECEIVED_PENDING_REPLY return + + if content.getTag('transport').getTag('activated'): + self.state = STATE_TRANSFERING + jid = gajim.get_jid_without_resource(self.session.ourjid) + gajim.socks5queue.send_file(self.file_props, + self.session.connection.name, 'client') + return + streamhost_cid = content.getTag('transport').getTag('candidate-used').\ getAttr('cid') streamhost_used = None @@ -274,7 +282,7 @@ class JingleFileTransfer(JingleContent): self.session.send_transport_info(content) - def _on_connect_error(self, to, _id, sid, code=404): + def _on_connect_error(self, sid): self.nominated_cand['our-cand'] = False self.send_error_candidate() @@ -321,7 +329,6 @@ class JingleFileTransfer(JingleContent): receiver = self.file_props['receiver'] sender = self.file_props['sender'] - sha_str = helpers.get_auth_sha(self.file_props['sid'], sender, receiver) self.file_props['sha_str'] = sha_str @@ -368,36 +375,52 @@ class JingleFileTransfer(JingleContent): self.state = STATE_TRANSFERING - # It tells wether we start the transfer as client or server - type = None - if self.isOurCandUsed(): - type = 'client' streamhost_used = self.nominated_cand['our-cand'] else: - type = 'server' streamhost_used = self.nominated_cand['peer-cand'] + + if streamhost_used['type'] == 'proxy': + self.file_props['is_a_proxy'] = True - # FIXME if streamhost_used is none where do we get the proxy host - if streamhost_used and streamhost_used['type'] == 'proxy': + if not self.weinitiate and streamhost_used['type'] == 'proxy': + r = gajim.socks5queue.readers + for reader in r: + if r[reader].host == streamhost_used['host'] and \ + r[reader].connected: + return + + if streamhost_used['type'] == 'proxy': self.file_props['streamhost-used'] = True - for proxy in self.file_props['proxyhosts']: - if proxy['host'] == streamhost_used['host'] and \ - proxy['port'] == streamhost_used['port'] and \ - proxy['jid'] == streamhost_used['jid']: - host_used = proxy - break - if 'streamhosts' not in self.file_props: - self.file_props['streamhosts'] = [] - self.file_props['streamhosts'].append(streamhost_used) - self.file_props['is_a_proxy'] = True - receiver = Socks5Receiver(gajim.idlequeue, streamhost_used, - self.file_props['sid'], self.file_props) - gajim.socks5queue.add_receiver(self.session.connection.name, - receiver) - streamhost_used['idx'] = receiver.queue_idx + streamhost_used['sid'] = self.file_props['sid'] + self.file_props['streamhosts'] = [] + self.file_props['streamhosts'].append(streamhost_used) + self.file_props['proxyhosts'] = [] + self.file_props['proxyhosts'].append(streamhost_used) + self.file_props['is_a_proxy'] = True + + gajim.socks5queue.idx += 1 + idx = gajim.socks5queue.idx + sockobj = Socks5Sender(gajim.idlequeue, idx, + gajim.socks5queue, + mode='client', + _sock=None, + host=str(streamhost_used['host']), + port=int(streamhost_used['port']), + fingerprint=None, + connected=False, + file_props=self.file_props) + sockobj.proxy = True + sockobj.streamhost = streamhost_used + gajim.socks5queue.add_sockobj(self.session.connection.name, + sockobj, 'sender') + streamhost_used['idx'] = sockobj.queue_idx + # If we offered the nominated candidate used, we activate + # the proxy + if not self.isOurCandUsed(): gajim.socks5queue.on_success[self.file_props['sid']] = \ - self.transport._on_proxy_auth_ok + self.transport._on_proxy_auth_ok + # TODO: add on failure else: jid = gajim.get_jid_without_resource(self.session.ourjid) gajim.socks5queue.send_file(self.file_props, diff --git a/src/common/jingle_transport.py b/src/common/jingle_transport.py index 46376045e..868e0f373 100644 --- a/src/common/jingle_transport.py +++ b/src/common/jingle_transport.py @@ -118,7 +118,7 @@ class JingleTransportSocks5(JingleTransport): return xmpp.Node('candidate', attrs=attrs) def make_transport(self, candidates=None, add_candidates = True): - if add_candidates: + if add_candidates: self._add_local_ips_as_candidates() self._add_additional_candidates() self._add_proxy_candidates() @@ -139,10 +139,10 @@ class JingleTransportSocks5(JingleTransport): 'state': 0, 'target': self.ourjid, 'host': candidate['host'], - 'port': candidate['port'], + 'port': int(candidate['port']), 'cid': candidate['cid'], 'type': typ, - 'priority': candidate['priority'] + 'priority': candidate['priority'] } candidates.append(cand) @@ -151,11 +151,22 @@ class JingleTransportSocks5(JingleTransport): return candidates + def _add_candidates(self, candidates): + for cand in candidates: + in_remote = False + for cand2 in self.remote_candidates: + if cand['host'] == cand2['host'] and \ + cand['port'] == cand2['port']: + in_remote = True + break + if not in_remote: + self.candidates.append(cand) + def _add_local_ips_as_candidates(self): if not self.connection: return local_ip_cand = [] - port = gajim.config.get('file_transfers_port') + port = int(gajim.config.get('file_transfers_port')) type_preference = 126 #type preference of connection type. XEP-0260 section 2.2 c = {'host': self.connection.peerhost[0]} c['candidate_id'] = self.connection.connection.getAnID() @@ -178,14 +189,14 @@ class JingleTransportSocks5(JingleTransport): c['target'] = self.file_props['receiver'] local_ip_cand.append(c) - self.candidates += local_ip_cand + self._add_candidates(local_ip_cand) def _add_additional_candidates(self): if not self.connection: return type_preference = 126 additional_ip_cand = [] - port = gajim.config.get('file_transfers_port') + port = int(gajim.config.get('file_transfers_port')) ft_add_hosts = gajim.config.get('ft_add_hosts_to_send') if ft_add_hosts: @@ -200,7 +211,8 @@ class JingleTransportSocks5(JingleTransport): c['initiator'] = self.file_props['sender'] c['target'] = self.file_props['receiver'] additional_ip_cand.append(c) - self.candidates += additional_ip_cand + + self._add_candidates(additional_ip_cand) def _add_proxy_candidates(self): if not self.connection: @@ -219,14 +231,15 @@ class JingleTransportSocks5(JingleTransport): for proxyhost in proxyhosts: c = {'host': proxyhost['host']} c['candidate_id'] = self.connection.connection.getAnID() - c['port'] = proxyhost['port'] + c['port'] = int(proxyhost['port']) c['type'] = 'proxy' c['jid'] = proxyhost['jid'] c['priority'] = (2**16) * type_preference c['initiator'] = self.file_props['sender'] c['target'] = self.file_props['receiver'] proxy_cand.append(c) - self.candidates += proxy_cand + + self._add_candidates(proxy_cand) def get_content(self): sesn = self.connection.get_jingle_session(self.ourjid, @@ -240,54 +253,59 @@ class JingleTransportSocks5(JingleTransport): # send activate request to proxy, send activated confirmation to peer if not self.connection: return - file_props = self.file_props - iq = xmpp.Iq(to=proxy['initiator'], typ='set') + sesn = self.connection.get_jingle_session(self.ourjid, + self.file_props['session-sid']) + if sesn is None: + return + + iq = xmpp.Iq(to=proxy['jid'], frm=self.ourjid, typ='set') auth_id = "au_" + proxy['sid'] iq.setID(auth_id) query = iq.setTag('query', namespace=xmpp.NS_BYTESTREAM) query.setAttr('sid', proxy['sid']) activate = query.setTag('activate') - activate.setData(file_props['proxy_receiver']) + activate.setData(sesn.peerjid) iq.setID(auth_id) self.connection.connection.send(iq) + content = xmpp.Node('content') content.setAttr('creator', 'initiator') c = self.get_content() content.setAttr('name', c.name) transport = xmpp.Node('transport') transport.setNamespace(xmpp.NS_JINGLE_BYTESTREAM) + transport.setAttr('sid', proxy['sid']) activated = xmpp.Node('activated') cid = None - for host in self.candidates: - if host['host'] == proxy['host'] and host['jid'] == proxy['jid'] \ - and host['port'] == proxy['port']: - cid = host['candidate_id'] - break + + if 'cid' in proxy: + cid = proxy['cid'] + else: + for host in self.candidates: + if host['host'] == proxy['host'] and host['jid'] == proxy['jid'] \ + and host['port'] == proxy['port']: + cid = host['candidate_id'] + break if cid is None: - return + raise Exception, 'cid is missing' activated.setAttr('cid', cid) transport.addChild(node=activated) content.addChild(node=transport) - sesn = self.connection.get_jingle_session(self.ourjid, - self.file_props['session-sid']) - - if sesn is None: - return sesn.send_transport_info(content) class JingleTransportIBB(JingleTransport): - + def __init__(self, node=None, block_sz=None): - + JingleTransport.__init__(self, TransportType.streaming) - + if block_sz: self.block_sz = block_sz else: self.block_sz = '4096' - + self.connection = None self.sid = None if node and node.getAttr('sid'): @@ -296,19 +314,19 @@ class JingleTransportIBB(JingleTransport): def set_sid(self, sid): self.sid = sid - + def make_transport(self): - + transport = xmpp.Node('transport') transport.setNamespace(xmpp.NS_JINGLE_IBB) transport.setAttr('block-size', self.block_sz) transport.setAttr('sid', self.sid) - return transport - + return transport + def set_file_props(self, file_props): self.file_props = file_props - + import farsight class JingleTransportICEUDP(JingleTransport): diff --git a/src/common/protocol/bytestream.py b/src/common/protocol/bytestream.py index 561ae3cae..cf4edd546 100644 --- a/src/common/protocol/bytestream.py +++ b/src/common/protocol/bytestream.py @@ -363,7 +363,7 @@ class ConnectionSocks5Bytestream(ConnectionBytestream): port = gajim.config.get('file_transfers_port') listener = gajim.socks5queue.start_listener(port, sha_str, - self._result_socks5_sid, file_props['sid']) + self._result_socks5_sid, file_props) if not listener: file_props['error'] = -5 from common.connection_handlers_events import FileRequestErrorEvent @@ -660,7 +660,7 @@ class ConnectionSocks5Bytestream(ConnectionBytestream): if 'stopped' in file_props and file_props['stopped']: self.remove_transfer(file_props) else: - gajim.socks5queue.send_file(file_props, self.name) + gajim.socks5queue.send_file(file_props, self.name, 'client') if 'fast' in file_props: fasts = file_props['fast'] if len(fasts) > 0: diff --git a/src/common/socks5.py b/src/common/socks5.py index 5c7e436ae..91849a296 100644 --- a/src/common/socks5.py +++ b/src/common/socks5.py @@ -113,8 +113,8 @@ class SocksQueue: file_props['streamhost-used'] is True: if 'proxyhosts' in file_props: for proxy in file_props['proxyhosts']: - if proxy == streamhost: - self.on_success[file_props['sid']](streamhost) + if proxy['host'] == streamhost['host']: + self.on_success[file_props['sid']](proxy) return 2 return 0 if 'streamhosts' in file_props: @@ -145,8 +145,13 @@ class SocksQueue: 'client', file_props, fingerprint=fp) self.add_sockobj(account, socks5obj) else: + if 'sha_str' in file_props: + idx = file_props['sha_str'] + else: + idx = self.idx + self.idx = self.idx + 1 self.type = 'sender' - socks5obj = Socks5Sender(self.idlequeue, file_props['sha_str'], + socks5obj = Socks5Sender(self.idlequeue, idx, self, mode='client' , _sock=None, host=str(streamhost['host']), port=int(streamhost['port']), fingerprint=fp, connected=False, file_props=file_props) @@ -202,7 +207,6 @@ class SocksQueue: if host['state'] == -2: host['state'] = 0 # FIXME: make the sender reconnect also - print 'reconnecting using socks receiver' client = Socks5Receiver(self.idlequeue, host, host['sid'], 'client',file_props) self.add_sockobj(client.account, client) @@ -225,15 +229,16 @@ class SocksQueue: if file_props is None: return streamhost['state'] = -1 + # FIXME: should only the receiver be remove? what if we are sending? self.remove_receiver(idx, False) if 'streamhosts' in file_props: for host in file_props['streamhosts']: if host['state'] != -1: return + self.readers = {} # failure_cb exists - this means that it has never been called if 'failure_cb' in file_props and file_props['failure_cb']: - file_props['failure_cb'](streamhost['initiator'], None, - file_props['sid'], code = 404) + file_props['failure_cb'](file_props['sid']) del(file_props['failure_cb']) def add_sockobj(self, account, sockobj, type='receiver'): @@ -306,15 +311,10 @@ class SocksQueue: sender = self.senders[key] file_props['streamhost-used'] = True sender.account = account - if file_props['type'] == 's': - sender.file_props = file_props - result = sender.send_file() - self.process_result(result, sender) - else: - file_props['elapsed-time'] = 0 - file_props['last-time'] = self.idlequeue.current_time() - file_props['received-len'] = 0 - sender.file_props = file_props + + sender.file_props = file_props + result = sender.send_file() + self.process_result(result, sender) def add_file_props(self, account, file_props): """ @@ -611,6 +611,10 @@ class Socks5: if self.queue.on_success: result = self.queue.send_success_reply(self.file_props, self.streamhost) + if self.type == 'sender' and self.proxy: + self.queue.process_result( self.send_file() + , self) + return if result == 0: self.state = 8 @@ -1143,6 +1147,7 @@ class Socks5: """ Get sha of sid + Initiator jid + Target jid """ + if 'is_a_proxy' in self.file_props: del(self.file_props['is_a_proxy']) return hashlib.sha1('%s%s%s' % (self.sid, @@ -1164,10 +1169,10 @@ class Socks5Sender(Socks5, IdleObject): self.queue = parent self.mode = mode # client or server self.file_props = file_props + self.proxy = False - - Socks5.__init__(self, idlequeue, host, port, None, None, None) + Socks5.__init__(self, idlequeue, host, port, None, None,file_props['sid']) self._sock = _sock @@ -1380,7 +1385,6 @@ class Socks5Receiver(Socks5, IdleObject): """ Start receiving the file over verified connection """ - print "receiving file" if self.file_props['started']: return self.file_props['error'] = 0