Skip to content

Instantly share code, notes, and snippets.

@theefer
Last active October 14, 2015 16:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save theefer/4b6be17a6344fed5b6bf to your computer and use it in GitHub Desktop.
Save theefer/4b6be17a6344fed5b6bf to your computer and use it in GitHub Desktop.
#!/bin/bash
#
# Open a URL in Chrome after SSH'ing to the URL's host and setting up a SOCKS proxy.
if [ $# -ne 1 ]
then
echo "usage: browse-with-proxy.sh <URL>"
echo
echo "Opens the given URL in a browser using an SSH tunnel to the host as a proxy"
echo
exit 1
fi
DEST_URL=$1
# for now, extract host from DEST_URL
BASTION_HOST=`echo $DEST_URL | sed -E 's,^(https?://)?([^:/]+)(.*)$,\2,'`
# random local port
LOCAL_PORT=$((RANDOM%10000+10000))
# try Linux path first
CHROME_COMMAND="/usr/bin/chromium-browser"
# fallback to OS X path
if [ ! -x "$CHROME_COMMAND" ]
then
CHROME_COMMAND="/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"
fi
CHROME_PROFILE_DIR=~/.config/chromium-socks
# Open SSH tunnel in the background
ssh -N -D $LOCAL_PORT ubuntu@$BASTION_HOST &
SSH_PID=$!
echo Opened SSH tunnel with pid $SSH_PID on port $LOCAL_PORT
trap finish SIGINT EXIT
function finish {
echo Kill SSH
kill $SSH_PID
}
sleep 1 # the proxy seems to need some time to warm up
"$CHROME_COMMAND" --proxy-server="socks5://localhost:$LOCAL_PORT" \
--host-resolver-rules="MAP * 0.0.0.0 , EXCLUDE localhost" \
--user-data-dir="$CHROME_PROFILE_DIR" "$DEST_URL"
#!/bin/bash
#
# Inspired from
# https://github.com/guardian/content-api/blob/master/elasticsearch/scripts/open-HEAD-plugin.sh
if [ $# -lt 1 ]
then
echo "usage: filch.sh <host selector>"
echo
echo "example: filch.sh media prod elasticsearch"
echo
echo "Opens one host matched by the selector in a Chrome browser, accessed via an SSH tunnelled SOCKS proxy"
echo
exit 1
fi
selector="$*"
this_dir=$(cd $(dirname "${BASH_SOURCE[0]}") && pwd -P)
es_host=$(marauder -s $* | ruby -e 'puts STDIN.readlines.shuffle' | head -1)
# try to guess what port and path to open
if [[ " $selector " == *" elastic "* ]] || [[ " $selector " == *" elasticsearch "* ]]
then
# Elasticsearch HEAD plugin
url=http://${es_host}:9200/_plugin/head/
else
# Any other play app
url=http://${es_host}:9000
fi
$this_dir/browse-with-proxy.sh $url
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment