coding standards

This commit is contained in:
Yann Leboulanger 2008-11-24 17:00:20 +00:00
parent 482cb1ee1d
commit 04e88bab3c

View file

@ -106,8 +106,7 @@ class SocksQueue:
return 1
return 0
def connect_to_hosts(self, account, sid, on_success = None,
on_failure = None):
def connect_to_hosts(self, account, sid, on_success=None, on_failure=None):
self.on_success = on_success
self.on_failure = on_failure
file_props = self.files_props[account][sid]
@ -156,7 +155,8 @@ class SocksQueue:
for host in file_props['streamhosts']:
if host['state'] == -2:
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)
host['idx'] = receiver.queue_idx
# we still have chances to connect
@ -204,9 +204,7 @@ class SocksQueue:
def get_file_from_sender(self, file_props, account):
if file_props is None:
return
if 'hash' in file_props and \
file_props['hash'] in self.senders:
if 'hash' in file_props and file_props['hash'] in self.senders:
sender = self.senders[file_props['hash']]
sender.account = account
result = self.get_file_contents(0)
@ -244,8 +242,7 @@ class SocksQueue:
self.process_result(result, reader)
def send_file(self, file_props, account):
if 'hash' in file_props and \
file_props['hash'] in self.senders:
if 'hash' in file_props and file_props['hash'] in self.senders:
sender = self.senders[file_props['hash']]
file_props['streamhost-used'] = True
sender.account = account
@ -263,8 +260,7 @@ class SocksQueue:
''' file_prop to the dict of current file_props.
It is identified by account name and sid
'''
if file_props is None or \
('sid' in file_props) is False:
if file_props is None or ('sid' in file_props) is False:
return
_id = file_props['sid']
if account not in self.files_props:
@ -291,8 +287,8 @@ class SocksQueue:
def on_connection_accepted(self, sock):
sock_hash = sock.__hash__()
if sock_hash not in self.senders:
self.senders[sock_hash] = Socks5Sender(self.idlequeue,
sock_hash, self, sock[0], sock[1][0], sock[1][1])
self.senders[sock_hash] = Socks5Sender(self.idlequeue, sock_hash, self,
sock[0], sock[1][0], sock[1][1])
self.connected += 1
def process_result(self, result, actor):
@ -348,7 +344,8 @@ class Socks5:
if host is not None:
try:
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:
self.ais = None
self.idlequeue = idlequeue
@ -413,7 +410,6 @@ class Socks5:
except Exception:
pass
def receive(self):
''' Reads small chunks of data.
Calls owner's disconnected() method if appropriate.'''
@ -485,10 +481,8 @@ class Socks5:
return -1
def get_file_contents(self, timeout):
''' 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:
''' read file contents from socket and write them to file '''
if self.file_props is None or ('file-name' in self.file_props) is False:
self.file_props['error'] = -2
return None
fd = None
@ -583,6 +577,7 @@ class Socks5:
except Exception:
return None
return auth_mechanisms
def _get_auth_response(self):
''' socks version(5), number of extra auth methods (we send
0x00 - no auth
@ -605,8 +600,8 @@ class Socks5:
def _parse_request_buff(self, buff):
try: # don't trust on what comes from the outside
version, req_type, reserved, host_type, = \
struct.unpack('!BBBB', buff[:4])
version, req_type, reserved, host_type, = struct.unpack('!BBBB',
buff[:4])
if host_type == 0x01:
host_arr = struct.unpack('!iiii', buff[4:8])
host, = '.'.join(str(s) for s in host_arr)
@ -651,11 +646,13 @@ class Socks5:
del(self.file_props['is_a_proxy'])
return sha.new('%s%s%s' % (self.sid, self.file_props['proxy_sender'],
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 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 = parent
Socks5.__init__(self, idlequeue, host, port, None, None, None)
@ -729,8 +726,7 @@ class Socks5Sender(Socks5, IdleObject):
self.disconnect()
elif self.state == 5:
if self.file_props is not None and \
self.file_props['type'] == 'r':
if self.file_props is not None and self.file_props['type'] == 'r':
result = self.get_file_contents(0)
self.queue.process_result(result, self)
else:
@ -871,8 +867,9 @@ class Socks5Receiver(Socks5, IdleObject):
self.file_props['paused'] = False
self.file_props['continue_cb'] = self.continue_paused_transfer
self.file_props['stalled'] = False
Socks5.__init__(self, idlequeue, streamhost['host'], int(streamhost['port']),
streamhost['initiator'], streamhost['target'], sid)
Socks5.__init__(self, idlequeue, streamhost['host'],
int(streamhost['port']), streamhost['initiator'], streamhost['target'],
sid)
def read_timeout(self):
self.idlequeue.remove_timeout(self.fd)
@ -919,6 +916,7 @@ class Socks5Receiver(Socks5, IdleObject):
if self.state < 5:
return False
return True
def pollout(self):
self.idlequeue.remove_timeout(self.fd)
if self.state == 0:
@ -965,7 +963,6 @@ class Socks5Receiver(Socks5, IdleObject):
self.idlequeue.set_read_timeout(self.fd, STALLED_TIMEOUT)
result = self.get_file_contents(0)
self.queue.process_result(result, self)
else:
self.disconnect()