dotfiles

My collection of dotfiles
git clone git://git.noxz.tech/dotfiles
Log | Files | Refs

proxify (1843B)


      1 #!/bin/sh
      2 
      3 readonly dmz_name="noxz.tech"
      4 readonly lan_name="lanprox.system.local"
      5 readonly dmz_port="9949"
      6 readonly lan_port="9950"
      7 readonly dmz_pid="$(pgrep -a ssh | grep "ssh -g -f -N -D $dmz_port $dmz_name" | awk '{print $1}')"
      8 readonly lan_pid="$(pgrep -a ssh | grep "ssh -g -f -N -D $lan_port $lan_name" | awk '{print $1}')"
      9 
     10 check_proxy() {
     11 	if [ "$lan_pid" != "" ] && [ "$dmz_pid" != "" ]; then
     12 		printf "[$(tput setaf 6)$(tput bold)+$(tput sgr0)] %s\n" \
     13 			"proxy is opened at 127.0.0.1:$lan_port"
     14 		return 0
     15 	else
     16 		printf "[$(tput setaf 1)$(tput bold)-$(tput sgr0)] %s\n" \
     17 			"proxy is closed"
     18 		return 1
     19 	fi
     20 }
     21 
     22 kill_proxy() {
     23 	[ "$lan_pid" != "" ] && kill "$lan_pid"
     24 	[ "$dmz_pid" != "" ] && kill "$dmz_pid"
     25 }
     26 
     27 start_proxy() {
     28 	printf "[$(tput setaf 6)$(tput bold)*$(tput sgr0)] %s\n" \
     29 		"(1/2) connecting to noxz.tech..."
     30 	ssh -g -f -N -D "$dmz_port" "$dmz_name" -p 9922
     31 	[ "$?" != "0" ] && return 1
     32 
     33 	printf "\n[$(tput setaf 6)$(tput bold)*$(tput sgr0)] %s\n" \
     34 		"(2/2) connecting to lanprox.system.local..."
     35 	ssh -g -f -N -D "$lan_port" "$lan_name" -o ProxyCommand="ncat --proxy 127.0.0.1:$dmz_port --proxy-type socks4 %h %p"
     36 	[ "$?" != "0" ] && return 1
     37 
     38 	printf "\n[$(tput setaf 2)$(tput bold)+$(tput sgr0)] %s\n" \
     39 		"proxy successfully opened at 127.0.0.1:$lan_port"
     40 
     41 	return 0
     42 
     43 	## Usage examples:
     44 	#
     45 	# setup the proxy for vimb
     46 	# # export http_proxy='socks4://localhost:9950'
     47 	#
     48 	# start ksh through the proxy
     49 	# # proxychains4 -q -f /etc/proxychains.noxz.conf ksh
     50 }
     51 
     52 # TODO :: check if proxy is up, before starting. And ask to restart
     53 
     54 case "$1" in
     55 	""|status) check_proxy ;;
     56 	start) kill_proxy; start_proxy;;
     57 	stop) kill_proxy;;
     58 	string) [ "$lan_pid" != "" ] && [ "$dmz_pid" != "" ] && echo \
     59 		"/bin/ncat --proxy 127.0.0.1:$lan_port --proxy-type socks4 %h %p";;
     60 	*) echo "usage: "$0" status|start|stop|string"
     61 esac