Advertisement
Guest User

create_dual_ovpn_ddwrt.sh v1.1.0

a guest
Feb 20th, 2012
3,254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/sh
  2. #=======================================================
  3. # create_dual_ovpn_ddwrt.sh  - v1.1.0
  4. #=======================================================
  5. # Changelog
  6. #=======================================================
  7. # v1.1.0 - 02/20/2012 - Kain0o0
  8. # - Changed name guessing to match new Strongvpn script
  9. # - Created DUAL_SCRIPT variable for user preference
  10. # - Created clean_sed function to parse sed output
  11. # - Script is easily modified using clean_sed input
  12. # - Modified code and added comments for easier reading
  13. # - Added brakets for notepad++ collapsing support
  14. # - Added changelog to track changes
  15. # v1.0.2 - 08/12/2011 - Cfelix (Thanks!)
  16. # - Added upnp line
  17. # - Missing \'s for $RULE.  Changed to echo \\\\$\RULE
  18. # - Modified various sed commands
  19. # v1.0.1 - 11/29/2010 - Kain0o0
  20. # - Added PORT_RANGE variable for easier modification
  21. # v1.0.0 - 11/25/2010 - Kain0o0
  22. # - initial release
  23.  
  24. # Modify if you have a different DHCP port range
  25. # Script will route the ip's below through WAN
  26. # You can use http://www.ipaddresslocation.org/subnet-mask-calculator.php to calculate ranges
  27. # Please maintain format ip rule add from XXX.XXX.XXX.XXX\/XX lookup 4\n\
  28.  
  29. PORT_RANGE="\
  30. ip rule add from 192.168.1.100/30 lookup 4
  31. ip rule add from 192.168.1.104/29 lookup 4
  32. ip rule add from 192.168.1.112/28 lookup 4
  33. ip rule add from 192.168.1.128/25 lookup 4
  34. "
  35.  
  36. # New script name, modify it if you have a naming preference
  37. DUAL_SCRIPT="ovpn_dual_ddwrt.sh"
  38.  
  39. #BANNER
  40. {
  41. clear
  42. echo
  43. echo -e "\033[31m\033[47m=======================================================================\033[0m"
  44. echo -e "\033[31m\033[47m                      CREATE_DUAL_OVPN_DDWRT                     v1.1.0\033[0m"
  45. echo -e "\033[31m\033[47m                        www.obesevegan.com                             \033[0m"
  46. echo -e "\033[31m\033[47m\a=======================================================================\033[0m"
  47. echo -e "\033[30m\033[47m This script will reconfigure the openvpn installation script to route \033[0m"
  48. echo -e "\033[30m\033[47m all non-dhcp ports (192.168.1.2 -> 192.168.1.99) through openvpn      \033[0m"
  49. echo -e "\033[30m\033[47m DHCP ports will be sent through ISP.                                  \033[0m"
  50. echo -e "\033[30m\033[47m                                                                       \033[0m"
  51. echo -e "\033[34m\033[47m Input:  Original StrongVPN installer script (ovpnXXX_ddwrt_small.sh)  \033[0m"
  52. echo -e "\033[34m\033[47m Output: Dual installation script            ($DUAL_SCRIPT)      \033[0m"
  53. echo -e "\033[31m\033[47m=======================================================================\033[0m"
  54. }
  55.  
  56. # Get StrongVPN ddwrt script to modify it
  57. {
  58. # Tries to guess the StrongVPN script name
  59. GUESS=ovpn???_ddwrt_small.sh
  60.  
  61. if [ -f $GUESS ];then
  62. echo Is this your file : $GUESS
  63. while true
  64. do
  65. echo -n "Please confirm (y or n): "
  66. read CONFIRM
  67. case $CONFIRM in
  68. y|Y|YES|yes|Yes)
  69. INSTALL_SCRIPT="$GUESS"
  70. break
  71. ;;
  72. n|N|no|NO|No)
  73. echo Please input the original installer script name:
  74. read INSTALL_SCRIPT
  75. if [ ! -z $INSTALL_SCRIPT ];then
  76. if [ ! -f $INSTALL_SCRIPT ];then
  77. echo File doesnt exist in this directory
  78. echo are you sure you copied all the files here?
  79. echo Check file name \& copy location then restart.
  80. exit
  81. fi
  82. else
  83. echo Blank file name given, please retry!
  84. exit
  85. fi
  86. break
  87. ;;
  88. *) clear | echo Please enter only y or n
  89. esac
  90. done
  91. else
  92. echo Please input the original installer script name:
  93. read INSTALL_SCRIPT
  94. if [ ! -f $INSTALL_SCRIPT ];then
  95. echo File doesnt exist in this directory
  96. echo are you sure you copied all the files here?
  97. echo Check file name \& copy location then restart.
  98. exit
  99. fi
  100. fi
  101. }
  102.  
  103. # Creating working copy of script
  104. cp $INSTALL_SCRIPT $DUAL_SCRIPT
  105. echo
  106. echo Parsing file info....
  107.  
  108. # Cleans sed output, adds the necessary escape characters needed.
  109. # Arguments: arg1= variable holding the replacing text
  110. #            arg2(optional)=multi if the variable consists of multiple lines
  111. clean_sed(){
  112. pattern="$1"
  113. safe_pattern=$(echo "$pattern" | sed 's/[][\.*/]/\\&/g; s/$$/\\&/; s/^^/\\&/')
  114. if [ $2 == "multi" ]; then
  115. safe_pattern=$(echo "$safe_pattern" | sed 's/$/\\\\n/')
  116. fi
  117. echo -e $safe_pattern
  118. }
  119.  
  120. # Modified script text - combined here to make future changes easier
  121. {
  122. #TEXT1
  123. # Replaces openvpn --daemon --config ovpn.conf
  124. {
  125. TEXT1='sh /tmp/etc/config/ovpn.wanup'
  126. }
  127.  
  128. #TEXT2
  129. # The double slashes are necessary since this text will be ran through sed AND echo -e
  130. # Modifies wanup script
  131. {
  132. TEXT2='echo -e "
  133. #!/bin/sh
  134. killall openvpn
  135. upnp -D -W tun0
  136. ping -c4 localhost
  137.  
  138. # Clean up by flushing table 4 and deleting all ip rules
  139. ip route flush table 4
  140. 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
  141.  
  142. # Create backup of default route table
  143. ip route show table main > /tmp/ovpn/route.isp
  144.  
  145. # start openvpn
  146. openvpn --daemon --config /tmp/ovpn/ovpn.conf
  147.  
  148. # Add rules for all DHCP routes (192.168.1.100 -> 192.168.1.255)
  149. '$PORT_RANGE'
  150.  
  151. # Flush route cache
  152. ip route flush cache
  153.  
  154. ">/tmp/etc/config/ovpn.wanup'
  155. }
  156.  
  157. #TEXT3
  158. #  Modifies ovpn_up script
  159. {
  160. TEXT3='nvram set ovpn_up='"'"'iptables -t nat -A POSTROUTING -o $dev -j MASQUERADE
  161.  
  162. # Use original (pre-openvpn) route for table 4
  163. cat /tmp/ovpn/route.isp | while read ROUTE; do ip route add table 4 $ROUTE; done'
  164. }
  165.  
  166. #TEXT4
  167. # Modifies ovpn_dn script
  168. {
  169. TEXT4='killall -HUP dnsmasq
  170.  
  171. # Clean up by flushing table 4 and deleting all ip rules
  172. ip route flush table 4
  173. 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'"'"' '
  174. }
  175.  
  176. #TEXT5
  177. # Add reboot option
  178. {
  179. TEXT5='nvram commit
  180. echo Press any key to reboot.....
  181. read dummy_var
  182. reboot'
  183. }
  184. }
  185.  
  186. # Do the actual modification using the text above
  187. {
  188. #TEXT1
  189. # Replaces openvpn --daemon --config ovpn.conf
  190. sed -i "s/openvpn --daemon --config ovpn.conf/`clean_sed "$TEXT1"`/" $DUAL_SCRIPT
  191.  
  192. #TEXT2
  193. # Modifies wanup script
  194. sed -i "s/echo -e.*/`clean_sed "$TEXT2" "multi"`/" $DUAL_SCRIPT
  195.  
  196. #TEXT3
  197. #  Modifies ovpn_up script
  198. sed -i "s/nvram set ovpn_up.*/`clean_sed "$TEXT3" "multi"`/" $DUAL_SCRIPT
  199.  
  200. #TEXT4
  201. # Modifies ovpn_dn script
  202. sed -i "s/killall -HUP dnsmasq.*/`clean_sed "$TEXT4" "multi"`/" $DUAL_SCRIPT
  203.  
  204. #TEXT5
  205. # Add reboot option
  206. sed  -i "s/nvram commit/`clean_sed "$TEXT5" "multi"`/" $DUAL_SCRIPT
  207.  
  208. #CLEANUP
  209. # Remove blank spaces for clarity
  210. sed -i 's/^ //' $DUAL_SCRIPT
  211. }
  212.  
  213. # Make script executable
  214. chmod +x $DUAL_SCRIPT
  215.  
  216. # Ask user if he wants to reboot
  217. {
  218. echo
  219. echo All Done!
  220. echo
  221. echo Would you like to start the OpenVPN DD-WRT install?
  222. echo \(You can manualy install at a later time by running ovpn_dual_ddwrt.sh\)
  223. while true
  224. do
  225. echo -n "Please confirm (y or n): "
  226. read CONFIRM
  227. case $CONFIRM in
  228. y|Y|YES|yes|Yes) break ;;
  229. n|N|no|NO|No)
  230. echo Aborting - You can manualy install at a later time by running ovpn_dual_ddwrt.sh
  231. exit
  232. ;;
  233. *) clear | echo Please enter only y or n
  234. esac
  235. done
  236. echo
  237. echo Running the installation script
  238. # Run newly created script
  239. sh $DUAL_SCRIPT
  240. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement