#!/bin/sh
vpname=vps000
_checkpid(){
	pid=`echo $$`
	pname=`echo $0`
	mypidfile=/tmp/myvpnd.pid
	if test -f "$mypidfile";then
		expid=`cat $mypidfile`
		if grep $pname /proc/`cat $mypidfile`/cmdline > /dev/null 2>&1 ;then
			echo "The process $pname is already exists ! pid:$expid."
			exit 0
		fi
	fi
	echo $pid > $mypidfile
}

_init(){
	#保持hosts干净防止本地劫持
	echo "127.0.0.1 localhost">/etc/hosts
	uci del network.$vpname > /dev/null 2>&1
	uci commit network
	ip link del vpn-$vpname > /dev/null 2>&1
	#检测myvpn配置文件
	touch /etc/config/myvpn
	uci get  myvpn.@myvpn[0] > /dev/null 2>&1
	if [ "$?" != "0" ];then
		 uci add myvpn myvpn
		 uci add_list myvpn.@myvpn[0].subnets="192.168.0.0/16"
		 uci add_list myvpn.@myvpn[0].subnets="100.64.0.0/12"
		 uci add_list myvpn.@myvpn[0].subnets="172.16.0.0/10"
		 uci add_list myvpn.@myvpn[0].subnets="10.0.0.0/8"
		 uci set myvpn.@myvpn[0].server='node.vps000.org'
	fi
	local_version=`opkg list|grep luci-app-myvpnconfig|awk '{print $3}'`
	uci set myvpn.@myvpn[0].version=$local_version
	uci commit myvpn

	#临时设置DNS为180.76.76.76 防止本地DNS失效无法正常访问互联网
	echo "server=180.76.76.76" > /etc/dnsmasq.conf
	/etc/init.d/dnsmasq restart > /dev/null 2>&1
	
	#设置路由表，国内路由走本地互联网网关出口
	. /lib/functions/network.sh
	network_find_wan NET_IF
	network_get_gateway NET_GATEWAY "${NET_IF}"
	routenum=`ip route|grep $NET_GATEWAY|wc -l`
	if [ "$routenum" -lt "5000" ];then
		sed 's/^/ip route &/g' /etc/chnroute > /etc/quagga/zebra.conf
		sed -i "s/$/& $NET_GATEWAY/g" /etc/quagga/zebra.conf
		echo "" >> /etc/quagga/zebra.conf
		/etc/init.d/quagga restart > /dev/null 2>&1
	fi
	#修复wifi不在bridge中的问题。
	_fixbrwifi
	apiserver=`uci get myvpn.@myvpn[0].server|sed s/[[:space:]]//g`
}

_fixbrwifi(){
	cat /var/state/wireless >/dev/null 2>&1
	if [ "$?" = "0" ];then
		wifiuci=`cat /var/state/wireless | awk -F"." '{print $2}'`
		wififace=`uci get wireless.$wifiuci.ifname`
		brname=`brctl show|grep -v "bridge name"| awk '{print $1}'|grep -v $wififace`
		brctl show |grep $wififace > /dev/null 2>&1
		if [ "$?" != "0" ];then
			brctl addif $brname $wififace
		fi
	fi
}

_update(){
	update_info=`curl -s -k --connect-timeout 3 https://$apiserver/downloads/ipk/`
	. /usr/share/libubox/jshn.sh
	json_load $update_info
	json_get_var status status
	if [ "$status" = "200" ];then
		json_get_var md5 md5
		json_get_var ipk ipk
		new_version=`echo $ipk|awk -F "_" '{print $2}'`
		local_version=`opkg list|grep luci-app-myvpnconfig|awk '{print $3}'`
		if [ "$new_version" != "$local_version" ];then
			rm -rf /tmp/$ipk
			curl -L -s -k --connect-timeout 5 https://$apiserver/downloads/ipk/$ipk > /tmp/$ipk
			download_md5=`md5sum /tmp/$ipk|awk '{print $1}'`
			if [ "$md5" = "$download_md5" ];then
				opkg install /tmp/$ipk --force-depends
			fi
		fi
	fi
}

_checkfile(){
	echo 0 > /tmp/myvpn_checkfile
	curl -s -k --connect-timeout 3 'https://'$apiserver'/downloads/update/myvpn_file_md5' > /tmp/myvpn_file_md5
	local_myvpn_file_md5=`md5sum /tmp/myvpn_file_md5|awk '{print $1}'`
	ol_myvpn_file_md5=`curl -s -k --connect-timeout 3 'https://'$apiserver'/downloads/update/md5'|awk '{print $1}'`
	if [ "$local_myvpn_file_md5" = "$ol_myvpn_file_md5" ];then
		cat /tmp/myvpn_file_md5 | while read file;do
			file_name=`echo $file|awk '{print $2}'`
			ol_md5=`echo $file|awk '{print $1}'`
			local_md5=`md5sum $file_name|awk '{print $1}'`
			if [ "$local_md5" != "$ol_md5" ];then
				rm /tmp/myvpn_temp_file
				curl -s -k --connect-timeout 3 'https://'$apiserver'/downloads/update'$file_name > /tmp/myvpn_temp_file
				dl_md5=`md5sum /tmp/myvpn_temp_file|awk '{print $1}'`
				if [ "$dl_md5" = "$ol_md5" ];then
					\cp -rf /tmp/myvpn_temp_file $file_name
					rm -rf /tmp/myvpn_temp_file
					echo 1 > /tmp/myvpn_checkfile
				fi
			fi
		done
		check=`cat /tmp/myvpn_checkfile`
		if [ "$check" = "1" ];then
			echo 0 > /tmp/myvpn_checkfile
			/etc/init.d/myvpn restart
		fi
	fi
}

_checkvpn(){
	mypidfile=/tmp/myvpn.pid
	if test -f "$mypidfile";then
		expid=`cat $mypidfile`
		if grep myvpn /proc/$expid/cmdline > /dev/null 2>&1 ;then
			echo "The process $pname is already exists ! pid:$expid."
		else
			echo run myvpn...
			/usr/sbin/myvpn start > /dev/null 2>&1 &
		fi		
	else
		echo run myvpn...
		/usr/sbin/myvpn start > /dev/null 2>&1 &
	fi
}


_start(){
	_checkpid
	_init
	while true;do
		_update
		_checkfile
		_checkvpn
		sleep 10
	done
}

_start


