Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/sh
- #=======================================================
- # create_dual_ovpn_ddwrt.sh - v1.1.0
- #=======================================================
- # Changelog
- #=======================================================
- # v1.1.0 - 02/20/2012 - Kain0o0
- # - Changed name guessing to match new Strongvpn script
- # - Created DUAL_SCRIPT variable for user preference
- # - Created clean_sed function to parse sed output
- # - Script is easily modified using clean_sed input
- # - Modified code and added comments for easier reading
- # - Added brakets for notepad++ collapsing support
- # - Added changelog to track changes
- # v1.0.2 - 08/12/2011 - Cfelix (Thanks!)
- # - Added upnp line
- # - Missing \'s for $RULE. Changed to echo \\\\$\RULE
- # - Modified various sed commands
- # v1.0.1 - 11/29/2010 - Kain0o0
- # - Added PORT_RANGE variable for easier modification
- # v1.0.0 - 11/25/2010 - Kain0o0
- # - initial release
- # Modify if you have a different DHCP port range
- # Script will route the ip's below through WAN
- # You can use http://www.ipaddresslocation.org/subnet-mask-calculator.php to calculate ranges
- # Please maintain format ip rule add from XXX.XXX.XXX.XXX\/XX lookup 4\n\
- PORT_RANGE="\
- ip rule add from 192.168.1.100/30 lookup 4
- ip rule add from 192.168.1.104/29 lookup 4
- ip rule add from 192.168.1.112/28 lookup 4
- ip rule add from 192.168.1.128/25 lookup 4
- "
- # New script name, modify it if you have a naming preference
- DUAL_SCRIPT="ovpn_dual_ddwrt.sh"
- #BANNER
- {
- clear
- echo
- echo -e "\033[31m\033[47m=======================================================================\033[0m"
- echo -e "\033[31m\033[47m CREATE_DUAL_OVPN_DDWRT v1.1.0\033[0m"
- echo -e "\033[31m\033[47m www.obesevegan.com \033[0m"
- echo -e "\033[31m\033[47m\a=======================================================================\033[0m"
- echo -e "\033[30m\033[47m This script will reconfigure the openvpn installation script to route \033[0m"
- echo -e "\033[30m\033[47m all non-dhcp ports (192.168.1.2 -> 192.168.1.99) through openvpn \033[0m"
- echo -e "\033[30m\033[47m DHCP ports will be sent through ISP. \033[0m"
- echo -e "\033[30m\033[47m \033[0m"
- echo -e "\033[34m\033[47m Input: Original StrongVPN installer script (ovpnXXX_ddwrt_small.sh) \033[0m"
- echo -e "\033[34m\033[47m Output: Dual installation script ($DUAL_SCRIPT) \033[0m"
- echo -e "\033[31m\033[47m=======================================================================\033[0m"
- }
- # Get StrongVPN ddwrt script to modify it
- {
- # Tries to guess the StrongVPN script name
- GUESS=ovpn???_ddwrt_small.sh
- if [ -f $GUESS ];then
- echo Is this your file : $GUESS
- while true
- do
- echo -n "Please confirm (y or n): "
- read CONFIRM
- case $CONFIRM in
- y|Y|YES|yes|Yes)
- INSTALL_SCRIPT="$GUESS"
- break
- ;;
- n|N|no|NO|No)
- echo Please input the original installer script name:
- read INSTALL_SCRIPT
- if [ ! -z $INSTALL_SCRIPT ];then
- if [ ! -f $INSTALL_SCRIPT ];then
- echo File doesnt exist in this directory
- echo are you sure you copied all the files here?
- echo Check file name \& copy location then restart.
- exit
- fi
- else
- echo Blank file name given, please retry!
- exit
- fi
- break
- ;;
- *) clear | echo Please enter only y or n
- esac
- done
- else
- echo Please input the original installer script name:
- read INSTALL_SCRIPT
- if [ ! -f $INSTALL_SCRIPT ];then
- echo File doesnt exist in this directory
- echo are you sure you copied all the files here?
- echo Check file name \& copy location then restart.
- exit
- fi
- fi
- }
- # Creating working copy of script
- cp $INSTALL_SCRIPT $DUAL_SCRIPT
- echo
- echo Parsing file info....
- # Cleans sed output, adds the necessary escape characters needed.
- # Arguments: arg1= variable holding the replacing text
- # arg2(optional)=multi if the variable consists of multiple lines
- clean_sed(){
- pattern="$1"
- safe_pattern=$(echo "$pattern" | sed 's/[][\.*/]/\\&/g; s/$$/\\&/; s/^^/\\&/')
- if [ $2 == "multi" ]; then
- safe_pattern=$(echo "$safe_pattern" | sed 's/$/\\\\n/')
- fi
- echo -e $safe_pattern
- }
- # Modified script text - combined here to make future changes easier
- {
- #TEXT1
- # Replaces openvpn --daemon --config ovpn.conf
- {
- TEXT1='sh /tmp/etc/config/ovpn.wanup'
- }
- #TEXT2
- # The double slashes are necessary since this text will be ran through sed AND echo -e
- # Modifies wanup script
- {
- TEXT2='echo -e "
- #!/bin/sh
- killall openvpn
- upnp -D -W tun0
- ping -c4 localhost
- # Clean up by flushing table 4 and deleting all ip rules
- ip route flush table 4
- ip rule show | grep -Ev \\"^(0|32766|32767)\\" | while read PRIO RULE; do ip rule del prio \\${PRIO%%:*} $( echo \\$RULE | sed \\"s|all|0/0|\\" ); done
- # Create backup of default route table
- ip route show table main > /tmp/ovpn/route.isp
- # start openvpn
- openvpn --daemon --config /tmp/ovpn/ovpn.conf
- # Add rules for all DHCP routes (192.168.1.100 -> 192.168.1.255)
- '$PORT_RANGE'
- # Flush route cache
- ip route flush cache
- ">/tmp/etc/config/ovpn.wanup'
- }
- #TEXT3
- # Modifies ovpn_up script
- {
- TEXT3='nvram set ovpn_up='"'"'iptables -t nat -A POSTROUTING -o $dev -j MASQUERADE
- # Use original (pre-openvpn) route for table 4
- cat /tmp/ovpn/route.isp | while read ROUTE; do ip route add table 4 $ROUTE; done'
- }
- #TEXT4
- # Modifies ovpn_dn script
- {
- TEXT4='killall -HUP dnsmasq
- # Clean up by flushing table 4 and deleting all ip rules
- ip route flush table 4
- ip rule show | grep -Ev "^(0|32766|32767)" | while read PRIO RULE; do ip rule del prio ${PRIO%%:*} $( echo $RULE | sed "s|all|0/0|" ); done'"'"' '
- }
- #TEXT5
- # Add reboot option
- {
- TEXT5='nvram commit
- echo Press any key to reboot.....
- read dummy_var
- reboot'
- }
- }
- # Do the actual modification using the text above
- {
- #TEXT1
- # Replaces openvpn --daemon --config ovpn.conf
- sed -i "s/openvpn --daemon --config ovpn.conf/`clean_sed "$TEXT1"`/" $DUAL_SCRIPT
- #TEXT2
- # Modifies wanup script
- sed -i "s/echo -e.*/`clean_sed "$TEXT2" "multi"`/" $DUAL_SCRIPT
- #TEXT3
- # Modifies ovpn_up script
- sed -i "s/nvram set ovpn_up.*/`clean_sed "$TEXT3" "multi"`/" $DUAL_SCRIPT
- #TEXT4
- # Modifies ovpn_dn script
- sed -i "s/killall -HUP dnsmasq.*/`clean_sed "$TEXT4" "multi"`/" $DUAL_SCRIPT
- #TEXT5
- # Add reboot option
- sed -i "s/nvram commit/`clean_sed "$TEXT5" "multi"`/" $DUAL_SCRIPT
- #CLEANUP
- # Remove blank spaces for clarity
- sed -i 's/^ //' $DUAL_SCRIPT
- }
- # Make script executable
- chmod +x $DUAL_SCRIPT
- # Ask user if he wants to reboot
- {
- echo
- echo All Done!
- echo
- echo Would you like to start the OpenVPN DD-WRT install?
- echo \(You can manualy install at a later time by running ovpn_dual_ddwrt.sh\)
- while true
- do
- echo -n "Please confirm (y or n): "
- read CONFIRM
- case $CONFIRM in
- y|Y|YES|yes|Yes) break ;;
- n|N|no|NO|No)
- echo Aborting - You can manualy install at a later time by running ovpn_dual_ddwrt.sh
- exit
- ;;
- *) clear | echo Please enter only y or n
- esac
- done
- echo
- echo Running the installation script
- # Run newly created script
- sh $DUAL_SCRIPT
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement