Skip to content

Commit 9c9752d

Browse files
committed
release: Update local scripts
1 parent 8e9fb1c commit 9c9752d

File tree

6 files changed

+173
-45
lines changed

6 files changed

+173
-45
lines changed

release/local/common.sh

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
#!/usr/bin/env bash
2+
3+
set -e -o pipefail
4+
5+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
6+
PROJECT_DIR="$(cd "$SCRIPT_DIR/../.." && pwd)"
7+
BINARY_NAME="sing-box"
8+
9+
INSTALL_BIN_PATH="/usr/local/bin"
10+
INSTALL_CONFIG_PATH="/usr/local/etc/sing-box"
11+
INSTALL_DATA_PATH="/var/lib/sing-box"
12+
SYSTEMD_SERVICE_PATH="/etc/systemd/system"
13+
14+
DEFAULT_BUILD_TAGS="with_gvisor,with_quic,with_dhcp,with_wireguard,with_utls,with_acme,with_clash_api,with_tailscale,with_ccm,badlinkname,tfogo_checklinkname0"
15+
16+
setup_environment() {
17+
if [ -d /usr/local/go ]; then
18+
export PATH="$PATH:/usr/local/go/bin"
19+
fi
20+
21+
if ! command -v go &> /dev/null; then
22+
echo "Error: Go is not installed or not in PATH"
23+
echo "Run install_go.sh to install Go"
24+
exit 1
25+
fi
26+
}
27+
28+
get_build_tags() {
29+
local extra_tags="$1"
30+
if [ -n "$extra_tags" ]; then
31+
echo "${DEFAULT_BUILD_TAGS},${extra_tags}"
32+
else
33+
echo "${DEFAULT_BUILD_TAGS}"
34+
fi
35+
}
36+
37+
get_version() {
38+
cd "$PROJECT_DIR"
39+
GOHOSTOS=$(go env GOHOSTOS)
40+
GOHOSTARCH=$(go env GOHOSTARCH)
41+
CGO_ENABLED=0 GOOS=$GOHOSTOS GOARCH=$GOHOSTARCH go run github.com/sagernet/sing-box/cmd/internal/read_tag@latest
42+
}
43+
44+
get_ldflags() {
45+
local version
46+
version=$(get_version)
47+
echo "-X 'github.com/sagernet/sing-box/constant.Version=${version}' -s -w -buildid= -checklinkname=0"
48+
}
49+
50+
build_sing_box() {
51+
local tags="$1"
52+
local ldflags
53+
ldflags=$(get_ldflags)
54+
55+
echo "Building sing-box with tags: $tags"
56+
cd "$PROJECT_DIR"
57+
export GOTOOLCHAIN=local
58+
go install -v -trimpath -ldflags "$ldflags" -tags "$tags" ./cmd/sing-box
59+
}
60+
61+
install_binary() {
62+
local gopath
63+
gopath=$(go env GOPATH)
64+
echo "Installing binary to $INSTALL_BIN_PATH/$BINARY_NAME"
65+
sudo cp "${gopath}/bin/${BINARY_NAME}" "${INSTALL_BIN_PATH}/"
66+
}
67+
68+
setup_config() {
69+
echo "Setting up configuration"
70+
sudo mkdir -p "$INSTALL_CONFIG_PATH"
71+
if [ ! -f "$INSTALL_CONFIG_PATH/config.json" ]; then
72+
sudo cp "$PROJECT_DIR/release/config/config.json" "$INSTALL_CONFIG_PATH/config.json"
73+
echo "Default config installed to $INSTALL_CONFIG_PATH/config.json"
74+
else
75+
echo "Config already exists at $INSTALL_CONFIG_PATH/config.json (not overwriting)"
76+
fi
77+
}
78+
79+
setup_systemd() {
80+
echo "Setting up systemd service"
81+
sudo cp "$SCRIPT_DIR/sing-box.service" "$SYSTEMD_SERVICE_PATH/"
82+
sudo systemctl daemon-reload
83+
}
84+
85+
stop_service() {
86+
if systemctl is-active --quiet sing-box; then
87+
echo "Stopping sing-box service"
88+
sudo systemctl stop sing-box
89+
fi
90+
}
91+
92+
start_service() {
93+
echo "Starting sing-box service"
94+
sudo systemctl start sing-box
95+
}
96+
97+
restart_service() {
98+
echo "Restarting sing-box service"
99+
sudo systemctl restart sing-box
100+
}

release/local/debug.sh

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,25 @@
22

33
set -e -o pipefail
44

5-
if [ -d /usr/local/go ]; then
6-
export PATH="$PATH:/usr/local/go/bin"
7-
fi
5+
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
6+
source "$SCRIPT_DIR/common.sh"
87

9-
DIR=$(dirname "$0")
10-
PROJECT=$DIR/../..
8+
setup_environment
119

12-
pushd $PROJECT
10+
echo "Updating sing-box from git repository..."
11+
cd "$PROJECT_DIR"
1312
git fetch
1413
git reset FETCH_HEAD --hard
1514
git clean -fdx
16-
go install -v -trimpath -ldflags "-s -w -buildid=" -tags with_quic,with_acme,debug ./cmd/sing-box
17-
popd
1815

