Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Install trojan-go
#!/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-go
VERSION=$(curl -fsSL https://api.github.com/repos/p4gefau1t/trojan-go/releases/latest | grep tag_name | sed -E 's/.*"v(.*)".*/\1/')
TARBALL="$NAME-linux-amd64.zip"
DOWNLOADURL="https://github.com/p4gefau1t/$NAME/releases/download/v$VERSION/$TARBALL"
TMPDIR="$(mktemp -d)"
SYSTEMDPREFIX=/etc/systemd/system
BINARYPATH="/usr/bin/$NAME"
CONFIGPATH="/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...
unzip "$TARBALL"
echo Installing $NAME $VERSION to $BINARYPATH...
install -Dm755 "$NAME" "$BINARYPATH"
install -Dm644 geoip.dat "/etc/$NAME/geoip.dat"
install -Dm644 geosite.dat "/etc/$NAME/geosite.dat"
echo Installing $NAME server config to $CONFIGPATH...
if ! [[ -f "$CONFIGPATH" ]] || prompt "The server config already exists in $CONFIGPATH, overwrite?"; then
install -Dm644 example/server.json "$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
install -Dm644 example/$NAME.service "$SYSTEMDPATH"
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!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment