connect to candidate with highest priority
This commit is contained in:
parent
f4cc439a7e
commit
609ee79791
2 changed files with 137 additions and 103 deletions
|
@ -342,6 +342,10 @@ class JingleFileTransfer(JingleContent):
|
||||||
# send error message, notify the user
|
# send error message, notify the user
|
||||||
return
|
return
|
||||||
def isOurCandUsed(self):
|
def isOurCandUsed(self):
|
||||||
|
'''
|
||||||
|
If this method returns true then the candidate we nominated will be
|
||||||
|
used, if false, the candidate nominated by peer will be used
|
||||||
|
'''
|
||||||
|
|
||||||
if self.nominated_cand['peer-cand'] == False:
|
if self.nominated_cand['peer-cand'] == False:
|
||||||
return True
|
return True
|
||||||
|
@ -352,32 +356,24 @@ class JingleFileTransfer(JingleContent):
|
||||||
our_pr = int(self.nominated_cand['our-cand']['priority'])
|
our_pr = int(self.nominated_cand['our-cand']['priority'])
|
||||||
|
|
||||||
if peer_pr != our_pr:
|
if peer_pr != our_pr:
|
||||||
if peer_pr > our_pr:
|
return peer_pr > our_pr
|
||||||
# Choose peer host
|
|
||||||
return False
|
|
||||||
else:
|
else:
|
||||||
# Choose our host
|
return self.weinitiate
|
||||||
return True
|
|
||||||
else:
|
|
||||||
if self.weinitiate:
|
|
||||||
# Choose our host
|
|
||||||
return True
|
|
||||||
else:
|
|
||||||
# Choose peer host
|
|
||||||
return False
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def start_transfer(self, streamhost_used):
|
def start_transfer(self, streamhost_used):
|
||||||
|
|
||||||
self.state = STATE_TRANSFERING
|
self.state = STATE_TRANSFERING
|
||||||
|
|
||||||
|
# It tells wether we start the transfer as client or server
|
||||||
|
type = None
|
||||||
|
|
||||||
if self.isOurCandUsed():
|
if self.isOurCandUsed():
|
||||||
print 'our'
|
type = 'client'
|
||||||
else:
|
else:
|
||||||
print 'peer'
|
type = 'server'
|
||||||
|
|
||||||
|
|
||||||
|
print type
|
||||||
# FIXME if streamhost_used is none where do we get the proxy host
|
# FIXME if streamhost_used is none where do we get the proxy host
|
||||||
if streamhost_used and streamhost_used['type'] == 'proxy':
|
if streamhost_used and streamhost_used['type'] == 'proxy':
|
||||||
self.file_props['streamhost-used'] = True
|
self.file_props['streamhost-used'] = True
|
||||||
|
@ -401,7 +397,7 @@ class JingleFileTransfer(JingleContent):
|
||||||
else:
|
else:
|
||||||
jid = gajim.get_jid_without_resource(self.session.ourjid)
|
jid = gajim.get_jid_without_resource(self.session.ourjid)
|
||||||
gajim.socks5queue.send_file(self.file_props,
|
gajim.socks5queue.send_file(self.file_props,
|
||||||
self.session.connection.name)
|
self.session.connection.name, type)
|
||||||
|
|
||||||
def get_content(desc):
|
def get_content(desc):
|
||||||
return JingleFileTransfer
|
return JingleFileTransfer
|
||||||
|
|
|
@ -152,11 +152,9 @@ class SocksQueue:
|
||||||
_sock=None, host=str(streamhost['host']),
|
_sock=None, host=str(streamhost['host']),
|
||||||
port=int(streamhost['port']), fingerprint=fp,
|
port=int(streamhost['port']), fingerprint=fp,
|
||||||
connected=False, file_props=file_props)
|
connected=False, file_props=file_props)
|
||||||
#socks5obj.file_props = file_props
|
|
||||||
socks5obj.streamhost = streamhost
|
socks5obj.streamhost = streamhost
|
||||||
self.add_sockobj(account, socks5obj, type='sender')
|
self.add_sockobj(account, socks5obj, type='sender')
|
||||||
|
|
||||||
socks5obj.file_props = file_props
|
|
||||||
streamhost['idx'] = socks5obj.queue_idx
|
streamhost['idx'] = socks5obj.queue_idx
|
||||||
|
|
||||||
def _socket_connected(self, streamhost, file_props):
|
def _socket_connected(self, streamhost, file_props):
|
||||||
|
@ -242,12 +240,13 @@ class SocksQueue:
|
||||||
|
|
||||||
def add_sockobj(self, account, sockobj, type='receiver'):
|
def add_sockobj(self, account, sockobj, type='receiver'):
|
||||||
"""
|
"""
|
||||||
Add new file a sockobj type receiver or sendder
|
Add new file a sockobj type receiver or sender, and use it to connect
|
||||||
|
to server
|
||||||
"""
|
"""
|
||||||
if type == 'receiver':
|
if type == 'receiver':
|
||||||
self.readers[self.idx] = sockobj
|
self._add(sockobj, self.readers, sockobj.file_props, self.idx)
|
||||||
else:
|
else:
|
||||||
self.senders[self.idx] = sockobj
|
self._add(sockobj, self.senders, sockobj.file_props, self.idx)
|
||||||
sockobj.queue_idx = self.idx
|
sockobj.queue_idx = self.idx
|
||||||
sockobj.queue = self
|
sockobj.queue = self
|
||||||
sockobj.account = account
|
sockobj.account = account
|
||||||
|
@ -260,6 +259,13 @@ class SocksQueue:
|
||||||
return 1
|
return 1
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
def _add(self, sockobj, sockobjects, fp, hash):
|
||||||
|
'''
|
||||||
|
Adds the sockobj to the current list of sockobjects
|
||||||
|
'''
|
||||||
|
keys = (fp['sid'], fp['name'], hash)
|
||||||
|
sockobjects[keys] = sockobj
|
||||||
|
|
||||||
|
|
||||||
def result_sha(self, sha_str, idx):
|
def result_sha(self, sha_str, idx):
|
||||||
if sha_str in self.sha_handlers:
|
if sha_str in self.sha_handlers:
|
||||||
|
@ -267,9 +273,11 @@ class SocksQueue:
|
||||||
props[0](props[1], idx)
|
props[0](props[1], idx)
|
||||||
|
|
||||||
def activate_proxy(self, idx):
|
def activate_proxy(self, idx):
|
||||||
if idx not in self.readers:
|
if not self.isHashInSockObjs(self.readers, idx):
|
||||||
return
|
return
|
||||||
reader = self.readers[idx]
|
for key in self.readers.keys():
|
||||||
|
if idx in key:
|
||||||
|
reader = self.readers[key]
|
||||||
if reader.file_props['type'] != 's':
|
if reader.file_props['type'] != 's':
|
||||||
return
|
return
|
||||||
if reader.state != 5:
|
if reader.state != 5:
|
||||||
|
@ -292,10 +300,13 @@ class SocksQueue:
|
||||||
result = reader.write_next()
|
result = reader.write_next()
|
||||||
self.process_result(result, reader)
|
self.process_result(result, reader)
|
||||||
|
|
||||||
def send_file(self, file_props, account):
|
def send_file(self, file_props, account, type):
|
||||||
if 'hash' in file_props and file_props['hash'] in self.senders:
|
for key in self.senders.keys():
|
||||||
|
if file_props['name'] in key and file_props['sid'] in key \
|
||||||
|
and self.senders[key].mode == type:
|
||||||
|
|
||||||
log.info("socks5: sending file")
|
log.info("socks5: sending file")
|
||||||
sender = self.senders[file_props['hash']]
|
sender = self.senders[key]
|
||||||
file_props['streamhost-used'] = True
|
file_props['streamhost-used'] = True
|
||||||
sender.account = account
|
sender.account = account
|
||||||
if file_props['type'] == 's':
|
if file_props['type'] == 's':
|
||||||
|
@ -308,9 +319,6 @@ class SocksQueue:
|
||||||
file_props['received-len'] = 0
|
file_props['received-len'] = 0
|
||||||
sender.file_props = file_props
|
sender.file_props = file_props
|
||||||
|
|
||||||
else:
|
|
||||||
|
|
||||||
log.info("socks5: NOT sending file")
|
|
||||||
|
|
||||||
def add_file_props(self, account, file_props):
|
def add_file_props(self, account, file_props):
|
||||||
"""
|
"""
|
||||||
|
@ -347,29 +355,48 @@ class SocksQueue:
|
||||||
return fl_props[sid]
|
return fl_props[sid]
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
def isHashInSockObjs(self, sockobjs, hash):
|
||||||
|
'''
|
||||||
|
It tells wether there is a particular hash in sockobjs or not
|
||||||
|
'''
|
||||||
|
for key in sockobjs:
|
||||||
|
if hash in key:
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
def on_connection_accepted(self, sock, listener):
|
def on_connection_accepted(self, sock, listener):
|
||||||
sock_hash = sock.__hash__()
|
sock_hash = sock.__hash__()
|
||||||
if self.type == 'sender' and (sock_hash not in self.senders):
|
if self.type == 'sender' and \
|
||||||
self.senders[sock_hash] = Socks5Sender(self.idlequeue, sock_hash, self,
|
not self.isHashInSockObjs(self.senders, sock_hash):
|
||||||
|
|
||||||
|
sockobj = Socks5Sender(self.idlequeue, sock_hash, self,
|
||||||
'server', sock[0], sock[1][0], sock[1][1],
|
'server', sock[0], sock[1][0], sock[1][1],
|
||||||
fingerprint='server', file_props=listener.file_props)
|
fingerprint='server', file_props=listener.file_props)
|
||||||
|
self._add(sockobj, self.senders, listener.file_props, sock_hash)
|
||||||
# Start waiting for data
|
# Start waiting for data
|
||||||
self.idlequeue.plug_idle(self.senders[sock_hash], False, True)
|
self.idlequeue.plug_idle(sockobj, False, True)
|
||||||
self.connected += 1
|
self.connected += 1
|
||||||
|
|
||||||
if self.type == 'receiver' and (sock_hash not in self.readers):
|
if self.type == 'receiver' and \
|
||||||
|
not self.isHashInSockObjs(self.readers, sock_hash):
|
||||||
sh = {}
|
sh = {}
|
||||||
sh['host'] = sock[1][0]
|
sh['host'] = sock[1][0]
|
||||||
sh['port'] = sock[1][1]
|
sh['port'] = sock[1][1]
|
||||||
sh['initiator'] = None
|
sh['initiator'] = None
|
||||||
sh['target'] = None
|
sh['target'] = None
|
||||||
self.readers[sock_hash] = Socks5Receiver(idlequeue=self.idlequeue,
|
|
||||||
streamhost=sh,sid=None, file_props=None,
|
sockobj = Socks5Receiver(idlequeue=self.idlequeue,
|
||||||
|
streamhost=sh,sid=None,
|
||||||
|
file_props=listener.file_props,
|
||||||
mode='server',fingerprint='server')
|
mode='server',fingerprint='server')
|
||||||
self.readers[sock_hash].set_sock(sock[0])
|
|
||||||
self.readers[sock_hash].queue = self
|
self._add(sockobj, self.readers, listener.file_props, sock_hash)
|
||||||
|
|
||||||
|
sockobj.set_sock(sock[0])
|
||||||
|
sockobj.queue = self
|
||||||
self.connected += 1
|
self.connected += 1
|
||||||
|
|
||||||
|
|
||||||
def process_result(self, result, actor):
|
def process_result(self, result, actor):
|
||||||
"""
|
"""
|
||||||
Take appropriate actions upon the result:
|
Take appropriate actions upon the result:
|
||||||
|
@ -393,8 +420,9 @@ class SocksQueue:
|
||||||
connections with 1
|
connections with 1
|
||||||
"""
|
"""
|
||||||
if idx != -1:
|
if idx != -1:
|
||||||
if idx in self.readers:
|
for key in self.readers.keys():
|
||||||
reader = self.readers[idx]
|
if idx in key:
|
||||||
|
reader = self.readers[key]
|
||||||
self.idlequeue.unplug_idle(reader.fd)
|
self.idlequeue.unplug_idle(reader.fd)
|
||||||
self.idlequeue.remove_timeout(reader.fd)
|
self.idlequeue.remove_timeout(reader.fd)
|
||||||
if do_disconnect:
|
if do_disconnect:
|
||||||
|
@ -402,7 +430,7 @@ class SocksQueue:
|
||||||
else:
|
else:
|
||||||
if reader.streamhost is not None:
|
if reader.streamhost is not None:
|
||||||
reader.streamhost['state'] = -1
|
reader.streamhost['state'] = -1
|
||||||
del(self.readers[idx])
|
del(self.readers[key])
|
||||||
|
|
||||||
def remove_sender(self, idx, do_disconnect=True):
|
def remove_sender(self, idx, do_disconnect=True):
|
||||||
"""
|
"""
|
||||||
|
@ -410,15 +438,16 @@ class SocksQueue:
|
||||||
connections with 1
|
connections with 1
|
||||||
"""
|
"""
|
||||||
if idx != -1:
|
if idx != -1:
|
||||||
if idx in self.senders:
|
for key in self.senders.keys():
|
||||||
sender = self.senders[idx]
|
if idx in key:
|
||||||
|
sender = self.senders[key]
|
||||||
if do_disconnect:
|
if do_disconnect:
|
||||||
self.senders[idx].disconnect()
|
sender.disconnect()
|
||||||
return
|
return
|
||||||
else:
|
else:
|
||||||
self.idlequeue.unplug_idle(sender.fd)
|
self.idlequeue.unplug_idle(sender.fd)
|
||||||
self.idlequeue.remove_timeout(sender.fd)
|
self.idlequeue.remove_timeout(sender.fd)
|
||||||
del(self.senders[idx])
|
del(self.senders[key])
|
||||||
if self.connected > 0:
|
if self.connected > 0:
|
||||||
self.connected -= 1
|
self.connected -= 1
|
||||||
if len(self.senders) == 0 and self.listener is not None:
|
if len(self.senders) == 0 and self.listener is not None:
|
||||||
|
@ -702,13 +731,23 @@ class Socks5:
|
||||||
self.disconnect()
|
self.disconnect()
|
||||||
|
|
||||||
elif self.state == 5:
|
elif self.state == 5:
|
||||||
|
self.state = 7
|
||||||
if self.type == 'sender':
|
if self.type == 'sender':
|
||||||
# We wait for the end of the negotiation to
|
# We wait for the end of the negotiation to
|
||||||
# send the file
|
# send the file
|
||||||
self.state = 7
|
|
||||||
self.idlequeue.plug_idle(self, False, False)
|
self.idlequeue.plug_idle(self, False, False)
|
||||||
else:
|
else:
|
||||||
result = self.start_transfer() # receive
|
# We plug for reading
|
||||||
|
self.idlequeue.plug_idle(self, False, True)
|
||||||
|
return
|
||||||
|
|
||||||
|
elif self.state == 7:
|
||||||
|
if self.file_props['paused']:
|
||||||
|
self.file_props['continue_cb'] = self.continue_paused_transfer
|
||||||
|
self.idlequeue.plug_idle(self, False, False)
|
||||||
|
return
|
||||||
|
self.idlequeue.set_read_timeout(self.fd, STALLED_TIMEOUT)
|
||||||
|
result = self.start_transfer() # send
|
||||||
self.queue.process_result(result, self)
|
self.queue.process_result(result, self)
|
||||||
except (OpenSSL.SSL.WantReadError, OpenSSL.SSL.WantWriteError,
|
except (OpenSSL.SSL.WantReadError, OpenSSL.SSL.WantWriteError,
|
||||||
OpenSSL.SSL.WantX509LookupError), e:
|
OpenSSL.SSL.WantX509LookupError), e:
|
||||||
|
@ -770,8 +809,8 @@ class Socks5:
|
||||||
if self.file_props['stalled'] == False:
|
if self.file_props['stalled'] == False:
|
||||||
self.file_props['stalled'] = True
|
self.file_props['stalled'] = True
|
||||||
self.queue.process_result(-1, self)
|
self.queue.process_result(-1, self)
|
||||||
#if 'received-len' not in self.file_props:
|
if 'received-len' not in self.file_props:
|
||||||
# self.file_props['received-len'] = 0
|
self.file_props['received-len'] = 0
|
||||||
if SEND_TIMEOUT > 0:
|
if SEND_TIMEOUT > 0:
|
||||||
self.idlequeue.set_read_timeout(self.fd, SEND_TIMEOUT)
|
self.idlequeue.set_read_timeout(self.fd, SEND_TIMEOUT)
|
||||||
else:
|
else:
|
||||||
|
@ -1322,13 +1361,11 @@ class Socks5Receiver(Socks5, IdleObject):
|
||||||
self.queue_idx = -1
|
self.queue_idx = -1
|
||||||
self.streamhost = streamhost
|
self.streamhost = streamhost
|
||||||
self.queue = None
|
self.queue = None
|
||||||
self.file_props = file_props
|
|
||||||
self.fingerprint = fingerprint
|
self.fingerprint = fingerprint
|
||||||
self.connect_timeout = 0
|
self.connect_timeout = 0
|
||||||
self.connected = False
|
self.connected = False
|
||||||
self.pauses = 0
|
self.pauses = 0
|
||||||
if not self.file_props:
|
self.file_props = file_props
|
||||||
self.file_props = {}
|
|
||||||
self.file_props['disconnect_cb'] = self.disconnect
|
self.file_props['disconnect_cb'] = self.disconnect
|
||||||
self.file_props['error'] = 0
|
self.file_props['error'] = 0
|
||||||
self.file_props['started'] = True
|
self.file_props['started'] = True
|
||||||
|
@ -1336,6 +1373,7 @@ class Socks5Receiver(Socks5, IdleObject):
|
||||||
self.file_props['paused'] = False
|
self.file_props['paused'] = False
|
||||||
self.file_props['continue_cb'] = self.continue_paused_transfer
|
self.file_props['continue_cb'] = self.continue_paused_transfer
|
||||||
self.file_props['stalled'] = False
|
self.file_props['stalled'] = False
|
||||||
|
self.file_props['received-len'] = 0
|
||||||
self.mode = mode # client or server
|
self.mode = mode # client or server
|
||||||
Socks5.__init__(self, idlequeue, streamhost['host'],
|
Socks5.__init__(self, idlequeue, streamhost['host'],
|
||||||
int(streamhost['port']), streamhost['initiator'], streamhost['target'],
|
int(streamhost['port']), streamhost['initiator'], streamhost['target'],
|
||||||
|
|
Loading…
Add table
Reference in a new issue