19-
sudo systemctl stop sing-box
20-
sudo cp $(go env GOPATH)/bin/sing-box /usr/local/bin/
21-
sudo systemctl start sing-box
16+
BUILD_TAGS=$(get_build_tags "debug")
17+
18+
build_sing_box "$BUILD_TAGS"
19+
20+
stop_service
21+
install_binary
22+
start_service
23+
24+
echo ""
25+
echo "Following service logs (Ctrl+C to exit)..."
2226
sudo journalctl -u sing-box --output cat -f

release/local/install.sh

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,18 @@
22

33
set -e -o pipefail
44

5-
if [ -d /usr/local/go ]; then
6-
export PATH="$PATH:/usr/local/go/bin"
7-
fi
5+
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
6+
source "$SCRIPT_DIR/common.sh"
87

9-
DIR=$(dirname "$0")
10-
PROJECT=$DIR/../..
8+
setup_environment
119

12-
pushd $PROJECT
13-
go install -v -trimpath -ldflags "-s -w -buildid=" -tags with_quic,with_wireguard,with_acme ./cmd/sing-box
14-
popd
10+
BUILD_TAGS=$(get_build_tags)
1511

16-
sudo cp $(go env GOPATH)/bin/sing-box /usr/local/bin/
17-
sudo mkdir -p /usr/local/etc/sing-box
18-
sudo cp $PROJECT/release/config/config.json /usr/local/etc/sing-box/config.json
19-
sudo cp $DIR/sing-box.service /etc/systemd/system
20-
sudo systemctl daemon-reload
12+
build_sing_box "$BUILD_TAGS"
13+
install_binary
14+
setup_config
15+
setup_systemd
16+
17+
echo ""
18+
echo "Installation complete!"
19+
echo "To enable and start the service, run: $SCRIPT_DIR/enable.sh"

release/local/reinstall.sh

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,18 @@
22

33
set -e -o pipefail
44

5-
if [ -d /usr/local/go ]; then
6-
export PATH="$PATH:/usr/local/go/bin"
7-
fi
5+
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
6+
source "$SCRIPT_DIR/common.sh"
87

9-
DIR=$(dirname "$0")
10-
PROJECT=$DIR/../..
8+
setup_environment
119

12-
pushd $PROJECT
13-
go install -v -trimpath -ldflags "-s -w -buildid=" -tags with_quic,with_wireguard,with_acme ./cmd/sing-box
14-
popd
10+
BUILD_TAGS=$(get_build_tags)
1511

16-
sudo systemctl stop sing-box
17-
sudo cp $(go env GOPATH)/bin/sing-box /usr/local/bin/
18-
sudo systemctl start sing-box
12+
build_sing_box "$BUILD_TAGS"
13+
14+
stop_service
15+
install_binary
16+
start_service
17+
18+
echo ""
19+
echo "Reinstallation complete!"

release/local/uninstall.sh

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,30 @@
11
#!/usr/bin/env bash
22

3-
sudo systemctl stop sing-box
4-
sudo rm -rf /var/lib/sing-box
5-
sudo rm -rf /usr/local/bin/sing-box
6-
sudo rm -rf /usr/local/etc/sing-box
7-
sudo rm -rf /etc/systemd/system/sing-box.service
3+
set -e -o pipefail
4+
5+
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
6+
source "$SCRIPT_DIR/common.sh"
7+
8+
echo "Uninstalling sing-box..."
9+
10+
if systemctl is-active --quiet sing-box 2>/dev/null; then
11+
echo "Stopping sing-box service..."
12+
sudo systemctl stop sing-box
13+
fi
14+
15+
if systemctl is-enabled --quiet sing-box 2>/dev/null; then
16+
echo "Disabling sing-box service..."
17+
sudo systemctl disable sing-box
18+
fi
19+
20+
echo "Removing files..."
21+
sudo rm -rf "$INSTALL_DATA_PATH"
22+
sudo rm -rf "$INSTALL_BIN_PATH/$BINARY_NAME"
23+
sudo rm -rf "$INSTALL_CONFIG_PATH"
24+
sudo rm -rf "$SYSTEMD_SERVICE_PATH/sing-box.service"
25+
26+
echo "Reloading systemd..."
827
sudo systemctl daemon-reload
28+
29+
echo ""
30+
echo "Uninstallation complete!"

release/local/update.sh

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22

33
set -e -o pipefail
44

5-
DIR=$(dirname "$0")
6-
PROJECT=$DIR/../..
5+
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
6+
source "$SCRIPT_DIR/common.sh"
77

8-
pushd $PROJECT
8+
echo "Updating sing-box from git repository..."
9+
cd "$PROJECT_DIR"
910
git fetch
1011
git reset FETCH_HEAD --hard
1112
git clean -fdx
12-
popd
1313

14-
$DIR/reinstall.sh
14+
echo ""
15+
echo "Running reinstall..."
16+
exec "$SCRIPT_DIR/reinstall.sh"

0 commit comments

Comments
 (0)