container: lib_fetch: implement Tor support

- Adds 'tor' option
- Uses `proxychains-ng`
- Expects docker-finance's Tor plugin
This commit is contained in:
2024-08-14 17:10:17 -07:00
parent 5b4bf6fbd0
commit 77a8b21893

View File

@@ -79,6 +79,10 @@ function lib_fetch::__parse_args()
(block)chain${global_arg_delim_2}<blockchain[${global_arg_delim_3}blockchain${global_arg_delim_1}subaccount]>
Tor (proxy):
tor${global_arg_delim_2}<{on|true}|{off|false}> (default 'off')
\e[32mExamples:\e[0m
\e[37;2m#\e[0m
@@ -136,6 +140,14 @@ function lib_fetch::__parse_args()
\e[37;2m# Fetch specific blockchain/subaccount/address for metamask\e[0m
$ $global_usage account${global_arg_delim_2}metamask chain${global_arg_delim_2}ethereum/phone:wallet-1/0x236ba53B56FEE4901cdac3170D17f827DF43E969
\e[37;2m# Fetch given blochchain-based subaccounts for current year, over the Tor network\e[0m
$ $global_usage account${global_arg_delim_2}metamask${global_arg_delim_3}ledger blockchain${global_arg_delim_2}ethereum${global_arg_delim_3}polygon${global_arg_delim_3}tezos${global_arg_delim_3}algorand tor${global_arg_delim_2}on
\e[32mNotes:\e[0m
- For all commands, you can proxy your fetch through Tor with the \`tor${global_arg_delim_2}on\` option
* IMPORTANT: client-side \`tor\` plugin must be started *prior* to fetch
"
#
@@ -151,6 +163,7 @@ function lib_fetch::__parse_args()
&& [[ ! "$_arg" =~ ^account[s]?${global_arg_delim_2} ]] \
&& [[ ! "$_arg" =~ ^(^block)?chain[s]?${global_arg_delim_2} ]] \
&& [[ ! "$_arg" =~ ^year[s]?${global_arg_delim_2} ]] \
&& [[ ! "$_arg" =~ ^tor${global_arg_delim_2} ]] \
&& lib_utils::die_usage "$_usage"
done
@@ -193,6 +206,11 @@ function lib_fetch::__parse_args()
local _arg_year="${_arg:${_len}}"
[ -z "$_arg_year" ] && lib_utils::die_usage "$_usage"
fi
if [[ "$_key" =~ ^tor$ ]]; then
local _arg_tor="${_arg:${_len}}"
[ -z "$_arg_tor" ] && lib_utils::die_usage "$_usage"
fi
done
#
@@ -247,6 +265,14 @@ function lib_fetch::__parse_args()
fi
fi
# Arg: tor
if [ ! -z "$_arg_tor" ]; then
# Need a valid arg
if [[ -z "$_arg_all" && -z "$_arg_price" && -z "$_arg_account" ]]; then
lib_utils::die_usage "$_usage"
fi
fi
#
# Test argument values, set globals
#
@@ -318,6 +344,12 @@ function lib_fetch::__parse_args()
global_arg_year="$(date +%Y)" # Set default current
declare -gr global_arg_year
fi
# Arg: tor
if [ ! -z "$_arg_tor" ]; then
[[ ! "$_arg_tor" =~ ^on$|^true$|^off$|^false$ ]] && lib_utils::die_usage "$_usage"
declare -gr global_arg_tor="$_arg_tor"
fi
}
function lib_fetch::__fetch()
@@ -752,15 +784,30 @@ function lib_fetch::__fetch_exec()
[ -z "$_subtype" ] && lib_utils::die_fatal
local _dir="${DOCKER_FINANCE_CONTAINER_REPO}/src/finance/lib/internal/fetch"
local _ifs="$IFS"
IFS=' '
# TODO: remove no-limit after internal fetching writes per-paginated instead of per-fetch
local _php=("/usr/bin/php" "-d" "memory_limit=\"-1\"" "-d" "include_path=\"/usr/local/lib:${_dir}\"")
lib_utils::print_debug "${_php[@]}"
#
# Setup fetch
#
"${_php[@]}" "${_dir}/fetch.php" "$_type" "$_subtype"
local _exec=()
[[ "$global_arg_tor" =~ ^on$|^true$ ]] && _exec+=("proxychains") # Wrap with proxychains
# Test for Tor plugin
if ! "${_exec[@]}" curl -s "https://check.torproject.org" &>/dev/null; then
lib_utils::die_fatal "Tor plugin not started (or needs restart)"
fi
# TODO: remove no-limit after internal fetching writes per-paginated instead of per-fetch
_exec+=("/usr/bin/php" "-d" "memory_limit=\"-1\"" "-d" "include_path=\"/usr/local/lib:${_dir}\"")
lib_utils::print_debug "${_exec[@]}"
#
# Execute fetch
#
"${_exec[@]}" "${_dir}/fetch.php" "$_type" "$_subtype"
IFS="$_ifs"
}