#!/bin/bash # Background: # https://medium.com/@lopp/how-to-run-bitcoin-as-a-tor-hidden-service-on-ubuntu-cff52d543756 # https://bitcoin.stackexchange.com/questions/70069/how-can-i-setup-bitcoin-to-be-anonymous-with-tor # https://github.com/lightningnetwork/lnd/blob/master/docs/configuring_tor.md # INFO # -------------------- # basic install of Tor is done by the build script now .. on/off will just switch service on/off # also thats where the sources are set and the preparation is done # command info if [ $# -eq 0 ] || [ "$1" = "-h" ] || [ "$1" = "-help" ]; then echo "script to switch TOR on or off" echo "internet.tor.sh [status|on|off|btcconf-on|btcconf-off|lndconf-on|update]" exit 1 fi torrc="/etc/tor/torrc" activateBitcoinOverTOR() { echo "*** Changing ${network} Config ***" btcExists=$(sudo ls /home/bitcoin/.${network}/${network}.conf | grep -c "${network}.conf") if [ ${btcExists} -gt 0 ]; then # make sure all is turned off and removed and then activate fresh (so that also old settings get removed) deactivateBitcoinOverTOR echo "Addding TOR config ..." sudo chmod 777 /home/bitcoin/.${network}/${network}.conf echo "onlynet=onion" >> /home/bitcoin/.${network}/${network}.conf echo "proxy=127.0.0.1:9050" >> /home/bitcoin/.${network}/${network}.conf echo "main.bind=127.0.0.1" >> /home/bitcoin/.${network}/${network}.conf echo "test.bind=127.0.0.1" >> /home/bitcoin/.${network}/${network}.conf echo "dnsseed=0" >> /home/bitcoin/.${network}/${network}.conf echo "dns=0" >> /home/bitcoin/.${network}/${network}.conf PASSWORD_B=$(sudo cat /mnt/hdd/${network}/${network}.conf | grep rpcpassword | cut -c 13-) echo "torpassword=$PASSWORD_B" >> /home/bitcoin/.${network}/${network}.conf if [ "${network}" = "bitcoin" ]; then # adding some bitcoin onion nodes to connect to to make connection easier echo "main.addnode=fno4aakpl6sg6y47.onion" >> /home/bitcoin/.${network}/${network}.conf echo "main.addnode=toguvy5upyuctudx.onion" >> /home/bitcoin/.${network}/${network}.conf echo "main.addnode=ndndword5lpb7eex.onion" >> /home/bitcoin/.${network}/${network}.conf echo "main.addnode=6m2iqgnqjxh7ulyk.onion" >> /home/bitcoin/.${network}/${network}.conf echo "main.addnode=5tuxetn7tar3q5kp.onion" >> /home/bitcoin/.${network}/${network}.conf echo "main.addnode=juo4oneckybinerq.onion" >> /home/bitcoin/.${network}/${network}.conf fi sudo chmod 444 /home/bitcoin/.${network}/${network}.conf # copy new bitcoin.conf to admin user for cli access sudo cp /home/bitcoin/.${network}/${network}.conf /home/admin/.${network}/${network}.conf sudo chown admin:admin /home/admin/.${network}/${network}.conf else echo "BTC config does not found (yet) - try with 'internet.tor.sh btcconf-on' again later" fi } deactivateBitcoinOverTOR() { # always make sure also to remove old settings sudo sed -i "s/^onlynet=.*//g" /home/bitcoin/.${network}/${network}.conf sudo sed -i "s/^torpassword=.*//g" /home/bitcoin/.${network}/${network}.conf sudo sed -i "s/^main.addnode=.*//g" /home/bitcoin/.${network}/${network}.conf sudo sed -i "s/^test.addnode=.*//g" /home/bitcoin/.${network}/${network}.conf sudo sed -i "s/^proxy=.*//g" /home/bitcoin/.${network}/${network}.conf sudo sed -i "s/^main.bind=.*//g" /home/bitcoin/.${network}/${network}.conf sudo sed -i "s/^test.bind=.*//g" /home/bitcoin/.${network}/${network}.conf sudo sed -i "s/^dnsseed=.*//g" /home/bitcoin/.${network}/${network}.conf sudo sed -i "s/^dns=.*//g" /home/bitcoin/.${network}/${network}.conf sudo sed -i '/^ *$/d' /home/bitcoin/.${network}/${network}.conf sudo cp /home/bitcoin/.${network}/${network}.conf /home/admin/.${network}/${network}.conf sudo chown admin:admin /home/admin/.${network}/${network}.conf } activateLndOverTOR() { echo "*** Putting LND behind TOR ***" lndExists=$(sudo ls /etc/systemd/system/lnd.service | grep -c "lnd.service") if [ ${lndExists} -gt 0 ]; then # modify LND service echo "Make sure LND is disabled" sudo systemctl disable lnd 2>/dev/null echo "editing /etc/systemd/system/lnd.service" sudo sed -i "s/^ExecStart=\/usr\/local\/bin\/lnd.*/ExecStart=\/usr\/local\/bin\/lnd --tor\.active --tor\.streamisolation --tor\.v3 --listen=127\.0\.0\.1\:9735 \${lndExtraParameter}/g" /etc/systemd/system/lnd.service # check if "tor.password" exists valueExists=$(sudo cat /mnt/hdd/lnd/lnd.conf | grep -c 'tor.password=') if [ ${valueExists} -eq 0 ]; then echo "Adding tor config defaults to /mnt/hdd/lnd/lnd.conf" PASSWORD_B=$(sudo cat /mnt/hdd/${network}/${network}.conf | grep rpcpassword | cut -c 13-) sudo -u bitcoin tee -a /mnt/hdd/lnd/lnd.conf >/dev/null <> /mnt/hdd/raspiblitz.conf fi # location of TOR config # make sure /etc/tor exists sudo mkdir /etc/tor 2>/dev/null if [ "$1" != "update" ]; then # stop services (if running) echo "making sure services are not running" sudo systemctl stop lnd 2>/dev/null sudo systemctl stop ${network}d 2>/dev/null sudo systemctl stop tor@default 2>/dev/null fi # switch on if [ "$1" = "1" ] || [ "$1" = "on" ]; then echo "# switching the TOR ON" # make sure the network was set (by sourcing raspiblitz.conf) if [ ${#network} -eq 0 ]; then echo "!! FAIL - unknwon network due to missing /mnt/hdd/raspiblitz.conf" echo "# switching Tor config on for RaspiBlitz services is just possible after basic hdd/ssd setup" echo "# but with new 'Tor by default' basic Tor socks will already be available from the start" exit 1 fi # setting value in raspi blitz config sudo sed -i "s/^runBehindTor=.*/runBehindTor=on/g" /mnt/hdd/raspiblitz.conf # check if TOR was already installed and is funtional echo "" echo "*** Check if TOR service is functional ***" torRunning=$(curl --connect-timeout 10 --socks5-hostname 127.0.0.1:9050 https://check.torproject.org 2>/dev/null | grep "Congratulations. This browser is configured to use Tor." -c) if [ ${torRunning} -gt 0 ]; then clear echo "You are all good - TOR is already running." echo "" exit 0 else echo "TOR not running ... proceed with switching to TOR." echo "" fi # install package just in case it was deinstalled packageInstalled=$(dpkg -s tor-arm | grep -c 'Status: install ok') if [ ${packageInstalled} -eq 0 ]; then sudo apt install tor tor-arm torsocks -y fi # create tor data directory if it not exist if [ ! -d "/mnt/hdd/tor" ]; then echo "# - creating tor data directory" sudo mkdir -p /mnt/hdd/tor sudo mkdir -p /mnt/hdd/tor/sys else echo "# - tor data directory exists" fi # make sure its the correct owner sudo chmod -R 700 /mnt/hdd/tor sudo chown -R debian-tor:debian-tor /mnt/hdd/tor # create tor config .. if not exists or is old isTorConfigOK=$(sudo cat /etc/tor/torrc 2>/dev/null | grep -c "BITCOIN") if [ ${isTorConfigOK} -eq 0 ]; then echo "# - updating Tor config ${torrc}" PASSWORD_B=$(sudo cat /mnt/hdd/${network}/${network}.conf | grep rpcpassword | cut -c 13-) HASHED_PASSWORD=$(sudo -u debian-tor tor --hash-password "$PASSWORD_B") cat > ./torrc <