summaryrefslogtreecommitdiffstats
path: root/net/bsbf-resources/files/usr/sbin/bsbf-bonding
blob: 8d7ef502dcce6838ef14af55322a83cb279d1488 (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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/bin/sh
# SPDX-License-Identifier: AGPL-3.0-or-later
# Copyright (C) 2026 Chester A. Unal <chester.a.unal@arinc9.com>

usage() {
	echo "Usage: $0 --status | --enable | --disable | --uninstall"
	exit 1
}

[ $# -ne 1 ] && usage

case "$1" in
--status)
	for service in bsbf-mptcp xray; do
		service "$service" status >/dev/null || disabled=1
	done
	service bsbf-bonding-nft enabled || disabled=1
	[ -n "$disabled" ] && echo "disabled" && exit 1
	echo "enabled"
	;;
--enable)
	# Source /etc/bsbf/bsbf-bonding.conf. Exit if server_ipv4, server_port,
	# or uuid is empty.
	. /etc/bsbf/bsbf-bonding.conf
	if [ -z "$server_ipv4" ] || [ -z "$server_port" ] || [ -z "$uuid" ]; then
		echo "server_ipv4, server_port, and uuid on /etc/bsbf/bsbf-bonding.conf must not be empty."
		exit 1
	fi

	# Configure xray.
	ucode -l fs -D infile="/usr/share/bsbf/xray.json" \
	      -D outfile="/etc/xray/config.json" \
	      -D addr="$server_ipv4" \
	      -D port="$server_port" \
	      -D id="$uuid" \
	      -e '
	  let j = json(fs.readfile(infile));
	  j.outbounds[0].settings.address = addr;
	  j.outbounds[0].settings.port = port;
	  j.outbounds[0].settings.id = id;
	  fs.writefile(outfile, sprintf("%.2J\n", j));'

	# Flush the MPTCP endpoint table.
	ip mp e f

	# Enable and (re)start init.d services.
	service bsbf-bonding-nft enable
	service bsbf-mptcp enable
	service xray enable
	service bsbf-mptcp restart 2>/dev/null
	service xray restart 2>/dev/null

	# (Re-)add nftables rules.
	nft -f /usr/share/bsbf/bsbf_bonding.nft
	;;
--disable)
	# Delete nftables rules.
	nft destroy table bsbf_bonding

	# Disable and stop init.d services.
	service bsbf-bonding-nft disable
	service bsbf-mptcp disable
	service xray disable
	service bsbf-mptcp stop 2>/dev/null
	service xray stop 2>/dev/null

	# Delete bsbf-mptcp files.
	rm -f /run/bsbf-mptcp-*

	# Flush the MPTCP endpoint table.
	ip mp e f
	;;
--uninstall)
	# Delete nftables rules.
	nft destroy table bsbf_bonding

	# Stop bsbf-mptcp init.d service.
	service bsbf-mptcp stop 2>/dev/null

	# Delete bsbf-mptcp files.
	rm -f /run/bsbf-mptcp-*

	# Flush the MPTCP endpoint table.
	ip mp e f

	# Restore xray.
	rm -f /etc/xray/config.json
	uci set xray.enabled.enabled='0'
	uci commit
	service xray restart 2>/dev/null

	# Uninstall bsbf-bonding, bsbf-client-web, bsbf-mptcp, and
	# bsbf-rate-limiting.
	apk del bsbf-bonding bsbf-client-web bsbf-mptcp bsbf-rate-limiting
	;;
*)
	usage
	;;
esac