Add login-support
This commit is contained in:
@ -29,10 +29,36 @@ class routerstats_client():
|
||||
self.sock.connect((self.host, self.port))
|
||||
self.sock.settimeout(1)
|
||||
logging.info('Connected to ' + str(self.host))
|
||||
self.login()
|
||||
self.connected = True
|
||||
except ConnectionRefusedError as error:
|
||||
logging.error('Could not connect to ' + str(self.host) + ':' + str(self.port))
|
||||
raise ConnectionRefusedError(error)
|
||||
except ConnectionError as error:
|
||||
logging.error('Could not connect to ' + str(self.host) + ':' + str(self.port) + ': ' + str(error))
|
||||
|
||||
def login(self):
|
||||
#Wait for server to say "hello"
|
||||
#Reply with password
|
||||
#Wait for "Welcome"
|
||||
logging.debug('Logging in')
|
||||
try:
|
||||
hello = self.sock.recv(5)
|
||||
except TimeoutError:
|
||||
raise ConnectionError('Timed out waiting for Hello when connecting')
|
||||
if not hello == b'Hello':
|
||||
raise ConnectionError('Server did not greet us with Hello during login')
|
||||
with open('passwd.client', 'r') as passwd_file:
|
||||
passwd = passwd_file.readline()
|
||||
passwd = passwd.rstrip()
|
||||
self.sock.send(passwd.encode('utf-8'))
|
||||
try:
|
||||
response = self.sock.recv(7)
|
||||
if not response == b'Welcome':
|
||||
raise ConnectionError('We are not greeted with Welcome after sending password')
|
||||
return True
|
||||
except TimeoutError:
|
||||
raise ConnectionError('Timed out while waiting for server to greet us after sending password')
|
||||
|
||||
def send(self, tosend):
|
||||
if self.connected:
|
||||
|
||||
@ -221,12 +221,44 @@ def load_start_pos(logfile):
|
||||
return tmp_start_pos
|
||||
return None
|
||||
|
||||
def check_login(answer):
|
||||
with open('passwd.client', 'r') as passwd_file:
|
||||
passwd = passwd_file.readline()
|
||||
passwd = passwd.rstrip() #Remove that newline
|
||||
try:
|
||||
answer = answer.decode('utf-8')
|
||||
except UnicodeDecodeError as error:
|
||||
logging.error('Could not decode %s as unicode: %s', answer, str(error))
|
||||
if answer == passwd:
|
||||
return True
|
||||
return False
|
||||
|
||||
class RequestHandler(socketserver.BaseRequestHandler):
|
||||
'''derived BaseRequestHandler'''
|
||||
def login(self):
|
||||
self.request.send(b'Hello')
|
||||
try:
|
||||
answer = self.request.recv(1024)
|
||||
except TimeoutError:
|
||||
#Client did not even bother to reply...
|
||||
logging.warning('Timed out during auth')
|
||||
self.request.send(b'timeout')
|
||||
return
|
||||
if not check_login(answer):
|
||||
logging.warning('Wrong passphrase')
|
||||
self.request.send(b'auth error')
|
||||
return
|
||||
self.request.send(b'Welcome')
|
||||
logging.info('Client ' + str(self.client_address[0]) + ' logged in')
|
||||
return True
|
||||
|
||||
def handle(self):
|
||||
logging.info('Connected to ' + str(self.client_address[0]))
|
||||
self.request.settimeout(5)
|
||||
start_time = datetime.now()
|
||||
if not self.login():
|
||||
self.request.close()
|
||||
return
|
||||
while True:
|
||||
try:
|
||||
if self.overflowqueue.empty != True:
|
||||
|
||||
Reference in New Issue
Block a user