coding standards

This commit is contained in:
Yann Leboulanger 2008-11-24 17:00:20 +00:00
parent 482cb1ee1d
commit 04e88bab3c
1 changed files with 142 additions and 145 deletions

View File

@ -106,8 +106,7 @@ class SocksQueue:
return 1 return 1
return 0 return 0
def connect_to_hosts(self, account, sid, on_success = None, def connect_to_hosts(self, account, sid, on_success=None, on_failure=None):
on_failure = None):
self.on_success = on_success self.on_success = on_success
self.on_failure = on_failure self.on_failure = on_failure
file_props = self.files_props[account][sid] file_props = self.files_props[account][sid]
@ -130,7 +129,7 @@ class SocksQueue:
return return
# set state -2, meaning that this streamhost is stopped, # set state -2, meaning that this streamhost is stopped,
# but it may be connectected later # but it may be connectected later
if host['state'] >=0: if host['state'] >= 0:
self.remove_receiver(host['idx']) self.remove_receiver(host['idx'])
host['idx'] = -1 host['idx'] = -1
host['state'] = -2 host['state'] = -2
@ -156,7 +155,8 @@ class SocksQueue:
for host in file_props['streamhosts']: for host in file_props['streamhosts']:
if host['state'] == -2: if host['state'] == -2:
host['state'] = 0 host['state'] = 0
receiver = Socks5Receiver(self.idlequeue, host, host['sid'], file_props) receiver = Socks5Receiver(self.idlequeue, host, host['sid'],
file_props)
self.add_receiver(receiver.account, receiver) self.add_receiver(receiver.account, receiver)
host['idx'] = receiver.queue_idx host['idx'] = receiver.queue_idx
# we still have chances to connect # we still have chances to connect
@ -204,9 +204,7 @@ class SocksQueue:
def get_file_from_sender(self, file_props, account): def get_file_from_sender(self, file_props, account):
if file_props is None: if file_props is None:
return return
if 'hash' in file_props and \ if 'hash' in file_props and file_props['hash'] in self.senders:
file_props['hash'] in self.senders:
sender = self.senders[file_props['hash']] sender = self.senders[file_props['hash']]
sender.account = account sender.account = account
result = self.get_file_contents(0) result = self.get_file_contents(0)
@ -244,8 +242,7 @@ class SocksQueue:
self.process_result(result, reader) self.process_result(result, reader)
def send_file(self, file_props, account): def send_file(self, file_props, account):
if 'hash' in file_props and \ if 'hash' in file_props and file_props['hash'] in self.senders:
file_props['hash'] in self.senders:
sender = self.senders[file_props['hash']] sender = self.senders[file_props['hash']]
file_props['streamhost-used'] = True file_props['streamhost-used'] = True
sender.account = account sender.account = account
@ -263,8 +260,7 @@ class SocksQueue:
''' file_prop to the dict of current file_props. ''' file_prop to the dict of current file_props.
It is identified by account name and sid It is identified by account name and sid
''' '''
if file_props is None or \ if file_props is None or ('sid' in file_props) is False:
('sid' in file_props) is False:
return return
_id = file_props['sid'] _id = file_props['sid']
if account not in self.files_props: if account not in self.files_props:
@ -291,8 +287,8 @@ class SocksQueue:
def on_connection_accepted(self, sock): def on_connection_accepted(self, sock):
sock_hash = sock.__hash__() sock_hash = sock.__hash__()
if sock_hash not in self.senders: if sock_hash not in self.senders:
self.senders[sock_hash] = Socks5Sender(self.idlequeue, self.senders[sock_hash] = Socks5Sender(self.idlequeue, sock_hash, self,
sock_hash, self, sock[0], sock[1][0], sock[1][1]) sock[0], sock[1][0], sock[1][1])
self.connected += 1 self.connected += 1
def process_result(self, result, actor): def process_result(self, result, actor):
@ -311,7 +307,7 @@ class SocksQueue:
elif self.progress_transfer_cb is not None: elif self.progress_transfer_cb is not None:
self.progress_transfer_cb(actor.account, actor.file_props) self.progress_transfer_cb(actor.account, actor.file_props)
def remove_receiver(self, idx, do_disconnect = True): def remove_receiver(self, idx, do_disconnect=True):
''' Remove reciver from the list and decrease ''' Remove reciver from the list and decrease
the number of active connections with 1''' the number of active connections with 1'''
if idx != -1: if idx != -1:
@ -326,7 +322,7 @@ class SocksQueue:
reader.streamhost['state'] = -1 reader.streamhost['state'] = -1
del(self.readers[idx]) del(self.readers[idx])
def remove_sender(self, idx, do_disconnect = True): def remove_sender(self, idx, do_disconnect=True):
''' Remove sender from the list of senders and decrease the ''' Remove sender from the list of senders and decrease the
number of active connections with 1''' number of active connections with 1'''
if idx != -1: if idx != -1:
@ -348,7 +344,8 @@ class Socks5:
if host is not None: if host is not None:
try: try:
self.host = host self.host = host
self.ais = socket.getaddrinfo(host, port, socket.AF_UNSPEC, socket.SOCK_STREAM) self.ais = socket.getaddrinfo(host, port, socket.AF_UNSPEC,
socket.SOCK_STREAM)
except socket.gaierror: except socket.gaierror:
self.ais = None self.ais = None
self.idlequeue = idlequeue self.idlequeue = idlequeue
@ -413,7 +410,6 @@ class Socks5:
except Exception: except Exception:
pass pass
def receive(self): def receive(self):
''' Reads small chunks of data. ''' Reads small chunks of data.
Calls owner's disconnected() method if appropriate.''' Calls owner's disconnected() method if appropriate.'''
@ -421,8 +417,8 @@ class Socks5:
try: try:
add = self._recv(64) add = self._recv(64)
except Exception, e: except Exception, e:
add='' add = ''
received +=add received += add
if len(add) == 0: if len(add) == 0:
self.disconnect() self.disconnect()
return add return add
@ -485,10 +481,8 @@ class Socks5:
return -1 return -1
def get_file_contents(self, timeout): def get_file_contents(self, timeout):
''' read file contents from socket and write them to file ''', \ ''' read file contents from socket and write them to file '''
self.file_props['type'], self.file_props['sid'] if self.file_props is None or ('file-name' in self.file_props) is False:
if self.file_props is None or \
('file-name' in self.file_props) is False:
self.file_props['error'] = -2 self.file_props['error'] = -2
return None return None
fd = None fd = None
@ -583,6 +577,7 @@ class Socks5:
except Exception: except Exception:
return None return None
return auth_mechanisms return auth_mechanisms
def _get_auth_response(self): def _get_auth_response(self):
''' socks version(5), number of extra auth methods (we send ''' socks version(5), number of extra auth methods (we send
0x00 - no auth 0x00 - no auth
@ -605,8 +600,8 @@ class Socks5:
def _parse_request_buff(self, buff): def _parse_request_buff(self, buff):
try: # don't trust on what comes from the outside try: # don't trust on what comes from the outside
version, req_type, reserved, host_type, = \ version, req_type, reserved, host_type, = struct.unpack('!BBBB',
struct.unpack('!BBBB', buff[:4]) buff[:4])
if host_type == 0x01: if host_type == 0x01:
host_arr = struct.unpack('!iiii', buff[4:8]) host_arr = struct.unpack('!iiii', buff[4:8])
host, = '.'.join(str(s) for s in host_arr) host, = '.'.join(str(s) for s in host_arr)
@ -651,11 +646,13 @@ class Socks5:
del(self.file_props['is_a_proxy']) del(self.file_props['is_a_proxy'])
return sha.new('%s%s%s' % (self.sid, self.file_props['proxy_sender'], return sha.new('%s%s%s' % (self.sid, self.file_props['proxy_sender'],
self.file_props['proxy_receiver'])).hexdigest() self.file_props['proxy_receiver'])).hexdigest()
return sha.new('%s%s%s' % (self.sid, self.initiator, self.target)).hexdigest() return sha.new('%s%s%s' % (self.sid, self.initiator, self.target)).\
hexdigest()
class Socks5Sender(Socks5, IdleObject): class Socks5Sender(Socks5, IdleObject):
''' class for sending file to socket over socks5 ''' ''' class for sending file to socket over socks5 '''
def __init__(self, idlequeue, sock_hash, parent, _sock, host = None, port = None): def __init__(self, idlequeue, sock_hash, parent, _sock, host=None,
port=None):
self.queue_idx = sock_hash self.queue_idx = sock_hash
self.queue = parent self.queue = parent
Socks5.__init__(self, idlequeue, host, port, None, None, None) Socks5.__init__(self, idlequeue, host, port, None, None, None)
@ -729,8 +726,7 @@ class Socks5Sender(Socks5, IdleObject):
self.disconnect() self.disconnect()
elif self.state == 5: elif self.state == 5:
if self.file_props is not None and \ if self.file_props is not None and self.file_props['type'] == 'r':
self.file_props['type'] == 'r':
result = self.get_file_contents(0) result = self.get_file_contents(0)
self.queue.process_result(result, self) self.queue.process_result(result, self)
else: else:
@ -776,7 +772,7 @@ class Socks5Sender(Socks5, IdleObject):
self.idlequeue.plug_idle(self, True, False) self.idlequeue.plug_idle(self, True, False)
return None return None
def disconnect(self, cb = True): def disconnect(self, cb=True):
''' Closes the socket. ''' ''' Closes the socket. '''
# close connection and remove us from the queue # close connection and remove us from the queue
Socks5.disconnect(self) Socks5.disconnect(self)
@ -871,8 +867,9 @@ 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
Socks5.__init__(self, idlequeue, streamhost['host'], int(streamhost['port']), Socks5.__init__(self, idlequeue, streamhost['host'],
streamhost['initiator'], streamhost['target'], sid) int(streamhost['port']), streamhost['initiator'], streamhost['target'],
sid)
def read_timeout(self): def read_timeout(self):
self.idlequeue.remove_timeout(self.fd) self.idlequeue.remove_timeout(self.fd)
@ -898,10 +895,10 @@ class Socks5Receiver(Socks5, IdleObject):
for ai in self.ais: for ai in self.ais:
try: try:
self._sock=socket.socket(*ai[:3]) self._sock = socket.socket(*ai[:3])
# this will not block the GUI # this will not block the GUI
self._sock.setblocking(False) self._sock.setblocking(False)
self._server=ai[4] self._server = ai[4]
break break
except Exception: except Exception:
if sys.exc_value[0] == errno.EINPROGRESS: if sys.exc_value[0] == errno.EINPROGRESS:
@ -919,6 +916,7 @@ class Socks5Receiver(Socks5, IdleObject):
if self.state < 5: if self.state < 5:
return False return False
return True return True
def pollout(self): def pollout(self):
self.idlequeue.remove_timeout(self.fd) self.idlequeue.remove_timeout(self.fd)
if self.state == 0: if self.state == 0:
@ -965,7 +963,6 @@ class Socks5Receiver(Socks5, IdleObject):
self.idlequeue.set_read_timeout(self.fd, STALLED_TIMEOUT) self.idlequeue.set_read_timeout(self.fd, STALLED_TIMEOUT)
result = self.get_file_contents(0) result = self.get_file_contents(0)
self.queue.process_result(result, self) self.queue.process_result(result, self)
else: else:
self.disconnect() self.disconnect()
@ -1000,7 +997,7 @@ class Socks5Receiver(Socks5, IdleObject):
self.idlequeue.plug_idle(self, True, False) self.idlequeue.plug_idle(self, True, False)
return 1 # we are connected return 1 # we are connected
def main(self, timeout = 0): def main(self, timeout=0):
''' begin negotiation. on success 'address' != 0 ''' ''' begin negotiation. on success 'address' != 0 '''
result = 1 result = 1
buff = self.receive() buff = self.receive()
@ -1069,7 +1066,7 @@ class Socks5Receiver(Socks5, IdleObject):
self.state += 1 self.state += 1
return None return None
def disconnect(self, cb = True): def disconnect(self, cb=True):
''' Closes the socket. Remove self from queue if cb is True''' ''' Closes the socket. Remove self from queue if cb is True'''
# close connection # close connection
Socks5.disconnect(self) Socks5.disconnect(self)