#! /bin/bash ### BEGIN INIT INFO # Provides: phantom_proxy # Required-Start: $remote_fs $syslog # Required-Stop: $remote_fs $syslog # Default-Start: 3 4 5 # Default-Stop: 0 1 2 6 # Short-Description: This script starts the phantom proxy # Description: With this script you can automaticly start phantom_proxy # instances. The script should be placed in /etc/init.d/phproxy ### END INIT INFO ### README # # You can put a configfile at # /etc/phantom_proxy/config # or # put several files into a folder at /etc/phantom_proxy/config # all files are loaded and one instance is created per file(CAUTION!! you have to set a unique PP_PID_NAME for each instance) # # Options you can set are: # PP_PID_NAME: The name of the process # PP_PORT: The port to run the proxy on (3003) # PP_HOST: The host to run the proxy on (127.0.0.1) # PP_HMAC: The hmac key to secure the server # # Example config file could look like the following: # # PP_PID_NAME="phantom_proxy_instance" # PP_PORT=3002 # PP_HMAC="abcdefghi" # # End Example # ### End README PP_NAME=phantom_proxy PP_PID_NAME="phantom_proxy" PP_HMAC="none" PP_USER_STANDARD=experteer PP_USER=experteer PP_LOG() { local log_dir="/tmp/$PP_NAME" #create PP_LOG folder if [ ! -d $log_dir ] then mkdir -p $log_dir fi echo $log_dir } PP_CONFIG() { echo "/etc/$PP_NAME/config" } PP_LOG_FILE() { echo "$(PP_LOG)/$PP_PID_NAME.log" } PP_PID() { echo "/tmp/pids/$PP_PID_NAME.pid" } PP_PORT=3003 PP_HOST=localhost PP_OPTIONS() { if [ $PP_HMAC == "none" ] then echo "-p $PP_PORT -a $PP_HOST -P $(PP_PID) -l $(PP_LOG_FILE) -d" else echo "-p $PP_PORT -a $PP_HOST -P $(PP_PID) -l $(PP_LOG_FILE) -d -hmac $PP_HMAC" fi } load_config_file() { PP_HMAC="none" if [ -e $1 ] then echo "Loaded Configfile: $1" source $1 fi } run_for_configs() { PP_USER=$PP_USER_STANDARD cfg_folder=$(PP_CONFIG) echo "Check config $cfg_folder" if [ -d $cfg_folder ]; then echo "Config folder: $cfg_folder" for i in $( ls $cfg_folder) ; do load_config_file "$cfg_folder/$i" #run the given method $1 sleep 2 done return elif [ -e $cfg_folder ]; then echo "Config file" load_config_file $cfg_folder #run the given method $1 return fi echo "No config detect using standard" $1 } do_start() { if [ -e $(PP_PID) ] then echo "Service $PP_NAME already running abort" return fi echo "Starting $PP_NAME service" sudo su -c "$PP_NAME $(PP_OPTIONS)" $PP_USER } do_stop() { if [ -e $(PP_PID) ] then echo "Stopping $PP_NAME service" pp_id=$(cat $(PP_PID)) echo "Stopping $PP_NAME service: $pp_id" kill $pp_id return fi echo "Error $PP_NAME service isn't running" } do_restart() { do_stop sleep 5 do_start } do_status() { echo "Checking status" if [ -e $(PP_PID) ] then pp_id=$(cat $(PP_PID)) if [ `ps -p $pp_id | grep -c $PP_NAME` -ne 1 ] ; then echo "failed - no process" return 2 else echo "Found PID: $pp_id" return 0 fi return fi echo "$PP_NAME is not running" } case "$1" in start) run_for_configs do_start ;; stop) run_for_configs do_stop ;; restart) run_for_configs do_restart ;; status) run_for_configs do_status ;; *) echo "Usage: $PP_NAME {start|stop|status|restart}" >&2 exit 3 ;; esac :