2018-09-05 02:59:34 +02:00
|
|
|
# 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/>.
|
2012-04-05 03:04:23 +02:00
|
|
|
|
2012-12-09 21:37:51 +01:00
|
|
|
import nbxmpp
|
2017-08-13 13:18:56 +02:00
|
|
|
from gajim.common import app
|
2017-06-13 23:58:06 +02:00
|
|
|
from gajim.common.jingle_transport import TransportType
|
|
|
|
from gajim.common.socks5 import Socks5ReceiverClient, Socks5SenderClient
|
2012-04-05 03:04:23 +02:00
|
|
|
|
2017-02-08 03:49:33 +01:00
|
|
|
import logging
|
|
|
|
log = logging.getLogger('gajim.c.jingle_ftstates')
|
|
|
|
|
2012-08-23 23:45:38 +02:00
|
|
|
|
2012-04-05 03:04:23 +02:00
|
|
|
class JingleFileTransferStates:
|
2012-08-22 12:21:45 +02:00
|
|
|
'''
|
|
|
|
This class implements the state machine design pattern
|
|
|
|
'''
|
2012-04-05 03:04:23 +02:00
|
|
|
|
|
|
|
def __init__(self, jingleft):
|
|
|
|
self.jft = jingleft
|
|
|
|
|
|
|
|
def action(self, args=None):
|
|
|
|
'''
|
|
|
|
This method MUST be overriden by a subclass
|
|
|
|
'''
|
2017-02-08 03:49:33 +01:00
|
|
|
raise NotImplementedError('This is an abstract method!')
|
2012-04-05 03:04:23 +02:00
|
|
|
|
|
|
|
|
|
|
|
class StateInitialized(JingleFileTransferStates):
|
|
|
|
'''
|
|
|
|
This state initializes the file transfer
|
|
|
|
'''
|
|
|
|
|
|
|
|
def action(self, args=None):
|
|
|
|
if self.jft.weinitiate:
|
|
|
|
# update connection's fileprops
|
2012-08-22 12:21:45 +02:00
|
|
|
self.jft._listen_host()
|
2012-04-05 03:04:23 +02:00
|
|
|
# Listen on configured port for file transfer
|
|
|
|
else:
|
|
|
|
fingerprint = None
|
|
|
|
if self.jft.use_security:
|
|
|
|
fingerprint = 'client'
|
|
|
|
# Connect to the candidate host, on success call on_connect method
|
2017-08-13 13:18:56 +02:00
|
|
|
app.socks5queue.connect_to_hosts(self.jft.session.connection.name,
|
2017-08-16 15:27:35 +02:00
|
|
|
self.jft.file_props.transport_sid, self.jft.on_connect,
|
|
|
|
self.jft._on_connect_error, fingerprint=fingerprint)
|
2012-04-05 03:04:23 +02:00
|
|
|
|
|
|
|
|
|
|
|
class StateCandSent(JingleFileTransferStates):
|
|
|
|
'''
|
|
|
|
This state sends our nominated candidate
|
|
|
|
'''
|
|
|
|
|
2017-02-08 03:49:33 +01:00
|
|
|
def _send_candidate(self, args):
|
2012-04-05 03:04:23 +02:00
|
|
|
if 'candError' in args:
|
|
|
|
self.jft.nominated_cand['our-cand'] = False
|
|
|
|
self.jft.send_error_candidate()
|
|
|
|
return
|
|
|
|
# Send candidate used
|
|
|
|
streamhost = args['streamhost']
|
|
|
|
self.jft.nominated_cand['our-cand'] = streamhost
|
2012-12-09 21:37:51 +01:00
|
|
|
content = nbxmpp.Node('content')
|
2012-04-05 03:04:23 +02:00
|
|
|
content.setAttr('creator', 'initiator')
|
|
|
|
content.setAttr('name', self.jft.name)
|
2012-12-09 21:37:51 +01:00
|
|
|
transport = nbxmpp.Node('transport')
|
|
|
|
transport.setNamespace(nbxmpp.NS_JINGLE_BYTESTREAM)
|
2012-04-05 03:04:23 +02:00
|
|
|
transport.setAttr('sid', self.jft.transport.sid)
|
2012-12-09 21:37:51 +01:00
|
|
|
candidateused = nbxmpp.Node('candidate-used')
|
2017-08-12 22:54:08 +02:00
|
|
|
candidateused.setAttr('cid', streamhost['candidate_id'])
|
2012-04-05 03:04:23 +02:00
|
|
|
transport.addChild(node=candidateused)
|
|
|
|
content.addChild(node=transport)
|
|
|
|
self.jft.session.send_transport_info(content)
|
|
|
|
|
|
|
|
def action(self, args=None):
|
2017-02-08 03:49:33 +01:00
|
|
|
self._send_candidate(args)
|
2012-04-05 03:04:23 +02:00
|
|
|
|
2012-08-23 23:45:38 +02:00
|
|
|
|
2017-02-08 03:49:33 +01:00
|
|
|
class StateCandReceived(JingleFileTransferStates):
|
2012-04-05 03:04:23 +02:00
|
|
|
'''
|
|
|
|
This state happens when we receive a candidate.
|
|
|
|
It takes the arguments: canError if we receive a candidate-error
|
|
|
|
'''
|
|
|
|
|
2017-02-08 03:49:33 +01:00
|
|
|
def _recv_candidate(self, args):
|
2012-04-05 03:04:23 +02:00
|
|
|
if 'candError' in args:
|
|
|
|
return
|
|
|
|
content = args['content']
|
|
|
|
streamhost_cid = content.getTag('transport').getTag('candidate-used').\
|
|
|
|
getAttr('cid')
|
|
|
|
streamhost_used = None
|
|
|
|
for cand in self.jft.transport.candidates:
|
|
|
|
if cand['candidate_id'] == streamhost_cid:
|
|
|
|
streamhost_used = cand
|
|
|
|
break
|
2017-02-08 03:49:33 +01:00
|
|
|
if streamhost_used is None:
|
2012-04-05 03:04:23 +02:00
|
|
|
log.info("unknow streamhost")
|
|
|
|
return
|
|
|
|
# We save the candidate nominated by peer
|
|
|
|
self.jft.nominated_cand['peer-cand'] = streamhost_used
|
|
|
|
|
|
|
|
def action(self, args=None):
|
2017-02-08 03:49:33 +01:00
|
|
|
self._recv_candidate(args)
|
2012-04-05 03:04:23 +02:00
|
|
|
|
2012-08-23 23:45:38 +02:00
|
|
|
|
2017-02-08 03:49:33 +01:00
|
|
|
class StateCandSentAndRecv(StateCandSent, StateCandReceived):
|
2012-04-05 03:04:23 +02:00
|
|
|
'''
|
|
|
|
This state happens when we have received and sent the candidates.
|
|
|
|
It takes the boolean argument: sendCand in order to decide whether
|
|
|
|
we should execute the action of when we receive or send a candidate.
|
|
|
|
'''
|
|
|
|
|
|
|
|
def action(self, args=None):
|
|
|
|
if args['sendCand']:
|
2017-02-08 03:49:33 +01:00
|
|
|
self._send_candidate(args)
|
2012-04-05 03:04:23 +02:00
|
|
|
else:
|
2017-02-08 03:49:33 +01:00
|
|
|
self._recv_candidate(args)
|
2012-04-05 03:04:23 +02:00
|
|
|
|
2012-08-23 23:45:38 +02:00
|
|
|
|
2012-04-05 03:04:23 +02:00
|
|
|
class StateTransportReplace(JingleFileTransferStates):
|
|
|
|
'''
|
|
|
|
This state initiates transport replace
|
|
|
|
'''
|
|
|
|
|
|
|
|
def action(self, args=None):
|
|
|
|
self.jft.session.transport_replace()
|
|
|
|
|
2012-08-23 23:45:38 +02:00
|
|
|
|
2012-04-05 03:04:23 +02:00
|
|
|
class StateTransfering(JingleFileTransferStates):
|
|
|
|
'''
|
|
|
|
This state will start the transfer depeding on the type of transport
|
|
|
|
we have.
|
|
|
|
'''
|
|
|
|
|
2017-02-08 03:49:33 +01:00
|
|
|
def _start_ibb_transfer(self, con):
|
2014-02-24 22:13:25 +01:00
|
|
|
self.jft.file_props.transport_sid = self.jft.transport.sid
|
2017-03-29 17:56:03 +02:00
|
|
|
fp = open(self.jft.file_props.file_name, 'rb')
|
2017-02-08 03:49:33 +01:00
|
|
|
con.OpenStream(self.jft.file_props.sid, self.jft.session.peerjid, fp,
|
|
|
|
blocksize=4096)
|
2012-04-05 03:04:23 +02:00
|
|
|
|
2017-02-08 03:49:33 +01:00
|
|
|
def _start_sock5_transfer(self):
|
2012-04-05 03:04:23 +02:00
|
|
|
# It tells wether we start the transfer as client or server
|
|
|
|
mode = None
|
2017-02-08 03:49:33 +01:00
|
|
|
if self.jft.is_our_candidate_used():
|
2012-04-05 03:04:23 +02:00
|
|
|
mode = 'client'
|
|
|
|
streamhost_used = self.jft.nominated_cand['our-cand']
|
2017-08-13 13:18:56 +02:00
|
|
|
app.socks5queue.remove_server(self.jft.file_props.transport_sid)
|
2012-04-05 03:04:23 +02:00
|
|
|
else:
|
|
|
|
mode = 'server'
|
|
|
|
streamhost_used = self.jft.nominated_cand['peer-cand']
|
2017-08-13 13:18:56 +02:00
|
|
|
app.socks5queue.remove_client(self.jft.file_props.transport_sid)
|
|
|
|
app.socks5queue.remove_other_servers(streamhost_used['host'])
|
2012-04-05 03:04:23 +02:00
|
|
|
if streamhost_used['type'] == 'proxy':
|
2012-06-14 18:27:23 +02:00
|
|
|
self.jft.file_props.is_a_proxy = True
|
2012-09-18 06:12:07 +02:00
|
|
|
if self.jft.file_props.type_ == 's' and self.jft.weinitiate:
|
2012-06-14 18:27:23 +02:00
|
|
|
self.jft.file_props.proxy_sender = streamhost_used['initiator']
|
|
|
|
self.jft.file_props.proxy_receiver = streamhost_used['target']
|
2012-04-05 03:04:23 +02:00
|
|
|
else:
|
2012-06-14 18:27:23 +02:00
|
|
|
self.jft.file_props.proxy_sender = streamhost_used['target']
|
2012-08-22 12:21:45 +02:00
|
|
|
self.jft.file_props.proxy_receiver = streamhost_used[
|
|
|
|
'initiator']
|
2012-09-18 06:12:07 +02:00
|
|
|
if self.jft.file_props.type_ == 's':
|
2017-08-13 13:18:56 +02:00
|
|
|
s = app.socks5queue.senders
|
2012-09-18 06:12:07 +02:00
|
|
|
for sender in s:
|
|
|
|
if s[sender].host == streamhost_used['host'] and \
|
|
|
|
s[sender].connected:
|
|
|
|
return
|
|
|
|
elif self.jft.file_props.type_ == 'r':
|
2017-08-13 13:18:56 +02:00
|
|
|
r = app.socks5queue.readers
|
2012-09-18 06:12:07 +02:00
|
|
|
for reader in r:
|
|
|
|
if r[reader].host == streamhost_used['host'] and \
|
|
|
|
r[reader].connected:
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
raise TypeError
|
2012-06-14 18:27:23 +02:00
|
|
|
self.jft.file_props.streamhost_used = True
|
2017-08-16 15:27:35 +02:00
|
|
|
streamhost_used['sid'] = self.jft.file_props.transport_sid
|
2012-06-14 18:27:23 +02:00
|
|
|
self.jft.file_props.streamhosts = []
|
|
|
|
self.jft.file_props.streamhosts.append(streamhost_used)
|
|
|
|
self.jft.file_props.proxyhosts = []
|
|
|
|
self.jft.file_props.proxyhosts.append(streamhost_used)
|
2012-09-18 06:12:07 +02:00
|
|
|
if self.jft.file_props.type_ == 's':
|
2017-08-13 13:18:56 +02:00
|
|
|
app.socks5queue.idx += 1
|
|
|
|
idx = app.socks5queue.idx
|
|
|
|
sockobj = Socks5SenderClient(app.idlequeue, idx,
|
|
|
|
app.socks5queue, _sock=None,
|
2017-02-08 03:49:33 +01:00
|
|
|
host=str(streamhost_used['host']),
|
|
|
|
port=int(streamhost_used['port']),
|
|
|
|
fingerprint=None, connected=False,
|
|
|
|
file_props=self.jft.file_props)
|
2012-04-05 03:04:23 +02:00
|
|
|
else:
|
2017-08-13 13:18:56 +02:00
|
|
|
sockobj = Socks5ReceiverClient(app.idlequeue, streamhost_used,
|
2017-08-16 15:27:35 +02:00
|
|
|
transport_sid=self.jft.file_props.transport_sid,
|
|
|
|
file_props=self.jft.file_props, fingerprint=None)
|
2012-04-05 03:04:23 +02:00
|
|
|
sockobj.proxy = True
|
|
|
|
sockobj.streamhost = streamhost_used
|
2017-08-13 13:18:56 +02:00
|
|
|
app.socks5queue.add_sockobj(self.jft.session.connection.name,
|
2017-02-08 03:49:33 +01:00
|
|
|
sockobj)
|
2012-04-05 03:04:23 +02:00
|
|
|
streamhost_used['idx'] = sockobj.queue_idx
|
|
|
|
# If we offered the nominated candidate used, we activate
|
|
|
|
# the proxy
|
2017-02-08 03:49:33 +01:00
|
|
|
if not self.jft.is_our_candidate_used():
|
2017-08-13 13:18:56 +02:00
|
|
|
app.socks5queue.on_success[self.jft.file_props.transport_sid]\
|
2017-08-16 15:27:35 +02:00
|
|
|
= self.jft.transport._on_proxy_auth_ok
|
2012-04-05 03:04:23 +02:00
|
|
|
# TODO: add on failure
|
|
|
|
else:
|
2017-08-13 13:18:56 +02:00
|
|
|
jid = app.get_jid_without_resource(self.jft.session.ourjid)
|
|
|
|
app.socks5queue.send_file(self.jft.file_props,
|
2017-02-08 03:49:33 +01:00
|
|
|
self.jft.session.connection.name, mode)
|
2012-04-05 03:04:23 +02:00
|
|
|
|
|
|
|
def action(self, args=None):
|
2012-08-22 12:55:57 +02:00
|
|
|
if self.jft.transport.type_ == TransportType.IBB:
|
2017-02-08 03:49:33 +01:00
|
|
|
self._start_ibb_transfer(self.jft.session.connection)
|
2012-08-22 12:55:57 +02:00
|
|
|
elif self.jft.transport.type_ == TransportType.SOCKS5:
|
2017-02-08 03:49:33 +01:00
|
|
|
self._start_sock5_transfer()
|