|
#!/bin/bash |
|
|
|
# DoH Proxy Monitor and Auto-Restart Script |
|
# Usage: ./doh-monitor.sh [--daemon] |
|
|
|
# Configuration |
|
DOH_PROCESS_NAME="doh-proxy" |
|
DOH_HOST="cdoh.vinayakg.dev" |
|
DOH_PATH="/Qz4hH-dns-query" |
|
DOH_PORT="443" |
|
CHECK_INTERVAL=60 # Check every 60 seconds |
|
MAX_RESPONSE_TIME=10 # Max response time in seconds |
|
LOG_FILE="/var/log/doh-monitor.log" |
|
PID_FILE="/var/run/doh-monitor.pid" |
|
|
|
# DoH command to restart (adjust as needed) |
|
DOH_COMMAND="/usr/local/bin/doh-proxy -u 127.0.0.1:53 -l 0.0.0.0:443 -H $DOH_HOST -p /Qz4hH-dns-query --tls-cert-key-path /etc/letsencrypt/live/$DOH_HOST/privkey.pem --tls-cert-path /etc/letsencrypt/live/$DOH_HOST/fullchain.pem" |
|
|
|
# Logging function |
|
log_message() { |
|
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" | tee -a "$LOG_FILE" |
|
} |
|
|
|
# Check if process is running |
|
is_process_running() { |
|
pgrep -f "$DOH_PROCESS_NAME" > /dev/null |
|
return $? |
|
} |
|
|
|
# Check if DoH service is responding |
|
is_doh_responding() { |
|
local response |
|
local exit_code |
|
|
|
# Test DoH endpoint with timeout |
|
response=$(curl -s --max-time "$MAX_RESPONSE_TIME" \ |
|
-H "accept: application/dns-json" \ |
|
"https://$DOH_HOST$DOH_PATH?name=google.com&type=A" 2>&1) |
|
exit_code=$? |
|
|
|
#if [ $exit_code -eq 0 ] && echo "$response" | grep -q "Answer\|Status"; then |
|
if [ $exit_code -eq 0 ]; then |
|
return 0 # Success |
|
else |
|
log_message "DoH health check failed: curl exit code $exit_code, response: $response" |
|
return 1 # Failed |
|
fi |
|
} |
|
|
|
# Kill stuck DoH process |
|
kill_doh_process() { |
|
local pids |
|
pids=$(pgrep -f "$DOH_PROCESS_NAME") |
|
|
|
if [ -n "$pids" ]; then |
|
log_message "Killing stuck DoH processes: $pids" |
|
# First try graceful termination |
|
kill $pids |
|
sleep 5 |
|
|
|
# Force kill if still running |
|
pids=$(pgrep -f "$DOH_PROCESS_NAME") |
|
if [ -n "$pids" ]; then |
|
log_message "Force killing DoH processes: $pids" |
|
kill -9 $pids |
|
sleep 2 |
|
fi |
|
fi |
|
} |
|
|
|
# Start DoH process |
|
start_doh_process() { |
|
log_message "Starting DoH proxy: $DOH_PROCESS_NAME" |
|
#nohup $DOH_COMMAND > /dev/null 2>&1 & |
|
systemctl start $DOH_PROCESS_NAME |
|
sleep 3 # Give it time to start |
|
|
|
if is_process_running; then |
|
local pid=$(pgrep -f "$DOH_PROCESS_NAME") |
|
log_message "DoH proxy started successfully with PID: $pid" |
|
return 0 |
|
else |
|
log_message "Failed to start DoH proxy" |
|
return 1 |
|
fi |
|
} |
|
|
|
# Main monitoring function |
|
monitor_doh() { |
|
log_message "DoH monitor started (PID: $$)" |
|
|
|
while true; do |
|
if ! is_process_running; then |
|
log_message "DoH process not running, starting..." |
|
start_doh_process |
|
else |
|
# Process is running, check if it's responding |
|
if ! is_doh_responding; then |
|
log_message "DoH process stuck/not responding, restarting..." |
|
kill_doh_process |
|
sleep 5 |
|
start_doh_process |
|
else |
|
log_message "DoH service is healthy" |
|
fi |
|
fi |
|
|
|
sleep "$CHECK_INTERVAL" |
|
done |
|
} |
|
|
|
# Daemon mode functions |
|
start_daemon() { |
|
if [ -f "$PID_FILE" ] && kill -0 "$(cat "$PID_FILE")" 2>/dev/null; then |
|
echo "Monitor daemon is already running (PID: $(cat "$PID_FILE"))" |
|
exit 1 |
|
fi |
|
|
|
echo "Starting DoH monitor daemon..." |
|
nohup "$0" --monitor > /dev/null 2>&1 & |
|
echo $! > "$PID_FILE" |
|
echo "Monitor daemon started with PID: $!" |
|
} |
|
|
|
stop_daemon() { |
|
if [ -f "$PID_FILE" ]; then |
|
local pid=$(cat "$PID_FILE") |
|
if kill -0 "$pid" 2>/dev/null; then |
|
echo "Stopping monitor daemon (PID: $pid)..." |
|
kill "$pid" |
|
rm -f "$PID_FILE" |
|
echo "Monitor daemon stopped" |
|
else |
|
echo "Monitor daemon not running" |
|
rm -f "$PID_FILE" |
|
fi |
|
else |
|
echo "Monitor daemon not running (no PID file)" |
|
fi |
|
} |
|
|
|
status_daemon() { |
|
if [ -f "$PID_FILE" ] && kill -0 "$(cat "$PID_FILE")" 2>/dev/null; then |
|
echo "Monitor daemon is running (PID: $(cat "$PID_FILE"))" |
|
|
|
# Show recent log entries |
|
if [ -f "$LOG_FILE" ]; then |
|
echo "Recent log entries:" |
|
tail -10 "$LOG_FILE" |
|
fi |
|
else |
|
echo "Monitor daemon is not running" |
|
if [ -f "$PID_FILE" ]; then |
|
rm -f "$PID_FILE" |
|
fi |
|
fi |
|
} |
|
|
|
# Handle command line arguments |
|
case "${1:-}" in |
|
--daemon) |
|
start_daemon |
|
;; |
|
--stop) |
|
stop_daemon |
|
;; |
|
--status) |
|
status_daemon |
|
;; |
|
--monitor) |
|
# Internal flag for daemon mode |
|
monitor_doh |
|
;; |
|
--test) |
|
echo "Testing DoH service..." |
|
if is_process_running; then |
|
echo "✓ DoH process is running" |
|
else |
|
echo "✗ DoH process is not running" |
|
fi |
|
|
|
if is_doh_responding; then |
|
echo "✓ DoH service is responding" |
|
else |
|
echo "✗ DoH service is not responding" |
|
fi |
|
;; |
|
*) |
|
cat << EOF |
|
DoH Proxy Monitor Script |
|
|
|
Usage: $0 [OPTION] |
|
|
|
Options: |
|
--daemon Start monitor as daemon |
|
--stop Stop monitor daemon |
|
--status Show daemon status and recent logs |
|
--test Test DoH service health |
|
--monitor Run monitoring loop (internal use) |
|
|
|
Configuration: |
|
Host: $DOH_HOST |
|
Path: $DOH_PATH |
|
Check interval: $CHECK_INTERVAL seconds |
|
Response timeout: $MAX_RESPONSE_TIME seconds |
|
Log file: $LOG_FILE |
|
|
|
Examples: |
|
$0 --daemon # Start monitoring in background |
|
$0 --test # Test if DoH is working |
|
$0 --status # Check monitor status |
|
$0 --stop # Stop the monitor |
|
|
|
EOF |
|
;; |
|
esac |