#!/bin/bash

TOGGLE_FILE="/tmp/toggle_ssocks"

report_running_status() {
    if [ -f "$TOGGLE_FILE" ]; then
        echo "true"
    else
        echo "false"
    fi
}

kill_process() {
    local process_name="$1"
    # Graceful kill
    killall "$process_name" 2>/dev/null
    sleep 0.25
    # Force kill if still running, suppressing all output
    killall -9 "$process_name" &>/dev/null
}

# Handle the "get" command
if [ "$1" = "get" ]; then
    report_running_status
    exit
fi

# If no argument or any other argument, perform toggle
kill_process sslocal
kill_process spoof-dpi

if [ -f "$TOGGLE_FILE" ]; then
    echo "Enabling spoofdpi"
    http_proxy="" spoof-dpi -addr 127.0.0.1 -port 1080 >/dev/null 2>&1 &
    http_proxy="" sslocal -c /etc/shadowsocks-rust/hostvds.json >/dev/null 2>&1 &
    rm "$TOGGLE_FILE"
else
    echo "Enabling shadowsocks"
    http_proxy="" sslocal -c /etc/shadowsocks-rust/mega.json >/dev/null 2>&1 &
    http_proxy="" sslocal -c /etc/shadowsocks-rust/hostvds.json >/dev/null 2>&1 &
    touch "$TOGGLE_FILE"
fi
