标签:
#!/bin/bash
# giving user passwordless sudo privileges
USER=`whoami`
MYPATH=$(cat /etc/passwd|grep $USER|awk -F: ‘{print $6}‘)
# need to be run as root
if [[ "$EUID" -ne 0 ]]; then
echo "This script must be run as root"
exit 1
fi
#parse args
while getopts "c:i:p:I:P:z:h" arg
do
case $arg in
c) CONFIG=$OPTARG
if [[ ! -f "$CONFIG" ]]; then
echo " unvalid arg for -c, ple make sure the config file is exist "
exit 1
fi
;;
i) PROXY_IP=${OPTARG:-127.0.0.1} ;;
p) PROXY_PORT=${OPTARG:-8888} ;;
I) SERVER_IP=$OPTARG ;;
P) SERVER_PORT=$OPTARG ;;
z) ZTE_PROXY=${OPTARG:-proxysh.zte.com.cn} ;;
h)
echo " -------------args for setting up stunnel--------------------"
echo " -c: client certification"
echo " -i: proxy ip, default to localhost
echo " -p: proxy port, default to 8888"
echo " -I: server ip
echo " -P: server port listened in server"
echo " -z: zte proxy, need to be consistent with system network set"
echo "-------------------------------------------------------------"
exit 0
;;
?) echo "unvalid arg"
exit 1
;;
esac
done
if [[ ! $CONFIG || ! $SERVER_IP || ! $SERVER_PORT ]]; then
echo " need args, ple use -h for help..."
exit 1
fi
HTTP_PROXY=http://$PROXY_IP:$PROXY_PORT
NO_PROXY=localhost,127.0.0.1,10.*.*.*/8
# use internal apt-source
INTER_APT_SOURCE=10.62.99.232
function use_internal_apt_source {
sudo mv /etc/apt/sources.list /etc/apt/sources.list.bk
sudo cat <<EOF >/etc/apt/sources.list
deb http://$INTER_APT_SOURCE/common/ubuntu trusty main restricted multiverse universe
deb http://$INTER_APT_SOURCE/common/ubuntu trusty-security main restricted multiverse universe
deb http://$INTER_APT_SOURCE/common/ubuntu trusty-updates main restricted multiverse universe
deb http://$INTER_APT_SOURCE/common/ubuntu trusty-proposed main restricted multiverse universe
deb http://$inter_apt_source/common/ubuntu trusty-backports main restricted multiverse universe
EOF
sudo apt-get update
}
# if use internal apt-source to install stunnel,
# after install stunnel successfully, ple change back original apt-source
function change_back_apt_source {
sudo mv /etc/apt/sources.list.bk /etc/apt/sources.list
}
# (optional) if can not connect to wwww, user internal apt-source
use_internal_apt_source
sudo apt-get install -y stunnel
# (optional) no matter if stunnel package is installed, change back to origin apt-source
change_back_apt_source
sudo cat << EOF > /etc/stunnel/client.conf
chroot = /var/lib/stunnel4/
setuid = stunnel4
setgid = stunnel4
pid = /stunnel4-client.pid
client = yes
cert = /etc/stunnel/client.pem
sslVersion = SSLv3
socket = l:TCP_NODELAY=1
socket = r:TCP_NODELAY=1
[squid]
accept = $PROXY_PORT
protocol = connect
protocolHost = $SERVER_IP:$SERVER_PORT
connect = $ZTE_PROXY:80
EOF
sudo cp $CONFIG /etc/stunnel/client.pem
sudo sed -i -r "s/ENABLED=0/ENABLED=1/g" /etc/default/stunnel4
# restart stunnel
sudo service stunnel4 restart
#config proxy: append to $MYPATH/.profile
cat <<EOF >> /$MYPATH/.profile
export http_proxy=$HTTP_PROXY
export https_proxy=$HTTP_PROXY
export no_proxy=$NO_PROXY
EOF
sudo cat <<EOF > /etc/apt/apt.conf
Acquire::http::proxy \"$HTTP_PROXY/\";
Acquire::https::proxy \"$HTTP_PROXY/\";
EOF
source $MYPATH/.profile
sudo apt-get update
标签:
原文地址:http://www.cnblogs.com/lifeinsmile/p/5578617.html