#!/usr/bin/env bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
##############################################################################################################################################
# This is proxy install script tool
# Author: RucLinux 海南胡说 海南仙岛
# Website: www.myzhenai.com www.myzhenai.com.cn www.haikou-china.com jiayu.mybabya.com www.0898-shop.com
##############################################################################################################################################

#Gets the IP address
###########################################################################################################
get_ip(){
    local IP=$( ip addr | egrep -o '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' | egrep -v "^192\.168|^172\.1[6-9]\.|^172\.2[0-9]\.|^172\.3[0-2]\.|^10\.|^127\.|^255\.|^0\." | head -n 1 )
    [ -z ${IP} ] && IP=$( wget -qO- -t1 -T2 ipv4.icanhazip.com )
    [ -z ${IP} ] && IP=$( wget -qO- -t1 -T2 ipinfo.io/ip )
    [ ! -z ${IP} ] && echo ${IP} || echo
}
###########################################################################################################

#Generate the key    echo `create`
########################################################################################################### 
Genkey() (
ip=$(get_ip)
if [ ! -n $1 ];then
echo "Do you use the default key?"
name=$ip
else
name=$1
fi
a=`echo $name |wc -L`
if [ $a -lt 6 ];then
site=$name.$ip
else
site=$name
fi
md=`echo $site |base64 -i`
b=`echo $md |wc -L`
if [ $b -lt 36 ];then
stin=`echo $md | md5sum |base64 -i`
else
stin=$md
fi
s=`echo $stin |cut -c1-42`
echo ${s:0:6}-${s:12:6}-${s:18:6}-${s:24:6}-${s:30:6}-${s:36:6}
)
###########################################################################################################
#key=`Genkey 123456789`
#echo $key

#Check the installation environment
###########################################################################################################
Centos() (
yum clean all
yum install redhat-lsb -y
el=`rpm -qa |grep epel` && yum -q remove $el -y
version=`lsb_release -a|grep -e Release|awk -F ":" '{ print $2 }'|awk -F "." '{ print $1 }'|sed s/[[:space:]]//g`
if [ "$version" == "6" ];then
if [ $(getconf WORD_BIT) = '32' ];then
wget http://dl.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm
rpm -ivh epel-release-6-8.noarch.rpm
else
wget http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
rpm -ivh epel-release-6-8.noarch.rpm
fi
fi
if [ "$version" == "5" ];then
if [ $(getconf WORD_BIT) = '32' ];then
wget http://dl.fedoraproject.org/pub/epel/5/i386/epel-release-5-4.noarch.rpm
rpm -ivh epel-release-5-4.noarch.rpm
else
wget http://dl.fedoraproject.org/pub/epel/5/x86_64/epel-release-5-4.noarch.rpm
rpm -ivh epel-release-5-4.noarch.rpm
fi
fi
if [ "$version" == "7" ];then
wget http://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-9.noarch.rpm
rpm -ivh epel-release-7-9.noarch.rpm
fi
return $version
)
###########################################################################################################

#install pptpd VPN
###########################################################################################################
Install_pptpd() (
ver=`Centos`
yum remove ppp ppp-devel pptp pptpd -y
yum clean all
yum install gcc gcc-c++ dkms lrzsz* lzo* iptables* ppp ppp-devel pptp pptpd -y
read -p "Please enter your pptpd username:" pname
[ -z "${pname}" ] && pname="pptpd"
name=${pname}
read -p "Please enter your pptpd Password:" pp
[ -z "${pp}" ] && pp="pptpd"
Password=$(Genkey ${pp})
ip=$(get_ip)
sed -i 's/#localip 192.168.0.1/localip 192.168.0.1/g' /etc/pptpd.conf
sed -i 's/#remoteip 192.168.0.234-238,192.168.0.245/remoteip 192.168.0.234-238,192.168.0.245/g' /etc/pptpd.conf
sed -i 's/#ms-dns 10.0.0.1/ms-dns 8.8.8.8/g' /etc/ppp/options.pptpd
sed -i 's/#ms-dns 10.0.0.2/ms-dns 8.8.4.4/g' /etc/ppp/options.pptpd
echo 'Username:'$name >> pptp.log
echo 'Password:'$Password >> pptp.log
echo 'ServerIP:'$ip >> pptp.log
echo "$name pptpd $Password *" >> /etc/ppp/chap-secrets
sed -i 's/net.ipv4.ip_forward = 0/net.ipv4.ip_forward = 1/g' /etc/sysctl.conf
Catalog='/proc/xen'
if ! [ -d "$Catalog" ];then
iptables -t nat -A POSTROUTING -s 192.168.0.0/24 -j SNAT --to-source $ip
iptables -t nat -A POSTROUTING -s 192.168.0.1/24 -j SNAT --to-source $ip
iptables -I INPUT -p tcp --dport 1723 -j ACCEPT
iptables -I INPUT -p tcp --dport 47 -j ACCEPT
iptables -I INPUT -p gre -j ACCEPT
else
iptables -t nat -A POSTROUTING -s 192.168.0.0/24 -o eth0 -j MASQUERADE
iptables -t nat -A POSTROUTING -s 192.168.0.1/24 -o eth0 -j MASQUERADE
fi
iptables -I INPUT -p tcp --dport 1723 -j ACCEPT
iptables -I INPUT -p tcp --dport 47 -j ACCEPT
iptables -I INPUT -p gre -j ACCEPT
if [ "${ver}" == "7" ];then
service iptables save
systemctl restart iptables.service
systemctl enable iptables.service
systemctl enable pptpd.service
systemctl restart pptpd.service
else
service iptables save
service iptables restart
chkconfig pptpd on
chkconfig iptables on
service pptpd restart-kill
service pptpd start
fi
el=`rpm -qa |grep epel` && yum -q remove $el -y
rp=`rpm -qa |grep rpmforge` && yum -q remove $rp -y
rm -rf *.rpm
yum clean all
)
###########################################################################################################

