2008-08-15 19:31:51 +02:00
|
|
|
# -*- coding:utf-8 -*-
|
2008-08-15 05:20:23 +02:00
|
|
|
## src/common/socks5.py
|
2005-07-30 12:58:46 +02:00
|
|
|
##
|
2008-08-15 05:20:23 +02:00
|
|
|
## Copyright (C) 2005-2006 Dimitur Kirov <dkirov AT gmail.com>
|
|
|
|
## Nikos Kouremenos <kourem AT gmail.com>
|
2014-01-02 09:33:54 +01:00
|
|
|
## Copyright (C) 2005-2014 Yann Leboulanger <asterix AT lagaule.org>
|
2008-08-15 05:20:23 +02:00
|
|
|
## Copyright (C) 2006-2008 Jean-Marie Traissard <jim AT lapin.org>
|
|
|
|
## Copyright (C) 2008 Jonathan Schleifer <js-gajim AT webkeks.org>
|
2005-07-30 12:58:46 +02:00
|
|
|
##
|
2007-10-22 13:13:13 +02:00
|
|
|
## This file is part of Gajim.
|
|
|
|
##
|
|
|
|
## Gajim is free software; you can redistribute it and/or modify
|
2005-07-30 12:58:46 +02:00
|
|
|
## it under the terms of the GNU General Public License as published
|
2007-10-22 13:13:13 +02:00
|
|
|
## by the Free Software Foundation; version 3 only.
|
2005-07-30 12:58:46 +02:00
|
|
|
##
|
2007-10-22 13:13:13 +02:00
|
|
|
## Gajim is distributed in the hope that it will be useful,
|
2005-07-30 12:58:46 +02:00
|
|
|
## but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2008-08-15 05:20:23 +02:00
|
|
|
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2005-07-30 12:58:46 +02:00
|
|
|
## GNU General Public License for more details.
|
|
|
|
##
|
2007-10-22 13:13:13 +02:00
|
|
|
## You should have received a copy of the GNU General Public License
|
2008-08-15 05:20:23 +02:00
|
|
|
## along with Gajim. If not, see <http://www.gnu.org/licenses/>.
|
2007-10-22 13:13:13 +02:00
|
|
|
##
|
2005-07-30 12:58:46 +02:00
|
|
|
|
|
|
|
import socket
|
|
|
|
import struct
|
2009-01-08 17:51:26 +01:00
|
|
|
import hashlib
|
2008-12-07 14:23:27 +01:00
|
|
|
import os
|
2016-04-04 21:10:57 +02:00
|
|
|
import time
|
2005-08-17 17:10:37 +02:00
|
|
|
from errno import EWOULDBLOCK
|
|
|
|
from errno import ENOBUFS
|
|
|
|
from errno import EINTR
|
2006-03-27 17:28:29 +02:00
|
|
|
from errno import EISCONN
|
2008-12-02 16:10:31 +01:00
|
|
|
from errno import EINPROGRESS
|
2010-01-03 11:31:22 +01:00
|
|
|
from errno import EAFNOSUPPORT
|
2012-12-09 21:37:51 +01:00
|
|
|
from nbxmpp.idlequeue import IdleObject
|
2017-06-13 23:58:06 +02:00
|
|
|
from gajim.common.file_props import FilesProp
|
2017-08-13 13:18:56 +02:00
|
|
|
from gajim.common import app
|
2017-06-13 23:58:06 +02:00
|
|
|
from gajim.common import jingle_xtls
|
2010-07-27 15:29:12 +02:00
|
|
|
if jingle_xtls.PYOPENSSL_PRESENT:
|
|
|
|
import OpenSSL
|
2010-07-03 10:22:47 +02:00
|
|
|
import logging
|
|
|
|
log = logging.getLogger('gajim.c.socks5')
|
2005-08-04 09:23:14 +02:00
|
|
|
MAX_BUFF_LEN = 65536
|
2006-02-11 22:32:48 +01:00
|
|
|
# after foo seconds without activity label transfer as 'stalled'
|
|
|
|
STALLED_TIMEOUT = 10
|
|
|
|
# after foo seconds of waiting to connect, disconnect from
|
|
|
|
# streamhost and try next one
|
2006-02-12 03:25:21 +01:00
|
|
|
CONNECT_TIMEOUT = 30
|
|
|
|
# nothing received for the last foo seconds - stop transfer
|
|
|
|
# if it is 0, then transfer will wait forever
|
|
|
|
READ_TIMEOUT = 180
|
|
|
|
# nothing sent for the last foo seconds - stop transfer
|
|
|
|
# if it is 0, then transfer will wait forever
|
|
|
|
SEND_TIMEOUT = 180
|
2006-02-11 22:32:48 +01:00
|
|
|
|
2012-08-23 23:45:38 +02:00
|
|
|
|
2005-07-30 12:58:46 +02:00
|
|
|
class SocksQueue:
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
|
|
|
Queue for all file requests objects
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, idlequeue, complete_transfer_cb=None,
|
2011-08-24 10:36:00 +02:00
|
|
|
progress_transfer_cb=None, error_cb=None):
|
2010-02-08 15:08:40 +01:00
|
|
|
self.connected = 0
|
|
|
|
self.readers = {}
|
|
|
|
self.senders = {}
|
|
|
|
self.idx = 1
|
|
|
|
self.listener = None
|
|
|
|
self.sha_handlers = {}
|
|
|
|
# handle all io events in the global idle queue, instead of processing
|
|
|
|
# each foo seconds
|
|
|
|
self.idlequeue = idlequeue
|
|
|
|
self.complete_transfer_cb = complete_transfer_cb
|
|
|
|
self.progress_transfer_cb = progress_transfer_cb
|
|
|
|
self.error_cb = error_cb
|
2010-08-26 11:56:12 +02:00
|
|
|
self.on_success = {} # {id: cb}
|
|
|
|
self.on_failure = {} # {id: cb}
|
2010-02-08 15:08:40 +01:00
|
|
|
|
2012-08-22 12:55:57 +02:00
|
|
|
def start_listener(self, port, sha_str, sha_handler, file_props,
|
|
|
|
fingerprint=None, typ='sender'):
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
|
|
|
Start waiting for incomming connections on (host, port) and do a socks5
|
|
|
|
authentication using sid for generated SHA
|
|
|
|
"""
|
2016-04-03 14:28:08 +02:00
|
|
|
log.debug('Start listening for socks5 connection')
|
2012-06-14 18:27:23 +02:00
|
|
|
sid = file_props.sid
|
2010-02-08 15:08:40 +01:00
|
|
|
self.sha_handlers[sha_str] = (sha_handler, sid)
|
2011-12-27 03:18:45 +01:00
|
|
|
if self.listener is None or self.listener.connections == []:
|
2012-06-14 18:27:23 +02:00
|
|
|
self.listener = Socks5Listener(self.idlequeue, port, file_props,
|
2012-08-22 12:55:57 +02:00
|
|
|
fingerprint=fingerprint)
|
2011-12-27 03:18:45 +01:00
|
|
|
self.listener.queue = self
|
|
|
|
self.listener.bind()
|
|
|
|
else:
|
|
|
|
# There is already a listener, we update the file's information
|
|
|
|
# on the new connection.
|
2012-06-14 18:27:23 +02:00
|
|
|
self.listener.file_props = file_props
|
2011-12-25 00:12:05 +01:00
|
|
|
self.connected += 1
|
2010-02-08 15:08:40 +01:00
|
|
|
return self.listener
|
|
|
|
|
|
|
|
def send_success_reply(self, file_props, streamhost):
|
2012-06-14 18:27:23 +02:00
|
|
|
if file_props.streamhost_used == True:
|
|
|
|
for proxy in file_props.proxyhosts:
|
|
|
|
if proxy['host'] == streamhost['host']:
|
2017-08-16 15:27:35 +02:00
|
|
|
self.on_success[file_props.transport_sid](proxy)
|
2012-06-14 18:27:23 +02:00
|
|
|
return 1
|
2010-02-08 15:08:40 +01:00
|
|
|
return 0
|
2012-06-14 18:27:23 +02:00
|
|
|
for host in file_props.streamhosts:
|
|
|
|
if streamhost['state'] == 1:
|
|
|
|
return 0
|
|
|
|
streamhost['state'] = 1
|
2017-08-16 15:27:35 +02:00
|
|
|
self.on_success[file_props.transport_sid](streamhost)
|
2012-06-20 23:58:08 +02:00
|
|
|
return 1
|
2010-02-08 15:08:40 +01:00
|
|
|
|
2017-08-16 15:27:35 +02:00
|
|
|
def connect_to_hosts(self, account, transport_sid, on_success=None,
|
|
|
|
on_failure=None, fingerprint=None, receiving=True):
|
|
|
|
self.on_success[transport_sid] = on_success
|
|
|
|
self.on_failure[transport_sid] = on_failure
|
|
|
|
file_props = FilesProp.getFilePropByTransportSid(account, transport_sid)
|
2012-06-14 18:27:23 +02:00
|
|
|
file_props.failure_cb = on_failure
|
2012-08-30 23:45:35 +02:00
|
|
|
streamhosts_to_test = []
|
|
|
|
# Remove local IPs to not connect to ourself
|
2012-06-14 18:27:23 +02:00
|
|
|
for streamhost in file_props.streamhosts:
|
2013-12-04 18:43:28 +01:00
|
|
|
if streamhost['host'] == '127.0.0.1' or streamhost['host'] == '::1':
|
2012-08-28 21:10:19 +02:00
|
|
|
continue
|
2012-08-30 23:45:35 +02:00
|
|
|
streamhosts_to_test.append(streamhost)
|
|
|
|
if not streamhosts_to_test:
|
2017-08-16 15:27:35 +02:00
|
|
|
on_failure(file_props.transport_sid)
|
2012-08-30 23:45:35 +02:00
|
|
|
# add streamhosts to the queue
|
|
|
|
for streamhost in streamhosts_to_test:
|
2010-08-25 12:05:14 +02:00
|
|
|
if 'type' in streamhost and streamhost['type'] == 'proxy':
|
|
|
|
fp = None
|
|
|
|
else:
|
2010-08-25 12:21:12 +02:00
|
|
|
fp = fingerprint
|
2011-08-09 01:43:07 +02:00
|
|
|
if receiving:
|
2017-08-12 22:54:08 +02:00
|
|
|
if 'candidate_id' in streamhost:
|
|
|
|
log.debug('Trying to connect as receiver to cid ' + \
|
|
|
|
streamhost['candidate_id'])
|
|
|
|
else:
|
|
|
|
log.debug('Trying to connect as receiver to jid ' + \
|
|
|
|
streamhost['jid'])
|
2012-09-18 06:12:07 +02:00
|
|
|
file_props.type_ = 'r'
|
2012-08-22 12:55:57 +02:00
|
|
|
socks5obj = Socks5ReceiverClient(self.idlequeue, streamhost,
|
2017-08-16 15:27:35 +02:00
|
|
|
transport_sid, file_props, fingerprint=fp)
|
2011-08-09 01:43:07 +02:00
|
|
|
self.add_sockobj(account, socks5obj)
|
2011-08-24 10:36:00 +02:00
|
|
|
else:
|
2017-08-12 22:54:08 +02:00
|
|
|
if 'candidate_id' in streamhost:
|
2017-08-14 14:41:21 +02:00
|
|
|
log.debug('Trying to connect as sender to cid ' + \
|
2017-08-12 22:54:08 +02:00
|
|
|
streamhost['candidate_id'])
|
|
|
|
else:
|
2017-08-14 14:41:21 +02:00
|
|
|
log.debug('Trying to connect as sender to jid ' + \
|
2017-08-12 22:54:08 +02:00
|
|
|
streamhost['jid'])
|
2012-06-14 18:27:23 +02:00
|
|
|
if file_props.sha_str:
|
|
|
|
idx = file_props.sha_str
|
2011-10-29 06:09:45 +02:00
|
|
|
else:
|
|
|
|
idx = self.idx
|
|
|
|
self.idx = self.idx + 1
|
2012-09-18 06:12:07 +02:00
|
|
|
file_props.type_ = 's'
|
2011-11-02 00:09:33 +01:00
|
|
|
if 'type' in streamhost and streamhost['type'] == 'proxy':
|
2012-06-14 18:27:23 +02:00
|
|
|
file_props.is_a_proxy = True
|
|
|
|
file_props.proxy_sender = streamhost['target']
|
|
|
|
file_props.proxy_receiver = streamhost['initiator']
|
2011-11-18 05:55:44 +01:00
|
|
|
socks5obj = Socks5SenderClient(self.idlequeue, idx,
|
2012-06-20 23:58:08 +02:00
|
|
|
self, _sock=None,host=str(streamhost['host']),
|
|
|
|
port=int(streamhost['port']),fingerprint=fp,
|
2015-07-28 20:32:14 +02:00
|
|
|
connected=False, file_props=file_props,
|
|
|
|
initiator=streamhost['initiator'],
|
2015-04-05 21:26:16 +02:00
|
|
|
target=streamhost['target'])
|
2011-08-09 01:43:07 +02:00
|
|
|
socks5obj.streamhost = streamhost
|
2012-09-18 06:12:07 +02:00
|
|
|
self.add_sockobj(account, socks5obj)
|
2011-08-24 10:36:00 +02:00
|
|
|
|
2011-08-09 01:43:07 +02:00
|
|
|
streamhost['idx'] = socks5obj.queue_idx
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
def _socket_connected(self, streamhost, file_props):
|
|
|
|
"""
|
|
|
|
Called when there is a host connected to one of the senders's
|
2011-08-09 01:43:07 +02:00
|
|
|
streamhosts. Stop other attempts for connections
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
2017-08-12 22:54:08 +02:00
|
|
|
if 'candidate_id' in streamhost:
|
|
|
|
log.debug('Connected to cid ' + streamhost['candidate_id'])
|
|
|
|
else:
|
|
|
|
log.debug('Connected to jid ' + streamhost['jid'])
|
2012-06-14 18:27:23 +02:00
|
|
|
for host in file_props.streamhosts:
|
2010-02-08 15:08:40 +01:00
|
|
|
if host != streamhost and 'idx' in host:
|
|
|
|
if host['state'] == 1:
|
|
|
|
# remove current
|
2012-09-18 06:12:07 +02:00
|
|
|
if file_props.type_ == 's':
|
2011-08-09 01:43:07 +02:00
|
|
|
self.remove_sender(streamhost['idx'], False)
|
|
|
|
else:
|
|
|
|
self.remove_receiver(streamhost['idx'])
|
2010-02-08 15:08:40 +01:00
|
|
|
return
|
|
|
|
# set state -2, meaning that this streamhost is stopped,
|
|
|
|
# but it may be connectected later
|
|
|
|
if host['state'] >= 0:
|
2012-09-18 06:12:07 +02:00
|
|
|
if file_props.type_ == 's':
|
2011-08-09 01:43:07 +02:00
|
|
|
self.remove_sender(host['idx'], False)
|
|
|
|
else:
|
|
|
|
self.remove_receiver(host['idx'])
|
2010-02-08 15:08:40 +01:00
|
|
|
host['idx'] = -1
|
|
|
|
host['state'] = -2
|
|
|
|
|
2011-08-09 01:43:07 +02:00
|
|
|
def reconnect_client(self, client, streamhost):
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
|
|
|
Check the state of all streamhosts and if all has failed, then emit
|
|
|
|
connection failure cb. If there are some which are still not connected
|
|
|
|
try to establish connection to one of them
|
|
|
|
"""
|
2011-08-09 01:43:07 +02:00
|
|
|
self.idlequeue.remove_timeout(client.fd)
|
|
|
|
self.idlequeue.unplug_idle(client.fd)
|
|
|
|
file_props = client.file_props
|
2010-02-08 15:08:40 +01:00
|
|
|
streamhost['state'] = -1
|
|
|
|
# boolean, indicates that there are hosts, which are not tested yet
|
|
|
|
unused_hosts = False
|
2012-06-14 18:27:23 +02:00
|
|
|
for host in file_props.streamhosts:
|
2010-02-08 15:08:40 +01:00
|
|
|
if 'idx' in host:
|
|
|
|
if host['state'] >= 0:
|
|
|
|
return
|
|
|
|
elif host['state'] == -2:
|
|
|
|
unused_hosts = True
|
|
|
|
if unused_hosts:
|
2012-06-14 18:27:23 +02:00
|
|
|
for host in file_props.streamhosts:
|
2010-02-08 15:08:40 +01:00
|
|
|
if host['state'] == -2:
|
|
|
|
host['state'] = 0
|
2011-08-09 01:43:07 +02:00
|
|
|
# FIXME: make the sender reconnect also
|
2012-08-22 12:55:57 +02:00
|
|
|
client = Socks5ReceiverClient(self.idlequeue, host,
|
2017-08-16 15:27:35 +02:00
|
|
|
client.sid, file_props)
|
2011-08-09 01:43:07 +02:00
|
|
|
self.add_sockobj(client.account, client)
|
|
|
|
host['idx'] = client.queue_idx
|
2010-02-08 15:08:40 +01:00
|
|
|
# we still have chances to connect
|
|
|
|
return
|
2012-06-14 18:27:23 +02:00
|
|
|
if file_props.received_len == 0:
|
2010-02-08 15:08:40 +01:00
|
|
|
# there are no other streamhosts and transfer hasn't started
|
2011-08-09 01:43:07 +02:00
|
|
|
self._connection_refused(streamhost, file_props, client.queue_idx)
|
2010-02-08 15:08:40 +01:00
|
|
|
else:
|
|
|
|
# transfer stopped, it is most likely stopped from sender
|
2011-08-09 01:43:07 +02:00
|
|
|
client.disconnect()
|
2012-06-14 18:27:23 +02:00
|
|
|
file_props.error = -1
|
2011-08-09 01:43:07 +02:00
|
|
|
self.process_result(-1, client)
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
def _connection_refused(self, streamhost, file_props, idx):
|
|
|
|
"""
|
|
|
|
Called when we loose connection during transfer
|
|
|
|
"""
|
2017-08-12 22:54:08 +02:00
|
|
|
if 'candidate_id' in streamhost:
|
|
|
|
log.debug('Connection refused to cid ' + streamhost['candidate_id'])
|
|
|
|
else:
|
|
|
|
log.debug('Connection refused to jid ' + streamhost['jid'])
|
2010-02-08 15:08:40 +01:00
|
|
|
if file_props is None:
|
|
|
|
return
|
|
|
|
streamhost['state'] = -1
|
2011-10-29 06:09:45 +02:00
|
|
|
# FIXME: should only the receiver be remove? what if we are sending?
|
2010-02-08 15:08:40 +01:00
|
|
|
self.remove_receiver(idx, False)
|
2012-06-14 18:27:23 +02:00
|
|
|
for host in file_props.streamhosts:
|
|
|
|
if host['state'] != -1:
|
|
|
|
return
|
2011-10-29 06:09:45 +02:00
|
|
|
self.readers = {}
|
2010-02-08 15:08:40 +01:00
|
|
|
# failure_cb exists - this means that it has never been called
|
2012-06-14 18:27:23 +02:00
|
|
|
if file_props.failure_cb:
|
2017-08-16 15:27:35 +02:00
|
|
|
file_props.failure_cb(file_props.transport_sid)
|
2012-06-14 18:27:23 +02:00
|
|
|
file_props.failure_cb = None
|
2010-02-08 15:08:40 +01:00
|
|
|
|
2012-09-18 06:12:07 +02:00
|
|
|
def add_sockobj(self, account, sockobj):
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
2011-08-22 01:02:58 +02:00
|
|
|
Add new file a sockobj type receiver or sender, and use it to connect
|
|
|
|
to server
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
2012-09-18 06:12:07 +02:00
|
|
|
if sockobj.file_props.type_ == 'r':
|
2011-08-22 01:02:58 +02:00
|
|
|
self._add(sockobj, self.readers, sockobj.file_props, self.idx)
|
2011-08-09 01:43:07 +02:00
|
|
|
else:
|
2011-08-22 01:02:58 +02:00
|
|
|
self._add(sockobj, self.senders, sockobj.file_props, self.idx)
|
2011-08-09 01:43:07 +02:00
|
|
|
sockobj.queue_idx = self.idx
|
|
|
|
sockobj.queue = self
|
|
|
|
sockobj.account = account
|
2010-02-08 15:08:40 +01:00
|
|
|
self.idx += 1
|
2011-08-09 01:43:07 +02:00
|
|
|
result = sockobj.connect()
|
2010-02-08 15:08:40 +01:00
|
|
|
self.connected += 1
|
|
|
|
if result is not None:
|
2011-08-09 01:43:07 +02:00
|
|
|
result = sockobj.main()
|
|
|
|
self.process_result(result, sockobj)
|
2010-02-08 15:08:40 +01:00
|
|
|
return 1
|
|
|
|
return None
|
|
|
|
|
2012-06-14 18:27:23 +02:00
|
|
|
def _add(self, sockobj, sockobjects, file_props, hash_):
|
2011-08-22 01:02:58 +02:00
|
|
|
'''
|
|
|
|
Adds the sockobj to the current list of sockobjects
|
|
|
|
'''
|
2017-08-16 15:27:35 +02:00
|
|
|
keys = (file_props.transport_sid, file_props.name, hash_)
|
2011-08-24 10:36:00 +02:00
|
|
|
sockobjects[keys] = sockobj
|
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
def result_sha(self, sha_str, idx):
|
|
|
|
if sha_str in self.sha_handlers:
|
|
|
|
props = self.sha_handlers[sha_str]
|
|
|
|
props[0](props[1], idx)
|
|
|
|
|
|
|
|
def activate_proxy(self, idx):
|
2012-08-28 19:10:13 +02:00
|
|
|
if not self.isHashInSockObjs(self.senders, idx):
|
2010-02-08 15:08:40 +01:00
|
|
|
return
|
2012-08-28 19:10:13 +02:00
|
|
|
for key in self.senders.keys():
|
2011-08-22 01:02:58 +02:00
|
|
|
if idx in key:
|
2012-08-28 19:10:13 +02:00
|
|
|
sender = self.senders[key]
|
|
|
|
if sender.file_props.type_ != 's':
|
2011-08-22 01:02:58 +02:00
|
|
|
return
|
2012-08-28 19:10:13 +02:00
|
|
|
sender.state = 6
|
|
|
|
if sender.connected:
|
|
|
|
sender.file_props.error = 0
|
|
|
|
sender.file_props.disconnect_cb = sender.disconnect
|
|
|
|
sender.file_props.started = True
|
|
|
|
sender.file_props.completed = False
|
|
|
|
sender.file_props.paused = False
|
|
|
|
sender.file_props.stalled = False
|
|
|
|
sender.file_props.elapsed_time = 0
|
2016-04-04 21:10:57 +02:00
|
|
|
sender.file_props.last_time = time.time()
|
2012-08-28 19:10:13 +02:00
|
|
|
sender.file_props.received_len = 0
|
|
|
|
sender.pauses = 0
|
2011-08-22 01:02:58 +02:00
|
|
|
# start sending file to proxy
|
2012-08-28 19:10:13 +02:00
|
|
|
self.idlequeue.set_read_timeout(sender.fd, STALLED_TIMEOUT)
|
|
|
|
self.idlequeue.plug_idle(sender, True, False)
|
|
|
|
result = sender.write_next()
|
|
|
|
self.process_result(result, sender)
|
2011-08-22 01:02:58 +02:00
|
|
|
|
2011-12-23 03:52:37 +01:00
|
|
|
def send_file(self, file_props, account, mode):
|
2011-08-22 01:02:58 +02:00
|
|
|
for key in self.senders.keys():
|
2017-08-16 15:27:35 +02:00
|
|
|
if file_props.name in key and file_props.transport_sid in key \
|
2011-12-23 03:52:37 +01:00
|
|
|
and self.senders[key].mode == mode:
|
2012-08-22 12:55:57 +02:00
|
|
|
log.info('socks5: sending file')
|
2011-08-22 01:02:58 +02:00
|
|
|
sender = self.senders[key]
|
2012-06-14 18:27:23 +02:00
|
|
|
file_props.streamhost_used = True
|
2011-08-22 01:02:58 +02:00
|
|
|
sender.account = account
|
2011-10-29 06:09:45 +02:00
|
|
|
sender.file_props = file_props
|
|
|
|
result = sender.send_file()
|
|
|
|
self.process_result(result, sender)
|
2010-02-08 15:08:40 +01:00
|
|
|
|
2011-08-22 01:02:58 +02:00
|
|
|
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
|
2010-02-08 15:08:40 +01:00
|
|
|
|
2011-08-15 05:59:39 +02:00
|
|
|
def on_connection_accepted(self, sock, listener):
|
2010-02-08 15:08:40 +01:00
|
|
|
sock_hash = sock.__hash__()
|
2012-09-18 06:12:07 +02:00
|
|
|
if listener.file_props.type_ == 's' and \
|
2011-08-24 10:36:00 +02:00
|
|
|
not self.isHashInSockObjs(self.senders, sock_hash):
|
2011-11-18 05:55:44 +01:00
|
|
|
sockobj = Socks5SenderServer(self.idlequeue, sock_hash, self,
|
2014-08-22 14:23:05 +02:00
|
|
|
sock[0], sock[1][0], sock[1][1], fingerprint=None,
|
2011-08-24 10:36:00 +02:00
|
|
|
file_props=listener.file_props)
|
2011-08-22 01:02:58 +02:00
|
|
|
self._add(sockobj, self.senders, listener.file_props, sock_hash)
|
2011-08-02 05:12:30 +02:00
|
|
|
# Start waiting for data
|
2011-08-22 01:02:58 +02:00
|
|
|
self.idlequeue.plug_idle(sockobj, False, True)
|
2010-02-08 15:08:40 +01:00
|
|
|
self.connected += 1
|
2012-09-18 06:12:07 +02:00
|
|
|
if listener.file_props.type_ == 'r' and \
|
2012-08-22 12:55:57 +02:00
|
|
|
not self.isHashInSockObjs(self.readers, sock_hash):
|
2011-08-09 01:43:07 +02:00
|
|
|
sh = {}
|
|
|
|
sh['host'] = sock[1][0]
|
|
|
|
sh['port'] = sock[1][1]
|
|
|
|
sh['initiator'] = None
|
|
|
|
sh['target'] = None
|
2011-11-18 05:55:44 +01:00
|
|
|
sockobj = Socks5ReceiverServer(idlequeue=self.idlequeue,
|
2017-08-16 15:27:35 +02:00
|
|
|
streamhost=sh,transport_sid=None,
|
|
|
|
file_props=listener.file_props, fingerprint=None)
|
2011-08-24 10:36:00 +02:00
|
|
|
|
2011-08-22 01:02:58 +02:00
|
|
|
self._add(sockobj, self.readers, listener.file_props, sock_hash)
|
|
|
|
sockobj.set_sock(sock[0])
|
|
|
|
sockobj.queue = self
|
2011-08-09 01:43:07 +02:00
|
|
|
self.connected += 1
|
2011-08-24 10:36:00 +02:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
def process_result(self, result, actor):
|
|
|
|
"""
|
|
|
|
Take appropriate actions upon the result:
|
|
|
|
[ 0, - 1 ] complete/end transfer
|
|
|
|
[ > 0 ] send progress message
|
|
|
|
[ None ] do nothing
|
|
|
|
"""
|
|
|
|
if result is None:
|
|
|
|
return
|
|
|
|
if result in (0, -1) and self.complete_transfer_cb is not None:
|
|
|
|
account = actor.account
|
2012-06-14 18:27:23 +02:00
|
|
|
if account is None and actor.file_props.tt_account:
|
|
|
|
account = actor.file_props.tt_account
|
2010-02-08 15:08:40 +01:00
|
|
|
self.complete_transfer_cb(account, actor.file_props)
|
|
|
|
elif self.progress_transfer_cb is not None:
|
|
|
|
self.progress_transfer_cb(actor.account, actor.file_props)
|
|
|
|
|
2014-06-01 15:42:06 +02:00
|
|
|
def remove_receiver_by_key(self, key, do_disconnect=True):
|
|
|
|
reader = self.readers[key]
|
|
|
|
self.idlequeue.unplug_idle(reader.fd)
|
|
|
|
self.idlequeue.remove_timeout(reader.fd)
|
|
|
|
if do_disconnect:
|
|
|
|
reader.disconnect()
|
|
|
|
else:
|
|
|
|
if reader.streamhost is not None:
|
|
|
|
reader.streamhost['state'] = -1
|
|
|
|
del(self.readers[key])
|
|
|
|
|
|
|
|
def remove_sender_by_key(self, key, do_disconnect=True):
|
|
|
|
sender = self.senders[key]
|
|
|
|
if do_disconnect:
|
|
|
|
sender.disconnect()
|
2017-08-16 23:05:43 +02:00
|
|
|
self.idlequeue.unplug_idle(sender.fd)
|
|
|
|
self.idlequeue.remove_timeout(sender.fd)
|
|
|
|
del(self.senders[key])
|
|
|
|
if self.connected > 0:
|
|
|
|
self.connected -= 1
|
2014-06-01 15:42:06 +02:00
|
|
|
|
2011-12-25 00:12:05 +01:00
|
|
|
def remove_receiver(self, idx, do_disconnect=True, remove_all=False):
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
|
|
|
Remove reciver from the list and decrease the number of active
|
|
|
|
connections with 1
|
|
|
|
"""
|
|
|
|
if idx != -1:
|
2015-07-28 20:32:14 +02:00
|
|
|
for key in list(self.readers.keys()):
|
2011-08-22 01:02:58 +02:00
|
|
|
if idx in key:
|
2014-06-01 15:42:06 +02:00
|
|
|
self.remove_receiver_by_key(key,
|
|
|
|
do_disconnect=do_disconnect)
|
|
|
|
if not remove_all:
|
|
|
|
break
|
2012-06-20 23:58:08 +02:00
|
|
|
|
2011-12-25 00:12:05 +01:00
|
|
|
def remove_sender(self, idx, do_disconnect=True, remove_all=False):
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
|
|
|
Remove sender from the list of senders and decrease the number of active
|
|
|
|
connections with 1
|
|
|
|
"""
|
|
|
|
if idx != -1:
|
2011-08-22 01:02:58 +02:00
|
|
|
for key in self.senders.keys():
|
|
|
|
if idx in key:
|
2014-06-01 15:42:06 +02:00
|
|
|
self.remove_sender_by_key(key, do_disconnect=do_disconnect)
|
|
|
|
if not remove_all:
|
|
|
|
break
|
2010-02-08 15:08:40 +01:00
|
|
|
if len(self.senders) == 0 and self.listener is not None:
|
|
|
|
self.listener.disconnect()
|
|
|
|
self.listener = None
|
|
|
|
self.connected -= 1
|
2008-11-24 18:00:20 +01:00
|
|
|
|
2017-08-16 15:27:35 +02:00
|
|
|
def remove_by_mode(self, transport_sid, mode, do_disconnect=True):
|
2015-07-30 15:18:03 +02:00
|
|
|
for (key, sock) in self.senders.copy().items():
|
2017-08-16 15:27:35 +02:00
|
|
|
if key[0] == transport_sid and sock.mode == mode:
|
2014-06-01 15:42:06 +02:00
|
|
|
self.remove_sender_by_key(key)
|
2015-07-30 15:18:03 +02:00
|
|
|
for (key, sock) in self.readers.copy().items():
|
2017-08-16 15:27:35 +02:00
|
|
|
if key[0] == transport_sid and sock.mode == mode:
|
2014-06-01 15:42:06 +02:00
|
|
|
self.remove_receiver_by_key(key)
|
|
|
|
|
2017-08-16 15:27:35 +02:00
|
|
|
def remove_server(self, transport_sid, do_disconnect=True):
|
|
|
|
self.remove_by_mode(transport_sid, 'server')
|
2014-06-01 15:42:06 +02:00
|
|
|
|
2017-08-16 15:27:35 +02:00
|
|
|
def remove_client(self, transport_sid, do_disconnect=True):
|
|
|
|
self.remove_by_mode(transport_sid, 'client')
|
2012-08-23 23:45:38 +02:00
|
|
|
|
2017-08-16 23:05:43 +02:00
|
|
|
def remove_other_servers(self, host_to_keep):
|
|
|
|
for (key, sock) in self.senders.copy().items():
|
|
|
|
if sock.host != host_to_keep and sock.mode == 'server':
|
|
|
|
self.remove_sender_by_key(key)
|
|
|
|
|
2013-12-04 18:43:28 +01:00
|
|
|
class Socks5(object):
|
2010-02-08 15:08:40 +01:00
|
|
|
def __init__(self, idlequeue, host, port, initiator, target, sid):
|
|
|
|
if host is not None:
|
|
|
|
try:
|
|
|
|
self.host = host
|
|
|
|
self.ais = socket.getaddrinfo(host, port, socket.AF_UNSPEC,
|
2011-08-24 10:36:00 +02:00
|
|
|
socket.SOCK_STREAM)
|
2010-02-08 15:08:40 +01:00
|
|
|
except socket.gaierror:
|
|
|
|
self.ais = None
|
|
|
|
self.idlequeue = idlequeue
|
|
|
|
self.fd = -1
|
|
|
|
self.port = port
|
|
|
|
self.initiator = initiator
|
|
|
|
self.target = target
|
|
|
|
self.sid = sid
|
|
|
|
self._sock = None
|
|
|
|
self.account = None
|
|
|
|
self.state = 0 # not connected
|
|
|
|
self.pauses = 0
|
|
|
|
self.size = 0
|
2015-07-28 20:32:14 +02:00
|
|
|
self.remaining_buff = b''
|
2010-02-08 15:08:40 +01:00
|
|
|
self.file = None
|
2011-08-09 01:43:07 +02:00
|
|
|
self.connected = False
|
2011-12-25 00:12:05 +01:00
|
|
|
self.mode = ''
|
2013-12-04 18:43:28 +01:00
|
|
|
self.ssl_cert = None
|
|
|
|
self.ssl_errnum = 0
|
2010-02-08 15:08:40 +01:00
|
|
|
|
2011-08-09 01:43:07 +02:00
|
|
|
def _is_connected(self):
|
|
|
|
if self.state < 5:
|
|
|
|
return False
|
|
|
|
return True
|
2011-08-24 10:36:00 +02:00
|
|
|
|
2013-12-04 18:43:28 +01:00
|
|
|
def ssl_verify_cb(self, ssl_conn, cert, error_num, depth, return_code):
|
|
|
|
if depth == 0:
|
|
|
|
self.ssl_cert = cert
|
|
|
|
self.ssl_errnum = error_num
|
|
|
|
return True
|
|
|
|
|
2011-08-09 01:43:07 +02:00
|
|
|
def connect(self):
|
|
|
|
"""
|
|
|
|
Create the socket and plug it to the idlequeue
|
|
|
|
"""
|
|
|
|
if self.ais is None:
|
|
|
|
return None
|
|
|
|
for ai in self.ais:
|
|
|
|
try:
|
|
|
|
self._sock = socket.socket(*ai[:3])
|
2017-02-08 13:07:12 +01:00
|
|
|
if self.fingerprint is not None:
|
2013-12-04 18:43:28 +01:00
|
|
|
if self.file_props.type_ == 's':
|
2017-08-13 13:18:56 +02:00
|
|
|
remote_jid = app.get_jid_without_resource(
|
2013-12-04 18:43:28 +01:00
|
|
|
self.file_props.receiver)
|
|
|
|
else:
|
2017-08-13 13:18:56 +02:00
|
|
|
remote_jid = app.get_jid_without_resource(
|
2013-12-04 18:43:28 +01:00
|
|
|
self.file_props.sender)
|
2011-08-09 01:43:07 +02:00
|
|
|
self._sock = OpenSSL.SSL.Connection(
|
2013-12-04 18:43:28 +01:00
|
|
|
jingle_xtls.get_context('client',
|
|
|
|
verify_cb=self.ssl_verify_cb, remote_jid=remote_jid),
|
|
|
|
self._sock)
|
2011-08-09 01:43:07 +02:00
|
|
|
# this will not block the GUI
|
|
|
|
self._sock.setblocking(False)
|
|
|
|
self._server = ai[4]
|
|
|
|
break
|
2013-01-01 23:18:36 +01:00
|
|
|
except socket.error as e:
|
2013-01-05 00:03:36 +01:00
|
|
|
if e.errno == EINPROGRESS:
|
2011-08-09 01:43:07 +02:00
|
|
|
break
|
|
|
|
# for all other errors, we try other addresses
|
|
|
|
continue
|
|
|
|
self.fd = self._sock.fileno()
|
|
|
|
self.state = 0 # about to be connected
|
|
|
|
self.idlequeue.plug_idle(self, True, False)
|
|
|
|
self.do_connect()
|
|
|
|
self.idlequeue.set_read_timeout(self.fd, CONNECT_TIMEOUT)
|
|
|
|
return None
|
|
|
|
|
|
|
|
def do_connect(self):
|
2010-02-08 15:08:40 +01:00
|
|
|
try:
|
2011-08-09 01:43:07 +02:00
|
|
|
self._sock.connect(self._server)
|
|
|
|
self._send=self._sock.send
|
|
|
|
self._recv=self._sock.recv
|
2013-12-30 21:19:15 +01:00
|
|
|
except (OpenSSL.SSL.WantReadError, OpenSSL.SSL.WantWriteError) as e:
|
2013-12-04 18:43:28 +01:00
|
|
|
pass
|
2013-01-01 23:18:36 +01:00
|
|
|
except Exception as ee:
|
2013-01-05 00:03:36 +01:00
|
|
|
errnum = ee.errno
|
2011-08-09 01:43:07 +02:00
|
|
|
self.connect_timeout += 1
|
|
|
|
if errnum == 111 or self.connect_timeout > 1000:
|
2011-08-24 10:36:00 +02:00
|
|
|
self.queue._connection_refused(self.streamhost, self.file_props,
|
|
|
|
self.queue_idx)
|
2011-08-09 01:43:07 +02:00
|
|
|
self.connected = False
|
|
|
|
return None
|
|
|
|
# win32 needs this
|
|
|
|
elif errnum not in (10056, EISCONN) or self.state != 0:
|
|
|
|
return None
|
|
|
|
else: # socket is already connected
|
|
|
|
self._sock.setblocking(False)
|
|
|
|
self._send=self._sock.send
|
|
|
|
self._recv=self._sock.recv
|
|
|
|
self.buff = ''
|
|
|
|
self.connected = True
|
2012-06-14 18:27:23 +02:00
|
|
|
self.file_props.connected = True
|
|
|
|
self.file_props.disconnect_cb = self.disconnect
|
|
|
|
self.file_props.paused = False
|
2011-08-09 01:43:07 +02:00
|
|
|
self.state = 1 # connected
|
|
|
|
# stop all others connections to sender's streamhosts
|
|
|
|
self.queue._socket_connected(self.streamhost, self.file_props)
|
|
|
|
self.idlequeue.plug_idle(self, True, False)
|
|
|
|
return 1 # we are connected
|
2011-08-24 10:36:00 +02:00
|
|
|
|
2011-08-09 01:43:07 +02:00
|
|
|
def read_timeout(self):
|
|
|
|
self.idlequeue.remove_timeout(self.fd)
|
|
|
|
if self.state > 5:
|
|
|
|
# no activity for foo seconds
|
2012-06-14 18:27:23 +02:00
|
|
|
if self.file_props.stalled == False:
|
|
|
|
self.file_props.stalled = True
|
2011-08-09 01:43:07 +02:00
|
|
|
self.queue.process_result(-1, self)
|
2012-06-14 18:27:23 +02:00
|
|
|
if not self.file_props.received_len:
|
|
|
|
self.file_props.received_len = 0
|
2011-08-09 01:43:07 +02:00
|
|
|
if SEND_TIMEOUT > 0:
|
|
|
|
self.idlequeue.set_read_timeout(self.fd, SEND_TIMEOUT)
|
|
|
|
else:
|
|
|
|
# stop transfer, there is no error code for this
|
|
|
|
self.pollend()
|
|
|
|
else:
|
2011-12-23 03:52:37 +01:00
|
|
|
if self.mode == 'client':
|
2011-08-09 01:43:07 +02:00
|
|
|
self.queue.reconnect_client(self, self.streamhost)
|
2011-08-24 10:36:00 +02:00
|
|
|
|
2011-08-09 01:43:07 +02:00
|
|
|
def open_file_for_reading(self):
|
|
|
|
if self.file is None:
|
|
|
|
try:
|
2012-06-14 18:27:23 +02:00
|
|
|
self.file = open(self.file_props.file_name, 'rb')
|
|
|
|
if self.file_props.offset:
|
|
|
|
self.size = self.file_props.offset
|
2011-08-09 01:43:07 +02:00
|
|
|
self.file.seek(self.size)
|
2012-06-14 18:27:23 +02:00
|
|
|
self.file_props.received_len = self.size
|
2013-01-01 23:18:36 +01:00
|
|
|
except IOError as e:
|
2011-08-09 01:43:07 +02:00
|
|
|
self.close_file()
|
2013-01-02 13:54:02 +01:00
|
|
|
raise IOError(str(e))
|
2011-08-09 01:43:07 +02:00
|
|
|
|
|
|
|
def close_file(self):
|
2013-01-24 21:23:57 +01:00
|
|
|
# Close file we're sending from
|
2011-08-09 01:43:07 +02:00
|
|
|
if self.file:
|
|
|
|
if not self.file.closed:
|
|
|
|
try:
|
|
|
|
self.file.close()
|
|
|
|
except Exception:
|
|
|
|
pass
|
|
|
|
self.file = None
|
2013-01-24 21:23:57 +01:00
|
|
|
# Close file we're receiving into
|
2017-08-12 14:40:26 +02:00
|
|
|
if self.file_props.fd and self.state >= 7:
|
2013-01-24 21:23:57 +01:00
|
|
|
try:
|
|
|
|
self.file_props.fd.close()
|
|
|
|
except Exception:
|
|
|
|
pass
|
2011-08-09 01:43:07 +02:00
|
|
|
|
|
|
|
def get_fd(self):
|
|
|
|
"""
|
2012-08-22 12:55:57 +02:00
|
|
|
Test if file is already open and return its fd, or just open the file
|
|
|
|
and return the fd
|
2011-08-09 01:43:07 +02:00
|
|
|
"""
|
2012-06-14 18:27:23 +02:00
|
|
|
if self.file_props.fd:
|
|
|
|
fd = self.file_props.fd
|
2011-08-09 01:43:07 +02:00
|
|
|
else:
|
|
|
|
offset = 0
|
|
|
|
opt = 'wb'
|
2012-06-14 18:27:23 +02:00
|
|
|
if self.file_props.offset:
|
|
|
|
offset = self.file_props.offset
|
2011-08-09 01:43:07 +02:00
|
|
|
opt = 'ab'
|
2012-06-14 18:27:23 +02:00
|
|
|
fd = open(self.file_props.file_name, opt)
|
|
|
|
self.file_props.fd = fd
|
|
|
|
self.file_props.elapsed_time = 0
|
2016-04-04 21:10:57 +02:00
|
|
|
self.file_props.last_time = time.time()
|
2012-06-14 18:27:23 +02:00
|
|
|
self.file_props.received_len = offset
|
2011-08-09 01:43:07 +02:00
|
|
|
return fd
|
|
|
|
|
|
|
|
def rem_fd(self, fd):
|
2012-06-14 18:27:23 +02:00
|
|
|
if self.file_props.fd:
|
|
|
|
self.file_props.fd = None
|
2011-08-09 01:43:07 +02:00
|
|
|
try:
|
|
|
|
fd.close()
|
|
|
|
except Exception:
|
|
|
|
pass
|
|
|
|
|
|
|
|
def receive(self):
|
|
|
|
"""
|
|
|
|
Read small chunks of data. Call owner's disconnected() method if
|
|
|
|
appropriate
|
|
|
|
"""
|
2015-07-28 20:32:14 +02:00
|
|
|
received = b''
|
2011-08-09 01:43:07 +02:00
|
|
|
try:
|
2015-07-28 20:32:14 +02:00
|
|
|
add = self._recv(64)
|
2011-08-09 01:43:07 +02:00
|
|
|
except (OpenSSL.SSL.WantReadError, OpenSSL.SSL.WantWriteError,
|
2013-01-01 23:18:36 +01:00
|
|
|
OpenSSL.SSL.WantX509LookupError) as e:
|
2011-08-09 01:43:07 +02:00
|
|
|
log.info('SSL rehandshake request : ' + repr(e))
|
|
|
|
raise e
|
|
|
|
except Exception:
|
2015-07-28 20:32:14 +02:00
|
|
|
add = b''
|
2011-08-09 01:43:07 +02:00
|
|
|
received += add
|
|
|
|
if len(add) == 0:
|
|
|
|
self.disconnect()
|
|
|
|
return add
|
|
|
|
|
|
|
|
def send_raw(self, raw_data):
|
|
|
|
"""
|
|
|
|
Write raw outgoing data
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
self._send(raw_data)
|
|
|
|
except (OpenSSL.SSL.WantReadError, OpenSSL.SSL.WantWriteError,
|
2013-01-01 23:18:36 +01:00
|
|
|
OpenSSL.SSL.WantX509LookupError) as e:
|
2011-08-09 01:43:07 +02:00
|
|
|
log.info('SSL rehandshake request :' + repr(e))
|
|
|
|
raise e
|
2013-01-05 00:03:36 +01:00
|
|
|
except Exception:
|
2011-08-09 01:43:07 +02:00
|
|
|
self.disconnect()
|
|
|
|
return len(raw_data)
|
|
|
|
|
|
|
|
def write_next(self):
|
2015-07-28 20:32:14 +02:00
|
|
|
if self.remaining_buff != b'':
|
2011-08-09 01:43:07 +02:00
|
|
|
buff = self.remaining_buff
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
self.open_file_for_reading()
|
2013-01-05 00:03:36 +01:00
|
|
|
except IOError:
|
2011-08-09 01:43:07 +02:00
|
|
|
self.state = 8 # end connection
|
|
|
|
self.disconnect()
|
2012-06-14 18:27:23 +02:00
|
|
|
self.file_props.error = -7 # unable to read from file
|
2011-08-09 01:43:07 +02:00
|
|
|
return -1
|
|
|
|
buff = self.file.read(MAX_BUFF_LEN)
|
|
|
|
if len(buff) > 0:
|
|
|
|
lenn = 0
|
|
|
|
try:
|
|
|
|
lenn = self._send(buff)
|
|
|
|
except (OpenSSL.SSL.WantReadError, OpenSSL.SSL.WantWriteError,
|
2013-01-01 23:18:36 +01:00
|
|
|
OpenSSL.SSL.WantX509LookupError) as e:
|
2011-08-09 01:43:07 +02:00
|
|
|
log.info('SSL rehandshake request :' + repr(e))
|
|
|
|
raise e
|
2013-01-01 23:18:36 +01:00
|
|
|
except Exception as e:
|
2013-01-05 00:03:36 +01:00
|
|
|
if e.errno not in (EINTR, ENOBUFS, EWOULDBLOCK):
|
2011-08-09 01:43:07 +02:00
|
|
|
# peer stopped reading
|
|
|
|
self.state = 8 # end connection
|
|
|
|
self.disconnect()
|
2012-06-14 18:27:23 +02:00
|
|
|
self.file_props.error = -1
|
2011-08-09 01:43:07 +02:00
|
|
|
return -1
|
|
|
|
self.size += lenn
|
2016-04-04 21:10:57 +02:00
|
|
|
current_time = time.time()
|
2012-06-14 18:27:23 +02:00
|
|
|
self.file_props.elapsed_time += current_time - \
|
|
|
|
self.file_props.last_time
|
|
|
|
self.file_props.last_time = current_time
|
|
|
|
self.file_props.received_len = self.size
|
2013-11-02 09:00:16 +01:00
|
|
|
if self.size >= self.file_props.size:
|
2011-08-09 01:43:07 +02:00
|
|
|
self.state = 8 # end connection
|
2012-06-14 18:27:23 +02:00
|
|
|
self.file_props.error = 0
|
2011-08-09 01:43:07 +02:00
|
|
|
self.disconnect()
|
|
|
|
return -1
|
|
|
|
if lenn != len(buff):
|
|
|
|
self.remaining_buff = buff[lenn:]
|
|
|
|
else:
|
2015-07-28 20:32:14 +02:00
|
|
|
self.remaining_buff = b''
|
2011-08-09 01:43:07 +02:00
|
|
|
self.state = 7 # continue to write in the socket
|
|
|
|
if lenn == 0:
|
|
|
|
return None
|
2012-06-14 18:27:23 +02:00
|
|
|
self.file_props.stalled = False
|
2011-08-09 01:43:07 +02:00
|
|
|
return lenn
|
|
|
|
else:
|
|
|
|
self.state = 8 # end connection
|
|
|
|
self.disconnect()
|
|
|
|
return -1
|
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
def get_file_contents(self, timeout):
|
|
|
|
"""
|
|
|
|
Read file contents from socket and write them to file
|
|
|
|
"""
|
2012-06-14 18:27:23 +02:00
|
|
|
if self.file_props is None or not self.file_props.file_name:
|
|
|
|
self.file_props.error = -2
|
2010-02-08 15:08:40 +01:00
|
|
|
return None
|
|
|
|
fd = None
|
2015-07-28 20:32:14 +02:00
|
|
|
if self.remaining_buff != b'':
|
2010-02-08 15:08:40 +01:00
|
|
|
try:
|
|
|
|
fd = self.get_fd()
|
2013-01-05 00:03:36 +01:00
|
|
|
except IOError:
|
2017-08-24 23:01:06 +02:00
|
|
|
self.disconnect()
|
2012-06-14 18:27:23 +02:00
|
|
|
self.file_props.error = -6 # file system error
|
2010-02-08 15:08:40 +01:00
|
|
|
return 0
|
|
|
|
fd.write(self.remaining_buff)
|
|
|
|
lenn = len(self.remaining_buff)
|
2016-04-04 21:10:57 +02:00
|
|
|
current_time = time.time()
|
2012-06-14 18:27:23 +02:00
|
|
|
self.file_props.elapsed_time += current_time - \
|
|
|
|
self.file_props.last_time
|
|
|
|
self.file_props.last_time = current_time
|
|
|
|
self.file_props.received_len += lenn
|
2015-07-28 20:32:14 +02:00
|
|
|
self.remaining_buff = b''
|
2013-11-02 09:00:16 +01:00
|
|
|
if self.file_props.received_len == self.file_props.size:
|
2010-02-08 15:08:40 +01:00
|
|
|
self.rem_fd(fd)
|
|
|
|
self.disconnect()
|
2012-06-14 18:27:23 +02:00
|
|
|
self.file_props.error = 0
|
|
|
|
self.file_props.completed = True
|
2010-02-08 15:08:40 +01:00
|
|
|
return 0
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
fd = self.get_fd()
|
2013-01-05 00:03:36 +01:00
|
|
|
except IOError:
|
2017-08-24 23:01:06 +02:00
|
|
|
self.disconnect()
|
2012-06-14 18:27:23 +02:00
|
|
|
self.file_props.error = -6 # file system error
|
2010-02-08 15:08:40 +01:00
|
|
|
return 0
|
|
|
|
try:
|
2015-07-28 20:32:14 +02:00
|
|
|
buff = self._recv(MAX_BUFF_LEN)
|
2010-07-29 15:40:40 +02:00
|
|
|
except (OpenSSL.SSL.WantReadError, OpenSSL.SSL.WantWriteError,
|
2013-01-01 23:18:36 +01:00
|
|
|
OpenSSL.SSL.WantX509LookupError) as e:
|
2010-07-29 15:40:40 +02:00
|
|
|
log.info('SSL rehandshake request :' + repr(e))
|
|
|
|
raise e
|
2010-02-08 15:08:40 +01:00
|
|
|
except Exception:
|
2015-07-28 20:32:14 +02:00
|
|
|
buff = b''
|
2016-04-04 21:10:57 +02:00
|
|
|
current_time = time.time()
|
2012-06-14 18:27:23 +02:00
|
|
|
self.file_props.elapsed_time += current_time - \
|
|
|
|
self.file_props.last_time
|
|
|
|
self.file_props.last_time = current_time
|
|
|
|
self.file_props.received_len += len(buff)
|
2010-02-08 15:08:40 +01:00
|
|
|
if len(buff) == 0:
|
|
|
|
# Transfer stopped somehow:
|
|
|
|
# reset, paused or network error
|
|
|
|
self.rem_fd(fd)
|
2011-12-27 03:18:45 +01:00
|
|
|
self.disconnect()
|
2012-06-14 18:27:23 +02:00
|
|
|
self.file_props.error = -1
|
2010-02-08 15:08:40 +01:00
|
|
|
return 0
|
|
|
|
try:
|
|
|
|
fd.write(buff)
|
2013-01-05 00:03:36 +01:00
|
|
|
except IOError:
|
2010-02-08 15:08:40 +01:00
|
|
|
self.rem_fd(fd)
|
2011-12-27 03:18:45 +01:00
|
|
|
self.disconnect()
|
2012-06-14 18:27:23 +02:00
|
|
|
self.file_props.error = -6 # file system error
|
2010-02-08 15:08:40 +01:00
|
|
|
return 0
|
2013-11-02 09:00:16 +01:00
|
|
|
if self.file_props.received_len >= self.file_props.size:
|
2010-02-08 15:08:40 +01:00
|
|
|
# transfer completed
|
|
|
|
self.rem_fd(fd)
|
|
|
|
self.disconnect()
|
2012-06-14 18:27:23 +02:00
|
|
|
self.file_props.error = 0
|
|
|
|
self.file_props.completed = True
|
2010-02-08 15:08:40 +01:00
|
|
|
return 0
|
|
|
|
# return number of read bytes. It can be used in progressbar
|
|
|
|
if fd is not None:
|
2012-06-14 18:27:23 +02:00
|
|
|
self.file_props.stalled = False
|
|
|
|
if fd is None and self.file_props.stalled is False:
|
2010-02-08 15:08:40 +01:00
|
|
|
return None
|
2012-06-14 18:27:23 +02:00
|
|
|
if self.file_props.received_len:
|
|
|
|
if self.file_props.received_len != 0:
|
|
|
|
return self.file_props.received_len
|
2010-02-08 15:08:40 +01:00
|
|
|
return None
|
|
|
|
|
|
|
|
def disconnect(self):
|
|
|
|
"""
|
|
|
|
Close open descriptors and remover socket descr. from idleque
|
|
|
|
"""
|
|
|
|
# be sure that we don't leave open file
|
|
|
|
self.close_file()
|
|
|
|
self.idlequeue.remove_timeout(self.fd)
|
|
|
|
self.idlequeue.unplug_idle(self.fd)
|
2014-06-01 15:42:06 +02:00
|
|
|
if self.mode == 'server' and self.queue.listener:
|
2011-12-27 03:18:45 +01:00
|
|
|
try:
|
|
|
|
self.queue.listener.connections.remove(self._sock)
|
|
|
|
except ValueError:
|
|
|
|
pass # Not in list
|
|
|
|
if self.queue.listener.connections == []:
|
|
|
|
self.queue.listener.disconnect()
|
2010-02-08 15:08:40 +01:00
|
|
|
try:
|
2012-08-23 11:36:51 +02:00
|
|
|
if isinstance(self._sock, OpenSSL.SSL.Connection):
|
|
|
|
self._sock.shutdown()
|
|
|
|
else:
|
|
|
|
self._sock.shutdown(socket.SHUT_RDWR)
|
2010-02-08 15:08:40 +01:00
|
|
|
self._sock.close()
|
|
|
|
except Exception:
|
|
|
|
# socket is already closed
|
|
|
|
pass
|
|
|
|
self.connected = False
|
|
|
|
self.fd = -1
|
|
|
|
self.state = -1
|
|
|
|
|
|
|
|
def _get_auth_buff(self):
|
|
|
|
"""
|
|
|
|
Message, that we support 1 one auth mechanism: the 'no auth' mechanism
|
|
|
|
"""
|
|
|
|
return struct.pack('!BBB', 0x05, 0x01, 0x00)
|
|
|
|
|
|
|
|
def _parse_auth_buff(self, buff):
|
|
|
|
"""
|
|
|
|
Parse the initial message and create a list of auth mechanisms
|
|
|
|
"""
|
2017-08-16 15:27:35 +02:00
|
|
|
if buff[0] != 5:
|
2010-02-08 15:08:40 +01:00
|
|
|
return None
|
2017-08-16 15:27:35 +02:00
|
|
|
num_auth = buff[1]
|
|
|
|
return list(buff[2:2+num_auth])
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
def _get_auth_response(self):
|
|
|
|
"""
|
|
|
|
Socks version(5), number of extra auth methods (we send 0x00 - no auth)
|
|
|
|
"""
|
|
|
|
return struct.pack('!BB', 0x05, 0x00)
|
|
|
|
|
|
|
|
def _get_connect_buff(self):
|
2011-08-24 10:36:00 +02:00
|
|
|
"""
|
|
|
|
Connect request by domain name
|
|
|
|
"""
|
2010-02-08 15:08:40 +01:00
|
|
|
buff = struct.pack('!BBBBB%dsBB' % len(self.host),
|
2013-01-05 00:03:36 +01:00
|
|
|
0x05, 0x01, 0x00, 0x03, len(self.host), self.host.encode('utf-8'),
|
|
|
|
self.port >> 8, self.port & 0xff)
|
2010-02-08 15:08:40 +01:00
|
|
|
return buff
|
|
|
|
|
|
|
|
def _get_request_buff(self, msg, command = 0x01):
|
|
|
|
"""
|
|
|
|
Connect request by domain name, sid sha, instead of domain name (jep
|
|
|
|
0096)
|
|
|
|
"""
|
2013-08-01 15:54:07 +02:00
|
|
|
if isinstance(msg, str):
|
|
|
|
msg = msg.encode('utf-8')
|
|
|
|
buff = struct.pack('!BBBBB%dsBB' % len(msg), 0x05, command, 0x00, 0x03,
|
|
|
|
len(msg), msg, 0, 0)
|
2010-02-08 15:08:40 +01:00
|
|
|
return buff
|
|
|
|
|
|
|
|
def _parse_request_buff(self, buff):
|
|
|
|
try: # don't trust on what comes from the outside
|
2015-07-28 20:32:14 +02:00
|
|
|
req_type, host_type, = struct.unpack('!xBxB', buff[:4])
|
2010-02-08 15:08:40 +01:00
|
|
|
if host_type == 0x01:
|
2015-07-28 20:32:14 +02:00
|
|
|
host_arr = struct.unpack('!iiii', buff[4:8])
|
2010-02-08 15:08:40 +01:00
|
|
|
host, = '.'.join(str(s) for s in host_arr)
|
|
|
|
host_len = len(host)
|
|
|
|
elif host_type == 0x03:
|
2017-08-16 15:27:35 +02:00
|
|
|
host_len = buff[4]
|
2015-07-28 20:32:14 +02:00
|
|
|
host, = struct.unpack('!%ds' % host_len, buff[5:5 + host_len])
|
2010-02-08 15:08:40 +01:00
|
|
|
portlen = len(buff[host_len + 5:])
|
|
|
|
if portlen == 1:
|
2015-07-28 20:32:14 +02:00
|
|
|
port, = struct.unpack('!B', buff[host_len + 5])
|
2010-02-08 15:08:40 +01:00
|
|
|
elif portlen == 2:
|
2015-07-28 20:32:14 +02:00
|
|
|
port, = struct.unpack('!H', buff[host_len + 5:])
|
2010-02-08 15:08:40 +01:00
|
|
|
# file data, comes with auth message (Gaim bug)
|
|
|
|
else:
|
2015-07-28 20:32:14 +02:00
|
|
|
port, = struct.unpack('!H', buff[host_len + 5: host_len + 7])
|
2010-02-08 15:08:40 +01:00
|
|
|
self.remaining_buff = buff[host_len + 7:]
|
|
|
|
except Exception:
|
|
|
|
return (None, None, None)
|
|
|
|
return (req_type, host, port)
|
|
|
|
|
|
|
|
def read_connect(self):
|
|
|
|
"""
|
|
|
|
Connect response: version, auth method
|
|
|
|
"""
|
2010-07-29 15:40:40 +02:00
|
|
|
try:
|
2013-01-05 00:03:36 +01:00
|
|
|
buff = self._recv().decode('utf-8')
|
2016-11-05 16:07:08 +01:00
|
|
|
except (OpenSSL.SSL.WantReadError, OpenSSL.SSL.WantWriteError,
|
|
|
|
OpenSSL.SSL.WantX509LookupError) as e:
|
2010-07-29 15:40:40 +02:00
|
|
|
log.info("SSL rehandshake request : " + repr(e))
|
|
|
|
raise e
|
2010-02-08 15:08:40 +01:00
|
|
|
try:
|
2015-07-28 20:32:14 +02:00
|
|
|
version, method = struct.unpack('!BB', buff)
|
2010-02-08 15:08:40 +01:00
|
|
|
except Exception:
|
|
|
|
version, method = None, None
|
|
|
|
if version != 0x05 or method == 0xff:
|
|
|
|
self.disconnect()
|
|
|
|
|
|
|
|
def continue_paused_transfer(self):
|
|
|
|
if self.state < 5:
|
|
|
|
return
|
2012-06-14 18:27:23 +02:00
|
|
|
if self.file_props.type_ == 'r':
|
2010-02-08 15:08:40 +01:00
|
|
|
self.idlequeue.plug_idle(self, False, True)
|
|
|
|
else:
|
|
|
|
self.idlequeue.plug_idle(self, True, False)
|
|
|
|
|
|
|
|
def _get_sha1_auth(self):
|
|
|
|
"""
|
|
|
|
Get sha of sid + Initiator jid + Target jid
|
|
|
|
"""
|
2013-07-07 14:22:37 +02:00
|
|
|
if self.file_props.is_a_proxy:
|
2013-07-28 20:50:11 +02:00
|
|
|
return hashlib.sha1(('%s%s%s' % (self.sid,
|
|
|
|
self.file_props.proxy_sender, self.file_props.proxy_receiver)).\
|
|
|
|
encode('utf-8')).hexdigest()
|
2013-01-05 00:03:36 +01:00
|
|
|
return hashlib.sha1(('%s%s%s' % (self.sid, self.initiator,
|
|
|
|
self.target)).encode('utf-8')).hexdigest()
|
2012-06-20 23:58:08 +02:00
|
|
|
|
2010-08-25 12:05:14 +02:00
|
|
|
|
2011-11-18 05:55:44 +01:00
|
|
|
class Socks5Sender(IdleObject):
|
2011-08-09 01:43:07 +02:00
|
|
|
"""
|
|
|
|
Class for sending file to socket over socks5
|
|
|
|
"""
|
2011-11-18 05:55:44 +01:00
|
|
|
def __init__(self, idlequeue, sock_hash, parent, _sock, host=None,
|
2015-02-16 12:55:10 +01:00
|
|
|
port=None, fingerprint = None, connected=True, file_props=None):
|
2011-08-09 01:43:07 +02:00
|
|
|
self.fingerprint = fingerprint
|
|
|
|
self.queue_idx = sock_hash
|
|
|
|
self.queue = parent
|
2011-08-15 05:59:39 +02:00
|
|
|
self.file_props = file_props
|
2011-10-29 06:09:45 +02:00
|
|
|
self.proxy = False
|
2011-08-09 01:43:07 +02:00
|
|
|
self._sock = _sock
|
|
|
|
if _sock is not None:
|
2012-08-23 11:36:51 +02:00
|
|
|
if self.fingerprint is not None and not isinstance(self._sock,
|
|
|
|
OpenSSL.SSL.Connection):
|
2011-08-09 01:43:07 +02:00
|
|
|
self._sock = OpenSSL.SSL.Connection(
|
2012-08-22 12:55:57 +02:00
|
|
|
jingle_xtls.get_context('server'), _sock)
|
2011-08-09 01:43:07 +02:00
|
|
|
else:
|
|
|
|
self._sock.setblocking(False)
|
|
|
|
self.fd = _sock.fileno()
|
|
|
|
self._recv = _sock.recv
|
|
|
|
self._send = _sock.send
|
|
|
|
self.connected = connected
|
|
|
|
self.state = 1 # waiting for first bytes
|
|
|
|
self.connect_timeout = 0
|
2011-08-24 10:36:00 +02:00
|
|
|
|
2012-06-14 18:27:23 +02:00
|
|
|
self.file_props.error = 0
|
|
|
|
self.file_props.disconnect_cb = self.disconnect
|
|
|
|
self.file_props.started = True
|
|
|
|
self.file_props.completed = False
|
|
|
|
self.file_props.paused = False
|
|
|
|
self.file_props.continue_cb = self.continue_paused_transfer
|
|
|
|
self.file_props.stalled = False
|
|
|
|
self.file_props.connected = True
|
|
|
|
self.file_props.elapsed_time = 0
|
2016-04-04 21:10:57 +02:00
|
|
|
self.file_props.last_time = time.time()
|
2012-06-14 18:27:23 +02:00
|
|
|
self.file_props.received_len = 0
|
2010-02-08 15:08:40 +01:00
|
|
|
|
2011-08-09 01:43:07 +02:00
|
|
|
def start_transfer(self):
|
|
|
|
"""
|
|
|
|
Send the file
|
|
|
|
"""
|
|
|
|
return self.write_next()
|
2011-08-24 10:36:00 +02:00
|
|
|
|
2011-08-15 05:59:39 +02:00
|
|
|
def set_connection_sock(self, _sock):
|
|
|
|
self._sock = _sock
|
|
|
|
if self.fingerprint is not None:
|
|
|
|
self._sock = OpenSSL.SSL.Connection(
|
2012-08-23 11:36:51 +02:00
|
|
|
jingle_xtls.get_context('client'), _sock)
|
2011-08-15 05:59:39 +02:00
|
|
|
else:
|
|
|
|
self._sock.setblocking(False)
|
|
|
|
self.fd = _sock.fileno()
|
|
|
|
self._recv = _sock.recv
|
|
|
|
self._send = _sock.send
|
|
|
|
self.connected = True
|
|
|
|
self.state = 1 # waiting for first bytes
|
|
|
|
self.file_props = None
|
|
|
|
# start waiting for data
|
|
|
|
self.idlequeue.plug_idle(self, False, True)
|
2011-08-24 10:36:00 +02:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
def send_file(self):
|
|
|
|
"""
|
|
|
|
Start sending the file over verified connection
|
|
|
|
"""
|
|
|
|
self.pauses = 0
|
|
|
|
self.state = 7
|
|
|
|
# plug for writing
|
|
|
|
self.idlequeue.plug_idle(self, True, False)
|
|
|
|
return self.write_next() # initial for nl byte
|
|
|
|
|
|
|
|
def disconnect(self, cb=True):
|
|
|
|
"""
|
|
|
|
Close the socket
|
|
|
|
"""
|
|
|
|
# close connection and remove us from the queue
|
|
|
|
Socks5.disconnect(self)
|
|
|
|
if self.file_props is not None:
|
2012-06-14 18:27:23 +02:00
|
|
|
self.file_props.connected = False
|
|
|
|
self.file_props.disconnect_cb = None
|
2010-02-08 15:08:40 +01:00
|
|
|
if self.queue is not None:
|
|
|
|
self.queue.remove_sender(self.queue_idx, False)
|
2005-08-23 23:52:56 +02:00
|
|
|
|
2012-08-23 23:45:38 +02:00
|
|
|
|
2011-11-18 05:55:44 +01:00
|
|
|
class Socks5Receiver(IdleObject):
|
|
|
|
def __init__(self, idlequeue, streamhost, sid, file_props = None,
|
|
|
|
fingerprint=None):
|
|
|
|
"""
|
2011-08-24 10:36:00 +02:00
|
|
|
fingerprint: fingerprint of certificates we shall use, set to None if
|
|
|
|
TLS connection not desired
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
|
|
|
self.queue_idx = -1
|
2011-11-18 05:55:44 +01:00
|
|
|
self.streamhost = streamhost
|
2010-02-08 15:08:40 +01:00
|
|
|
self.queue = None
|
2010-07-27 15:29:12 +02:00
|
|
|
self.fingerprint = fingerprint
|
2011-11-18 05:55:44 +01:00
|
|
|
self.connect_timeout = 0
|
|
|
|
self.connected = False
|
|
|
|
self.pauses = 0
|
2017-08-16 15:27:35 +02:00
|
|
|
self.sid = sid
|
2011-11-18 05:55:44 +01:00
|
|
|
self.file_props = file_props
|
2012-06-14 18:27:23 +02:00
|
|
|
self.file_props.disconnect_cb = self.disconnect
|
|
|
|
self.file_props.error = 0
|
|
|
|
self.file_props.started = True
|
|
|
|
self.file_props.completed = False
|
|
|
|
self.file_props.paused = False
|
|
|
|
self.file_props.continue_cb = self.continue_paused_transfer
|
|
|
|
self.file_props.stalled = False
|
|
|
|
self.file_props.received_len = 0
|
2012-06-20 23:58:08 +02:00
|
|
|
|
2011-11-18 05:55:44 +01:00
|
|
|
def receive_file(self):
|
|
|
|
"""
|
|
|
|
Start receiving the file over verified connection
|
|
|
|
"""
|
2012-06-14 18:27:23 +02:00
|
|
|
if self.file_props.started:
|
2011-11-18 05:55:44 +01:00
|
|
|
return
|
2012-06-14 18:27:23 +02:00
|
|
|
self.file_props.error = 0
|
|
|
|
self.file_props.disconnect_cb = self.disconnect
|
|
|
|
self.file_props.started = True
|
|
|
|
self.file_props.completed = False
|
|
|
|
self.file_props.paused = False
|
|
|
|
self.file_props.continue_cb = self.continue_paused_transfer
|
|
|
|
self.file_props.stalled = False
|
|
|
|
self.file_props.connected = True
|
|
|
|
self.file_props.elapsed_time = 0
|
2016-04-04 21:10:57 +02:00
|
|
|
self.file_props.last_time = time.time()
|
2012-06-14 18:27:23 +02:00
|
|
|
self.file_props.received_len = 0
|
2011-11-18 05:55:44 +01:00
|
|
|
self.pauses = 0
|
|
|
|
self.state = 7
|
|
|
|
# plug for reading
|
|
|
|
self.idlequeue.plug_idle(self, False, True)
|
|
|
|
return self.get_file_contents(0) # initial for nl byte
|
|
|
|
|
|
|
|
def start_transfer(self):
|
|
|
|
"""
|
|
|
|
Receive the file
|
|
|
|
"""
|
|
|
|
return self.get_file_contents(0)
|
|
|
|
|
|
|
|
def set_sock(self, _sock):
|
|
|
|
self._sock = _sock
|
|
|
|
self._sock.setblocking(False)
|
|
|
|
self.fd = _sock.fileno()
|
|
|
|
self._recv = _sock.recv
|
|
|
|
self._send = _sock.send
|
|
|
|
self.connected = True
|
|
|
|
self.state = 1 # waiting for first bytes
|
|
|
|
# start waiting for data
|
|
|
|
self.idlequeue.plug_idle(self, False, True)
|
|
|
|
|
|
|
|
def disconnect(self, cb=True):
|
|
|
|
"""
|
|
|
|
Close the socket. Remove self from queue if cb is True
|
|
|
|
"""
|
|
|
|
# close connection
|
|
|
|
Socks5.disconnect(self)
|
|
|
|
if cb is True:
|
2012-06-14 18:27:23 +02:00
|
|
|
self.file_props.disconnect_cb = None
|
2011-11-18 05:55:44 +01:00
|
|
|
if self.queue is not None:
|
|
|
|
self.queue.remove_receiver(self.queue_idx, False)
|
|
|
|
|
|
|
|
class Socks5Server(Socks5):
|
2012-08-23 23:45:38 +02:00
|
|
|
|
2011-11-18 05:55:44 +01:00
|
|
|
def __init__(self, idlequeue, host, port, initiator, target, sid):
|
|
|
|
Socks5.__init__(self, idlequeue, host, port, initiator, target, sid)
|
2011-12-23 03:52:37 +01:00
|
|
|
self.mode = 'server'
|
2011-11-18 05:55:44 +01:00
|
|
|
|
|
|
|
def main(self):
|
|
|
|
"""
|
|
|
|
Initial requests for verifying the connection
|
|
|
|
"""
|
|
|
|
if self.state == 1: # initial read
|
|
|
|
buff = self.receive()
|
|
|
|
if not self.connected:
|
|
|
|
return -1
|
|
|
|
mechs = self._parse_auth_buff(buff)
|
|
|
|
if mechs is None:
|
|
|
|
return -1 # invalid auth methods received
|
|
|
|
elif self.state == 3: # get next request
|
|
|
|
buff = self.receive()
|
|
|
|
req_type, self.sha_msg = self._parse_request_buff(buff)[:2]
|
|
|
|
if req_type != 0x01:
|
|
|
|
return -1 # request is not of type 'connect'
|
|
|
|
self.state += 1 # go to the next step
|
|
|
|
# unplug & plug for writing
|
|
|
|
self.idlequeue.plug_idle(self, True, False)
|
|
|
|
return None
|
|
|
|
|
|
|
|
def pollin(self):
|
2011-12-27 03:18:45 +01:00
|
|
|
self.idlequeue.remove_timeout(self.fd)
|
2011-11-18 05:55:44 +01:00
|
|
|
if self.connected:
|
|
|
|
try:
|
|
|
|
if self.state < 5:
|
|
|
|
result = self.main()
|
|
|
|
if self.state == 4:
|
|
|
|
self.queue.result_sha(self.sha_msg, self.queue_idx)
|
|
|
|
if result == -1:
|
|
|
|
self.disconnect()
|
|
|
|
elif self.state == 5:
|
|
|
|
self.state = 7
|
2012-09-18 06:12:07 +02:00
|
|
|
if self.file_props.type_ == 's':
|
2011-11-18 05:55:44 +01:00
|
|
|
# We wait for the end of the negotiation to
|
|
|
|
# send the file
|
|
|
|
self.idlequeue.plug_idle(self, False, False)
|
|
|
|
else:
|
|
|
|
# We plug for reading
|
|
|
|
self.idlequeue.plug_idle(self, False, True)
|
|
|
|
return
|
|
|
|
elif self.state == 7:
|
2012-06-14 18:27:23 +02:00
|
|
|
if self.file_props.paused:
|
2012-08-22 12:55:57 +02:00
|
|
|
self.file_props.continue_cb = \
|
|
|
|
self.continue_paused_transfer
|
2011-11-18 05:55:44 +01:00
|
|
|
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)
|
|
|
|
except (OpenSSL.SSL.WantReadError, OpenSSL.SSL.WantWriteError,
|
2013-01-01 23:18:36 +01:00
|
|
|
OpenSSL.SSL.WantX509LookupError) as e:
|
2011-11-18 05:55:44 +01:00
|
|
|
log.info('caught SSL exception, ignored')
|
|
|
|
else:
|
|
|
|
self.disconnect()
|
|
|
|
|
|
|
|
def pollend(self):
|
|
|
|
self.state = 8 # end connection
|
|
|
|
self.disconnect()
|
2012-06-14 18:27:23 +02:00
|
|
|
self.file_props.error = -1
|
2011-11-18 05:55:44 +01:00
|
|
|
self.queue.process_result(-1, self)
|
|
|
|
|
|
|
|
def pollout(self):
|
|
|
|
if not self.connected:
|
|
|
|
self.disconnect()
|
|
|
|
return
|
|
|
|
self.idlequeue.remove_timeout(self.fd)
|
2012-08-22 14:49:44 +02:00
|
|
|
try:
|
|
|
|
if self.state == 2: # send reply with desired auth type
|
|
|
|
self.send_raw(self._get_auth_response())
|
|
|
|
elif self.state == 4: # send positive response to the 'connect'
|
|
|
|
self.send_raw(self._get_request_buff(self.sha_msg, 0x00))
|
|
|
|
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
|
|
|
|
result = self.start_transfer() # send
|
|
|
|
self.queue.process_result(result, self)
|
|
|
|
if result is None or result <= 0:
|
|
|
|
self.disconnect()
|
|
|
|
return
|
|
|
|
self.idlequeue.set_read_timeout(self.fd, STALLED_TIMEOUT)
|
|
|
|
elif self.state == 8:
|
2011-11-18 05:55:44 +01:00
|
|
|
self.disconnect()
|
|
|
|
return
|
2012-08-22 14:49:44 +02:00
|
|
|
else:
|
|
|
|
self.disconnect()
|
|
|
|
except (OpenSSL.SSL.WantReadError, OpenSSL.SSL.WantWriteError,
|
2013-01-01 23:18:36 +01:00
|
|
|
OpenSSL.SSL.WantX509LookupError) as e:
|
2012-08-22 14:49:44 +02:00
|
|
|
log.info('caught SSL exception, ignored')
|
2011-11-18 05:55:44 +01:00
|
|
|
return
|
|
|
|
if self.state < 5:
|
|
|
|
self.state += 1
|
|
|
|
# unplug and plug this time for reading
|
|
|
|
self.idlequeue.plug_idle(self, False, True)
|
|
|
|
|
|
|
|
|
|
|
|
class Socks5Client(Socks5):
|
|
|
|
|
|
|
|
def __init__(self, idlequeue, host, port, initiator, target, sid):
|
|
|
|
Socks5.__init__(self, idlequeue, host, port, initiator, target, sid)
|
2011-12-23 03:52:37 +01:00
|
|
|
self.mode = 'client'
|
2011-11-19 00:21:34 +01:00
|
|
|
|
2011-11-18 05:55:44 +01:00
|
|
|
def main(self, timeout=0):
|
|
|
|
"""
|
|
|
|
Begin negotiation. on success 'address' != 0
|
|
|
|
"""
|
|
|
|
result = 1
|
|
|
|
buff = self.receive()
|
2017-08-14 14:41:21 +02:00
|
|
|
if buff == b'':
|
2011-11-18 05:55:44 +01:00
|
|
|
# end connection
|
|
|
|
self.pollend()
|
|
|
|
return
|
|
|
|
if self.state == 2: # read auth response
|
|
|
|
if buff is None or len(buff) != 2:
|
|
|
|
return None
|
2015-07-28 20:32:14 +02:00
|
|
|
version, method = struct.unpack('!BB', buff[:2])
|
2011-11-18 05:55:44 +01:00
|
|
|
if version != 0x05 or method == 0xff:
|
|
|
|
self.disconnect()
|
|
|
|
elif self.state == 4: # get approve of our request
|
|
|
|
if buff is None:
|
|
|
|
return None
|
|
|
|
sub_buff = buff[:4]
|
|
|
|
if len(sub_buff) < 4:
|
|
|
|
return None
|
2015-07-28 20:32:14 +02:00
|
|
|
version, address_type = struct.unpack('!BxxB', buff[:4])
|
2011-11-18 05:55:44 +01:00
|
|
|
addrlen = 0
|
|
|
|
if address_type == 0x03:
|
2015-07-28 20:32:14 +02:00
|
|
|
addrlen = buff[4]
|
|
|
|
address = struct.unpack('!%ds' % addrlen, buff[5:addrlen + 5])
|
2011-11-18 05:55:44 +01:00
|
|
|
portlen = len(buff[addrlen + 5:])
|
|
|
|
if portlen == 1:
|
2015-07-28 20:32:14 +02:00
|
|
|
port, = struct.unpack('!B', buff[addrlen + 5])
|
2011-11-18 05:55:44 +01:00
|
|
|
elif portlen == 2:
|
2015-07-28 20:32:14 +02:00
|
|
|
port, = struct.unpack('!H', buff[addrlen + 5:])
|
2011-11-18 05:55:44 +01:00
|
|
|
else: # Gaim bug :)
|
2015-07-28 20:32:14 +02:00
|
|
|
port, = struct.unpack('!H', buff[addrlen + 5:addrlen + 7])
|
2011-11-18 05:55:44 +01:00
|
|
|
self.remaining_buff = buff[addrlen + 7:]
|
|
|
|
self.state = 5 # for senders: init file_props and send '\n'
|
|
|
|
if self.queue.on_success:
|
|
|
|
result = self.queue.send_success_reply(self.file_props,
|
|
|
|
self.streamhost)
|
2012-09-18 06:12:07 +02:00
|
|
|
if self.file_props.type_ == 's' and self.proxy:
|
2012-08-22 12:55:57 +02:00
|
|
|
self.queue.process_result(self.send_file(), self)
|
2011-11-18 05:55:44 +01:00
|
|
|
return
|
|
|
|
if result == 0:
|
|
|
|
self.state = 8
|
|
|
|
self.disconnect()
|
|
|
|
# for senders: init file_props
|
|
|
|
if result == 1 and self.state == 5:
|
2012-06-14 18:27:23 +02:00
|
|
|
if self.file_props.type_ == 's':
|
|
|
|
self.file_props.error = 0
|
|
|
|
self.file_props.disconnect_cb = self.disconnect
|
|
|
|
self.file_props.started = True
|
|
|
|
self.file_props.completed = False
|
|
|
|
self.file_props.paused = False
|
|
|
|
self.file_props.stalled = False
|
|
|
|
self.file_props.elapsed_time = 0
|
2016-04-04 21:10:57 +02:00
|
|
|
self.file_props.last_time = time.time()
|
2012-06-14 18:27:23 +02:00
|
|
|
self.file_props.received_len = 0
|
2011-11-18 05:55:44 +01:00
|
|
|
self.pauses = 0
|
|
|
|
# start sending file contents to socket
|
|
|
|
#self.idlequeue.set_read_timeout(self.fd, STALLED_TIMEOUT)
|
|
|
|
#self.idlequeue.plug_idle(self, True, False)
|
|
|
|
self.idlequeue.plug_idle(self, False, False)
|
|
|
|
else:
|
|
|
|
# receiving file contents from socket
|
|
|
|
self.idlequeue.plug_idle(self, False, True)
|
2012-06-14 18:27:23 +02:00
|
|
|
self.file_props.continue_cb = self.continue_paused_transfer
|
2011-11-18 05:55:44 +01:00
|
|
|
# we have set up the connection, next - retrieve file
|
|
|
|
self.state = 6
|
|
|
|
if self.state < 5:
|
|
|
|
self.idlequeue.plug_idle(self, True, False)
|
|
|
|
self.state += 1
|
|
|
|
return None
|
|
|
|
|
2013-12-04 18:43:28 +01:00
|
|
|
def send_file(self):
|
|
|
|
if self.ssl_errnum > 0:
|
|
|
|
log.error('remote certificate does not match the announced one.' + \
|
|
|
|
'\nSSL Error: %d\nCancelling file transfer' % self.ssl_errnum)
|
|
|
|
self.file_props.error = -12
|
|
|
|
return -1
|
|
|
|
return super(Socks5Client, self).send_file()
|
|
|
|
|
2011-11-18 05:55:44 +01:00
|
|
|
def pollin(self):
|
|
|
|
self.idlequeue.remove_timeout(self.fd)
|
|
|
|
if self.connected:
|
|
|
|
try:
|
2012-06-14 18:27:23 +02:00
|
|
|
if self.file_props.paused:
|
2011-11-18 05:55:44 +01:00
|
|
|
self.idlequeue.plug_idle(self, False, False)
|
|
|
|
return
|
|
|
|
if self.state < 5:
|
|
|
|
self.idlequeue.set_read_timeout(self.fd, CONNECT_TIMEOUT)
|
|
|
|
result = self.main(0)
|
|
|
|
self.queue.process_result(result, self)
|
|
|
|
elif self.state == 5: # wait for proxy reply
|
|
|
|
pass
|
2012-06-14 18:27:23 +02:00
|
|
|
elif self.file_props.type_ == 'r':
|
2011-11-18 05:55:44 +01:00
|
|
|
self.idlequeue.set_read_timeout(self.fd, STALLED_TIMEOUT)
|
|
|
|
result = self.start_transfer() # receive
|
|
|
|
self.queue.process_result(result, self)
|
|
|
|
except (OpenSSL.SSL.WantReadError, OpenSSL.SSL.WantWriteError,
|
2013-01-01 23:18:36 +01:00
|
|
|
OpenSSL.SSL.WantX509LookupError) as e:
|
2011-11-18 05:55:44 +01:00
|
|
|
log.info('caught SSL exception, ignored')
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
self.disconnect()
|
|
|
|
|
|
|
|
def pollout(self):
|
|
|
|
self.idlequeue.remove_timeout(self.fd)
|
|
|
|
try:
|
|
|
|
if self.state == 0:
|
|
|
|
self.do_connect()
|
|
|
|
return
|
|
|
|
elif self.state == 1: # send initially: version and auth types
|
|
|
|
self.send_raw(self._get_auth_buff())
|
|
|
|
elif self.state == 3: # send 'connect' request
|
|
|
|
self.send_raw(self._get_request_buff(self._get_sha1_auth()))
|
2012-06-14 18:27:23 +02:00
|
|
|
elif self.file_props.type_ != 'r':
|
|
|
|
if self.file_props.paused:
|
2011-11-18 05:55:44 +01:00
|
|
|
self.idlequeue.plug_idle(self, False, False)
|
|
|
|
return
|
|
|
|
result = self.start_transfer() # send
|
|
|
|
self.queue.process_result(result, self)
|
|
|
|
return
|
|
|
|
except (OpenSSL.SSL.WantReadError, OpenSSL.SSL.WantWriteError,
|
2013-01-01 23:18:36 +01:00
|
|
|
OpenSSL.SSL.WantX509LookupError) as e:
|
2011-11-18 05:55:44 +01:00
|
|
|
log.info('caught SSL exception, ignored')
|
|
|
|
return
|
|
|
|
self.state += 1
|
|
|
|
# unplug and plug for reading
|
|
|
|
self.idlequeue.plug_idle(self, False, True)
|
|
|
|
self.idlequeue.set_read_timeout(self.fd, CONNECT_TIMEOUT)
|
|
|
|
|
|
|
|
def pollend(self):
|
|
|
|
if self.state >= 5:
|
|
|
|
# error during transfer
|
|
|
|
self.disconnect()
|
2012-06-14 18:27:23 +02:00
|
|
|
self.file_props.error = -1
|
2011-11-18 05:55:44 +01:00
|
|
|
self.queue.process_result(-1, self)
|
|
|
|
else:
|
|
|
|
self.queue.reconnect_client(self, self.streamhost)
|
|
|
|
|
|
|
|
|
|
|
|
class Socks5SenderClient(Socks5Client, Socks5Sender):
|
|
|
|
|
|
|
|
def __init__(self, idlequeue, sock_hash, parent,_sock, host=None,
|
2015-04-05 21:26:16 +02:00
|
|
|
port=None, fingerprint = None, connected=True, file_props=None,
|
|
|
|
initiator=None, target=None):
|
|
|
|
Socks5Client.__init__(self, idlequeue, host, port, initiator, target,
|
2017-08-16 15:27:35 +02:00
|
|
|
file_props.transport_sid)
|
2012-06-20 23:58:08 +02:00
|
|
|
Socks5Sender.__init__(self,idlequeue, sock_hash, parent,_sock,
|
2012-08-22 12:55:57 +02:00
|
|
|
host, port, fingerprint , connected, file_props)
|
2011-11-18 05:55:44 +01:00
|
|
|
|
|
|
|
|
|
|
|
class Socks5SenderServer(Socks5Server, Socks5Sender):
|
2012-06-20 23:58:08 +02:00
|
|
|
|
2011-11-18 05:55:44 +01:00
|
|
|
def __init__(self, idlequeue, sock_hash, parent,_sock, host=None,
|
2015-02-16 12:55:10 +01:00
|
|
|
port=None, fingerprint = None, connected=True, file_props=None):
|
2011-12-23 03:52:37 +01:00
|
|
|
Socks5Server.__init__(self, idlequeue, host, port, None, None,
|
2017-08-16 15:27:35 +02:00
|
|
|
file_props.transport_sid)
|
2012-06-20 23:58:08 +02:00
|
|
|
Socks5Sender.__init__(self,idlequeue, sock_hash, parent, _sock,
|
2012-08-22 12:55:57 +02:00
|
|
|
host, port, fingerprint , connected, file_props)
|
2011-11-18 05:55:44 +01:00
|
|
|
|
2011-11-18 21:07:25 +01:00
|
|
|
|
2011-11-18 05:55:44 +01:00
|
|
|
class Socks5ReceiverClient(Socks5Client, Socks5Receiver):
|
2017-08-16 15:27:35 +02:00
|
|
|
def __init__(self, idlequeue, streamhost, transport_sid, file_props=None,
|
2013-12-04 18:43:28 +01:00
|
|
|
fingerprint=None):
|
2011-12-23 03:52:37 +01:00
|
|
|
Socks5Client.__init__(self, idlequeue, streamhost['host'],
|
2012-08-22 12:55:57 +02:00
|
|
|
int(streamhost['port']), streamhost['initiator'],
|
2017-08-16 15:27:35 +02:00
|
|
|
streamhost['target'], transport_sid)
|
|
|
|
Socks5Receiver.__init__(self, idlequeue, streamhost, transport_sid,
|
|
|
|
file_props, fingerprint)
|
2012-06-20 23:58:08 +02:00
|
|
|
|
2011-11-18 05:55:44 +01:00
|
|
|
|
|
|
|
class Socks5ReceiverServer(Socks5Server, Socks5Receiver):
|
|
|
|
|
2017-08-16 15:27:35 +02:00
|
|
|
def __init__(self, idlequeue, streamhost, transport_sid, file_props=None,
|
2011-11-18 05:55:44 +01:00
|
|
|
fingerprint=None):
|
2011-12-23 03:52:37 +01:00
|
|
|
Socks5Server.__init__(self, idlequeue, streamhost['host'],
|
2012-08-22 12:55:57 +02:00
|
|
|
int(streamhost['port']), streamhost['initiator'],
|
2017-08-16 15:27:35 +02:00
|
|
|
streamhost['target'], transport_sid)
|
|
|
|
Socks5Receiver.__init__(self, idlequeue, streamhost, transport_sid,
|
|
|
|
file_props, fingerprint)
|
2011-11-18 05:55:44 +01:00
|
|
|
|
|
|
|
|
|
|
|
class Socks5Listener(IdleObject):
|
|
|
|
def __init__(self, idlequeue, port, fp, fingerprint=None):
|
|
|
|
"""
|
|
|
|
Handle all incomming connections on (0.0.0.0, port)
|
|
|
|
|
|
|
|
This class implements IdleObject, but we will expect
|
|
|
|
only pollin events though
|
|
|
|
|
|
|
|
fingerprint: fingerprint of certificates we shall use, set to None if
|
|
|
|
TLS connection not desired
|
|
|
|
"""
|
|
|
|
self.port = port
|
|
|
|
self.ais = socket.getaddrinfo(None, port, socket.AF_UNSPEC,
|
2012-08-22 12:55:57 +02:00
|
|
|
socket.SOCK_STREAM, socket.SOL_TCP, socket.AI_PASSIVE)
|
2011-11-18 05:55:44 +01:00
|
|
|
self.ais.sort(reverse=True) # Try IPv6 first
|
|
|
|
self.queue_idx = -1
|
|
|
|
self.idlequeue = idlequeue
|
|
|
|
self.queue = None
|
|
|
|
self.started = False
|
|
|
|
self._sock = None
|
|
|
|
self.fd = -1
|
|
|
|
self.fingerprint = fingerprint
|
|
|
|
self.file_props = fp
|
2011-12-27 03:18:45 +01:00
|
|
|
self.connections = []
|
2011-11-18 05:55:44 +01:00
|
|
|
|
|
|
|
def bind(self):
|
|
|
|
for ai in self.ais:
|
|
|
|
# try the different possibilities (ipv6, ipv4, etc.)
|
2010-02-08 15:08:40 +01:00
|
|
|
try:
|
|
|
|
self._serv = socket.socket(*ai[:3])
|
2011-12-28 05:47:00 +01:00
|
|
|
if self.fingerprint is not None:
|
2010-07-27 15:29:12 +02:00
|
|
|
self._serv = OpenSSL.SSL.Connection(
|
2011-12-28 05:47:00 +01:00
|
|
|
jingle_xtls.get_context('server'), self._serv)
|
2013-01-01 23:18:36 +01:00
|
|
|
except socket.error as e:
|
2013-01-05 00:03:36 +01:00
|
|
|
if e.errno == EAFNOSUPPORT:
|
2010-02-08 15:08:40 +01:00
|
|
|
self.ai = None
|
|
|
|
continue
|
|
|
|
raise
|
|
|
|
self._serv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
|
|
|
self._serv.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
|
|
|
|
self._serv.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
|
|
|
|
# Under windows Vista, we need that to listen on ipv6 AND ipv4
|
|
|
|
# Doesn't work under windows XP
|
|
|
|
if os.name == 'nt':
|
|
|
|
ver = os.sys.getwindowsversion()
|
2013-07-05 21:10:16 +02:00
|
|
|
if (ver[3], ver[0]) == (2, 6): # Win Vista +
|
|
|
|
# 47 is socket.IPPROTO_IPV6
|
2010-02-08 15:08:40 +01:00
|
|
|
# 27 is socket.IPV6_V6ONLY under windows, but not defined ...
|
2013-07-05 21:10:16 +02:00
|
|
|
self._serv.setsockopt(41, 27, 0)
|
2010-02-08 15:08:40 +01:00
|
|
|
# will fail when port as busy, or we don't have rights to bind
|
|
|
|
try:
|
|
|
|
self._serv.bind(ai[4])
|
2011-08-15 05:59:39 +02:00
|
|
|
f = ai[4]
|
2010-02-08 15:08:40 +01:00
|
|
|
self.ai = ai
|
|
|
|
break
|
|
|
|
except Exception:
|
|
|
|
self.ai = None
|
|
|
|
continue
|
|
|
|
if not self.ai:
|
2012-06-09 02:02:45 +02:00
|
|
|
log.error('unable to bind to port ' + str(self.port))
|
2010-02-08 15:08:40 +01:00
|
|
|
return None
|
|
|
|
self._serv.listen(socket.SOMAXCONN)
|
|
|
|
self._serv.setblocking(False)
|
|
|
|
self.fd = self._serv.fileno()
|
|
|
|
self.idlequeue.plug_idle(self, False, True)
|
|
|
|
self.started = True
|
|
|
|
|
|
|
|
def pollend(self):
|
|
|
|
"""
|
|
|
|
Called when we stop listening on (host, port)
|
|
|
|
"""
|
|
|
|
self.disconnect()
|
|
|
|
|
|
|
|
def pollin(self):
|
|
|
|
"""
|
|
|
|
Accept a new incomming connection and notify queue
|
|
|
|
"""
|
|
|
|
sock = self.accept_conn()
|
2011-08-15 05:59:39 +02:00
|
|
|
self.queue.on_connection_accepted(sock, self)
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
def disconnect(self):
|
|
|
|
"""
|
|
|
|
Free all resources, we are not listening anymore
|
|
|
|
"""
|
|
|
|
self.idlequeue.remove_timeout(self.fd)
|
|
|
|
self.idlequeue.unplug_idle(self.fd)
|
|
|
|
self.fd = -1
|
|
|
|
self.state = -1
|
|
|
|
self.started = False
|
|
|
|
try:
|
|
|
|
self._serv.close()
|
|
|
|
except Exception:
|
|
|
|
pass
|
|
|
|
|
|
|
|
def accept_conn(self):
|
|
|
|
"""
|
|
|
|
Accept a new incomming connection
|
|
|
|
"""
|
|
|
|
_sock = self._serv.accept()
|
|
|
|
_sock[0].setblocking(False)
|
2011-12-27 03:18:45 +01:00
|
|
|
self.connections.append(_sock[0])
|
2010-02-08 15:08:40 +01:00
|
|
|
return _sock
|
2012-08-23 23:45:38 +02:00
|
|
|
|