From efc49f30352636d72095703058bf4983f012235f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karel=20Ko=C4=8D=C3=AD?= Date: Sat, 9 Feb 2019 15:37:32 +0100 Subject: surf-menu; drop server detection and add SOCKS --- surf-menu | 88 ++++++++++++++++++++------------------------------------------- 1 file changed, 27 insertions(+), 61 deletions(-) diff --git a/surf-menu b/surf-menu index 8084d05..7b31f13 100755 --- a/surf-menu +++ b/surf-menu @@ -7,64 +7,19 @@ set -e export GDK_BACKEND=x11 BOOKMARDS=~/notes/bookmarks.md -# TODO history -#HISTORY=~/notes/.surf_history +#HISTORY=~/notes/.surf_history # TODO history +SOCKS_PROXY="socks://localhost:8123" -set_uri() { - if [ -n "$XID" ]; then - xprop -id "$XID" -f "_SURF_GO" 8s -set "_SURF_GO" "$1" +open_uri() { + if [ -n "$xid" ]; then + xprop -id "$xid" -f "_SURF_GO" 8s -set "_SURF_GO" "$1" else surf "$1" & fi } -run() { - if echo "$1" | grep -qE '^\?'; then # We do search on duckduckgo - - set_uri "https://duckduckgo.com/?q=${L#?}&t=surf&kk=-1&ia=web" - - elif echo "$1" | grep -qE '^~?/'; then # This is local path - - set_uri "${1/#\~/$HOME}" - - elif echo "$1" | grep -qE '^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+'; then # This ipv4 address - - set_uri "$1" - - # TODO ipv6 address - - else # We follow address - - LPROTOCOL="$(echo "$1" | sed -n 's#^\([^:]*\)://.*#\1#p')" - LHOST="$(echo "$1" | sed -n 's#^[^:]*://##;s#^\([^/]\+\)/\?.*#\1#p')" - LPATH="$(echo "$1" | sed 's#^[^:]*://##;s#^[^/]\+/\?##')" - - if ! getent hosts "$LHOST" >/dev/null; then - if getent hosts "$LHOST.cz" >/dev/null; then - LHOST="$LHOST.cz" - elif getent hosts "$LHOST.org" >/dev/null; then - LHOST="$LHOST.org" - elif getent hosts "$LHOST.com" >/dev/null; then - LHOST="$LHOST.com" - fi - # TODO what to do when we can't expand it? - fi - # Decide on protocol (if connection to 443 is not possible then use http otherwise https) - if [ -z "$LPROTOCOL" ]; then # We already have protocol (given explicitly) - if nc -z -w1 "$LHOST" 443 2>/dev/null; then - LPROTOCOL="https" - else - LPROTOCOL="http" - fi - fi - - set_uri "$LPROTOCOL://$LHOST/$LPATH" - - fi -} - -XID="" -ADDCH="" +xid="" +addch="" while [ $# -gt 0 ]; do case "$1" in @@ -77,17 +32,24 @@ while [ $# -gt 0 ]; do echo " Print this help text." echo " --xid XID" echo " Instead of opening new surf instance set url to existing one" + echo " --socks" + echo " Use SOCKS proxy" exit ;; --xid) - XID="$2" + xid="$2" shift ;; -a|--add) shift - ADDCH="$ADDCH + addch="$addch $1" ;; + --socks) + export ALL_PROXY="$SOCKS_PROXY" + export http_proxy="$SOCKS_PROXY" + export https_proxy="$SOCKS_PROXY" + ;; *) echo "Unknown option $1" >&2 exit 1 @@ -96,16 +58,20 @@ $1" shift done -# If XID is given then as first one add current uri -if [ -n "$XID" ]; then - ADDCH="$(xprop -id "$XID" _SURF_URI | sed -n 's/.*(STRING) = "\(.*\)"/\1/p')" - DMENU_ARGS="-w $XID -p Go:" +# If xid is given then as first one add current uri +if [ -n "$xid" ]; then + addch="$(xprop -id "$xid" _SURF_URI | sed -n 's/.*(STRING) = "\(.*\)"/\1/p')" + dmenu_args="-w $xid -p Go:" fi # Note: Bookmarks starts with '* ' -CHOOSE="$ADDCH +choose="$addch $(sed -n 's/\* //p' "$BOOKMARDS")" -echo "$CHOOSE" | sed '/^\s*$/d' | dmenu -p 'surf' $DMENU_ARGS | while read L; do - run "$L" +echo "$choose" | sed '/^\s*$/d' | dmenu -p 'surf' $dmenu_args | while read L; do + if echo "$L" | grep -qE '^\?'; then # We do search on duckduckgo + open_uri "https://duckduckgo.com/?q=${L#?}&t=surf&kk=-1&ia=web" + else + open_uri "$L" + fi done -- cgit v1.2.3