#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
clear;
rm -- "$0" 
#=================================变量储存=================================
ServerLocation='firstovpn';
MirrorHost='http://rainy.bid/soft/firstovpn';
IPAddress=`wget http://members.3322.org/dyndns/getip -O - -q ; echo`;
#==========================================================================
echo "1.安装主服务器"
echo "2.安装副服务器"
echo -e "请选择安装模式"
read Install
if [ -z $Install ]
	then
		echo "选项不能为空，程序即将退出"
		exit 0;
fi 
if [[ $Install == "2" ]] 
then
echo -n "  请输入数据库地址："
read DBHOST
if [ -z $DBHOST ]
	then
		echo "数据库地址不能为空，程序即将退出"
		exit 0;
fi 
fi 


if [[ $Install == "1" ]] 
then
echo -n "  请输入后台管理员密码："
read SuperPass
if [ -z $SuperPass ]
	then
		echo "管理员密码不能为空，程序即将退出"
		exit 0;
fi
fi

echo -n "  请输入数据库密码："
read MySQLPass
if [ -z $MySQLPass ]
	then
		echo "数据库密码不能为空，程序即将退出"
		exit 0;
fi 

echo "系统正在安装OpenVPN服务，请耐心等待："
echo 
echo -n "正在检测网卡..."
if [ ! -e "/dev/net/tun" ];
    then
	    echo
		echo "安装被终止！"
        echo "TUN/TAP网卡未开启，请联系服务商开启TUN/TAP。"
		echo 
		echo "如果你是网易蜂巢Centos 6.7，请更换安装方式！"
        exit 0;
	else
	    echo "                 [  OK  ]"
fi

