Commit 740cbe6c authored by nextime's avatar nextime

Add comprehensive wssshc init script and Debian packaging support

- Create wssshc.init with /etc/default/wssshc START variable validation
- Implement configuration hierarchy: CLI > ~/.config > /etc
- Add syslog redirection for daemon output
- Create Debian postinst/postrm scripts for user/group management
- Add wssshc.conf.example with comprehensive configuration options
- Update debian/rules to install init scripts and Python components
- Create /etc/default/wssshc template file
- Enable proper service management with chkconfig/update-rc.d
- Support for custom config file via --config option
- Comprehensive error handling and user feedback
parent 14525e6c
# WebSocket SSH Client (wssshc) Configuration Example
#
# This is an example configuration file for wssshc.
# Copy this file to /etc/wssshc.conf or ~/.config/wsssh/wssshc.conf
# and modify the settings as needed.
#
# Configuration hierarchy (highest priority first):
# 1. Command line arguments
# 2. ~/.config/wsssh/wssshc.conf (user config)
# 3. /etc/wssshc.conf (system config)
[wssshc]
# WebSocket server IP address or hostname
server-ip = wssshd.example.com
# WebSocket server port
port = 9898
# Client ID for registration with the server
id = my-client
# Registration password (must match server configuration)
password = my-secret-password
# Reconnection interval in seconds (default: 30)
interval = 30
\ No newline at end of file
#!/bin/bash
#
# wssshc Startup script for WebSocket SSH Client
#
# chkconfig: 345 84 16
# description: WebSocket SSH Client - Registers with wssshd server and handles SSH connections
# processname: wssshc
# pidfile: /var/run/wssshc.pid
# config: /etc/wssshc.conf
### BEGIN INIT INFO
# Provides: wssshc
# Required-Start: $local_fs $network $syslog
# Required-Stop: $local_fs $network $syslog
# Default-Start: 3 4 5
# Default-Stop: 0 1 2 6
# Short-Description: WebSocket SSH Client
# Description: WebSocket SSH Client registers with wssshd server and handles SSH connections
### END INIT INFO
# Source function library
. /etc/rc.d/init.d/functions
# Configuration
NAME="wssshc"
DAEMON="/usr/bin/wssshc.py"
PIDFILE="/var/run/wssshc.pid"
DEFAULT_FILE="/etc/default/wssshc"
CONFIG_SYSTEM="/etc/wssshc.conf"
CONFIG_USER="$HOME/.config/wsssh/wssshc.conf"
LOG_FACILITY="daemon"
USER="wsssh"
GROUP="wsssh"
# Check if we're running as root
if [ $(id -u) != 0 ]; then
echo "Error: This script must be run as root"
exit 1
fi
# Function to check if START is enabled in /etc/default/wssshc
check_start_enabled() {
if [ ! -f "$DEFAULT_FILE" ]; then
echo "Configuration file $DEFAULT_FILE not found"
return 1
fi
# Read the START variable
START_VALUE=$(grep -E "^START=" "$DEFAULT_FILE" | cut -d'=' -f2 | tr -d '[:space:]')
if [ -z "$START_VALUE" ]; then
echo "START variable not found in $DEFAULT_FILE"
return 1
fi
# Check if START is set to a positive value
case "$START_VALUE" in
[Yy]|[Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|1)
return 0
;;
*)
echo "START is set to '$START_VALUE', service will not start"
return 1
;;
esac
}
# Function to find configuration file
find_config_file() {
# Check system config first
if [ -f "$CONFIG_SYSTEM" ]; then
echo "$CONFIG_SYSTEM"
return 0
fi
# Check user config (run as wsssh user to get correct HOME)
if su - "$USER" -c "[ -f '$CONFIG_USER' ]" 2>/dev/null; then
echo "$CONFIG_USER"
return 0
fi
echo "No configuration file found"
return 1
}
# Function to check if process is running
is_running() {
if [ -f "$PIDFILE" ]; then
local pid=$(cat "$PIDFILE")
if [ -d "/proc/$pid" ]; then
return 0
else
# Stale PID file
rm -f "$PIDFILE"
return 1
fi
fi
return 1
}
# Function to start the daemon
start() {
echo -n $"Starting $NAME: "
# Check if already running
if is_running; then
echo -n "already running"
echo_success
echo
return 0
fi
# Check if START is enabled
if ! check_start_enabled; then
echo -n "disabled in $DEFAULT_FILE"
echo_failure
echo
echo "To enable $NAME, set START=Y in $DEFAULT_FILE"
return 1
fi
# Find configuration file
CONFIG_FILE=$(find_config_file)
if [ $? -ne 0 ]; then
echo -n "configuration file not found"
echo_failure
echo
echo "Please create a configuration file:"
echo " System-wide: $CONFIG_SYSTEM"
echo " User-specific: $CONFIG_USER"
echo "You can use the example configuration as a template:"
echo " cp /usr/share/wsssh/wssshc.conf.example $CONFIG_SYSTEM"
return 1
fi
# Check if daemon executable exists
if [ ! -x "$DAEMON" ]; then
echo -n "daemon executable $DAEMON not found or not executable"
echo_failure
echo
return 1
fi
# Create PID directory if it doesn't exist
mkdir -p /var/run
chown $USER:$GROUP /var/run 2>/dev/null || true
# Start the daemon with syslog redirection
daemon --pidfile="$PIDFILE" --user="$USER" \
"exec $DAEMON --config '$CONFIG_FILE' 2>&1 | logger -t $NAME -p $LOG_FACILITY.info"
local retval=$?
if [ $retval -eq 0 ]; then
# Wait a moment for the daemon to start
sleep 2
# Check if it actually started
if is_running; then
echo_success
echo
return 0
else
echo_failure
echo
echo "Daemon failed to start properly"
return 1
fi
else
echo_failure
echo
return $retval
fi
}
# Function to stop the daemon
stop() {
echo -n $"Stopping $NAME: "
if ! is_running; then
echo -n "not running"
echo_success
echo
return 0
fi
# Try graceful shutdown first
if [ -f "$PIDFILE" ]; then
local pid=$(cat "$PIDFILE")
kill -TERM $pid 2>/dev/null
# Wait up to 30 seconds for graceful shutdown
local count=0
while [ $count -lt 30 ] && is_running; do
sleep 1
count=$((count + 1))
done
if is_running; then
# Force kill if graceful shutdown failed
echo -n "forcing shutdown... "
kill -KILL $pid 2>/dev/null
sleep 2
fi
fi
# Clean up PID file
rm -f "$PIDFILE"
if is_running; then
echo_failure
echo
return 1
else
echo_success
echo
return 0
fi
}
# Function to restart the daemon
restart() {
stop
sleep 2
start
}
# Function to check status
status() {
if is_running; then
local pid=$(cat "$PIDFILE")
echo "$NAME is running (PID: $pid)"
return 0
else
echo "$NAME is not running"
return 3
fi
}
# Function to reload configuration
reload() {
echo -n $"Reloading $NAME configuration: "
if ! is_running; then
echo -n "not running"
echo_failure
echo
return 1
fi
local pid=$(cat "$PIDFILE")
kill -HUP $pid 2>/dev/null
if [ $? -eq 0 ]; then
echo_success
echo
return 0
else
echo_failure
echo
return 1
fi
}
# Main script logic
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
reload)
reload
;;
status)
status
;;
condrestart|try-restart)
if is_running; then
restart
fi
;;
*)
echo "Usage: $0 {start|stop|restart|reload|status|condrestart}"
exit 2
;;
esac
exit $?
\ No newline at end of file
#!/bin/sh
# postinst script for wsssh-tools
set -e
# summary of how this script can be called:
# * <postinst> `configure' <most-recently-configured-version>
# * <old-postinst> `abort-upgrade' <new version>
# * <conflictor's-postinst> `abort-remove' `in-favour' <package>
# <new-version>
# * <postinst> `abort-remove'
# * <deconfigured's-postinst> `abort-deconfigure' `in-favour'
# <package-being-installed> <version> `removing'
# <conflicting-package> <version>
# for details, see http://www.debian.org/doc/debian-policy/ or
# the debian-policy package
case "$1" in
configure)
# Create wsssh user and group if they don't exist
if ! getent group wsssh >/dev/null 2>&1; then
addgroup --system wsssh
fi
if ! getent passwd wsssh >/dev/null 2>&1; then
adduser --system --ingroup wsssh --home /var/lib/wsssh \
--no-create-home --shell /bin/false wsssh
fi
# Create home directory for wsssh user
if [ ! -d /var/lib/wsssh ]; then
mkdir -p /var/lib/wsssh
chown wsssh:wsssh /var/lib/wsssh
chmod 755 /var/lib/wsssh
fi
# Create /etc/default/wssshc if it doesn't exist
if [ ! -f /etc/default/wssshc ]; then
cat > /etc/default/wssshc << EOF
# WebSocket SSH Client (wssshc) configuration
# Set to Y, 1, TRUE, true, YES, or yes to enable the service
START=no
# Additional configuration can be done in /etc/wssshc.conf
# or ~/.config/wsssh/wssshc.conf
EOF
chmod 644 /etc/default/wssshc
fi
# Create example configuration file if it doesn't exist
if [ ! -f /etc/wssshc.conf.example ]; then
if [ -f /usr/share/wsssh/wssshc.conf.example ]; then
cp /usr/share/wsssh/wssshc.conf.example /etc/wssshc.conf.example
chmod 644 /etc/wssshc.conf.example
fi
fi
# Set up init script
if [ -x /etc/init.d/wssshc ]; then
update-rc.d wssshc defaults >/dev/null 2>&1 || true
fi
;;
abort-upgrade|abort-remove|abort-deconfigure)
;;
*)
echo "postinst called with unknown argument \`$1'" >&2
exit 1
;;
esac
# dh_installdeb will replace this with shell code automatically
# generated by other debhelper scripts.
#DEBHELPER#
exit 0
\ No newline at end of file
#!/bin/sh
# postrm script for wsssh-tools
set -e
# summary of how this script can be called:
# * <postrm> `remove'
# * <postrm> `purge'
# * <old-postrm> `upgrade' <new-version>
# * <new-postrm> `failed-upgrade' <old-version>
# * <new-postrm> `abort-install'
# * <new-postrm> `abort-install' <old-version>
# * <new-postrm> `abort-upgrade' <old-version>
# * <disappearer's-postrm> `disappear' <overwriter>
# <overwriter-version>
# for details, see http://www.debian.org/doc/debian-policy/ or
# the debian-policy package
case "$1" in
purge|remove|upgrade|failed-upgrade|abort-install|abort-upgrade|disappear)
# Stop the service if it's running
if [ -x /etc/init.d/wssshc ]; then
invoke-rc.d wssshc stop >/dev/null 2>&1 || true
fi
# Remove init script symlinks
if [ -x /etc/init.d/wssshc ]; then
update-rc.d wssshc remove >/dev/null 2>&1 || true
fi
;;
*)
echo "postrm called with unknown argument \`$1'" >&2
exit 1
;;
esac
# dh_installdeb will replace this with shell code automatically
# generated by other debhelper scripts.
#DEBHELPER#
exit 0
\ No newline at end of file
......@@ -26,6 +26,19 @@ override_dh_auto_install:
make install DESTDIR=debian/wsssh-tools
dh_installman man/*.1
# Install wssshc init script
install -m 755 ../wssshc.init debian/wsssh-tools/etc/init.d/wssshc
# Install wssshc configuration files
install -m 644 ../wssshc.conf.example debian/wsssh-tools/usr/share/wsssh/
install -m 644 debian/wssshc.default debian/wsssh-tools/etc/default/wssshc
# Install Python scripts
install -m 755 ../wssshc.py debian/wsssh-tools/usr/bin/
install -m 755 ../wssshd.py debian/wsssh-tools/usr/bin/
install -m 755 ../wsssh.py debian/wsssh-tools/usr/bin/
install -m 755 ../wsscp.py debian/wsssh-tools/usr/bin/
override_dh_auto_clean:
make clean
rm -f configure.sh.stamp
\ No newline at end of file
# WebSocket SSH Client (wssshc) configuration
# Set to Y, 1, TRUE, true, YES, or yes to enable the service
START=no
# Additional configuration can be done in /etc/wssshc.conf
# or ~/.config/wsssh/wssshc.conf
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment