Make fallbacks opt-in

This commit is contained in:
Dennis Reimann 2020-07-08 13:35:18 +02:00
parent 86730524a3
commit 6e2b71acab
No known key found for this signature in database
GPG key ID: 5009E1797F03F8D0
2 changed files with 28 additions and 22 deletions

View file

@ -1,11 +1,17 @@
# Server port
DISPLAY_SERVER_PORT=3030
# Bitcoin RPC credentials  omit these setting to use blockchain.info as a fallback
DISPLAY_BITCOIN_RPC_USER="bitcoin-rpc-user"
DISPLAY_BITCOIN_RPC_PASS="bitcoin-rpc-password"
# Bitcoin RPC credentials for getting the blockcount.
# Omit these setting to use blockchain.info as a fallback.
# DISPLAY_BITCOIN_RPC_USER="bitcoin-rpc-user"
# DISPLAY_BITCOIN_RPC_PASS="bitcoin-rpc-password"
# BTCPay Settings for rate fetching  omit these setting to use Bitstamp as a fallback
BTCPAY_HOST="https://my.btcpayserver.com"
# BTCPay Settings for rate fetching.
# Generate API via Store > Access Tokens > Legacy API Keys
BTCPAY_API_TOKEN="myBtcPayLegacyApiKey"
# Omit these setting to use Kraken as a fallback.
# BTCPAY_HOST="https://my.btcpayserver.com"
# BTCPAY_API_TOKEN="myBtcPayLegacyApiKey"
# Shall the fallbacks be used?
DISPLAY_FALLBACK_BLOCK=true
DISPLAY_FALLBACK_RATES=true

View file

@ -4,39 +4,39 @@ dir=$(cd `dirname $0` && pwd)
envFile=$(ls -l $dir/.env | awk '{print $NF}')
now=$(date +'%F %R')
# load config
# Load config
source $envFile
# use clearnet by default, check for Tor and use it if working
# Use clearnet by default, check for Tor and use it if working
tor=""
torCheck=$(torsocks curl -s https://check.torproject.org/ 2>/dev/null | grep -c "Congratulations. This browser is configured to use Tor.")
if [ ${torCheck} -gt 0 ]; then
tor="torsocks"
fi
# Blockchain
# Get block height using RPC connection or Blockchain.info as fallback
if [[ "${DISPLAY_BITCOIN_RPC_USER}" && "${DISPLAY_BITCOIN_RPC_PASS}" ]]; then
blockcount=$(bitcoin-cli -rpcuser=$DISPLAY_BITCOIN_RPC_USER -rpcpassword=$DISPLAY_BITCOIN_RPC_PASS getblockcount 2> /dev/null || echo "null")
blockcount=$(bitcoin-cli -rpcuser=$DISPLAY_BITCOIN_RPC_USER -rpcpassword=$DISPLAY_BITCOIN_RPC_PASS getblockcount 2> /dev/null)
fi
if [[ -z ${blockcount} ]]; then
blockcount=$($tor curl -s -f https://blockchain.info/q/getblockcount 2> /dev/null || echo "null")
if [[ "${blockcount}" = "" && ${DISPLAY_FALLBACK_BLOCK} = true ]]; then
blockcount=$($tor curl -s -f https://blockchain.info/q/getblockcount 2> /dev/null)
fi
# Fetch rates using custom BTCPay or Kraken as fallback
if [[ "${BTCPAY_API_TOKEN}" && "${BTCPAY_HOST}" ]]; then
usdrate=$($tor curl -s -f -H "Authorization: Basic $BTCPAY_API_TOKEN" $BTCPAY_HOST/rates/BTC/USD | jq -r '.data.rate // "[]"')
eurrate=$($tor curl -s -f -H "Authorization: Basic $BTCPAY_API_TOKEN" $BTCPAY_HOST/rates/BTC/EUR | jq -r '.data.rate // "[]"')
usdrate=$($tor curl -s -f -H "Authorization: Basic $BTCPAY_API_TOKEN" $BTCPAY_HOST/rates/BTC/USD | jq -r '.data.rate')
eurrate=$($tor curl -s -f -H "Authorization: Basic $BTCPAY_API_TOKEN" $BTCPAY_HOST/rates/BTC/EUR | jq -r '.data.rate')
fi
if [[ "${usdrate}" = "" && ${DISPLAY_FALLBACK_RATES} = true ]]; then
usdrate=$($tor curl -s -f https://api.kraken.com/0/public/Ticker\?pair=XBTUSD | jq -r '.result.XXBTZUSD.c[0] // "[]"')
fi
if [[ "${eurrate}" = "" && ${DISPLAY_FALLBACK_RATES} = true ]]; then
eurrate=$($tor curl -s -f https://api.kraken.com/0/public/Ticker\?pair=XBTEUR | jq -r '.result.XXBTZEUR.c[0] // "[]"')
fi
if [[ -z ${usdrate} ]]; then
usdrate=$($tor curl -s -f https://api.kraken.com/0/public/Ticker?pair=XBTUSD | jq -r '.result.XXBTZUSD.c[0] // "[]"')
if [[ "${usdrate}" && "${eurrate}" ]]; then
rates=$(jo -p -a $(jo rate="$usdrate" code="USD") $(jo rate="$eurrate" code="EUR"))
fi
if [[ -z ${eurrate} ]]; then
eurrate=$($tor curl -s -f https://api.kraken.com/0/public/Ticker?pair=XBTEUR | jq -r '.result.XXBTZEUR.c[0] // "[]"')
fi
rates=$(jo -p -a $(jo rate="$usdrate" code="USD") $(jo rate="$eurrate" code="EUR"))
# Bitcoin Quotes
quote=$( $tor curl -s -f https://www.bitcoin-quotes.com/quotes/random.json 2> /dev/null || echo "null")