From 84b0f8c14c8663de0f057acf56fa2608560ddc77 Mon Sep 17 00:00:00 2001 From: Utkarsh Raj Date: Thu, 20 Oct 2016 20:34:44 +0530 Subject: [PATCH 1/2] More verbose install/uninstall messages; 'phelp -S is now default behaviour'; new instructions in update.sh; improved torpinger Import error message --- install.sh | 62 +++++++++++++++++++++++++++++++++++--------------- proxyhelper.py | 29 ++++++++++++++--------- torpinger | 13 +++++++---- uninstall.sh | 16 +++++-------- update.sh | 3 +++ zetproxy | 20 ++++++++-------- 6 files changed, 91 insertions(+), 52 deletions(-) create mode 100755 update.sh diff --git a/install.sh b/install.sh index 59be349..605e3b2 100755 --- a/install.sh +++ b/install.sh @@ -1,25 +1,26 @@ #!/bin/sh # exit if root -if [ $(id -u) -eq 0 ] -then - echo 'Run without sudo!!' - exit -fi +#if [ $(id -u) -eq 0 ] +#then + #echo 'Run without sudo!!' + #exit +#fi -home=$HOME +#home=$HOME -if [ ! $(pwd | sed 's/\/.*\/.*\///') = '.proxyhelper' ] -then - echo Exiting. This script should be run from "~/.proxyhelper" directory - exit -fi +#if [ ! $(pwd | sed 's/\/.*\/.*\///') = '.proxyhelper' ] +#then + #echo Exiting. This script should be run from "~/.proxyhelper" directory + #exit +#fi -chmod +x $home/.proxyhelper/zetproxy -chmod +x $home/.proxyhelper/proxyhelper.py -chmod +x $home/.proxyhelper/torpinger +chmod +x ./zetproxy +chmod +x ./update.sh +chmod +x ./proxyhelper.py +chmod +x ./torpinger #chmod +x ./auto-update.sh -chmod +x $home/.proxyhelper/uninstall.sh +chmod +x ./uninstall.sh # Remove earlier installations if they exist # Needs for people with earlier version of PH @@ -27,25 +28,50 @@ chmod +x $home/.proxyhelper/uninstall.sh if [ -x "$(command -v /usr/bin/phelp)" ] then sudo rm /usr/bin/phelp + echo Removing old phelp code fi if [ -x "$(command -v /usr/bin/zetproxy)" ] then sudo rm /usr/bin/zetproxy + echo Removing old zetproxy fi if [ -x "$(command -v /usr/bin/torpinger)" ] then sudo rm /usr/bin/torpinger + echo Removing old torpinger fi if [ -x "$(command -v /etc/network/if-up.d/zetproxy)" ] then sudo rm /etc/network/if-up.d/zetproxy + echo Removing outdated zetproxy fi if [ -x "$(command -v /etc/network/if-up.d/torpinger)" ] then sudo rm /etc/network/if-up.d/torpinger + echo Removing outdated torpinger +fi + +DIR_PATH=/usr/share/proxyhelper + +if [ -x "$(command -v $DIR_PATH/install.sh)" ] +then + sudo rm -rf /usr/share/proxyhelper + echo Removing all old source files fi + # symlinks fail if the path is not absolute -sudo ln -s $home/.proxyhelper/zetproxy /etc/network/if-up.d/ -sudo ln -s $home/.proxyhelper/torpinger /etc/network/if-up.d/ -sudo ln -s $home/.proxyhelper/proxyhelper.py /usr/bin/phelp +#sudo ln -s $home/.proxyhelper/zetproxy /etc/network/if-up.d/ +#sudo ln -s $home/.proxyhelper/torpinger /etc/network/if-up.d/ +#sudo ln -s $home/.proxyhelper/proxyhelper.py /usr/bin/phelp + +sudo mkdir $DIR_PATH +echo Creating source directory /usr/share/proxyhelper +sudo cp ./proxyhelper.py ./update.sh ./zetproxy ./zettorproxy ./torpinger ./README.md ./install.sh ./uninstall.sh ./surely_parallel.py $DIR_PATH +echo Copying src files to directory +echo Creating symlinks +sudo ln -s $DIR_PATH/proxyhelper.py /usr/bin/phelp +sudo ln -s $DIR_PATH/zetproxy /etc/network/if-up.d/ +sudo ln -s $DIR_PATH/torpinger /etc/network/if-up.d/ +echo =============================== +echo "" echo Installation complete diff --git a/proxyhelper.py b/proxyhelper.py index 666cfa3..88bb74a 100755 --- a/proxyhelper.py +++ b/proxyhelper.py @@ -4,7 +4,8 @@ if __name__ == '__main__': - home = subprocess.os.environ.get('HOME') + # home = subprocess.os.environ.get('HOME') + DIR_PATH = '/usr/share/proxyhelper' parser = argparse.ArgumentParser(description='A simple command-line tool to manage proxy settings') main_args = parser.add_argument_group() @@ -45,31 +46,37 @@ if arg.setProxy: subprocess.call(['bash', - '{}/.proxyhelper/zetproxy'.format(home)]) + '{}/zetproxy'.format(DIR_PATH)]) elif arg.torPing: subprocess.call(['python3', - '{}/.proxyhelper/torpinger'.format(home)]) + '{}/torpinger'.format(DIR_PATH)]) elif arg.customProxy: subprocess.call(['bash', - '{}/.proxyhelper/zetproxy'.format(home) + '{}/zetproxy'.format(DIR_PATH) ,'Proxy',arg.customProxy[0]]) elif arg.clearProxy: subprocess.call(['bash', - '{}/.proxyhelper/zetproxy'.format(home), + '{}/zetproxy'.format(DIR_PATH), 'None']) elif arg.getProxy: subprocess.call(['python3', - '{}/.proxyhelper/surely_parallel.py'.format(home)]) + '{}/surely_parallel.py'.format(DIR_PATH)]) elif arg.manual: subprocess.call('sudo rm /etc/network/if-up.d/zetproxy', shell=True) subprocess.call('sudo rm /etc/network/if-up.d/torpinger', shell=True) elif arg.auto: - subprocess.call('sudo cp {}/.proxyhelper/zetproxy /etc/network/if-up.d/'.format(home),shell=True) - subprocess.call('sudo cp {}/.proxyhelper/torpinger /etc/network/if-up.d/'.format(home),shell=True) + subprocess.call( + 'sudo cp {}/zetproxy /etc/network/if-up.d/' + .format(DIR_PATH),shell=True) + subprocess.call( + 'sudo cp {}/torpinger /etc/network/if-up.d/' + .format(DIR_PATH),shell=True) elif arg.update: print('Updating ProxyHelper.') - # To implement !! - # Here !! + subprocess.call(['sh', + '{}/update.sh'.format(DIR_PATH)]) else: - parser.print_help() + print('Type "phelp -h" to get description of all features of phelp"') + subprocess.call(['bash', + '{}/zetproxy'.format(DIR_PATH)]) diff --git a/torpinger b/torpinger index 16299d3..c5e84e0 100755 --- a/torpinger +++ b/torpinger @@ -10,9 +10,14 @@ import time try: import socks except ImportError: - print('Pysocks not installed, do "pip3 install pysocks"') - print('If pip3 is not installed do "sudo apt-get install python3-pip"') - print('On older versions of Ubuntu it is possible that python3 is not installed\n\ Look up google for that') + print('Import error: Ensure all requirements are matched') + print('Try doing this :->\n') + print('\t> sudo apt-get install python3-pip') + print('\t> pip3 install pysocks\n') + # print('If pip3 is not installed do "sudo apt-get install python3-pip"') + print('On older versions of Ubuntu it is possible ' + 'that python3 is not installed. ' + 'Look up on google for that.\n') sys.exit(1) try: from urllib.request import urlopen @@ -97,7 +102,7 @@ class TorPinger(object): ## If we get away from loop due to timeout, then exit if len(SOCKS) == 0: print('Sorry dude couldn\'t find any active tor connection in the\ - given time, exitting') + given time, exiting') return ## Else we are good to go and have a list of open tor sockets ## Of which we choose the one with highest priority diff --git a/uninstall.sh b/uninstall.sh index 027d326..431cdee 100755 --- a/uninstall.sh +++ b/uninstall.sh @@ -1,15 +1,11 @@ #!/bin/sh -# exit if not root -if [ ! $(id -u) -eq 0 ] -then - echo 'This script should be run with root permission' - exit -fi - -rm /etc/network/if-up.d/torpinger -rm /etc/network/if-up.d/zetproxy -rm /usr/bin/phelp +echo Removing torpinger file +sudo rm /etc/network/if-up.d/torpinger +echo Removing zetproxy file +sudo rm /etc/network/if-up.d/zetproxy +echo Removing phelp file +sudo rm /usr/bin/phelp echo Uninstallation complete diff --git a/update.sh b/update.sh new file mode 100755 index 0000000..5af8949 --- /dev/null +++ b/update.sh @@ -0,0 +1,3 @@ +echo '## Updating ProxyHelper' + +cd $HOME && sudo git clone https://github.com/Nithmr/ProxyHelper.git .proxyhelper && cd .proxyhelper && sh ./install.sh diff --git a/zetproxy b/zetproxy index f51e167..d9c0e9e 100755 --- a/zetproxy +++ b/zetproxy @@ -71,7 +71,7 @@ proxy_apt() # need thorough testing proxy_gnome() { - echo '## Handling gnome proxy' + echo '## Setting gnome proxy' gsettings set org.gnome.system.proxy.http host '' gsettings set org.gnome.system.proxy.http port 0 gsettings set org.gnome.system.proxy.https host '' @@ -177,7 +177,7 @@ else echo '----------------------------------' echo '' echo '## Evaluating fastest proxy' - PROXY_OP=$(python3 $HOME/.proxyhelper/surely_parallel.py | tail -1) + PROXY_OP=$(python3 /usr/share/proxyhelper/surely_parallel.py | tail -1) echo ' ' $PROXY_OP phrase='-> Best proxy' key=$(echo $PROXY_OP | sed 's/-> Best proxy : .*/phrase/') @@ -223,14 +223,16 @@ then fi # We don't want to update when we are uninstalling or just resetting proxy -if [[ $1 != "Proxy" && $1 != "None" && $1 != "Socks" ]] -then - home=$HOME - cd $home/.proxyhelper && git pull origin master && sh ./install.sh - echo '## Updating ProxyHelper' -fi +#if [[ $1 != "Update" ]] +#then + #echo '## Updating ProxyHelper' + #cd $HOME && sudo mkdir .proxyhelper && cd .proxyhelper && git pull origin master && sh ./install.sh +#fi ## Better method would be to source the config script ## But that doesn't work for unknown reason +## Update: The reason is known now, running this script +## via python runs it independent of the active session +## kindof obvious behaviour, but sucks for us. -# exec $SHELL +exec $SHELL From f3ae90c40f442291a3207758eb20d337507133d5 Mon Sep 17 00:00:00 2001 From: Utkarsh Raj Date: Sat, 22 Oct 2016 11:12:38 +0530 Subject: [PATCH 2/2] support for multiple users; fix for when logged in as root --- install.sh | 19 ------------- zetproxy | 81 ++++++++++++++++++++++++++++++++---------------------- 2 files changed, 48 insertions(+), 52 deletions(-) diff --git a/install.sh b/install.sh index 605e3b2..d93b600 100755 --- a/install.sh +++ b/install.sh @@ -1,20 +1,5 @@ #!/bin/sh -# exit if root -#if [ $(id -u) -eq 0 ] -#then - #echo 'Run without sudo!!' - #exit -#fi - -#home=$HOME - -#if [ ! $(pwd | sed 's/\/.*\/.*\///') = '.proxyhelper' ] -#then - #echo Exiting. This script should be run from "~/.proxyhelper" directory - #exit -#fi - chmod +x ./zetproxy chmod +x ./update.sh chmod +x ./proxyhelper.py @@ -60,10 +45,6 @@ then fi # symlinks fail if the path is not absolute -#sudo ln -s $home/.proxyhelper/zetproxy /etc/network/if-up.d/ -#sudo ln -s $home/.proxyhelper/torpinger /etc/network/if-up.d/ -#sudo ln -s $home/.proxyhelper/proxyhelper.py /usr/bin/phelp - sudo mkdir $DIR_PATH echo Creating source directory /usr/share/proxyhelper sudo cp ./proxyhelper.py ./update.sh ./zetproxy ./zettorproxy ./torpinger ./README.md ./install.sh ./uninstall.sh ./surely_parallel.py $DIR_PATH diff --git a/zetproxy b/zetproxy index d9c0e9e..03c3a4c 100755 --- a/zetproxy +++ b/zetproxy @@ -12,43 +12,58 @@ FP=FTP_PROXY NP=NO_PROXY SP=SOCKS_PROXY +# other userful variables +ROOT_HOME="/root" +NORMAL_HOME_BASE="/home" # function to set proxy in enviroment file proxy_shell() { - echo '## Setting environment variables in' ${1} - # if None proxy then this next line is enough - # pipe is in extended regular expr, reqs -r to enable it in GNU sed - # Doubt: Are capital proxies needed/recommended. Don't know yet ?? - # Doubt: Socks proxy are not set rn, just removed if it exists - sed -r "/($hp|$HP|$hsp|$HSP|$fp|$FP|$np|$NP|$sp|$SP).*/d" $HOME/.${1}rc> ./.temp_file_env; - unset $hp - unset $hsp - unset $fp - unset $np - unset $sp - unset $HP - unset $HSP - unset $FP - unset $NP - unset $SP - if [ $WIFI_PROXY = "None" ] - then - hp_pr= - hsp_pr= - fp_pr= - np_pr= - elif [ ! $WIFI_PROXY = "None" ] - then - hp_pr=http://$WIFI_PROXY:$PORT/ - hsp_pr=https://$WIFI_PROXY:$PORT/ - fp_pr=ftp://$WIFI_PROXY:$PORT/ - np_pr="localhost,127.0.0.1" - fi - printf "export $hp=\"$hp_pr\"\nexport $hsp=\"$hsp_pr\"\nexport $fp=\"$fp_pr\"\nexport $np=\"$np_pr\"\n" >> ./.temp_file_env; - printf "export $HP=\"$hp_pr\"\nexport $HSP=\"$hsp_pr\"\nexport $FP=\"$fp_pr\"\nexport $NP=\"$np_pr\"\n" >> ./.temp_file_env; - mv ./.temp_file_env $HOME/.${1}rc - echo ' -> Proxy set in' ${1} + USERS=($(users)) + for user in ${USERS[@]} + do + echo "## Current user -> " $user + if [ $user = "root" ] + then + CUR_HOME=$ROOT_HOME + else + CUR_HOME=${NORMAL_HOME_BASE}/$user + fi + + echo '## Setting environment variables in' ${1} + # if None proxy then this next line is enough + # pipe is in extended regular expr, reqs -r to enable it in GNU sed + # Doubt: Are capital proxies needed/recommended. Don't know yet ?? + # Doubt: Socks proxy are not set rn, just removed if it exists + sed -r "/($hp|$HP|$hsp|$HSP|$fp|$FP|$np|$NP|$sp|$SP).*/d" $CUR_HOME/.${1}rc> ./.temp_file_env; + unset $hp + unset $hsp + unset $fp + unset $np + unset $sp + unset $HP + unset $HSP + unset $FP + unset $NP + unset $SP + if [ $WIFI_PROXY = "None" ] + then + hp_pr= + hsp_pr= + fp_pr= + np_pr= + elif [ ! $WIFI_PROXY = "None" ] + then + hp_pr=http://$WIFI_PROXY:$PORT/ + hsp_pr=https://$WIFI_PROXY:$PORT/ + fp_pr=ftp://$WIFI_PROXY:$PORT/ + np_pr="localhost,127.0.0.1" + fi + printf "export $hp=\"$hp_pr\"\nexport $hsp=\"$hsp_pr\"\nexport $fp=\"$fp_pr\"\nexport $np=\"$np_pr\"\n" >> ./.temp_file_env; + printf "export $HP=\"$hp_pr\"\nexport $HSP=\"$hsp_pr\"\nexport $FP=\"$fp_pr\"\nexport $NP=\"$np_pr\"\n" >> ./.temp_file_env; + mv ./.temp_file_env $CUR_HOME/.${1}rc + echo ' -> Proxy set in' ${1} + done } # setting proxy for apt in file apt.conf