-
Notifications
You must be signed in to change notification settings - Fork 108
/
trojan-quickstart.sh
executable file
·88 lines (72 loc) · 2.35 KB
/
trojan-quickstart.sh
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
#!/bin/bash
set -euo pipefail
function prompt() {
while true; do
read -p "$1 [y/N] " yn
case $yn in
[Yy] ) return 0;;
[Nn]|"" ) return 1;;
esac
done
}
if [[ $(id -u) != 0 ]]; then
echo Please run this script as root.
exit 1
fi
if [[ $(uname -m 2> /dev/null) != x86_64 ]]; then
echo Please run this script on x86_64 machine.
exit 1
fi
NAME=trojan
VERSION=$(curl -fsSL https://api.github.com/repos/trojan-gfw/trojan/releases/latest | grep tag_name | sed -E 's/.*"v(.*)".*/\1/')
TARBALL="$NAME-$VERSION-linux-amd64.tar.xz"
DOWNLOADURL="https://github.com/trojan-gfw/$NAME/releases/download/v$VERSION/$TARBALL"
TMPDIR="$(mktemp -d)"
INSTALLPREFIX=/usr/local
SYSTEMDPREFIX=/etc/systemd/system
BINARYPATH="$INSTALLPREFIX/bin/$NAME"
CONFIGPATH="$INSTALLPREFIX/etc/$NAME/config.json"
SYSTEMDPATH="$SYSTEMDPREFIX/$NAME.service"
echo Entering temp directory $TMPDIR...
cd "$TMPDIR"
echo Downloading $NAME $VERSION...
curl -LO --progress-bar "$DOWNLOADURL" || wget -q --show-progress "$DOWNLOADURL"
echo Unpacking $NAME $VERSION...
tar xf "$TARBALL"
cd "$NAME"
echo Installing $NAME $VERSION to $BINARYPATH...
install -Dm755 "$NAME" "$BINARYPATH"
echo Installing $NAME server config to $CONFIGPATH...
if ! [[ -f "$CONFIGPATH" ]] || prompt "The server config already exists in $CONFIGPATH, overwrite?"; then
install -Dm644 examples/server.json-example "$CONFIGPATH"
else
echo Skipping installing $NAME server config...
fi
if [[ -d "$SYSTEMDPREFIX" ]]; then
echo Installing $NAME systemd service to $SYSTEMDPATH...
if ! [[ -f "$SYSTEMDPATH" ]] || prompt "The systemd service already exists in $SYSTEMDPATH, overwrite?"; then
cat > "$SYSTEMDPATH" << EOF
[Unit]
Description=$NAME
Documentation=https://trojan-gfw.github.io/$NAME/config https://trojan-gfw.github.io/$NAME/
After=network.target network-online.target nss-lookup.target mysql.service mariadb.service mysqld.service
[Service]
Type=simple
StandardError=journal
ExecStart="$BINARYPATH" "$CONFIGPATH"
ExecReload=/bin/kill -HUP \$MAINPID
LimitNOFILE=51200
Restart=on-failure
RestartSec=1s
[Install]
WantedBy=multi-user.target
EOF
echo Reloading systemd daemon...
systemctl daemon-reload
else
echo Skipping installing $NAME systemd service...
fi
fi
echo Deleting temp directory $TMPDIR...
rm -rf "$TMPDIR"
echo Done!