Skip to content

Instantly share code, notes, and snippets.

@hangj
Last active September 2, 2022 07:40
Show Gist options
  • Save hangj/5cb1d255e1451a118defc83864ef428c to your computer and use it in GitHub Desktop.
Save hangj/5cb1d255e1451a118defc83864ef428c to your computer and use it in GitHub Desktop.
setup shadowsocks server for my vps
#!/usr/bin/env bash
# change the working directory to where this file is located
full_file_path=`realpath $0`
absolute_dir=`dirname $full_file_path`
cd $absolute_dir
# crate new user if not exists
username="bot"
userpassword="password123456"
# shadowsocks server config
server_port="6666"
password="123456"
method="chacha20-ietf-poly1305"
if ! id -u $username >/dev/null 2>&1; then
sudo useradd -m -s /bin/bash -U -p `openssl passwd -1 $userpassword` $username
echo "$username ALL=(ALL:ALL) NOPASSWD:ALL" | sudo bash -c "cat > /etc/sudoers.d/$username"
sudo chmod 0440 /etc/sudoers.d/$username
sudo -u $username mkdir -p /home/$username/.ssh
sudo cp ~/.ssh/authorized_keys /home/$username/.ssh/authorized_keys
sudo chown $username:$username /home/$username/.ssh/authorized_keys
sudo sed -i "s/.*PasswordAuthentication.*yes.*/PasswordAuthentication no/" /etc/ssh/sshd_config
sudo systemctl restart sshd
fi
if ! which snap >/dev/null 2>&1; then
sudo apt update
sudo apt -y install build-essential
sudo apt install snapd -y
fi
if ! snap services shadowsocks-rust.ssserver-daemon >/dev/null 2>&1; then
sudo snap install shadowsocks-rust
sudo mkdir -p /var/snap/shadowsocks-rust/common/etc/shadowsocks-rust
fi
json=`cat << EndOfMessage
{
"server": "0.0.0.0",
"server_port": $server_port,
"password": "$password",
"method": "$method",
"mode":"tcp_and_udp"
}
EndOfMessage
`
echo $json | sudo bash -c "cat > /var/snap/shadowsocks-rust/common/etc/shadowsocks-rust/config.json"
if [ `snap services shadowsocks-rust.ssserver-daemon | awk 'NR > 1 {print $3}'` != "active" ]; then
sudo snap start --enable shadowsocks-rust.ssserver-daemon
# sudo snap logs shadowsocks-rust
fi
sleep 2
if [ `snap services shadowsocks-rust.ssserver-daemon | awk 'NR > 1 {print $3}'` = "active" ]; then
exit 0
fi
############## If the snap does not work, we will install our own shadowsocks-rust ##############
printf "snap start shadowsocks-rust.ssserver-daemon failed.\n"
while true; do
read -p "Do you wish to try cargo install shadowsocks-rust? " yn
case $yn in
[Yy]* ) break;;
[Nn]* ) exit;;
* ) echo "Please answer yes or no.";;
esac
done
if ! which cargo >/dev/null 2>&1; then
sudo apt update
sudo apt -y install build-essential # binutils-dev libunwind-dev libblocksruntime-dev liblzma-dev
# https://unix.stackexchange.com/questions/491090/how-to-set-parameters-when-pipe-bash-script-to-bash
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y
source "$HOME/.cargo/env"
fi
if ! which ssserver >/dev/null 2>&1; then
cargo install shadowsocks-rust
fi
ssserver -d -c /var/snap/shadowsocks-rust/common/etc/shadowsocks-rust/config.json
# EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment