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 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]
@ -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):
@ -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.'''
@ -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:
@ -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)
@ -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()