feat: Add complete init system for wsssht tunnel setup tool

- Create wsssht.init init script by copying and modifying wssshc.init
- Add wsssht logrotate script for log management
- Create wsssht systemd service file for systemd integration
- Implement wsssht-watchdog script for process monitoring and restart
- Add /etc/default/wsssht configuration file
- Update debian/rules to include wsssht init files in package
- Modify debian/postinst to create wsssht user/group and setup init system
- Enable both sysv init and systemd service integration
- Configure proper permissions and ownership for wsssht daemon
parent 388bad08
#!/bin/bash
#
# wsssht Startup script for WSSSH Tunnel Setup Tool
#
# chkconfig: 345 85 15
# description: WSSSH Tunnel Setup Tool - Creates WebSocket tunnels for manual connections
# processname: wsssht
# pidfile: /var/run/wsssht.pid
### BEGIN INIT INFO
# Provides: wsssht
# 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: WSSSH Tunnel Setup Tool
# Description: WSSSH Tunnel Setup Tool creates WebSocket tunnels for manual connections
### END INIT INFO
# Source function library (Debian/Ubuntu)
. /lib/lsb/init-functions
# Configuration
NAME="wsssht"
DAEMON="/usr/bin/wsssht"
WATCHDOG="/usr/sbin/wsssht-watchdog"
PIDFILE="/var/run/wsssht.pid"
WATCHDOG_PIDFILE="/var/run/wsssht-watchdog.pid"
DEFAULT_FILE="/etc/default/wsssht"
CONFIG_SYSTEM="/etc/wsssht.conf"
CONFIG_USER="$HOME/.config/wsssh/wsssht.conf"
LOG_FACILITY="daemon"
USER="wsssht"
GROUP="wsssht"
# 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/wsssht
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 wsssht 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 daemon is already running
if is_running; then
local pid=$(cat "$PIDFILE")
echo "already running (PID: $pid)"
return 0
fi
# Check if START is enabled
if ! check_start_enabled; then
echo "disabled in $DEFAULT_FILE"
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 "configuration file not found"
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/wsssht.conf.example $CONFIG_SYSTEM"
return 1
fi
# Check if daemon executable exists
if [ ! -x "$DAEMON" ]; then
echo "daemon executable $DAEMON not found or not executable"
return 1
fi
# Create necessary directories
mkdir -p /var/run /var/log/wsssht
chown $USER:$GROUP /var/run /var/log/wsssht 2>/dev/null || true
# Start the daemon directly
echo "Starting daemon..."
if "$DAEMON" >> /var/log/wsssht/wsssht.log 2>&1 &
then
echo $! > "$PIDFILE"
echo "Daemon started successfully"
else
echo "FAILED"
echo "Could not start daemon"
return 1
fi
# Wait for daemon to be fully running
local count=0
while [ $count -lt 10 ] && ! is_running; do
echo "Waiting for daemon to start... ($count/10)"
sleep 1
count=$((count + 1))
done
if is_running; then
local pid=$(cat "$PIDFILE")
echo "Daemon is running (PID: $pid)"
# Stop any existing watchdog before starting a new one
if [ -f "$WATCHDOG_PIDFILE" ] || pgrep -f "wsssht-watchdog" >/dev/null 2>&1; then
echo "Stopping existing watchdog..."
$WATCHDOG stop >/dev/null 2>&1
sleep 2
fi
# Now start the watchdog to monitor the running daemon
echo "Starting watchdog..."
if [ -x "$WATCHDOG" ]; then
$WATCHDOG start >/dev/null 2>&1
# Wait for watchdog PID file to be created (max 5 seconds)
local count=0
while [ $count -lt 10 ] && [ ! -f "$WATCHDOG_PIDFILE" ]; do
sleep 0.5
count=$((count + 1))
done
if [ -f "$WATCHDOG_PIDFILE" ]; then
watchdog_pid=$(cat "$WATCHDOG_PIDFILE" 2>/dev/null)
if [ -n "$watchdog_pid" ] && kill -0 "$watchdog_pid" 2>/dev/null; then
echo "OK"
return 0
else
echo "OK (daemon running, watchdog process not responding)"
return 0
fi
else
echo "OK (daemon running, watchdog PID file not found)"
return 0
fi
else
echo "OK (daemon running, no watchdog available)"
return 0
fi
else
echo "FAILED"
echo "Daemon failed to start properly"
return 1
fi
}
# Function to stop the daemon
stop() {
echo -n "Stopping $NAME: "
# First, try to stop the watchdog if it's running
if [ -f "$WATCHDOG_PIDFILE" ]; then
echo "Stopping watchdog..."
$WATCHDOG stop >/dev/null 2>&1
sleep 2
fi
# Check if daemon is running via PID file
if is_running; then
echo "Stopping daemon via PID file..."
local pid=$(cat "$PIDFILE")
# Try to stop gracefully first
kill -TERM "$pid" 2>/dev/null
sleep 2
# Check if it's still running
if kill -0 "$pid" 2>/dev/null; then
# Force kill if still running
kill -KILL "$pid" 2>/dev/null
sleep 1
fi
fi
# Also kill any wsssht processes that might be running (regardless of PID file)
echo "Ensuring all wsssht processes are stopped..."
pkill -TERM -f "^/usr/bin/wsssht" 2>/dev/null || true
sleep 2
pkill -KILL -f "^/usr/bin/wsssht" 2>/dev/null || true
# Clean up PID files
rm -f "$PIDFILE" "$WATCHDOG_PIDFILE"
# Final check
if is_running; then
echo "FAILED"
return 1
else
echo "OK"
return 0
fi
}
# Function to restart the daemon
restart() {
stop
sleep 2
start
}
# Function to check status
status() {
# Check watchdog status
if [ -f "$WATCHDOG_PIDFILE" ]; then
local watchdog_pid=$(cat "$WATCHDOG_PIDFILE")
if kill -0 "$watchdog_pid" 2>/dev/null; then
echo "Watchdog is running (PID: $watchdog_pid)"
else
echo "Watchdog PID file exists but process is not running"
rm -f "$WATCHDOG_PIDFILE"
fi
else
echo "Watchdog is not running"
fi
# Check daemon status
if is_running; then
local pid=$(cat "$PIDFILE")
echo "$NAME daemon is running (PID: $pid)"
return 0
else
echo "$NAME daemon is not running"
return 3
fi
}
# Function to reload configuration
reload() {
echo -n "Reloading $NAME configuration: "
if ! is_running; then
echo "not running"
return 1
fi
local pid=$(cat "$PIDFILE")
kill -HUP $pid 2>/dev/null
if [ $? -eq 0 ]; then
echo "OK"
return 0
else
echo "FAILED"
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
......@@ -46,19 +46,61 @@ case "$1" in
usermod -g wssshc wssshc 2>/dev/null || true
fi
# Create log directory
# Create log directory for wssshc
if [ ! -d /var/log/wssshc ]; then
mkdir -p /var/log/wssshc
chown wssshc:wssshc /var/log/wssshc
chmod 755 /var/log/wssshc
fi
# Install watchdog script
# Install wssshc watchdog script
if [ -f /usr/sbin/wssshc-watchdog ]; then
chown wssshc:wssshc /usr/sbin/wssshc-watchdog
chmod 755 /usr/sbin/wssshc-watchdog
fi
# Create wsssht user and group if they don't exist
if ! getent group wsssht >/dev/null 2>&1; then
addgroup --system wsssht
fi
if ! getent passwd wsssht >/dev/null 2>&1; then
adduser --system --ingroup wsssht --home /var/lib/wsssht \
--no-create-home --shell /bin/false wsssht
fi
# Create home directory for wsssht user
if [ ! -d /var/lib/wsssht ]; then
mkdir -p /var/lib/wsssht
fi
# Ensure wsssht user owns its home directory and can write to it
chown wsssht:wsssht /var/lib/wsssht
chmod 755 /var/lib/wsssht
# Ensure the user can actually write to its home directory
# Try to create a test file to verify write permissions
if ! su -s /bin/sh wsssht -c "touch /var/lib/wsssht/.test_write 2>/dev/null && rm /var/lib/wsssht/.test_write 2>/dev/null" 2>/dev/null; then
echo "Warning: wsssht user cannot write to /var/lib/wsssht, fixing permissions"
# Try to fix permissions by making directory writable
chmod 775 /var/lib/wsssht
# Also ensure the user is in the right group
usermod -g wsssht wsssht 2>/dev/null || true
fi
# Create log directory for wsssht
if [ ! -d /var/log/wsssht ]; then
mkdir -p /var/log/wsssht
chown wsssht:wsssht /var/log/wsssht
chmod 755 /var/log/wsssht
fi
# Install wsssht watchdog script
if [ -f /usr/sbin/wsssht-watchdog ]; then
chown wsssht:wsssht /usr/sbin/wsssht-watchdog
chmod 755 /usr/sbin/wsssht-watchdog
fi
# Create /etc/default/wssshc if it doesn't exist
if [ ! -f /etc/default/wssshc ]; then
cat > /etc/default/wssshc << EOF
......@@ -72,7 +114,20 @@ EOF
chmod 644 /etc/default/wssshc
fi
# Create example configuration file if it doesn't exist
# Create /etc/default/wsssht if it doesn't exist
if [ ! -f /etc/default/wsssht ]; then
cat > /etc/default/wsssht << EOF
# WSSSH Tunnel Setup Tool (wsssht) configuration
# Set to Y, 1, TRUE, true, YES, or yes to enable the service
START=no
# Additional configuration can be done in /etc/wsssht.conf
# or ~/.config/wsssh/wsssht.conf
EOF
chmod 644 /etc/default/wsssht
fi
# Create example configuration files if they don'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
......@@ -80,17 +135,34 @@ EOF
fi
fi
# Set up init script
if [ ! -f /etc/wsssht.conf.example ]; then
if [ -f /usr/share/wsssh/wsssht.conf.example ]; then
cp /usr/share/wsssh/wsssht.conf.example /etc/wsssht.conf.example
chmod 644 /etc/wsssht.conf.example
fi
fi
# Set up init scripts
if [ -x /etc/init.d/wssshc ]; then
update-rc.d wssshc defaults >/dev/null 2>&1 || true
fi
# Enable systemd service if available and sysv init is not active
if [ -x /etc/init.d/wsssht ]; then
update-rc.d wsssht defaults >/dev/null 2>&1 || true
fi
# Enable systemd services if available and sysv init is not active
if [ -f /lib/systemd/system/wssshc.service ] && command -v systemctl >/dev/null 2>&1; then
if ! systemctl is-active wssshc >/dev/null 2>&1 && [ ! -f /var/run/wssshc-watchdog.pid ]; then
systemctl enable wssshc.service >/dev/null 2>&1 || true
fi
fi
if [ -f /lib/systemd/system/wsssht.service ] && command -v systemctl >/dev/null 2>&1; then
if ! systemctl is-active wsssht >/dev/null 2>&1 && [ ! -f /var/run/wsssht-watchdog.pid ]; then
systemctl enable wsssht.service >/dev/null 2>&1 || true
fi
fi
;;
abort-upgrade|abort-remove|abort-deconfigure)
......
......@@ -44,15 +44,24 @@ override_dh_auto_install:
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 wsssht configuration file
install -m 644 ../wssht.conf.example debian/wsssh-tools/usr/share/wsssh/
# Install wsssht init script
install -m 755 ../wsssht.init debian/wsssh-tools/etc/init.d/wsssht
# Install wsssht watchdog script
install -m 755 debian/wsssht-watchdog debian/wsssh-tools/usr/sbin/wsssht-watchdog
# Install wsssht configuration files
install -m 644 ../wsssht.conf.example debian/wsssh-tools/usr/share/wsssh/
install -m 644 debian/wsssht.default debian/wsssh-tools/etc/default/wsssht
# Install logrotate configuration
install -m 644 debian/wssshc.logrotate debian/wsssh-tools/etc/logrotate.d/wssshc
install -m 644 debian/wsssht.logrotate debian/wsssh-tools/etc/logrotate.d/wsssht
# Install systemd service file
mkdir -p debian/wsssh-tools/lib/systemd/system
install -m 644 debian/wssshc.service debian/wsssh-tools/lib/systemd/system/
install -m 644 debian/wsssht.service debian/wsssh-tools/lib/systemd/system/
# Install Python scripts
install -m 755 ../wssshd.py debian/wsssh-tools/usr/bin/
......
This diff is collapsed.
# WSSSH Tunnel Setup Tool (wsssht) configuration
# Set to Y, 1, TRUE, true, YES, or yes to enable the service
START=no
# Additional configuration can be done in /etc/wsssht.conf
# or ~/.config/wsssh/wsssht.conf
\ No newline at end of file
/var/log/wsssht/wsssht.log {
weekly
missingok
rotate 52
compress
delaycompress
notifempty
create 644 wsssht wsssht
postrotate
if [ -f /var/run/wsssht.pid ]; then
kill -HUP $(cat /var/run/wsssht.pid) 2>/dev/null || true
fi
endscript
}
/var/log/wsssht/watchdog.log {
weekly
missingok
rotate 52
compress
delaycompress
notifempty
create 644 wsssht wsssht
postrotate
if [ -f /var/run/wsssht-watchdog.pid ]; then
kill -HUP $(cat /var/run/wsssht-watchdog.pid) 2>/dev/null || true
fi
endscript
}
\ No newline at end of file
[Unit]
Description=WSSSH Tunnel Setup Tool
After=network.target syslog.target
Requires=network.target
ConditionPathExists=!/etc/init.d/wsssht
ConditionPathExists=!/var/run/wsssht-watchdog.pid
[Service]
Type=forking
User=wsssht
Group=wsssht
EnvironmentFile=-/etc/default/wsssht
ExecStart=/usr/sbin/wsssht-watchdog start
ExecStop=/usr/sbin/wsssht-watchdog stop
ExecReload=/usr/sbin/wsssht-watchdog restart
PIDFile=/var/run/wsssht-watchdog.pid
Restart=on-failure
RestartSec=5
[Install]
WantedBy=multi-user.target
\ 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