#install openvpn
###########################################################################################################
install_openvpn() (
ver=`Centos`
yum remove openvpn -y
op=`find / -name openvpn`
if [[ $op != "" ]];then
yum remove openvpn -y
cp -R /etc/openvpn /etc/openvpnback
rm -rf /etc/openvpn/*
fi
yum install wget gcc gcc-c++ cmake make lrzsz* lzo* lz4 lz4-devel git openssl openssl-devel iptables pkcs11-helper pkcs11-helper-devel openssh-clients pam pam-devel automake pkgconfig -y

yum install openvpn -y
#wget https://swupdate.openvpn.org/community/releases/openvpn-2.4.0.tar.gz

# wget https://goo.gl/TdR11J?openvpn-2.4.0.tar.gz
# wget easy-rsa 
# wget https://goo.gl/6xm8LP?openvpn.tar.gz

#tar -xzvf openvpn-2.4.0.tar.gz
#cd openvpn-2.4.0
#./configure && make && make install
#cd
#wget https://goo.gl/6xm8LP?openvpn.tar.gz
#tar -xzvf openvpn.tar.gz
yum install easy-rsa -y
easy=`find / -name easy-rsa` 
cp -R $easy /etc/openvpn/
cd /etc/openvpn/easy-rsa/2.0/
chmod +rwx *
./vars
sed -i 's/export KEY_COUNTRY="US"/export KEY_COUNTRY="CN"/g' vars
sed -i 's/export KEY_PROVINCE="CA"/export KEY_PROVINCE="HN"/g' vars
sed -i 's/export KEY_CITY="SanFrancisco"/export KEY_CITY="HAIKOU"/g' vars
sed -i 's/export KEY_ORG="Fort-Funston"/export KEY_ORG="OpenVPN"/g' vars
sed -i 's/export KEY_EMAIL="me@myhost.mydomain"/export KEY_EMAIL="jiayublog@foxmail.com"/g' vars
#sed -i 's/export KEY_EMAIL=mail@host.domain/export KEY_EMAIL=jiayublog@foxmail.com/g' vars
server=`find / -name sample-config-files` && cp $server/server.conf /etc/openvpn/
sed -i 's/;push "route 192.168.10.0 255.255.255.0"/push "route 192.168.10.0 255.255.255.0"/g' /etc/openvpn/server.conf
sed -i 's/;push "dhcp-option DNS 208.67.222.222"/push "dhcp-option DNS 8.8.8.8"/g' /etc/openvpn/server.conf
sed -i 's/;push "dhcp-option DNS 208.67.220.220"/push "dhcp-option DNS 8.8.4.4"/g' /etc/openvpn/server.conf
sed -i 's/;client-to-client/client-to-client/g' /etc/openvpn/server.conf
sed -i 's/net.ipv4.ip_forward = 0/net.ipv4.ip_forward = 1/g' /etc/sysctl.conf
. ./vars
./clean-all
./build-ca
./build-key-server server
./build-key client-name
./build-dh
chmod +x keys/*
mkdir /home/vpn
cp keys/{ca.crt,ca.key,client-name.crt,client-name.csr,client-name.key,server.crt,server.key,dh2048.pem} /etc/openvpn/
cp keys/{ca.crt,ca.key,client-name.crt,client-name.csr,client-name.key,server.crt,server.key,dh2048.pem} /home/vpn/
cd /home/
tar -zcvf vpn.tar.gz vpn/*
cd /
ip=$(get_ip)
iptables -t nat -A POSTROUTING -s 192.168.10.0/24 -j SNAT --to-source $ip
iptables -t nat -A POSTROUTING -s 10.8.0.20/24 -j SNAT --to-source $ip
iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -j SNAT --to-source $ip
iptables -t nat -A POSTROUTING -s 10.10.10.0/24 -j SNAT --to-source $ip
iptables -I INPUT -m state --state NEW -m tcp -p tcp --dport 1194 -j ACCEPT
iptables -I INPUT -m state --state NEW -m udp -p udp --dport 1194 -j ACCEPT
if [ "${ver}" == "7" ];then
service iptables save
systemctl restart iptables.service
systemctl enable iptables.service
systemctl enable openvpn.service
systemctl restart openvpn.service
else
service iptables save
service iptables restart
chkconfig openvpn on
chkconfig iptables on
service openvpn restart
fi
el=`rpm -qa |grep epel` && yum -q remove $el -y
rm -rf *.rpm
yum clean all
ii="Please scp download /home/vpn.tar.gz this document"
echo "${ii}"
)
###########################################################################################################


#install Shadowsocks-libev
###########################################################################################################
install_ss() (
wget --no-check-certificate https://raw.githubusercontent.com/teddysun/shadowsocks_install/master/shadowsocks-libev.sh
chmod +x shadowsocks-libev.sh
./shadowsocks-libev.sh 2>&1 | tee shadowsocks-libev.log
)
###########################################################################################################


#install l2tp
###########################################################################################################
install_l2tp() (
var=`Centos`
ip=$(get_ip)
yum install make gcc gmp-devel xmlto bison flex xmlto libpcap-devel lsof vim-enhanced man openswan ppp xl2tpd iptables -y
cat >> /etc/ipsec.conf<<END
conn L2TP-PSK-NAT
rightsubnet=vhost:%priv
also=L2TP-PSK-noNAT
conn L2TP-PSK-noNAT
dpddelay=40
dpdtimeout=130
dpdaction=clear
leftnexthop=%defaultroute
rightnexthop=%defaultroute
authby=secret
pfs=no
auto=add
keyingtries=3
rekey=no
ikelifetime=8h
keylife=1h
type=transport
left=${ip}
leftprotoport=17/1701
right=%any
rightprotoport=17/%any
END
read -p "Please enter your L2tpd psk:" ipsk
[ -z "${ipsk}" ] && ipsk="ipsec"
psk=${ipsk}
cat >>/etc/ipsec.secrets<<EOF
${ip} %any: PSK "${psk}"
EOF
sed -i 's/net.ipv4.ip_forward = 0/net.ipv4.ip_forward = 1/g' /etc/sysctl.conf
sed -i 's/net.ipv4.conf.default.rp_filter = 1/net.ipv4.conf.default.rp_filter = 0/g' /etc/sysctl.conf
sysctl -p
read -p "Please enter your L2tpd username:" lname
[ -z "${lname}" ] && lname="l2tpd"
name=${lname}
read -p "Please enter your L2tpd connection password:" lpassword
[ -z "${lpassword}" ] && lpassword="l2tpd"
Password=$(Genkey ${lpassword})
sed '/[global]/a\ipsec saref = yes' /etc/xl2tpd/xl2tpd.conf
sed -i '/ip range/d' /etc/xl2tpd/xl2tpd.conf
sed -i '/local ip/d' /etc/xl2tpd/xl2tpd.conf
sed '/[lns default]/a\local ip = 168.168.168.1' /etc/xl2tpd/xl2tpd.conf
sed '/[lns default]/a\ip range = 168.168.168.128-168.168.168.254' /etc/xl2tpd/xl2tpd.conf
sed -i 's/name = LinuxVPNserver/name = ${name}/g' /etc/xl2tpd/xl2tpd.conf
cat >>/etc/ppp/options.xl2tpd<<EOF
ms-dns 8.8.4.4
asyncmap 0
hide-password
modem
debug
name ${name}
proxyarp
lcp-echo-interval 30
lcp-echo-failure 4
EOF
cat >>/etc/ppp/chap-secrets<<EOF
${name} ${name} ${Password} *
EOF
cp /usr/libexec/iptables/iptables.init /etc/init.d/iptables
iptables --table nat --append POSTROUTING --jump MASQUERADE
iptables -I INPUT -m state --state NEW -m tcp -p tcp --dport 1701 -j ACCEPT
iptables -I INPUT -m state --state NEW -m udp -p udp --dport 1701 -j ACCEPT
iptables -I INPUT -m state --state NEW -m tcp -p tcp --dport 500 -j ACCEPT
iptables -I INPUT -m state --state NEW -m udp -p udp --dport 500 -j ACCEPT
iptables -I INPUT -m state --state NEW -m tcp -p tcp --dport 4500 -j ACCEPT
iptables -I INPUT -m state --state NEW -m udp -p udp --dport 4500 -j ACCEPT
Catalog='/proc/xen'
if ! [ -d "$Catalog" ];then
iptables -t nat -A POSTROUTING -s 168.168.168.1/24 -j SNAT --to-source $ip
iptables -t nat -A POSTROUTING -s 168.168.168.128/24 -j SNAT --to-source $ip
else
iptables -t nat -A POSTROUTING -s 168.168.168.1/24 -o eth0 -j MASQUERADE
iptables -t nat -A POSTROUTING -s 168.168.168.128/24 -o eth0 -j MASQUERADE
fi
if [ "${ver}" == "7" ];then
service iptables save
systemctl restart iptables.service
systemctl enable iptables.service
systemctl enable xl2tpd.service
systemctl enable ipsec.service
systemctl restart ipsec.service
systemctl restart xl2tpd.service
else
service iptables save
service iptables restart
chkconfig iptables on
chkconfig ipsec on
chkconfig xl2tpd on
service ipsec restart
service xl2tpd restart
fi
echo 'Username:'$name >> l2tpd.log
echo 'Password:'$Password >> l2tpd.log
echo 'ServerIP:'$ip >> l2tpd.log
echo 'PSK:'$psk >> l2tpd.log
)
###########################################################################################################

#This is the documentation help
###########################################################################################################
Open_help() (
echo 'Proxy install script tool Option Description'
echo ``
echo '-p install pptpd Two parameters $1 is the user name $2 is the password'
echo '-o install openvpn There are no parameters'
echo '-s install Shadowsocks-libev There are no parameters'
echo '-l install l2tp Three parameters, $1 is psk, $2 is the user name, $3 is the password'
echo '-h This is the documentation'
)
###########################################################################################################

#uninstall Server service
###########################################################################################################
uninstall_proxy() (
#uninstall pptpd
if ! [[ $2=="-p" ]];then
service pptpd stop
chkconfig --del pptpd
yum remove ppp ppp-devel pptp pptpd -y
rm -rf /etc/pptpd.conf
rm -rf /etc/ppp/options.pptpd
fi
#uninstall openvpn
if ! [[ $2=="-o" ]];then
service openvpn stop
chkconfig --del openvpn
yum remove pkcs11-helper pkcs11-helper-devel pam pam-devel openvpn easy-rsa -y
rm -rf /etc/openvpn
rm -rf /etc/rc.d/init.d/openvpn
rm -rf /usr/lib/openvpn
rm -rf /usr/sbin/openvpn
rm -rf /usr/share/doc/openvpn-2.2.2
rm -rf /usr/share/man/man8/openvpn.8.gz
rm -rf /usr/share/openvpn
rm -rf /var/run/openvpn
fi
#uninstall Shadowsocks
if ! [[ $2=="-s" ]];then
service shadowsocks stop
chkconfig --del shadowsocks
f=`find / -name shadowsocks-libev.sh`
./shadowsocks-libev.sh uninstall
fi
#uninstall L2tpd
if ! [[ $2=="-l" ]];then
service ipsec stop
chkconfig --del ipsec
service xl2tpd stop
chkconfig --del xl2tpd
yum remove gmp-devel xmlto bison flex xmlto man openswan ppp xl2tpd -y
rm -rf /etc/ipsec.conf
rm -rf /etc/ipsec.secrets
rm -rf /etc/xl2tpd/xl2tpd.conf
rm -rf /etc/ppp/options.xl2tpd
fi
)
###########################################################################################################


#Install Proxy
###########################################################################################################
par=$1
[ -z $1 ] && action='-h'
case "$par" in
    -h)
    Open_help
    ;;
    -p)
    Install_pptpd
    ;;
    -o)
    install_openvpn
    ;;
    -s)
    install_ss
    ;;
    -l)
    install_l2tp
    ;;
    *)
    echo "Arguments error! [${par}]"
    echo "Usage: `basename $0` {-h|-p|-o|-s|-l}"
    ;;
esac
###########################################################################################################
cur_dir=`pwd`
path=`find $cur_dir -name pptp.log`
if [ -e $path ];then
pptp=${path}
fi
path=`find $cur_dir -name l2tpd.log`
if [ -e $path ];then
l2tpd=${path}
fi
path=`find $cur_dir -name shadowsocks-libev.log`
if [ -e $path ];then
ss=${path}
fi
path=`find /home/ -name vpn.tar.gz`
if [ -e $path ];then
openvpn=${path}
fi
printf "
##############################################################################################################################################
# This is proxy install script tool
# Author: RucLinux
# Website: www.myzhenai.com www.myzhenai.com.cn www.haikou-china.com jiayu.mybabya.com www.0898-shop.com
##############################################################################################################################################
# pptpd Profile information: ${pptp}
# l2tpd Profile information: ${l2tpd}
# shadowsocks Profile information: ${ss}
# openvpn Profile information: ${openvpn}
"
