Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 29 additions & 22 deletions install.sh
Original file line number Diff line number Diff line change
@@ -1,51 +1,58 @@
#!/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 $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
# Which might clash
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 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
29 changes: 18 additions & 11 deletions proxyhelper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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)])

13 changes: 9 additions & 4 deletions torpinger
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
16 changes: 6 additions & 10 deletions uninstall.sh
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions update.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
echo '## Updating ProxyHelper'

cd $HOME && sudo git clone https://github.com/Nithmr/ProxyHelper.git .proxyhelper && cd .proxyhelper && sh ./install.sh
101 changes: 59 additions & 42 deletions zetproxy
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -71,7 +86,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 ''
Expand Down Expand Up @@ -177,7 +192,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/')
Expand Down Expand Up @@ -223,14 +238,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