Advertisement
Guest User

pure.py

a guest
Apr 26th, 2019
5,394
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.32 KB | None | 0 0
  1. #!/bin/sh
  2. #
  3. # This file is a part of PureVPN router applet.
  4. #
  5.  
  6. ## Externally generated options.
  7. PUREVPN_APPLET_URL='http://routerapplet.purevpn.com'
  8. PUREVPN_USER='username'
  9. PUREVPN_PASS='password'
  10. PUREVPN_EXPIRE='2025-12-20'
  11. PUREVPN_BRANCH='main'
  12.  
  13. ARCH=$(uname -m)
  14. if [ -e /www/tomato.js ]; then FIRMWARE="tomato"
  15. else FIRMWARE="ddwrt"
  16. fi
  17.  
  18. ## Install options
  19. PUREVPN_VERSION_URL="$PUREVPN_APPLET_URL/cgi-bin/applet-cgi.py?action=version&arch=$ARCH&firmware=$FIRMWARE&branch=$PUREVPN_BRANCH"
  20. PUREVPN_INSTALL_URL="$PUREVPN_APPLET_URL/cgi-bin/applet-cgi.py?action=download&arch=$ARCH&firmware=$FIRMWARE&branch=$PUREVPN_BRANCH"
  21. PUREVPN_HWCHECK_URL="$PUREVPN_APPLET_URL/cgi-bin/applet-cgi.py?action=hwcheck&arch=$ARCH&firmware=$FIRMWARE&branch=$PUREVPN_BRANCH"
  22. PUREVPN_JFFS2_ROOT=/jffs/purevpn
  23. PUREVPN_TMP_ROOT=/tmp/purevpn
  24. PUREVPN_OVPN_MIN_MAJOR=2
  25. PUREVPN_OVPN_MIN_MINOR=1
  26. PUREVPN_NVRAM_BYTES=$((5*1024))
  27. PUREVPN_USE_JFFS2=0
  28. PUREVPN_APPLET_SIZE=$((2*1024))
  29. PUREVPN_DDWRT_BUILD=13309 # MyPage requires 13309 build
  30. PUREVPN_TOMATO_KVER=2.6
  31. PUREVPN_VERSION=`/tmp/www/cgi-bin/purevpn.cgi -v 2>&1 > /dev/null | grep 'PUREVPN_APPLET_VERSION=' | tr '=' ' ' | awk '{print $2}'`
  32. PUREVPN_DEVICE=$(nvram get DD_BOARD)
  33.  
  34. ## These are hardcoded inside other scripts. You should not change them.
  35. PUREVPN_WWW=/tmp/www
  36. PUREVPN_TMP=/tmp/purevpn
  37.  
  38. echo "Machine name..." `uname -m`
  39. echo "System info..." `cat /proc/cpuinfo | grep -i 'system type' | sed 's/[A-Za-z\t ]*:[\t ]*\(.*\)/\1/' || echo 'could not detect'`
  40. echo "CPU model..." `cat /proc/cpuinfo | grep -i 'cpu model' | sed 's/[A-Za-z\t ]*:[\t ]*\(.*\)/\1/' || echo 'could not detect'`
  41. echo -n "Check Firmware..."
  42. DDWRT_BUILD=`/sbin/softwarerevision 2> /dev/null | grep -oE "^[0-9]+" `
  43. if [ "$FIRMWARE" = "ddwrt" ]; then
  44. if ! [ "$DDWRT_BUILD" ]; then
  45. echo "Could not detect DDWRT build number. Aborting."
  46. exit 1
  47. fi
  48. if [ "$DDWRT_BUILD" -lt "$PUREVPN_DDWRT_BUILD" ]; then
  49. echo "PureVPN Applet can only run $PUREVPN_DDWRT_BUILD DD-WRT builds or newer."
  50. echo "Your router runs $DDWRT_BUILD build. Aborting."
  51. exit 1
  52. fi
  53. elif [ "$FIRMWARE" = "tomato" ]; then
  54. KERNEL=`cat /proc/version | cut -d' ' -f3 | cut -d'.' -f1-2`
  55. if ! [ "$KERNEL" ] || [ "$KERNEL" != "$PUREVPN_TOMATO_KVER" ]; then
  56. echo "Warning: only tomato with $PUREVPN_TOMATO_KVER kernel builds are supported."
  57. fi
  58. else
  59. echo "Your router has unsupported firmware. PureVPN applet can only be "
  60. echo " installed on DDWRT-$PUREVPN_DDWRT_BUILD or Tomato Firmware. Aborting."
  61. exit 1
  62. fi
  63. echo $FIRMWARE
  64.  
  65. ## Check if applet is already installed
  66. echo -n "Detect PureVPN Applet... "
  67. PUREVPN_OLD_ROOT=`nvram get purevpn_root`
  68. if [ "$PUREVPN_OLD_ROOT" ]; then
  69. # Print old applet version in the log
  70. PUREVPN_OLDVER=""
  71. PUREVPN_OLDCGI=$PUREVPN_OLD_ROOT/www/cgi-bin/purevpn.cgi
  72. if [ -f "$PUREVPN_OLDCGI" ]; then
  73. VERSION=`"$PUREVPN_OLDCGI" -v 2>&1 > /dev/null | grep 'PUREVPN_APPLET_VERSION=' | tr '=' ' ' | awk '{print $2}'`
  74. fi
  75. echo "$VERSION in $PUREVPN_OLD_ROOT"
  76. PUREVPN_UNINSTALL=$PUREVPN_OLD_ROOT/www/scripts/purevpn_uninstall.sh
  77. if [ -d "$PUREVPN_OLD_ROOT" ]; then
  78. [ -f $PUREVPN_UNINSTALL ] && $PUREVPN_UNINSTALL reinstall
  79. else
  80. ## Reset settings to defaults on the first startup
  81. mkdir -p $PUREVPN_TMP
  82. touch $PUREVPN_TMP/.reset_to_defaults
  83. fi
  84. else
  85. echo "not found"
  86. fi
  87.  
  88. ## Check if PPTP is supported by the router
  89. #echo -n "Check PPTP support... "
  90. #if [ "$FIRMWARE" = "ddwrt" ]; then
  91. # PPTP_BIN=`which pptp`
  92. # if [ "$PPTP_BIN" ]; then
  93. # echo "found $PPTP_BIN"
  94. # USE_PPTP=1
  95. # else
  96. # echo "not found (PPTP will not be available)"
  97. # USE_PPTP=0
  98. # fi
  99. #elif [ "$FIRMWARE" = "tomato" ]; then
  100. # PPPD_BIN=`which pppd`
  101. # PPTP_PLUGIN=/usr/lib/pppd/pptp.so
  102. # if [ -f $PPTP_PLUGIN ] && [ -f "$PPPD_BIN" ]; then
  103. # echo "found $PPTP_PLUGIN"
  104. # USE_PPTP=1
  105. # else
  106. # echo "not found (PPTP will not be available)"
  107. # USE_PPTP=0
  108. # fi
  109. #fi
  110.  
  111. ## Check if we can deploy on JFFS2 partition
  112. if [ "$PUREVPN_USE_JFFS2" -eq 1 ]; then
  113. echo -n "Check if we can install on JFFS... "
  114. HAS_JFFS2=`grep jffs2 /proc/filesystems`
  115. JFFS2_DEVICE=`mount -t jffs2 | head -n 1 | awk '{print $1}'`
  116. JFFS2_MOUNT=`mount -t jffs2 | head -n 1 | awk '{print $3}'`
  117. JFFS2_KBYTES=`df "$JFFS2_MOUNT" 2> /dev/null | tail -n+2 | head -n 1 | awk '{print $4}'`
  118. if ! [ $HAS_JFFS2 ] || ! [ $JFFS2_KBYTES ] || ! [ $JFFS2_DEVICE ] || ! [ $JFFS2_MOUNT ]; then
  119. echo "jffs2 partiton not found. "
  120. echo "Applet will be reinstalled upon next reboot."
  121. elif [ "$JFFS2_KBYTES" -lt "$PUREVPN_APPLET_SIZE" ]; then
  122. echo "jffs2 partition has not enough free space."
  123. echo "PureVPN applet requires $(((PUREVPN_APPLET_SIZE+1023)/1024)) MB of free space"
  124. echo "where is only $((JFFS2_KBYTES/1024)) available."
  125. else
  126. USE_JFFS2=1
  127. echo "found in $JFFS2_MOUNT"
  128. fi
  129. fi
  130.  
  131. ## Check NVRAM available space
  132. echo -n "Check available nvram space... "
  133. if [ "$FIRMWARE" = "ddwrt" ]; then
  134. NVRAM_BYTES=`nvram show 2>&1 > /dev/null | sed -r 's/size\: ([0-9]*) bytes \(([0-9]*) left\)/\2/g'`
  135. ## TODO uncomment me and test me
  136. # NVRAM_BYTES=`nvram show | tail -n 1 | cut -d' ' -f4`
  137. elif [ "$FIRMWARE" = "tomato" ]; then
  138. NVRAM_BYTES=`nvram show | tail -n 1 | cut -d' ' -f6`
  139. fi
  140. if [ "$NVRAM_BYTES" ]; then
  141. if [ "$NVRAM_BYTES" -lt "$PUREVPN_NVRAM_BYTES" ]; then
  142. echo "less than $(((PUREVPN_NVRAM_BYTES+1023)/1024)) KB free. Abort. "
  143. exit 1
  144. else
  145. echo "$NVRAM_BYTES bytes available"
  146. fi
  147. else
  148. echo "not found. Aborting."
  149. exit 1
  150. fi
  151.  
  152. ## Check OpenVPN binaries and version
  153. #echo -n "Check OpenVPN version... "
  154. #OPENVPN_BIN=`which openvpn`
  155. #OPENVPN_VERSION=`$OPENVPN_BIN --version 2> /dev/null | head -n 1 | cut -d' ' -f 2`
  156. #OPENVPN_MAJOR=`echo $OPENVPN_VERSION | cut -d'.' -f1`
  157. #OPENVPN_MINOR=`echo $OPENVPN_VERSION | cut -d'.' -f2`
  158. #if ! [ "$OPENVPN_BIN" ]; then
  159. # echo "openvpn not found (abort installation)"
  160. # exit 1
  161. #elif [ "$OPENVPN_MAJOR" -lt "$PUREVPN_OVPN_MIN_MAJOR" ] ||
  162. # ( [ "$OPENVPN_MAJOR" -eq "$PUREVPN_OVPN_MIN_MAJOR" ] && [ "$OPENVPN_MINOR" -lt "$PUREVPN_OVPN_MIN_MINOR" ] ); then
  163. # echo "OpenVPN is outdated. Aborting."
  164. # echo "Please update your OpenVPN to version at least $PUREVPN_OVPN_MIN_MAJOR.$PUREVPN_OVPN_MIN_MINOR"
  165. # exit 1
  166. #else
  167. # echo "$OPENVPN_MAJOR.$OPENVPN_MINOR found"
  168. #fi
  169.  
  170. ## Check PPTP and OpenVPN for installation
  171. echo -n "Check PPTP and OpenVPN support... "
  172. if [ "$FIRMWARE" = "ddwrt" ]; then
  173. PPTP_BIN=`which pptp`
  174. OPENVPN_BIN=`which openvpn`
  175. OPENVPN_VERSION=`$OPENVPN_BIN --version 2> /dev/null | head -n 1 | cut -d' ' -f 2`
  176. OPENVPN_MAJOR=`echo $OPENVPN_VERSION | cut -d'.' -f1`
  177. OPENVPN_MINOR=`echo $OPENVPN_VERSION | cut -d'.' -f2`
  178. if ! ( [ "$PPTP_BIN" ] && [ $OPENVPN_BIN ] ); then
  179. echo "OpenVPN and PPTP not found (abort installation)"
  180. USE_PPTP=0
  181. exit 1
  182. elif [ "$OPENVPN_MAJOR" -lt "$PUREVPN_OVPN_MIN_MAJOR" ] ||
  183. ( [ "$OPENVPN_MAJOR" -eq "$PUREVPN_OVPN_MIN_MAJOR" ] && [ "$OPENVPN_MINOR" -lt "$PUREVPN_OVPN_MIN_MINOR" ] ); then
  184. if [ "$PPTP_BIN" ]; then
  185. echo "found $PPTP_BIN"
  186. echo "Please update your OpenVPN to version at least $PUREVPN_OVPN_MIN_MAJOR.$PUREVPN_OVPN_MIN_MINOR"
  187. USE_PPTP=1
  188. else
  189. echo "not found (PPTP will not be available)"
  190. echo "Please update your OpenVPN to version at least $PUREVPN_OVPN_MIN_MAJOR.$PUREVPN_OVPN_MIN_MINOR"
  191. USE_PPTP=0
  192. exit 1
  193. fi
  194. elif [ "$PPTP_BIN" ] && [ $OPENVPN_BIN ]; then
  195. echo "found $PPTP_BIN"
  196. echo "OpenVPN: $OPENVPN_MAJOR.$OPENVPN_MINOR found"
  197. USE_PPTP=1
  198. elif [ "$PPTP_BIN" ] && ! [ $OPENVPN_BIN ]; then
  199. echo "found $PPTP_BIN"
  200. echo "OpenVPN not found"
  201. USE_PPTP=1
  202. else
  203. echo "not found (PPTP will not be available)"
  204. echo "OpenVPN: $OPENVPN_MAJOR.$OPENVPN_MINOR found"
  205. USE_PPTP=0
  206. fi
  207. elif [ "$FIRMWARE" = "tomato" ]; then
  208. PPPD_BIN=`which pppd`
  209. PPTP_PLUGIN=/usr/lib/pppd/pptp.so
  210. OPENVPN_BIN=`which openvpn`
  211. OPENVPN_VERSION=`$OPENVPN_BIN --version 2> /dev/null | head -n 1 | cut -d' ' -f 2`
  212. OPENVPN_MAJOR=`echo $OPENVPN_VERSION | cut -d'.' -f1`
  213. OPENVPN_MINOR=`echo $OPENVPN_VERSION | cut -d'.' -f2`
  214. if ! ( [ -f $PPTP_PLUGIN ] && [ -f "$PPPD_BIN" ] && [ $OPENVPN_BIN ] ); then
  215. echo "OpenVPN and PPTP not found (abort installation)"
  216. USE_PPTP=0
  217. exit 1
  218. elif [ "$OPENVPN_MAJOR" -lt "$PUREVPN_OVPN_MIN_MAJOR" ] ||
  219. ( [ "$OPENVPN_MAJOR" -eq "$PUREVPN_OVPN_MIN_MAJOR" ] && [ "$OPENVPN_MINOR" -lt "$PUREVPN_OVPN_MIN_MINOR" ] ); then
  220. if [ -f $PPTP_PLUGIN ] && [ -f "$PPPD_BIN" ]; then
  221. echo "found $PPTP_PLUGIN"
  222. echo "Please update your OpenVPN to version at least $PUREVPN_OVPN_MIN_MAJOR.$PUREVPN_OVPN_MIN_MINOR"
  223. USE_PPTP=1
  224. else
  225. echo "not found (PPTP will not be available)"
  226. echo "Please update your OpenVPN to version at least $PUREVPN_OVPN_MIN_MAJOR.$PUREVPN_OVPN_MIN_MINOR"
  227. USE_PPTP=0
  228. exit 1
  229. fi
  230. elif [ -f $PPTP_PLUGIN ] && [ -f "$PPPD_BIN" ] && [ $OPENVPN_BIN ]; then
  231. echo "found $PPTP_PLUGIN"
  232. echo "OpenVPN: $OPENVPN_MAJOR.$OPENVPN_MINOR found"
  233. USE_PPTP=1
  234. elif [ -f $PPTP_PLUGIN ] && [ -f "$PPPD_BIN" ] && ! [ $OPENVPN_BIN ]; then
  235. echo "found $PPTP_PLUGIN"
  236. echo "OpenVPN not found"
  237. USE_PPTP=1
  238. else
  239. echo "not found (PPTP will not be available)"
  240. echo "OpenVPN: $OPENVPN_MAJOR.$OPENVPN_MINOR found"
  241. USE_PPTP=0
  242. fi
  243. fi
  244.  
  245. ## Make final decision about installation dir
  246. if [ $USE_JFFS2 ]; then
  247. PUREVPN_ROOT=$PUREVPN_JFFS2_ROOT
  248. else
  249. PUREVPN_ROOT=$PUREVPN_TMP_ROOT
  250. fi
  251.  
  252. echo -n "Check CPU arch support... "
  253. HWCHECK=`wget -q -O - "$PUREVPN_HWCHECK_URL"`;
  254. if [ "$HWCHECK" = "OK" ]; then
  255. echo "Supported"
  256. else
  257. echo "$HWCHECK"
  258. echo "Your router is not supported. Aborted."
  259. exit 1
  260. fi
  261.  
  262. BOOTSTRAP_ENTRY="
  263. while ! [ -f $PUREVPN_TMP/.lock ] ; do
  264. mkdir -p $PUREVPN_ROOT/..;
  265. wget \"$PUREVPN_INSTALL_URL\" -q -O - | gunzip -c | tar -x -C $PUREVPN_ROOT/..;
  266. $PUREVPN_ROOT/www/scripts/purevpn_startup.sh;
  267. sleep 5;
  268. done
  269. "
  270.  
  271. echo -n "Init nvram... "
  272. nvram set purevpn_root="$PUREVPN_ROOT"
  273. nvram set purevpn_bootstrap="$BOOTSTRAP_ENTRY"
  274. nvram set purevpn_pptp="$USE_PPTP"
  275. #nvram set purevpn_user="$PUREVPN_USER"
  276. #nvram set purevpn_pass="$PUREVPN_PASS"
  277. nvram set purevpn_expire="$PUREVPN_EXPIRE"
  278. nvram set purevpn_version_url="$PUREVPN_VERSION_URL"
  279. nvram set purevpn_firmware="$FIRMWARE"
  280. nvram set purevpn_update_versionid=""
  281. nvram set purevpn_update_version=""
  282. nvram set purevpn_update_timestamp=""
  283.  
  284. RUN_BOOTSTRAP='eval `nvram get purevpn_bootstrap`;'
  285. RC_PARAM=""
  286. if [ "$FIRMWARE" = "ddwrt" ]; then
  287. RC_PARAM=rc_startup
  288. elif [ "$FIRMWARE" = "tomato" ]; then
  289. RC_PARAM=script_init
  290. fi
  291.  
  292. nvram set $RC_PARAM="$RUN_BOOTSTRAP$(nvram get $RC_PARAM|sed s/"$RUN_BOOTSTRAP"//g)"
  293. nvram commit >& /dev/null
  294.  
  295. echo "done"
  296.  
  297. echo "Installed in $PUREVPN_ROOT"
  298.  
  299. echo "Starting..."
  300. eval "$BOOTSTRAP_ENTRY"
  301. if [ -f $PUREVPN_TMP/.lock ]; then
  302. echo "Started"
  303. else
  304. echo "Aborted"
  305. fi
  306.  
  307. DATA_SEND="{\"device_type\":\"DDWRT\",\"applet_version\":\"$PUREVPN_VERSION\",\"device_name\":\"$PUREVPN_DEVICE\"}"
  308. $PUREVPN_WWW/scripts/https_client -m POST -h "Content-Type: application/x-www-form-urlencoded" -h "User-agent: purevpn-agent" -d '$DATA_SEND' -a null -e RouterApplet_Install
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement