summaryrefslogtreecommitdiffstats
path: root/.root/usr/local/bin/proxyctl
blob: 839ef661ea5fa3cd93e7bc729e2c075b3eccb568 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/bin/bash

. /etc/profile.d/proxy.sh

function proxy_update_config() {
	if [ $UID -ne 0 ]; then
		echo "This command requires root privileges. Aborting..."
		return 1
	fi
	SRCD=/etc/sing-box
	${SRCD}/libconfig_to_json.py ${SRCD}/libconfig ${SRCD}/config.json 
	if [ $? -ne 0 ]; then
		echo "Failed to write config. Aborting..."
		return 1
	fi

	systemctl restart sing-box
	status=$(systemctl status sing-box)
	if [ $? -ne 0 ]; then
		echo "Failed to start sing-box. Manual intervention needed."
		return 1
	else
		echo "Success"
	fi
}

function __help() {
	echo "This script controls the sing-box proxy on a system"
	echo "Usage: "
	echo "	proxyctl [update/reload | on/up | off/down | start | stop | enable | disable]"
	echo "	update or reload: reloads the sing-box configuration (in libconfig)"
	echo "	on or up: sets the environmental variables to tell programs to use proxy on 127.0.0.1:2080"
	echo "	off or down: unsets the proxy-related enviromnental variables"
	echo "	start, stop, enable and disable: corresponding systemctl commands for sing-box service"
	echo "Return status: "
	echo "	0: success"
	echo "	any other: something went wrong."
	echo "	[If running systemctl commands]: the return status of 'systemctl status sing-box'"
}

if [ $# -lt 1 ]; then
	echo "insufficient arguments. needed: . provided: $#"
	__help
	return 1
fi
if [ $1 = update ] || [ $1 = reload ]; then
	proxy_update_config
elif [ $1 = on ] || [ $1 = up ]; then
	proxy-enable
elif [ $1 = off ] || [ $1 = down ]; then
	proxy-disable
elif [ $1 = start ] || [ $1 = stop ] || [ $1 = enable ] || [ $1 = disable ]; then
	systemctl $1 sing-box 
	STATUS_TEXT=$(systemctl status sing-box)
	STATUS=$?
	if [ ${STATUS} -ne 0 ]; then
		echo "Failed to $1 sing-box"
		echo "otput of 'systemctl status sing-box':"
		echo ${STATUS_TEXT}
		return ${STATUS}
	fi
elif [ $1 = "--help" ] || [ $1 = help ] || [ $1 = "-h" ]; then
	__help
else
	echo "Unknown command."
	return 1
fi