2013-04-23 00:25:16 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
#
|
|
|
|
# asrc-server.py
|
|
|
|
#
|
|
|
|
# Copyright 2012 - 2013 Fanir-Thuban <projects@mail.fanir.de>
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program; if not, write to the Free Software
|
|
|
|
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
|
|
|
# MA 02110-1301, USA.
|
|
|
|
#
|
|
|
|
#
|
|
|
|
# aSRC (Aliased Server Remote Control)
|
|
|
|
# - SERVER -
|
|
|
|
#
|
2013-04-24 20:25:50 +02:00
|
|
|
# program version: 0.0.0.20130424
|
2013-04-23 12:51:52 +02:00
|
|
|
# protocol version: 0.2.20130423
|
2013-04-23 00:25:16 +02:00
|
|
|
#
|
|
|
|
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import sys, os, socket, socketserver, threading, time
|
|
|
|
|
|
|
|
|
2013-04-23 12:51:52 +02:00
|
|
|
def motd():
|
|
|
|
return MOTD
|
|
|
|
|
|
|
|
|
2013-04-24 20:25:50 +02:00
|
|
|
#def content(client_address, data):
|
|
|
|
#ret = ""
|
2013-04-23 12:51:52 +02:00
|
|
|
|
2013-04-24 20:25:50 +02:00
|
|
|
#ret = ret +\
|
|
|
|
#"{BEGIN}\n"\
|
|
|
|
#"asrcp" + ProtocolVersion + "\n"
|
2013-04-23 12:51:52 +02:00
|
|
|
|
2013-04-24 20:25:50 +02:00
|
|
|
## Look if the received message is an
|
|
|
|
## valid alias or a predefined command
|
2013-04-23 12:51:52 +02:00
|
|
|
|
2013-04-24 20:25:50 +02:00
|
|
|
## if it's 'version', return the server and protocol version
|
|
|
|
#if data == "version":
|
2013-04-23 12:51:52 +02:00
|
|
|
|
2013-04-24 20:25:50 +02:00
|
|
|
#if VERBOSITY >= 2: print("Got valid service command from"
|
|
|
|
#+ str(client_address) + ": ", data)
|
2013-04-23 12:51:52 +02:00
|
|
|
|
2013-04-24 20:25:50 +02:00
|
|
|
#ret = ret +\
|
|
|
|
#"202 Valid Service Command\n"\
|
|
|
|
#"002 Version\n"\
|
|
|
|
#"ServerVersion:" + ServerVersion + "\n"\
|
|
|
|
#"ProtocolVersion:" + ProtocolVersion + "\n"
|
2013-04-23 12:51:52 +02:00
|
|
|
|
2013-04-24 20:25:50 +02:00
|
|
|
## if it's 'help', give a little help
|
|
|
|
#elif data == 'help':
|
2013-04-23 12:51:52 +02:00
|
|
|
|
2013-04-24 20:25:50 +02:00
|
|
|
#if VERBOSITY >= 2: print("Got valid command from"
|
|
|
|
#+ str(client_address) + ": ", data)
|
2013-04-23 12:51:52 +02:00
|
|
|
|
2013-04-24 20:25:50 +02:00
|
|
|
## send status code
|
|
|
|
#ret = ret + "202 Valid Service Command\n\n"
|
2013-04-23 12:51:52 +02:00
|
|
|
|
2013-04-24 20:25:50 +02:00
|
|
|
## send the list of aliases
|
|
|
|
#ret = ret + "Aviable aliases:\n"
|
|
|
|
#for i in aliases.keys():
|
|
|
|
#ret = ret + str(i) + "\n"
|
2013-04-23 12:51:52 +02:00
|
|
|
|
2013-04-24 20:25:50 +02:00
|
|
|
## if it's a valid userdefined command
|
|
|
|
#elif data in aliases:
|
2013-04-23 12:51:52 +02:00
|
|
|
|
2013-04-24 20:25:50 +02:00
|
|
|
## send status code
|
|
|
|
#ret = ret + "201 Valid Command\n\n"
|
2013-04-23 12:51:52 +02:00
|
|
|
|
2013-04-24 20:25:50 +02:00
|
|
|
## ohmagawd! a debug message!!1!
|
|
|
|
#if VERBOSITY >= 2: print("Got valid command from"
|
|
|
|
#+ str(client_address) + ": ", data)
|
2013-04-23 12:51:52 +02:00
|
|
|
|
2013-04-24 20:25:50 +02:00
|
|
|
## execute the aliased command
|
|
|
|
#g_dict, l_dict = {}, {}
|
|
|
|
#exec(str(aliases[data]), g_dict, l_dict)
|
2013-04-23 12:51:52 +02:00
|
|
|
|
2013-04-24 20:25:50 +02:00
|
|
|
## send may contain data to send to the client
|
|
|
|
#if l_dict["send"]:
|
|
|
|
#content = str(l_dict["send"]).replace('{', '\{')
|
|
|
|
#content = content.replace('}', '\}')
|
2013-04-23 12:51:52 +02:00
|
|
|
|
2013-04-24 20:25:50 +02:00
|
|
|
#ret = ret + content + "\n"
|
2013-04-23 12:51:52 +02:00
|
|
|
|
2013-04-24 20:25:50 +02:00
|
|
|
## ALL IS LOST!!1! this has to be invalid!
|
|
|
|
#else:
|
2013-04-23 12:51:52 +02:00
|
|
|
|
2013-04-24 20:25:50 +02:00
|
|
|
## send status code
|
|
|
|
#ret = ret + "203 Invalid Command\n"
|
2013-04-23 12:51:52 +02:00
|
|
|
|
2013-04-24 20:25:50 +02:00
|
|
|
#if VERBOSITY >= 2: print("Got invalid command from",
|
|
|
|
#str(client_address), ": ", data)
|
2013-04-23 12:51:52 +02:00
|
|
|
|
2013-04-24 20:25:50 +02:00
|
|
|
#ret = ret + "{END}\n"
|
2013-04-23 12:51:52 +02:00
|
|
|
|
2013-04-24 20:25:50 +02:00
|
|
|
#return ret
|
2013-04-23 12:51:52 +02:00
|
|
|
|
|
|
|
|
2013-04-23 00:25:16 +02:00
|
|
|
class ThreadedRequestHandler(socketserver.StreamRequestHandler):
|
|
|
|
|
|
|
|
def handle(self):
|
|
|
|
|
|
|
|
# Set time for timeout in seconds
|
|
|
|
self.timeout = TIMEOUT
|
|
|
|
|
|
|
|
# Print a line with the adress of the connected client
|
2013-04-24 20:25:50 +02:00
|
|
|
if VERBOSITY >=3:
|
2013-04-23 00:25:16 +02:00
|
|
|
print("Client connected: " + str(self.client_address))
|
|
|
|
|
2013-04-23 12:51:52 +02:00
|
|
|
# send header line 1
|
|
|
|
self.request.sendall(bytes
|
|
|
|
("asrpc " + ProtocolVersion + "\n", ENCODING))
|
|
|
|
|
|
|
|
# send motd
|
|
|
|
self.request.sendall(bytes(motd() + "\n", ENCODING))
|
2013-04-23 00:25:16 +02:00
|
|
|
|
|
|
|
# Receive data
|
|
|
|
self.data = str(self.rfile.readline().strip(), ENCODING)
|
|
|
|
|
2013-04-23 12:51:52 +02:00
|
|
|
# content handler
|
|
|
|
self.request.sendall(bytes(
|
2013-04-24 20:25:50 +02:00
|
|
|
content.handler(str(self.client_address), self.data, aliases,
|
|
|
|
ServerVersion, ProtocolVersion, VERBOSITY), ENCODING))
|
2013-04-23 00:25:16 +02:00
|
|
|
|
|
|
|
|
|
|
|
class ThreadedTCPServer(socketserver.ThreadingMixIn, socketserver.TCPServer):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
2013-04-25 10:22:18 +02:00
|
|
|
global VERBOSITY, HOST, PORT, TIMEOUT, ENCODING
|
|
|
|
|
2013-04-24 20:25:50 +02:00
|
|
|
from include import argparser, content
|
|
|
|
|
|
|
|
parser = argparser
|
|
|
|
args = parser.parse(ServerVersion, ProtocolVersion)
|
|
|
|
|
|
|
|
if os.path.exists("pid"):
|
|
|
|
if args.delete_pid_file: os.remove("pid")
|
|
|
|
elif args.allow_multiple_instances: pass
|
|
|
|
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
|
|
|
|
|
|
|
|
print(args)
|
|
|
|
|
|
|
|
if args.host: HOST = args.host
|
|
|
|
if args.port: PORT = args.port
|
|
|
|
if args.timeout: TIMEOUT = args.timeout
|
|
|
|
if args.encoding: ENCODING = args.encoding
|
|
|
|
if args.verbosity: VERBOSITY = args.verbosity
|
|
|
|
|
|
|
|
# write pid file
|
|
|
|
pidf = open("pid", 'w')
|
|
|
|
pidf.write(str(os.getpid()))
|
|
|
|
pidf.close()
|
|
|
|
|
2013-04-23 12:51:52 +02:00
|
|
|
print("aSRC Server\n"\
|
2013-04-24 20:25:50 +02:00
|
|
|
"Server version: " + ServerVersion + "\n"\
|
|
|
|
"Protocol version: " + ProtocolVersion + "\n\n"\
|
|
|
|
"To stop the server, press Ctrl-C\n")
|
2013-04-23 00:25:16 +02:00
|
|
|
|
|
|
|
try:
|
2013-04-24 20:25:50 +02:00
|
|
|
if VERBOSITY >= 1: print("Initializing server...")
|
2013-04-23 00:25:16 +02:00
|
|
|
# Create server
|
|
|
|
server = ThreadedTCPServer((HOST, PORT), ThreadedRequestHandler)
|
|
|
|
|
2013-04-24 20:25:50 +02:00
|
|
|
if VERBOSITY >= 1: print("Starting server on",
|
2013-04-23 00:25:16 +02:00
|
|
|
server.server_address[0], "port", server.server_address[1], "...")
|
|
|
|
# Start a thread with the server -- that thread will then start one
|
|
|
|
# more thread for each request
|
|
|
|
ServerThread = threading.Thread(target = server.serve_forever)
|
|
|
|
|
|
|
|
# Exit the server thread when the main thread terminates
|
|
|
|
ServerThread.daemon = True
|
|
|
|
ServerThread.start()
|
2013-04-24 20:25:50 +02:00
|
|
|
|
2013-04-23 00:25:16 +02:00
|
|
|
while True:
|
|
|
|
time.sleep(10)
|
|
|
|
|
|
|
|
except KeyboardInterrupt:
|
2013-04-23 12:51:52 +02:00
|
|
|
print("\nGot Ctrl-C, shutting down server...")
|
2013-04-23 00:25:16 +02:00
|
|
|
|
2013-04-24 20:25:50 +02:00
|
|
|
try:
|
|
|
|
server.shutdown()
|
|
|
|
os.remove("pid")
|
2013-04-23 00:25:16 +02:00
|
|
|
except: print("Failed to shutdown server correctly, "\
|
|
|
|
"socket may be still in use or wasn't even started:",
|
|
|
|
sys.exc_info())
|
2013-04-24 20:25:50 +02:00
|
|
|
#except:
|
|
|
|
# print("\nAn error occured:\n", sys.exc_info(), "\n")
|
2013-04-23 00:25:16 +02:00
|
|
|
|
2013-04-24 20:25:50 +02:00
|
|
|
if VERBOSITY >= 3: input("Press Enter to continue\n")
|
2013-04-23 00:25:16 +02:00
|
|
|
return 0
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
|
|
|
ServerVersion = "0.0.0"
|
2013-04-23 12:51:52 +02:00
|
|
|
ProtocolVersion = "0.2"
|
2013-04-23 00:25:16 +02:00
|
|
|
|
|
|
|
|
|
|
|
##############
|
|
|
|
# SETTINGS #
|
|
|
|
##############
|
|
|
|
|
|
|
|
# IP or hostname (use 0.0.0.0 for all interfaces) and port, on which
|
|
|
|
# the server should listen
|
|
|
|
HOST = '127.0.0.1'
|
|
|
|
PORT = 24642
|
|
|
|
|
2013-04-24 20:25:50 +02:00
|
|
|
# Timeout of a connection in seconds - still doesn't work obviously...
|
|
|
|
TIMEOUT = 15
|
2013-04-23 00:25:16 +02:00
|
|
|
|
|
|
|
# Encoding to be used when communicating with a client
|
|
|
|
ENCODING = 'utf-8'
|
|
|
|
|
|
|
|
# Dictionary of aliases. Use python syntax. You can use
|
|
|
|
# the variable send for text to send to the client.
|
|
|
|
#
|
|
|
|
# Shell commands can be executed with:
|
|
|
|
# import subprocess
|
|
|
|
# send = subprocess.check_output(["command", "arg", "somemorearg"])
|
|
|
|
#
|
|
|
|
# You don't have to include sys, os, socket, socketserver,
|
|
|
|
# threading and time, through they are included already.
|
|
|
|
aliases = dict(
|
|
|
|
who = '''
|
|
|
|
import subprocess
|
|
|
|
send = subprocess.check_output(["whoami"])
|
|
|
|
''',
|
|
|
|
where = '''
|
|
|
|
import subprocess
|
|
|
|
send = subprocess.check_output(["pwd"])
|
|
|
|
''',
|
|
|
|
uname = '''
|
|
|
|
import os
|
|
|
|
send = os.uname()
|
|
|
|
''',
|
|
|
|
date = '''
|
|
|
|
import subprocess
|
|
|
|
send = subprocess.check_output(["date"])
|
|
|
|
''',
|
|
|
|
ping_fanir = '''
|
|
|
|
import subprocess
|
|
|
|
send = subprocess.check_output(["ping", "-c 2", "fanir.de"])
|
|
|
|
''')
|
|
|
|
|
|
|
|
# This is sent to the client after the connection is established
|
2013-04-23 12:51:52 +02:00
|
|
|
MOTD = "Welcome! This is only a test server, for developing purposes.\n"\
|
2013-04-23 00:25:16 +02:00
|
|
|
"Here (may) be more text..."
|
|
|
|
|
|
|
|
# Verbosity of logging.
|
|
|
|
# Can be from 0 (only default output) to 3 (debug messages)
|
2013-04-24 20:25:50 +02:00
|
|
|
VERBOSITY = 3
|
2013-04-23 00:25:16 +02:00
|
|
|
##############
|
|
|
|
|
|
|
|
|
|
|
|
main()
|