Last active
July 29, 2020 13:41
-
-
Save aduzsardi/80c9229b3340d18bb8ac570b6103a953 to your computer and use it in GitHub Desktop.
inlets bash helper function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# add this to your .bashrc | |
# call it with httproxy http://proxied-url.tld | |
httproxy() { | |
local bold='\e[1m' reset='\e[0m' bright='\e[37m' | |
local info='\e[96m' warn='\e[93m' error='\e[91m' | |
local helpmsg=$(cat <<HELP | |
${info}Usage:${reset} | |
${bright}${FUNCNAME[0]} <proxied-url>${reset} | |
HELP | |
) | |
local token='<-- INLETS TOKEN -->' | |
local wss_url='wss://<-- INLETS HOST -->' | |
local proxy_addr="$(head -c 50 /dev/urandom | tr -cd [:alnum:] | tr '[A-Z]' '[a-z]').<--INLETS DOMAIN-->" | |
local proxied_addr="$1" | |
local opts="client --remote=${wss_url} --token=${token}" | |
if ! type inlets &> /dev/null; then | |
printf "\n\n%b\n\n\t%b\n\n" "${bold}${bright}inlets${reset} ${warn}is not installed , install it with${reset}" \ | |
"${info}curl -sLS https://get.inlets.dev | sudo sh${reset}" | |
else | |
if [[ $# -ne 1 ]]; then | |
printf "\n\n\t%b\n\n" "${bold}${error}Error:${reset} ${error}You need to provide exactly one argument${reset}" | |
printf "%b\n\n" "${helpmsg}" | |
elif [[ $1 == "-h" ]] || [[ $1 == "--help" ]]; then | |
printf "\n\t%b\n\n" "${bright}${FUNCNAME[0]}${reset} — Create a dynamic externally accesible url (reverse proxy)" | |
printf "%b\n\n" "${helpmsg}" | |
else | |
local sre='.+Upstream:\s([a-z0-9]+\.[a-z0-9]+\.[a-z0-9]{2,3})\s+=>\s+(.+)' | |
local srpl='\n\nHTTP Proxy URL:\thttp://\1 \nHTTPS Proxy URL:\thttps://\1\nProxied Service:\t\2' | |
local quitxt=$(printf "${info}Press Ctrl+C to quit${reset}") | |
inlets ${opts} --upstream="${proxy_addr}"="${proxied_addr}" |& \ | |
sed -nr "s#${sre}#${srpl}\n\n${quitxt}#p" | |
fi | |
fi | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment