- bugfix in class statuscodes: putted a 's' before all code numbers, dict is working now...
- bugfix in class comm, header(): status codes couldn't be handled - added import for include/statuscodes.py, class statuscodes to include/__init__.py - added encode_message() and decode_message() to class comm - added use of encode_message() to motd() in class comm - added more docstrings and comments in class comm - added more comments in asrc-server.py - moved pid file check and creation around in asrc-server.py for less oddness when starting the server with -n
This commit is contained in:
parent
1207c6b225
commit
a4f497115a
4 changed files with 185 additions and 156 deletions
|
@ -33,6 +33,7 @@
|
||||||
|
|
||||||
|
|
||||||
import sys, os, socket, socketserver, threading, time
|
import sys, os, socket, socketserver, threading, time
|
||||||
|
from include import argparser, comm
|
||||||
|
|
||||||
|
|
||||||
class ThreadedRequestHandler(socketserver.StreamRequestHandler):
|
class ThreadedRequestHandler(socketserver.StreamRequestHandler):
|
||||||
|
@ -50,14 +51,14 @@ class ThreadedRequestHandler(socketserver.StreamRequestHandler):
|
||||||
print("Client connected: " + str(self.client_address))
|
print("Client connected: " + str(self.client_address))
|
||||||
|
|
||||||
# send motd
|
# send motd
|
||||||
self.request.sendall(comm.motd(MOTD) + "\n", ENCODING))
|
self.request.sendall(bytes((comm.motd(MOTD) + "\n"), ENCODING))
|
||||||
|
|
||||||
# Receive data
|
# Receive data
|
||||||
self.data = str(self.rfile.readline().strip(), ENCODING)
|
self.data = str(self.rfile.readline().strip(), ENCODING)
|
||||||
|
|
||||||
# content handler
|
# content handler
|
||||||
self.request.sendall(bytes(
|
self.request.sendall(bytes(
|
||||||
comm.handler(str(self.client_address), self.data), ENCODING))
|
comm.command(str(self.client_address), self.data), ENCODING))
|
||||||
|
|
||||||
|
|
||||||
class ThreadedTCPServer(socketserver.ThreadingMixIn, socketserver.TCPServer):
|
class ThreadedTCPServer(socketserver.ThreadingMixIn, socketserver.TCPServer):
|
||||||
|
@ -68,7 +69,6 @@ def main():
|
||||||
# import global settings
|
# import global settings
|
||||||
global VERBOSITY, HOST, PORT, TIMEOUT, ENCODING
|
global VERBOSITY, HOST, PORT, TIMEOUT, ENCODING
|
||||||
|
|
||||||
from include import argparser, comm
|
|
||||||
|
|
||||||
comm.init(ServerVersion, ProtocolVersion, VERBOSITY, aliases)
|
comm.init(ServerVersion, ProtocolVersion, VERBOSITY, aliases)
|
||||||
|
|
||||||
|
@ -76,20 +76,6 @@ def main():
|
||||||
parser = argparser
|
parser = argparser
|
||||||
args = parser.parse(ServerVersion, ProtocolVersion)
|
args = parser.parse(ServerVersion, ProtocolVersion)
|
||||||
|
|
||||||
# look for pid file
|
|
||||||
if os.path.exists("pid"):
|
|
||||||
# remove, ...
|
|
||||||
if args.delete_pid_file: os.remove("pid")
|
|
||||||
# ... ignore ...
|
|
||||||
elif args.allow_multiple_instances: pass
|
|
||||||
# ... or exit
|
|
||||||
else:
|
|
||||||
print(
|
|
||||||
"\npid file already exists\n"\
|
|
||||||
"If the server didn't shut down correctly, "\
|
|
||||||
"just delete this file or pass -n\n"\
|
|
||||||
"If you want to start multiple instances, pass -m\n")
|
|
||||||
return 1
|
|
||||||
|
|
||||||
# set settings if command line options are given
|
# set settings if command line options are given
|
||||||
if args.host: HOST = args.host
|
if args.host: HOST = args.host
|
||||||
|
@ -98,16 +84,25 @@ def main():
|
||||||
if args.encoding: ENCODING = args.encoding
|
if args.encoding: ENCODING = args.encoding
|
||||||
if args.verbosity: VERBOSITY = args.verbosity
|
if args.verbosity: VERBOSITY = args.verbosity
|
||||||
|
|
||||||
# write pid file
|
|
||||||
pidf = open("pid", 'w')
|
|
||||||
pidf.write(str(os.getpid()))
|
|
||||||
pidf.close()
|
|
||||||
|
|
||||||
print("aSRC Server\n"\
|
print("aSRC Server\n"\
|
||||||
"Server version: " + ServerVersion + "\n"\
|
"Server version: " + ServerVersion + "\n"\
|
||||||
"Protocol version: " + ProtocolVersion + "\n\n"\
|
"Protocol version: " + ProtocolVersion + "\n\n"\
|
||||||
"To stop the server, press Ctrl-C\n")
|
"To stop the server, press Ctrl-C\n")
|
||||||
|
|
||||||
|
# look for pid file
|
||||||
|
if os.path.exists("pid"):
|
||||||
|
# ignore ...
|
||||||
|
if args.delete_pid_file or args.allow_multiple_instances: pass
|
||||||
|
# ... or exit
|
||||||
|
else:
|
||||||
|
print(
|
||||||
|
"\npid file already exists\n"\
|
||||||
|
"If the server didn't shut down correctly, "\
|
||||||
|
"just delete this file or pass -n\n"\
|
||||||
|
"If you want to start multiple instances, pass -m\n")
|
||||||
|
return 1
|
||||||
|
|
||||||
try:
|
try:
|
||||||
if VERBOSITY >= 1: print("Initializing server...")
|
if VERBOSITY >= 1: print("Initializing server...")
|
||||||
# Create server
|
# Create server
|
||||||
|
@ -123,9 +118,19 @@ def main():
|
||||||
ServerThread.daemon = True
|
ServerThread.daemon = True
|
||||||
ServerThread.start()
|
ServerThread.start()
|
||||||
|
|
||||||
|
# look for pid file again ...
|
||||||
|
if os.path.exists("pid"):
|
||||||
|
# ... and remove it if -n is given
|
||||||
|
if args.delete_pid_file: os.remove("pid")
|
||||||
|
|
||||||
|
# write pid file
|
||||||
|
pidf = open("pid", 'w')
|
||||||
|
pidf.write(str(os.getpid()))
|
||||||
|
pidf.close()
|
||||||
|
|
||||||
|
# do nothing further
|
||||||
while True:
|
while True:
|
||||||
time.sleep(10)
|
time.sleep(60)
|
||||||
|
|
||||||
# exit if Ctrl-C is pressed
|
# exit if Ctrl-C is pressed
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
|
|
|
@ -1,2 +1,3 @@
|
||||||
from .argparser import argparser
|
from .argparser import argparser
|
||||||
from .comm import comm
|
from .comm import comm
|
||||||
|
from .statuscodes import statuscodes
|
||||||
|
|
236
include/comm.py
236
include/comm.py
|
@ -1,119 +1,135 @@
|
||||||
# includes/comm.py
|
# includes/comm.py
|
||||||
#
|
#
|
||||||
# module version: 0.0.20130426
|
# module version: 0.0.20130426
|
||||||
|
# for protocol versions 0.2.20130423 and later
|
||||||
#
|
#
|
||||||
|
|
||||||
|
|
||||||
import statuscodes
|
from .statuscodes import statuscodes
|
||||||
|
|
||||||
|
|
||||||
class comm:
|
class comm:
|
||||||
|
|
||||||
aliases = dict()
|
# some settings
|
||||||
server_version = ""
|
aliases = dict()
|
||||||
protocol_version = ""
|
server_version = ""
|
||||||
verbosity = 0
|
protocol_version = ""
|
||||||
|
verbosity = 0
|
||||||
# initializes global settings
|
|
||||||
def init(ServerVersion, ProtocolVersion, Verbosity, Aliases):
|
# initializes global settings
|
||||||
|
def init(ServerVersion, ProtocolVersion, Verbosity, Aliases):
|
||||||
aliases = Aliases
|
"""
|
||||||
server_version = ServerVersion
|
makes settings aviable in this class
|
||||||
protocol_version = ProtocolVersion
|
"""
|
||||||
verbosity = Verbosity
|
global aliases, server_version, protocol_version, verbosity
|
||||||
|
aliases = Aliases
|
||||||
def header(statuscode, AdditionalHeaderLines = ""):
|
server_version = ServerVersion
|
||||||
"""
|
protocol_version = ProtocolVersion
|
||||||
returns the header
|
verbosity = Verbosity
|
||||||
"""
|
|
||||||
|
# builds an header
|
||||||
ret =\
|
def header(CodeList, AdditionalHeaderLines = ""):
|
||||||
"{BEGIN}\n"\
|
"""
|
||||||
"asrcp" + protocol_version + "\n"\
|
returns the header with one ore mode given status codes
|
||||||
statuscode + " " + statuscodes.description[statuscodes] + "\n"\
|
and optional additional header lines
|
||||||
"ServerVersion:" + server_version + "\n"
|
"""
|
||||||
if AdditionalHeaderLines != "": ret += AdditionalHeaderLines + "\n"
|
ret =\
|
||||||
ret += "\n\n"
|
"{BEGIN}\n"\
|
||||||
|
"asrcp" + protocol_version + "\n"
|
||||||
return ret
|
for i in range(0, len(CodeList)):
|
||||||
|
ret += CodeList[int(i)] + " " +\
|
||||||
# returns the motd
|
statuscodes.description['s' + CodeList[int(i)]] + "\n"
|
||||||
def motd(motd):
|
ret += "ServerVersion: " + server_version + "\n" +\
|
||||||
"""
|
AdditionalHeaderLines + "\n\n"
|
||||||
builds and returns a motd package
|
return ret
|
||||||
"""
|
|
||||||
|
# formats a massage
|
||||||
ret = motd
|
def encode_message(text):
|
||||||
|
"""
|
||||||
return ret
|
formats a massage, replaces some things
|
||||||
|
"""
|
||||||
# handles the content
|
return text.replace('{', '\{').replace('}', '\}')
|
||||||
def command(client_address, data):
|
|
||||||
"""
|
# "deformats" a message
|
||||||
processes a command
|
def decode_message(text):
|
||||||
"""
|
"""
|
||||||
|
removes replacements in a message
|
||||||
ret = ""
|
"""
|
||||||
|
return text.replace('\{', '{').replace('\}', '}')
|
||||||
# Look if the received message is an
|
|
||||||
# valid alias or a predefined command
|
# returns the motd
|
||||||
|
def motd(motd):
|
||||||
# if it's 'version', return the server and protocol version
|
"""
|
||||||
if data == "version":
|
builds and returns a motd package
|
||||||
|
"""
|
||||||
if verbosity >= 2: print("Got valid service command from"
|
return comm.header(['202', '003']) + comm.encode_message(motd)
|
||||||
+ str(client_address) + ": ", data)
|
|
||||||
|
# handles the content
|
||||||
ret = ret +\
|
def command(client_address, data):
|
||||||
"202 Valid Service Command\n"\
|
"""
|
||||||
"002 Version\n"\
|
processes a command
|
||||||
"ServerVersion:" + server_version + "\n"\
|
"""
|
||||||
"ProtocolVersion:" + protocol_version + "\n"
|
ret = ""
|
||||||
|
|
||||||
# if it's 'help', give a little help
|
# Look if the received message is an
|
||||||
elif data == 'help':
|
# valid alias or a predefined command
|
||||||
|
|
||||||
if verbosity >= 2: print("Got valid command from"
|
# if it's 'version', return the server and protocol version
|
||||||
+ str(client_address) + ": ", data)
|
if data == "version":
|
||||||
|
|
||||||
# send status code
|
if verbosity >= 2: print("Got valid service command from"
|
||||||
ret = ret + "202 Valid Service Command\n\n"
|
+ str(client_address) + ": ", data)
|
||||||
|
|
||||||
# send the list of aliases
|
ret = ret +\
|
||||||
ret = ret + "Aviable aliases:\n"
|
"202 Valid Service Command\n"\
|
||||||
for i in aliases.keys():
|
"002 Version\n"\
|
||||||
ret = ret + str(i) + "\n"
|
"ServerVersion:" + server_version + "\n"\
|
||||||
|
"ProtocolVersion:" + protocol_version + "\n"
|
||||||
# if it's a valid userdefined command
|
|
||||||
elif data in aliases:
|
# if it's 'help', give a little help
|
||||||
|
elif data == 'help':
|
||||||
# send status code
|
|
||||||
ret = ret + "201 Valid Command\n\n"
|
if verbosity >= 2: print("Got valid command from"
|
||||||
|
+ str(client_address) + ": ", data)
|
||||||
# ohmagawd! a debug message!!1!
|
|
||||||
if verbosity >= 2: print("Got valid command from"
|
# send status code
|
||||||
+ str(client_address) + ": ", data)
|
ret = ret + "202 Valid Service Command\n\n"
|
||||||
|
|
||||||
# execute the aliased command
|
# send the list of aliases
|
||||||
g_dict, l_dict = {}, {}
|
ret = ret + "Aviable aliases:\n"
|
||||||
exec(str(aliases[data]), g_dict, l_dict)
|
for i in aliases.keys():
|
||||||
|
ret = ret + str(i) + "\n"
|
||||||
# send may contain data to send to the client
|
|
||||||
if l_dict["send"]:
|
# if it's a valid userdefined command
|
||||||
content = str(l_dict["send"]).replace('{', '\{')
|
elif data in aliases:
|
||||||
content = content.replace('}', '\}')
|
|
||||||
|
# send status code
|
||||||
ret = ret + content + "\n"
|
ret = ret + "201 Valid Command\n\n"
|
||||||
|
|
||||||
# ALL IS LOST!!1! this has to be invalid!
|
# ohmagawd! a debug message!!1!
|
||||||
else:
|
if verbosity >= 2: print("Got valid command from"
|
||||||
|
+ str(client_address) + ": ", data)
|
||||||
# send status code
|
|
||||||
ret = ret + "203 Invalid Command\n"
|
# execute the aliased command
|
||||||
|
g_dict, l_dict = {}, {}
|
||||||
if verbosity >= 2: print("Got invalid command from",
|
exec(str(aliases[data]), g_dict, l_dict)
|
||||||
str(client_address), ": ", data)
|
|
||||||
|
# send may contain data to send to the client
|
||||||
ret = ret + "{END}\n"
|
if l_dict["send"]:
|
||||||
|
content = str(l_dict["send"]).replace('{', '\{')
|
||||||
return ret
|
content = content.replace('}', '\}')
|
||||||
|
|
||||||
|
ret = ret + content + "\n"
|
||||||
|
|
||||||
|
# ALL IS LOST!!1! this has to be invalid!
|
||||||
|
else:
|
||||||
|
|
||||||
|
# send status code
|
||||||
|
ret = ret + "203 Invalid Command\n"
|
||||||
|
|
||||||
|
if verbosity >= 2: print("Got invalid command from",
|
||||||
|
str(client_address), ": ", data)
|
||||||
|
|
||||||
|
ret = ret + "{END}\n"
|
||||||
|
|
||||||
|
return ret
|
|
@ -1,37 +1,44 @@
|
||||||
|
# include/statuscodes.py
|
||||||
|
#
|
||||||
|
# module version: 1.0.20130426
|
||||||
|
# for protocol versions 0.2.20130423 and later
|
||||||
|
#
|
||||||
|
|
||||||
|
|
||||||
class statuscodes:
|
class statuscodes:
|
||||||
description = dict(
|
description = dict(
|
||||||
# 000 - 400 server side
|
# 000 - 400 server side
|
||||||
# 000 information
|
# 000 information
|
||||||
001 = "OK",
|
s001 = "OK",
|
||||||
002 = "Version",
|
s002 = "Version",
|
||||||
003 = "MOTD",
|
s003 = "MOTD",
|
||||||
# 100 authentication and maintenance
|
# 100 authentication and maintenance
|
||||||
101 = "Challenge",
|
s101 = "Challenge",
|
||||||
102 = "Success",
|
s102 = "Success",
|
||||||
103 = "Failure",
|
s103 = "Failure",
|
||||||
104 = "To Many Tries",
|
s104 = "To Many Tries",
|
||||||
# 200 command
|
# 200 command
|
||||||
201 = "Valid",
|
s201 = "Valid",
|
||||||
202 = "Valid Service Command",
|
s202 = "Valid Service Command",
|
||||||
203 = "Invalid",
|
s203 = "Invalid",
|
||||||
204 = "Failure",
|
s204 = "Failure",
|
||||||
205 = "Continue",
|
s205 = "Continue",
|
||||||
# 300 server
|
# 300 server
|
||||||
301 = "Unhandled Exception",
|
s301 = "Unhandled Exception",
|
||||||
302 = "Shutting Down",
|
s302 = "Shutting Down",
|
||||||
303 = "Restarting",
|
s303 = "Restarting",
|
||||||
304 = "Encoding Error",
|
s304 = "Encoding Error",
|
||||||
305 = "SSL Error",
|
s305 = "SSL Error",
|
||||||
# 500 - 900 client side
|
# 500 - 900 client side
|
||||||
# 500 information
|
# 500 information
|
||||||
501 = "OK",
|
s501 = "OK",
|
||||||
502 = "Version",
|
s502 = "Version",
|
||||||
# 600 authentication and maintenance
|
# 600 authentication and maintenance
|
||||||
601 = "Response",
|
s601 = "Response",
|
||||||
602 = "Failure",
|
s602 = "Failure",
|
||||||
# 700 command
|
# 700 command
|
||||||
701 = "Reqiuest",
|
s701 = "Reqiuest",
|
||||||
702 = "Cancel",
|
s702 = "Cancel",
|
||||||
# 800 client
|
# 800 client
|
||||||
801 = "SSL Error"
|
s801 = "SSL Error"
|
||||||
)
|
)
|
||||||
|
|
Loading…
Reference in a new issue