changeset f734ef0113c2 in /home/hg/repos/gajim
details:http://hg.gajim.org/gajim?cmd=changeset;node=f734ef0113c2 description: socks5 proxy fixed
diffstat:
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(-)
diffs (truncated from 501 to 300 lines):
diff -r 505b0680b4cb -r f734ef0113c2 src/common/jingle_content.py --- a/src/common/jingle_content.py Wed Aug 24 23:53:36 2011 +0200 +++ b/src/common/jingle_content.py Sat Oct 29 00:09:45 2011 -0400 @@ -101,7 +101,6 @@ 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 @@ 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 @@ 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 @@ tp.addChild(name='candidate-error') content.addChild(node=tp) self.session.send_transport_info(content) - +
def send_description_info(self): content = self.__content() diff -r 505b0680b4cb -r f734ef0113c2 src/common/jingle_ft.py --- a/src/common/jingle_ft.py Wed Aug 24 23:53:36 2011 +0200 +++ b/src/common/jingle_ft.py Sat Oct 29 00:09:45 2011 -0400 @@ -24,7 +24,7 @@ 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 @@ 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 @@ 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 @@
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 @@
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 -r 505b0680b4cb -r f734ef0113c2 src/common/jingle_transport.py --- a/src/common/jingle_transport.py Wed Aug 24 23:53:36 2011 +0200 +++ b/src/common/jingle_transport.py Sat Oct 29 00:09:45 2011 -0400 @@ -118,7 +118,7 @@ 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 @@ '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 @@ 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 @@ 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 @@ 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 @@ 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 @@ # 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: