basicswap-bash/bsx/enabletor.sh

99 lines
2.9 KiB
Bash
Raw Normal View History

2024-01-30 03:38:18 +00:00
#!/bin/bash
2024-02-25 13:28:40 +00:00
# Colors
2024-11-09 03:03:53 +00:00
red="printf \e[31;1m"
green="printf \e[32;1m"
nocolor="printf \e[0m"
2024-02-25 13:28:40 +00:00
# Detect Operating system
INSTALL=""
UPDATE=""
2024-11-09 03:03:53 +00:00
INIT_TOR=""
SYSTEMD_TOR="sudo systemctl restart tor"
check_tails() {
if [[ $USER == amnesia ]]; then
$green"\n\nDetected Tails";$nocolor
TAILS=1
else
$green"\n\nDetected Debian";$nocolor
fi
}
2024-02-25 13:28:40 +00:00
detect_os_arch() {
2024-11-09 03:03:53 +00:00
if [[ $(uname -s) = "Darwin" ]]; then
# MacOS
export MACOS=1
if type -p brew > /dev/null; then
$green"Homebrew is installed\n";$nc
INSTALL="brew install"
else
$green"Installing Homebrew\n";$nc
INSTALL="curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh | /bin/bash && brew install"
fi
INIT_TOR="pkill tor && tor"
$green"\n\nDetected MacOS";$nocolor
elif type -p apt > /dev/null; then
check_tails
2024-02-25 13:28:40 +00:00
# Debian / Ubuntu / Mint
2024-11-09 03:03:53 +00:00
INSTALL="sudo apt install"
2024-02-25 13:28:40 +00:00
UPDATE="sudo apt update"
2024-11-09 03:03:53 +00:00
INIT_TOR=$SYSTEMD_TOR
elif type -p dnf > /dev/null; then
2024-02-25 13:28:40 +00:00
# Fedora
2024-11-09 03:03:53 +00:00
INSTALL="sudo dnf install"
2024-02-25 13:28:40 +00:00
UPDATE="sudo dnf check-update"
2024-11-09 03:03:53 +00:00
INIT_TOR=$SYSTEMD_TOR
$green"\n\nDetected Fedora";$nocolor
elif type -p pacman > /dev/null; then
2024-02-25 13:28:40 +00:00
# Arch Linux
INSTALL="sudo pacman -S"
UPDATE="sudo pacman -Syu"
2024-11-09 03:03:53 +00:00
INIT_TOR=$SYSTEMD_TOR
$green"\n\nDetected Arch Linux";$nocolor
2024-02-25 13:28:40 +00:00
else
2024-11-09 03:03:53 +00:00
$red"\nFailed to detect OS. Unsupported or unknown distribution.\nInstall Failed.\n";$nocolor
2024-02-25 13:28:40 +00:00
exit
fi
}
detect_os_arch
2024-02-05 02:03:49 +00:00
# Check for Tor installation
2024-11-09 03:03:53 +00:00
if type -p tor > /dev/null; then
printf "\nTor is already installed :)\n"
2024-02-05 02:03:49 +00:00
else
# Install and configure tor
echo "Installing Tor..."
2024-02-25 13:28:40 +00:00
$UPDATE
$INSTALL tor
2024-02-05 02:03:49 +00:00
fi
2024-06-05 18:36:36 +00:00
# Create HashedControlPassword
2024-11-30 16:25:40 +00:00
torcontrolpass=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 24 | head -n 1)
2024-02-05 02:03:49 +00:00
# Edit /etc/tor/torrc
2024-01-30 03:38:18 +00:00
torhashedpass=$(tor --hash-password $torcontrolpass)
2024-02-05 02:03:49 +00:00
enabledcontrol=$(echo "ControlPort 9051")
2024-06-04 02:20:34 +00:00
skipcontrol=$(sudo grep -x "$enabledcontrol" /etc/tor/torrc)
echo "Check torrc for enabled ControlPort"
2024-02-05 02:03:49 +00:00
if [[ $skipcontrol ]]; then
# Use Existing enabled ControlPort and append HashedControlPassword
2024-11-09 03:03:53 +00:00
printf "# Added by basicswap-bash\nHashedControlPassword $torhashedpass\n" | sudo tee -a /etc/tor/torrc
2024-02-05 02:03:49 +00:00
else
2024-11-09 03:03:53 +00:00
printf "# Added by basicswap-bash\n$enabledcontrol\nHashedControlPassword $torhashedpass\n" | sudo tee -a /etc/tor/torrc
2024-02-05 02:03:49 +00:00
fi
2024-01-30 03:38:18 +00:00
# Restart tor to apply
2024-02-25 13:28:40 +00:00
$INIT_TOR
2024-02-05 02:03:49 +00:00
echo "Waiting for Tor... 5sec" && sleep 5
2024-01-30 03:38:18 +00:00
# lol are we there yet?
TOR_PROXY_HOST=127.0.0.1
basicswap-prepare --datadir=$SWAP_DATADIR --enabletor
2024-02-05 02:03:49 +00:00
# Workaround: Replace torpassword in various config files
2024-01-30 03:38:18 +00:00
oldtorpass=$(cat $SWAP_DATADIR/basicswap.json | jq -r .tor_control_password)
sed -i "s/$oldtorpass/$torcontrolpass/" $SWAP_DATADIR/*/*.conf $SWAP_DATADIR/basicswap.json
2024-02-05 02:03:49 +00:00
# Fix: localhost binding for btc/ltc/part (etc) configs
2024-01-30 03:38:18 +00:00
sed -i -z "s/\nbind=0.0.0.0/\nbind=127.0.0.1/" $SWAP_DATADIR/*/*.conf