echo "正在部署环境..."
service openvpn stop >/dev/null 2>&1
killall squid >/dev/null 2>&1
rm -rf /etc/openvpn/*

rpm -ivh ${MirrorHost}/${ServerLocation}/epel-release-6-8.noarch.rpm --force >/dev/null 2>&1
rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6
rpm -ivh ${MirrorHost}/${ServerLocation}/remi-release-6.rpm --force >/dev/null 2>&1
rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-remi
yum clean all
yum makecache
sleep 5
yum install -y squid openssl openssl-devel lzo lzo-devel pam pam-devel automake pkgconfig curl tar expect unzip telnet
yum install -y openvpn

echo "配置网络环境..."
iptables -F >/dev/null 2>&1
service iptables save >/dev/null 2>&1
service iptables restart >/dev/null 2>&1
iptables -t nat -A POSTROUTING -s 10.8.0.0/16 -o eth0 -j MASQUERADE >/dev/null 2>&1
#禁止ping
iptables -A INPUT -p icmp --icmp-type 8 -s 0/0 -j DROP
#开启ping
#iptables -D INPUT -p icmp --icmp-type 8 -s 0/0 -j DROP
iptables -A INPUT -p TCP --dport 443 -j ACCEPT >/dev/null 2>&1
iptables -A INPUT -p TCP --dport 80 -j ACCEPT >/dev/null 2>&1
iptables -A INPUT -p TCP --dport 8080 -j ACCEPT >/dev/null 2>&1
iptables -A INPUT -p TCP --dport 88 -j ACCEPT >/dev/null 2>&1
iptables -A INPUT -p TCP --dport 3306 -j ACCEPT >/dev/null 2>&1
iptables -A INPUT -p TCP --dport 22 -j ACCEPT >/dev/null 2>&1
iptables -A INPUT -p TCP --dport 137 -j ACCEPT >/dev/null 2>&1
iptables -A INPUT -p TCP --dport 138 -j ACCEPT >/dev/null 2>&1
iptables -A INPUT -p TCP --dport 351 -j ACCEPT >/dev/null 2>&1
iptables -A INPUT -p TCP --dport 366 -j ACCEPT >/dev/null 2>&1
iptables -A INPUT -p TCP --dport 53 -j ACCEPT >/dev/null 2>&1
iptables -t nat -A POSTROUTING -j MASQUERADE >/dev/null 2>&1
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT >/dev/null 2>&1
service iptables save
service iptables restart
chkconfig iptables on
setenforce 0

echo "正在安装配置文件，请稍后......."
cd /etc
rm -f sysctl.conf
wget ${MirrorHost}/${ServerLocation}/sysctl.conf >/dev/null 2>&1
chmod 777 /etc/sysctl.conf
sysctl -p >/dev/null 2>&1

cd /etc/squid
rm -f squid.conf squid_passwd
wget ${MirrorHost}/${ServerLocation}/squid.conf >/dev/null 2>&1
wget ${MirrorHost}/${ServerLocation}/squid_passwd >/dev/null 2>&1
chmod 0755 squid.conf squid_passwd

cd /etc/openvpn
rm -f server-passwd.tar.gz EasyRSA-2.2.2.tar.gz 52ML
wget ${MirrorHost}/${ServerLocation}/EasyRSA-2.2.2.tar.gz >/dev/null 2>&1
wget ${MirrorHost}/${ServerLocation}/server-passwd.tar.gz >/dev/null 2>&1
wget ${MirrorHost}/${ServerLocation}/proxy >/dev/null 2>&1
tar -zxvf EasyRSA-2.2.2.tar.gz >/dev/null 2>&1
tar -zxvf server-passwd.tar.gz >/dev/null 2>&1
sed -i "s/MySQLPass/$MySQLPass/g" /etc/openvpn/disconnect.sh 
sed -i "s/MySQLPass/$MySQLPass/g" /etc/openvpn/login.php
sed -i "s/MySQLPass/$MySQLPass/g" /etc/openvpn/time_task.sh
if [[ $Install == "2" ]] 
then
sed -i "s/localhost/$DBHOST/g" /etc/openvpn/disconnect.sh /etc/openvpn/login.php /etc/openvpn/time_task.sh
fi
chmod -R 0777 /etc/openvpn

echo "请稍等，正在安装LAMP环境"
yum -y install httpd
chkconfig httpd on
sed -i 's/#ServerName www.example.com:80/ServerName www.example.com:88/g' /etc/httpd/conf/httpd.conf >/dev/null 2>&1
sed -i 's/Listen 80/Listen 88/g' /etc/httpd/conf/httpd.conf >/dev/null 2>&1
service httpd start
yum remove -y mysql*
yum --enablerepo=remi install -y mysql mysql-server mysql-devel
sleep 3
chkconfig mysqld on
service mysqld start 
sleep 3
yum --enablerepo=remi install -y php php-mysql php-gd php-mbstring
sleep 3
service httpd restart

if [[ $Install == "2" ]] 
then
mkdir -p /var/www/html/res/
touch /var/www/html/res/openvpn-status.txt
fi

if [[ $Install == "1" ]] 
then
echo "请稍等，正在配置WEB面板"
cd /var/www
rm -rf html
wget ${MirrorHost}/${ServerLocation}/WEB-52ml.tar.gz
tar -zxvf WEB-52ml.tar.gz >/dev/null 2>&1
mysqladmin -u root password $MySQLPass
mysql -uroot -p$MySQLPass -e"GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '$MySQLPass' WITH GRANT OPTION;"
mysql -uroot -p$MySQLPass -e"FLUSH PRIVILEGES;"
mysql -uroot -p$MySQLPass -e"DROP DATABASE test;"
mysql -uroot -p$MySQLPass -e"CREATE DATABASE ov;"
mysql -uroot -p$MySQLPass ov < ov.sql
rm -f ov.sql
sed -i "s/MySQLPass/$MySQLPass/g" /var/www/html/config.php >/dev/null 2>&1
sed -i "s/SuperPass/$SuperPass/g" /var/www/html/config.php >/dev/null 2>&1
rm -f WEB-52ml.tar.gz
chmod -R 0777 /var/www/html
fi

#cd /etc/openvpn/easy-rsa/
#source vars
#./clean-all
#echo "正在生成CA/服务端证书..."
#./ca && ./centos centos >/dev/null 2>&1
#echo "证书创建完成 "
#echo "正在生成TLS密钥..."
#sleep 2
#openvpn --genkey --secret ta.key
#echo "正在生成SSL加密证书，这是一个漫长的等待过程..."
#sleep 1
#./build-dh

echo "正在启动服务..."
squid –z >/dev/null 2>&1
squid -s >/dev/null 2>&1
chkconfig openvpn on  >/dev/null 2>&1
chkconfig squid on >/dev/null 2>&1
service squid start >/dev/null 2>&1
service openvpn start >/dev/null 2>&1
cd /etc/openvpn >/dev/null 2>&1
./proxy -l 8080 -d >/dev/null 2>&1
./proxy -l 137 -d >/dev/null 2>&1
./proxy -l 138 -d >/dev/null 2>&1

echo "正在启动实时监控
      频率为每分钟一次"
yum install crontabs -y
if [ ! -e "/var/spool/cron/" ];
then
mkdir -p /var/spool/cron/ >/dev/null 2>&1
fi
echo "* * * * * /etc/openvpn/time_task.sh">>/var/spool/cron/root
chkconfig crond on
service crond reload
service crond restart

echo "正在写入快捷命令..."
cd /bin
rm -f 52ML
wget ${MirrorHost}/${ServerLocation}/vpn1 >/dev/null 2>&1
mv vpn1 /bin/52ML
chmod 0777 /bin/52ML
echo "sh /bin/52ML" >>/etc/rc.d/rc.local

cp /etc/openvpn/easy-rsa/keys/ca.crt /home/ >/dev/null 2>&1
cp /etc/openvpn/easy-rsa/ta.key /home/ >/dev/null 2>&1
cd /home/ >/dev/null 2>&1
clear
echo
echo "正在生成OpenVPN-HTTP.ovpn配置文件..."
echo 
echo "写入前端代码"
sleep 3
echo '# 免流配置
# HTTP转接模式
setenv IV_GUI_VER "de.blinkt.openvpn 0.6.17" 
machine-readable-output
client
dev tun
proto tcp
connect-retry-max 5
connect-retry 5
resolv-retry 60
########免流代码########
remote wap.17wo.cn 80
http-proxy-option EXT1 "POST http://wap.17wo.cn"
http-proxy-option EXT1 "VPN 127.0.0.1:443"
http-proxy-option EXT1 "Host: wap.17wo.cn / HTTP/1.1"' >ovpn.1
echo 写入代理端口 （$IPAddress:8080）
sleep 2
echo http-proxy $IPAddress 8080 >myip
cat ovpn.1 myip>ovpn.2
echo '########免流代码########
' >ovpn.3
cat ovpn.2 ovpn.3>ovpn.4
echo resolv-retry infinite >ovpn.5
cat ovpn.4 ovpn.5>ovpn.6
echo "写入中端代码"
sleep 2
echo 'nobind
persist-key
persist-tun
#push route 114.114.114.144 114.114.115.115

<ca>' >ovpn.7
cat ovpn.6 ovpn.7>ovpn.8
echo "写入CA证书"
sleep 2
cat ovpn.8 ca.crt>ovpn.9
echo '</ca>
key-direction 1
<tls-auth>' >ovpn.10
cat ovpn.9 ovpn.10>ovpn.11
echo "写入TLS密钥"
sleep 2
cat ovpn.11 ta.key>ovpn.12
echo "写入后端代码"
sleep 2
echo '</tls-auth>
auth-user-pass
ns-cert-type server
comp-lzo
verb 3
' >ovpn.13
echo "生成OpenVPN-HTTP.ovpn文件"
sleep 2
cat ovpn.12 ovpn.13>OpenVPN-HTTP.ovpn
echo "配置文件制作完毕"
echo
rm -rf ./{myip,ovpn.1,ovpn.2,ovpn.3,ovpn.4,ovpn.5,ovpn.6,ovpn.7,ovpn.8,ovpn.9,ovpn.10,ovpn.11,ovpn.12,ovpn.13,ovpn.14,ovpn.15,ovpn.16}
sleep 2
clear

echo
echo 
echo "正在生成OpenVPN.ovpn配置文件..."
echo 
echo 
echo "写入前端代码"
sleep 3
echo '# 免流配置
# 本文件由系统自动生成
# 常规模式
setenv IV_GUI_VER "de.blinkt.openvpn 0.6.17" 
machine-readable-output
client
dev tun
proto tcp
connect-retry-max 5
connect-retry 5
resolv-retry 60
########免流代码########
http-proxy-option EXT1 "POST http://wap.10010.com" 
http-proxy-option EXT1 "GET http://wap.10010.com" 
http-proxy-option EXT1 "X-Online-Host: wap.10010.com" 
http-proxy-option EXT1 "POST http://wap.10010.com" 
http-proxy-option EXT1 "X-Online-Host: wap.10010.com" 
http-proxy-option EXT1 "POST http://wap.10010.com" 
http-proxy-option EXT1 "Host: wap.10010.com" 
http-proxy-option EXT1 "GET http://wap.10010.com" 
http-proxy-option EXT1 "Host: wap.10010.com"' >ovpn.1
echo 写入代理端口 （$IPAddress:80）
sleep 2
echo http-proxy $IPAddress 80 >myip
cat ovpn.1 myip>ovpn.2
echo '########免流代码########
' >ovpn.3
cat ovpn.2 ovpn.3>ovpn.4
echo "<http-proxy-user-pass>" >>ovpn.4
echo root >>ovpn.4
echo 52ml >>ovpn.4
echo "</http-proxy-user-pass>
" >>ovpn.4
echo 写入OpenVPN端口 （$IPAddress:443）
echo remote $IPAddress 443 >ovpn.5
cat ovpn.4 ovpn.5>ovpn.6
echo "写入中端代码"
sleep 2
echo 'resolv-retry infinite
nobind
persist-key
persist-tun
#push route 114.114.114.144 114.114.115.115

<ca>' >ovpn.7
cat ovpn.6 ovpn.7>ovpn.8
echo "写入CA证书"
sleep 2
cat ovpn.8 ca.crt>ovpn.9
echo '</ca>
key-direction 1
<tls-auth>' >ovpn.10
cat ovpn.9 ovpn.10>ovpn.11
echo "写入TLS密钥"
sleep 2
cat ovpn.11 ta.key>ovpn.12
echo "写入后端代码"
echo '</tls-auth>
auth-user-pass
ns-cert-type server
comp-lzo
verb 3
' >ovpn.13
echo "生成OpenVPN.ovpn文件"
cat ovpn.12 ovpn.13>OpenVPN.ovpn
echo "配置文件制作完毕"
echo
rm -rf ./{myip,ovpn.1,ovpn.2,ovpn.3,ovpn.4,ovpn.5,ovpn.6,ovpn.7,ovpn.8,ovpn.9,ovpn.10,ovpn.11,ovpn.12,ovpn.13,ovpn.14,ovpn.15,ovpn.16}
sleep 2

tar -zcvf OpenVPN-52ML.tar.gz ./{OpenVPN-HTTP.ovpn,OpenVPN.ovpn,ca.crt,ta.key} >/dev/null 2>&1
cp OpenVPN-52ML.tar.gz /var/www/html/OpenVPN-52ML.tar.gz
rm -rf ./{ta.key,ca.crt,user01.{crt,key}}
clear
echo
# OpenVPN Installing ****************************************************************************
if [[ $Install == "1" ]] 
then
echo 主服务器安装完成
echo 
echo 重启命令 52ML
echo 
echo 控制面板为 http://$IPAddress:88 管理后台为 http://$IPAddress:88/admin
echo
echo 配置文件下载地址 http://$IPAddress:88/OpenVPN-52ML.tar.gz
echo
echo 管理账号 admin 密码 $SuperPass 
fi

if [[ $Install == "2" ]] 
then
echo 副服务器安装完成
echo 
echo 重启命令 52ML
echo 
echo 配置文件下载地址 http://$IPAddress:88/OpenVPN-52ML.tar.gz
fi

rm -rf /openvpn /root/openvpn /home/openvpn
rm -rf /etc/openvpn/server-passwd.tar.gz /etc/openvpn/ca
rm -f /etc/openvpn/EasyRSA-2.2.2.tar.gz
exit 0;
# OpenVPN Installation Complete *****************************************************************
