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-06-19 16:25:43 +02:00
|
|
|
# program version: 0.0.0.20130617
|
|
|
|
# protocol version: 0.2.20130617
|
2013-04-23 00:25:16 +02:00
|
|
|
#
|
|
|
|
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import sys, os, socket, socketserver, threading, time
|
2013-04-29 21:51:20 +02:00
|
|
|
from include import argparser, comm
|
2013-04-23 00:25:16 +02:00
|
|
|
|
|
|
|
|
|
|
|
class ThreadedRequestHandler(socketserver.StreamRequestHandler):
|
2013-04-26 12:55:37 +02:00
|
|
|
|
|
|
|
def handle(self):
|
|
|
|
"""
|
|
|
|
Handles incoming connections
|
|
|
|
"""
|
|
|
|
|
|
|
|
# Set time for timeout in seconds
|
|
|
|
self.timeout = TIMEOUT
|
|
|
|
|
|
|
|
# Print a line with the adress of the connected client
|
|
|
|
if VERBOSITY >=3:
|
|
|
|
print("Client connected: " + str(self.client_address))
|
|
|
|
|
|
|
|
# send motd
|
2013-04-29 21:51:20 +02:00
|
|
|
self.request.sendall(bytes((comm.motd(MOTD) + "\n"), ENCODING))
|
2013-04-26 12:55:37 +02:00
|
|
|
|
2013-06-19 16:25:43 +02:00
|
|
|
repeat = True
|
2013-04-26 12:55:37 +02:00
|
|
|
|
2013-06-19 16:25:43 +02:00
|
|
|
while repeat:
|
|
|
|
# Receive data
|
|
|
|
self.data = str(self.rfile.readline().strip(), ENCODING)
|
|
|
|
|
|
|
|
if self.data == "exit": repeat = False
|
|
|
|
# content handler
|
|
|
|
self.request.sendall(bytes(
|
|
|
|
comm.command(str(self.client_address), self.data), ENCODING))
|
2013-04-23 00:25:16 +02:00
|
|
|
|
|
|
|
|
|
|
|
class ThreadedTCPServer(socketserver.ThreadingMixIn, socketserver.TCPServer):
|
2013-04-26 12:55:37 +02:00
|
|
|
pass
|
2013-04-23 00:25:16 +02:00
|
|
|
|
|
|
|
|
|
|
|
def main():
|
2013-04-26 12:55:37 +02:00
|
|
|
# import global settings
|
2013-06-19 16:25:43 +02:00
|
|
|
global VERBOSITY, HOST, PORT, TIMEOUT, ENCODING, PASSWORD
|
2013-04-26 12:55:37 +02:00
|
|
|
|
|
|
|
|
2013-06-19 16:25:43 +02:00
|
|
|
comm.init(ServerVersion, ProtocolVersion, VERBOSITY, aliases, PASSWORD)
|
2013-04-26 12:55:37 +02:00
|
|
|
|
|
|
|
# parse arguments
|
|
|
|
parser = argparser
|
|
|
|
args = parser.parse(ServerVersion, ProtocolVersion)
|
|
|
|
|
|
|
|
|
|
|
|
# set settings if command line options are given
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
print("aSRC Server\n"\
|
|
|
|
"Server version: " + ServerVersion + "\n"\
|
|
|
|
"Protocol version: " + ProtocolVersion + "\n\n"\
|
|
|
|
"To stop the server, press Ctrl-C\n")
|
|
|
|
|
2013-04-29 21:51:20 +02:00
|
|
|
# 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
|
|
|
|
|
2013-04-26 12:55:37 +02:00
|
|
|
try:
|
|
|
|
if VERBOSITY >= 1: print("Initializing server...")
|
|
|
|
# Create server
|
|
|
|
server = ThreadedTCPServer((HOST, PORT), ThreadedRequestHandler)
|
|
|
|
|
|
|
|
if VERBOSITY >= 1: print("Starting server on",
|
|
|
|
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-29 21:51:20 +02:00
|
|
|
# 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()
|
2013-04-26 12:55:37 +02:00
|
|
|
|
2013-04-29 21:51:20 +02:00
|
|
|
# do nothing further
|
2013-04-26 12:55:37 +02:00
|
|
|
while True:
|
2013-04-29 21:51:20 +02:00
|
|
|
time.sleep(60)
|
2013-04-26 12:55:37 +02:00
|
|
|
|
|
|
|
# exit if Ctrl-C is pressed
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
print("\nGot Ctrl-C, shutting down server...")
|
|
|
|
|
|
|
|
try:
|
|
|
|
server.shutdown()
|
|
|
|
os.remove("pid")
|
|
|
|
except: print("Failed to shutdown server correctly, "\
|
|
|
|
"socket may be still in use or wasn't even started:",
|
|
|
|
sys.exc_info())
|
|
|
|
#except:
|
|
|
|
# print("\nAn error occured:\n", sys.exc_info(), "\n")
|
|
|
|
|
|
|
|
if VERBOSITY >= 3: input("Press Enter to continue\n")
|
|
|
|
return 0
|
2013-04-23 00:25:16 +02:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2013-04-26 12:55:37 +02:00
|
|
|
|
|
|
|
ServerVersion = "0.0.0"
|
|
|
|
ProtocolVersion = "0.2"
|
|
|
|
|
|
|
|
|
|
|
|
##############
|
|
|
|
# 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
|
|
|
|
|
|
|
|
# Timeout of a connection in seconds - still doesn't work obviously...
|
|
|
|
TIMEOUT = 15
|
|
|
|
|
|
|
|
# Encoding to be used when communicating with a client
|
|
|
|
ENCODING = 'utf-8'
|
|
|
|
|
2013-06-19 16:25:43 +02:00
|
|
|
# SHA512-encoded Password for authentication with the server
|
|
|
|
PASSWORD = 'a52fb4e552326fd8216f52a96f3d037309ef25acb22e5ead60bf258d2ea6652ab9fd5ab1117eb4bafe7476224d081ad7737132c4c096e9e8287a3c3f9d7d14f6'
|
|
|
|
|
2013-04-26 12:55:37 +02:00
|
|
|
# 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 = '''
|
2013-04-23 00:25:16 +02:00
|
|
|
import subprocess
|
|
|
|
send = subprocess.check_output(["whoami"])
|
|
|
|
''',
|
2013-04-26 12:55:37 +02:00
|
|
|
where = '''
|
2013-04-23 00:25:16 +02:00
|
|
|
import subprocess
|
|
|
|
send = subprocess.check_output(["pwd"])
|
|
|
|
''',
|
2013-04-26 12:55:37 +02:00
|
|
|
uname = '''
|
2013-04-23 00:25:16 +02:00
|
|
|
import os
|
|
|
|
send = os.uname()
|
|
|
|
''',
|
2013-04-26 12:55:37 +02:00
|
|
|
date = '''
|
2013-04-23 00:25:16 +02:00
|
|
|
import subprocess
|
|
|
|
send = subprocess.check_output(["date"])
|
|
|
|
''',
|
2013-04-26 12:55:37 +02:00
|
|
|
ping_fanir = '''
|
2013-04-23 00:25:16 +02:00
|
|
|
import subprocess
|
|
|
|
send = subprocess.check_output(["ping", "-c 2", "fanir.de"])
|
|
|
|
''')
|
2013-04-26 12:55:37 +02:00
|
|
|
|
|
|
|
# This is sent to the client after the connection is established
|
|
|
|
MOTD = "Welcome! This is only a test server, for developing purposes.\n"\
|
|
|
|
"Here (may) be more text..."
|
|
|
|
|
|
|
|
# Verbosity of logging.
|
|
|
|
# Can be from 0 (only default output) to 3 (debug messages)
|
|
|
|
VERBOSITY = 3
|
|
|
|
##############
|
|
|
|
|
|
|
|
|
|
|
|
main()
|