admin2.cgi 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744
  1. #!/usr/bin/haserl --shell=/bin/bash --upload-limit=32768 --upload-dir=/tmp
  2. <%# upload limit: 32Mb %>
  3. <%
  4. ## SuperGlue project | http://superglue.it | 2014-2015 | GPLv3
  5. ## http://git.superglue.it/superglue/serverfiles
  6. ##
  7. ## admin2.cgi - control panel for Superglue personal server
  8. ##
  9. ## example POST request:
  10. ## curl --data-urlencode 'key=value' http://host/uri
  11. ##
  12. ## returns: 200 (+ output of operation) on success
  13. ## 406 (+ error message in debug mode) on error
  14. readonly _WWW='/www'
  15. readonly _PWDFILE="/opt/lib/htpasswd"
  16. readonly _HOSTPSK='/opt/lib/host.psk'
  17. readonly _TMP='/tmp'
  18. readonly _LOG="${_WWW}/log/admin.log"
  19. readonly _SCRIPTS='/opt/lib/scripts'
  20. readonly _DEBUG=1
  21. readonly _IFS=$IFS
  22. err() {
  23. _ERR="$?"
  24. [[ "$_ERR" -gt 0 ]] || return 0
  25. logThis "$1"
  26. headerPrint "${2:='400'}"
  27. exit "$_ERR"
  28. }
  29. logThis() {
  30. [[ "$_DEBUG" -gt 0 ]] || return 0
  31. local _TYPE='I'
  32. [[ "$_ERR" -eq 0 ]] || _TYPE='E'
  33. local _TIME; printf -v _TIME '%(%Y-%m-%d %H:%M:%S)T' -1
  34. printf '%b\n' "$_TIME: [$_TYPE] ${@} " >> "$_LOG"
  35. [[ "$_DEBUG" -le 1 ]] || printf '%b\n' "[verbose] $_TYPE ${1}"
  36. return $_ERR
  37. }
  38. headerPrint() {
  39. case "$1" in
  40. 200|'') printf '%b' 'Status: 200 OK\r\n';;
  41. 301) printf '%b' "Status: 301 Moved Permanently\r\nLocation: ${HTTP_REFERER:=$2}\r\n";;
  42. 403) printf '%b' 'Status: 403 Forbidden\r\n';;
  43. 405) printf '%b' 'Status: 405 Method Not Allowed\r\n';;
  44. 406) printf '%b' 'Status: 406 Not Acceptable\r\n';;
  45. *) printf '%b' 'Status: 400 Bad Request\r\n';;
  46. esac
  47. printf '%b' 'Content-Type: text/html\r\n\r\n';
  48. }
  49. ## faster echo
  50. _echo() {
  51. printf "%b" "${*}"
  52. }
  53. urlDec() {
  54. local value=${*//+/%20}
  55. for part in ${value//%/ \\x}; do
  56. printf "%b%s" "${part:0:4}" "${part:4}"
  57. done
  58. }
  59. ## only useful for debugging (remove?)
  60. setQueryVars() {
  61. if [[ "$_DEBUG" -gt 0 ]]; then
  62. _VARS=( ${!POST_*} )
  63. local v
  64. for v in ${_VARS[@]}; do
  65. logThis "$v=${!v}"
  66. done
  67. fi
  68. }
  69. getQueryFile() {
  70. local _UPLD="${HASERL_fwupload_path##*/}"
  71. logThis "'multipart': decoding stream"
  72. mv "$_TMP/$_UPLD" "$_TMP/fwupload.bin" 2>/dev/null || _ERR=$?
  73. if [[ $_ERR -gt 0 ]]; then
  74. showMesg 'Firmware upload has failed' '60' 'Reboot your Superglue server and try again'
  75. fi
  76. }
  77. validIp() {
  78. local _IP=$1
  79. local _RET=1
  80. if [[ $_IP =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
  81. OIFS=$IFS
  82. IFS='.'
  83. _IP=($_IP)
  84. IFS=$OIFS
  85. [[ ${_IP[0]} -le 255 && ${_IP[1]} -le 255 && ${_IP[2]} -le 255 && ${_IP[3]} -le 255 ]]
  86. _RET=$?
  87. fi
  88. return $_RET
  89. }
  90. pwdChange() {
  91. local _USER='admin'
  92. local _REALM='superglue'
  93. [[ -e $_PWDFILE ]] || showMesg 'Password file not found'
  94. [[ -z "${POST_pwd##$POST_pwdd}" ]] || showMesg 'Passwords did not match'
  95. [[ ${#POST_pwd} -ge 6 ]] || showMesg 'Password must be at least 6 characters long'
  96. local _HASH=$(printf '%s' "$_USER:$_REALM:${POST_pwd}" | md5sum | cut -b -32) &&
  97. printf '%b' "${POST_pwd}\n${POST_pwd}\n" | passwd root &&
  98. printf '%b' "$_USER:$_REALM:$_HASH\n" > $_PWDFILE &&
  99. showMesg 'Password is changed' '2' ||
  100. showMesg 'Password change failed!' '5'
  101. }
  102. lanAddr() {
  103. logThis "new LAN addr is: $POST_lanipaddr"
  104. validIp $POST_lanipaddr || showMesg 'Not valid network address'
  105. doUci set lanipaddr $POST_lanipaddr &&
  106. doUci commit network &&
  107. dtach -n -zE $_SCRIPTS/net-restart.sh &>/dev/null &&
  108. showMesg 'New network address is set' '30' "Your server is now accessible under <a href='http://superglue.local/admin'>http://superglue.local/admin</a>" ||
  109. showMesg 'Setting network address failed'
  110. }
  111. wanSet() {
  112. if [[ ! -z $POST_wanifname ]]; then
  113. ## eth and wlan wan cases are different!
  114. ## eth wan requires:
  115. ## config interface 'wan'
  116. ## option proto 'dhcp'
  117. ## option ifname 'eth0'
  118. ##
  119. ## wlan wan requires:
  120. ## config interface 'wan'
  121. ## option proto 'dhcp' (and no 'option ifname' specified!)
  122. logThis "wan.ifname=$POST_wanifname"
  123. if [[ $POST_wanifname == 'eth0' ]]; then
  124. doUci set wanifname $POST_wanifname
  125. doUci set wanwifacedis '1'
  126. elif [[ $POST_wanifname == 'wlan1' ]]; then
  127. doUci set wanifname ''
  128. doUci set wanwifacedis ''
  129. fi
  130. if [[ $POST_wanproto == 'dhcp' ]]; then
  131. doUci set wanproto dhcp
  132. doUci set wanipaddr ''
  133. doUci set wanmask ''
  134. doUci set wangw ''
  135. doUci set wandns ''
  136. elif [[ $POST_wanproto == 'static' ]]; then
  137. logThis "wan.ipaddr=$POST_wanipaddr"
  138. validIp $POST_wanipaddr || showMesg 'Our IP address is not valid' '3' 'make sure to use correct address notation'
  139. validIp $POST_wangw || showMesg 'Gateway is not a valid IP address' '3' 'make sure to use correct address notation'
  140. validIp $POST_wandns || showMesg 'DNS is not a valid IP address' '3' 'make sure to use correct address notation'
  141. doUci set wanproto static
  142. doUci set wanipaddr $POST_wanipaddr
  143. doUci set wanmask '255.255.255.0' ## fix me
  144. doUci set wangw $POST_wangw
  145. doUci set wandns $POST_wandns
  146. fi
  147. if [[ $POST_wanifname == 'wlan1' ]]; then
  148. ssidChange || showMesg 'wanSet: Wireless configuration failed'
  149. else
  150. doUci commit network &&
  151. doUci commit wireless &&
  152. dtach -n -zE $_SCRIPTS/net-restart.sh &>/dev/null
  153. fi
  154. _ERR=$?
  155. [[ $_ERR -eq 0 ]] &&
  156. showMesg 'Internet connection is being configured' '25' 'check your Internet connection on completion' ||
  157. showMesg 'Configuring Internet connection failed' '5'
  158. fi
  159. }
  160. ssidChange() {
  161. ## check for iface
  162. [[ $POST_iface =~ ^('wan'|'lan')$ ]] || showMesg 'Error changing wireless settings' '5' 'unknown or unconfigured interface'
  163. logThis "$POST_iface is being set"
  164. _p=$POST_iface
  165. ## default enc for now
  166. local _enc='psk2'
  167. if [[ $POST_iface == 'wan' ]]; then
  168. local _mode='sta'
  169. local _ssid="${POST_wanssid}"
  170. local _key="${POST_wankey}"
  171. else
  172. local _mode='ap'
  173. local _ssid="${POST_lanssid}"
  174. local _key="${POST_lankey}"
  175. fi
  176. #logThis "ssid: $_ssid [$_mode], key: $_key [$_enc]"
  177. #logThis $POST_wanssid
  178. [[ ${#_ssid} -ge 3 ]] || showMesg 'SSID must be at least 3 characters long'
  179. doUci set $_p'ssid' "${_ssid}"
  180. if [[ -z $_key ]]; then
  181. ## if key is empty set encryption to none and remove key
  182. doUci set $_p'key' && doUci set $_p'enc' 'none'
  183. _ERR=$?
  184. else
  185. [[ ${#_key} -ge 8 ]] || showMesg 'Passphrase must be at least 8 characters long'
  186. doUci set $_p'key' "${_key}" && doUci set $_p'enc' "${_enc}"
  187. fi
  188. [[ $_ERR -eq 0 ]] || showMesg 'ssidChange: Wireless configuration failed'
  189. if [[ $POST_iface == 'lan' ]]; then
  190. if [[ "$(doUci get lanipaddr)" != "${POST_lanipaddr}" ]]; then
  191. logThis 'local IP was changed'
  192. lanAddr
  193. fi
  194. fi
  195. doUci commit wireless
  196. _ERR=$?
  197. [[ $_ERR -eq 0 ]] &&
  198. dtach -n -zE $_SCRIPTS/net-restart.sh &>/dev/null ||
  199. showMesg 'Wireless configuration failed' '5'
  200. [[ $POST_iface == 'lan' ]] &&
  201. showMesg 'Local network configuration is progress' '30' 'check your connection on completion' || return 0
  202. ## in this case wanSet() handles success message
  203. }
  204. showMesg() {
  205. local _RET=$?
  206. local _MSG=$1
  207. local _TIMEOUT=$2
  208. local _SUBMSG=$3
  209. ## if set, global _ERR value prevails
  210. [[ $_ERR -gt $_RET ]] && _RET=$_ERR
  211. _MSG=${_MSG:='Configuration'}
  212. _TIMEOUT=${_TIMEOUT:='5'}
  213. _SUBMSG="${_SUBMSG} <span id='timeout'>${_TIMEOUT}</span> seconds to get ready.."
  214. if [[ $_RET -gt 0 ]]; then
  215. local _TYPE='ERROR: '
  216. headerPrint 406
  217. else
  218. local _TYPE='OK: '
  219. headerPrint 200
  220. fi
  221. htmlHead "<meta http-equiv='refresh' content='${_TIMEOUT};url=http://${HTTP_HOST}/admin'>"
  222. _echo "<body>
  223. <h1>Superglue control panel</h1>
  224. <img src='http://${HTTP_HOST}/resources/img/superglueLogo.png' class='logo'>
  225. <hr>
  226. <h2 style='display:inline'>$_TYPE $_MSG</h2>
  227. <span style='display:block'>$_SUBMSG</span>
  228. <hr>
  229. </body>
  230. <script type='text/javascript'>(function(e){var t=document.getElementById(e);var n=t.innerHTML;var r=setInterval(function(){if(n==0){t.innerHTML='0';clearInterval(r);return}t.innerHTML=n;n--},1e3)})('timeout')
  231. </script>
  232. </html>"
  233. exit $_RET
  234. }
  235. updateFw() {
  236. local _FWFILE="${_TMP}/fwupload.bin"
  237. _OUT="$(sysupgrade -T $_FWFILE 2>&1)" ||
  238. { _ERR=$?; rm -rf $_FWFILE; showMesg 'This is not a firmware!' '3' "$_OUT"; }
  239. [[ $POST_fwreset == 'on' ]] && { _FWRESET='-n'; logThis 'fw reset requested'; }
  240. ## using dtach to prevent sysupgrade getting killed
  241. dtach -n -zE sysupgrade $_FWRESET $_FWFILE 2>&1 &&
  242. showMesg 'Firmware update is in progress..' '120' 'DO NOT INTERRUPT!' ||
  243. { _ERR=$?; rm -rf $_FWFILE; showMesg 'Firmware update failed..' '3'; }
  244. }
  245. usbInit() {
  246. . $_SCRIPTS/usb-part.sh
  247. _OUT="$(usbPart)" &&
  248. showMesg 'USB storage initialization is completed' '30' ||
  249. showMesg "USB init failed!\n$_OUT"
  250. # logThis 'usb init..'
  251. }
  252. rebootNow() {
  253. logThis "reboot: now!"
  254. # reboot
  255. reboot -d 3 || reboot -f
  256. showMesg 'Rebooting..' '60'
  257. }
  258. upTime() {
  259. local _T="$(uptime)" &&
  260. { headerPrint 200; printf '%b' "$_T\n"; exit 0; } ||
  261. headerPrint 406
  262. }
  263. iwScan() {
  264. . $_SCRIPTS/iw-scan.sh
  265. headerPrint 200
  266. iwScan
  267. exit 0
  268. }
  269. findUsbstor() {
  270. local _P='/sys/block/'
  271. local _D _DEV
  272. for _D in ${_P}sd*; do
  273. _DEV=$(readlink -f ${_D}/device)
  274. if [[ ${_DEV/usb} != $_DEV ]]; then
  275. _USBDEV="/dev/${_D/$_P}"
  276. fi
  277. done
  278. [[ $_USBDEV ]] || return 1
  279. }
  280. storageInfo() {
  281. if mountpoint -q $_WWW; then
  282. IFS=$'\n' _STOR=( $(df -h $_WWW) ) IFS=$_IFS
  283. _STOR=( ${_STOR[1]} )
  284. else
  285. return 1
  286. fi
  287. }
  288. swapInfo() {
  289. IFS=$'\n' _SWAP=( $(swapon -s) ) IFS=$_IFS
  290. if [[ ${_SWAP[1]} ]]; then
  291. IFS=$'\t' _SWAP=( ${_SWAP[1]} ) IFS=$_IFS
  292. ## for the lack of floats add trailing 0
  293. ## divide by 1023 and split last digit by a period
  294. _SWAP[1]="$((${_SWAP[1]}0/1023))"
  295. _SWAP[1]="${_SWAP[1]%?}.${_SWAP[1]/??}M"
  296. else
  297. unset _SWAP
  298. return 1
  299. fi
  300. }
  301. trimSpaces() {
  302. local v="$*"
  303. v="${v#"${v%%[![:space:]]*}"}"
  304. v="${v%"${v##*[![:space:]]}"}"
  305. return "$v"
  306. }
  307. sgDdns() {
  308. [[ $POST_sgddnsdomain && $POST_sgddnssubdomain ]] || showMesg 'All values must be set!'
  309. [[ ! "$POST_sgddnssubdomain" =~ [^a-zA-Z0-9$] ]] || showMesg 'Invalid domain name'
  310. [[ ! "$POST_sgddnsupdateurl" == '' ]] || showMesg 'Invalid update URL'
  311. local _CLIENT _DOMAIN _JSON _UPDATEURL _MSG _HTTPCODE _x
  312. [[ -r $_HOSTPSK ]] || showMesg 'Local PSK is not available'
  313. ## produce md5 hash from client PSK
  314. hashClient() {
  315. local _L
  316. while read -r _L; do
  317. printf "%s" ${_L%%[\#\-]*}
  318. done < $_HOSTPSK | md5sum
  319. }
  320. read -r _CLIENT _x < <(hashClient)
  321. logThis "sgvpn client hash: $_CLIENT"
  322. _UPDATEURL="$POST_sgddnsupdateurl"
  323. doUci set sgddnssubdomain "${POST_sgddnssubdomain,,}" &&
  324. doUci set sgddnsdomain "${POST_sgddnsdomain,,}" &&
  325. doUci set sgddnsclient "$_CLIENT" &&
  326. doUci set sgddnsurl "$_UPDATEURL" &&
  327. doUci set sgddnsenable '1' || showMesg 'Error setting DDNS configuration'
  328. doUci commit dyndns
  329. ## attempt first update to check if domain is available and everything checks out
  330. IFS=$'\t'; read -r _MSG _HTTPCODE < <($_SCRIPTS/dyndns-update.sh); IFS=$_IFS
  331. $_MSG="${_MSG:='Unknown error'}"
  332. logThis "sgvpn check: $_MSG"
  333. if [[ ! $_HTTPCODE == 200 ]]; then
  334. _ERR=1
  335. logThis 'error in dns update, disabling configuration'
  336. doUci set sgddnsdomain ''
  337. doUci set sgddnssubdomain ''
  338. doUci set sgddnsclient ''
  339. doUci set sgddnsurl ''
  340. doUci set sgddnsenable '0'
  341. doUci commit dyndns
  342. showMesg "$_MSG" '10'
  343. fi
  344. showMesg 'Domain configuration is in progress..' '10' 'Your address will become available right away'
  345. }
  346. sgOpenvpn() {
  347. logThis "enabling SG openvpn"
  348. if [[ $(doUci get sgopenvpnenable) == '0' ]]; then
  349. doUci set sgopenvpnenable '1' &&
  350. /etc/init.d/openvpn start &&
  351. showMesg 'VPN service enabled' '5'
  352. else
  353. doUci set sgopenvpnenable '0' &&
  354. /etc/init.d/openvpn stop &&
  355. showMesg 'VPN service disabled' '5'
  356. fi
  357. showMesg 'VPN configuration failed' '5' 'make sure your device is online'
  358. }
  359. doUci() {
  360. local _CMD=''
  361. local _ARG=''
  362. case $1 in
  363. get|set|commit) _CMD=$1;;
  364. *) logThis 'bad UCI command'; headerPrint 405; echo 'bad UCI command'; exit 1 ;;
  365. esac
  366. case $2 in
  367. lanssid) _ARG='wireless.@wifi-iface[0].ssid';;
  368. lanenc) _ARG='wireless.@wifi-iface[0].encryption';;
  369. lankey) _ARG='wireless.@wifi-iface[0].key';;
  370. wanwifacedis) _ARG='wireless.@wifi-iface[1].disabled';;
  371. wanssid) _ARG='wireless.@wifi-iface[1].ssid';;
  372. wanenc) _ARG='wireless.@wifi-iface[1].encryption';;
  373. wankey) _ARG='wireless.@wifi-iface[1].key';;
  374. lanipaddr) _ARG='network.lan.ipaddr';;
  375. wanifname) _ARG='network.wan.ifname';;
  376. wanproto) _ARG='network.wan.proto';;
  377. wanipaddr) _ARG='network.wan.ipaddr';;
  378. wanmask) _ARG='network.wan.netmask';;
  379. wangw) _ARG='network.wan.gateway';;
  380. wandns) _ARG='network.wan.dns';;
  381. sgddnsenable) _ARG='dyndns.superglue.enable';;
  382. sgddnsurl) _ARG='dyndns.superglue.updateurl';;
  383. sgddnsclient) _ARG='dyndns.superglue.client';;
  384. sgddnssubdomain) _ARG='dyndns.superglue.subdomain';;
  385. sgddnsdomain) _ARG='dyndns.superglue.domain';;
  386. sgopenvpnenable) _ARG='openvpn.superglue.enable';;
  387. *) if [[ $_CMD == 'commit' ]]; then
  388. _ARG=$2
  389. else
  390. logThis "bad UCI entry: $2"
  391. _ERR=1
  392. showMesg 'bad UCI entry'
  393. fi ;;
  394. esac
  395. if [[ $_CMD == 'get' ]]; then
  396. if [ ! -z $_ARG ]; then
  397. /sbin/uci -q get $_ARG || return $?
  398. fi
  399. fi
  400. if [[ $_CMD == 'set' ]]; then
  401. local _VAL=$3
  402. if [ -z $_VAL ]; then
  403. logThis "empty $_ARG value, removing record"
  404. /sbin/uci delete $_ARG || ( echo "uci delete $_ARG: error"; exit 1; )
  405. fi
  406. if [ ! -z $_ARG ]; then
  407. logThis "setting $_ARG value"
  408. /sbin/uci set $_ARG=$_VAL || ( echo "uci set $_ARG: error"; exit 1; )
  409. fi
  410. fi
  411. if [[ $_CMD == 'commit' ]]; then
  412. /sbin/uci commit $_ARG || echo "uci commit $_ARG: error"
  413. fi
  414. }
  415. ## call with argument to inject additional lines
  416. ## ie: htmlhead "<meta http-equiv='refresh' content='2;URL=http://${HTTP_REFERER}'>"
  417. htmlHead() {
  418. _echo "<!-- obnoxious code below, keep your ports tight -->
  419. <!doctype html>
  420. <html>
  421. <head>
  422. <meta http-equiv='Content-Type' content='text/html; charset=UTF-8' />
  423. <meta http-equiv='Cache-Control' content='no-cache, no-store, must-revalidate' />
  424. <meta http-equiv='Pragma' content='no-cache' />
  425. <meta http-equiv='Expires' content='0' />
  426. <link rel='icon' href='http://${HTTP_HOST}/resources/img/favicon.ico' type='image/x-icon' />
  427. <title>Superglue | Control panel</title>
  428. <link rel='stylesheet' type='text/css' href='http://${HTTP_HOST}/resources/admin/admin.css' />
  429. $@
  430. </head>"
  431. }
  432. footerBody() {
  433. _echo "</body>
  434. <script type='text/javascript' src='http://${HTTP_HOST}/resources/admin/admin.js'></script>
  435. </html>"
  436. }
  437. if [[ "${REQUEST_METHOD^^}" == "POST" ]]; then
  438. [[ $CONTENT_LENGTH -gt 0 ]] || err 'content length is zero, 301 back to referer' '301'
  439. case "${CONTENT_TYPE^^}" in
  440. APPLICATION/X-WWW-FORM-URLENCODED*) setQueryVars;;
  441. MULTIPART/FORM-DATA*) getQueryFile;;
  442. *) _ERR=1; _OUT='this is not a post';;
  443. esac
  444. case $REQUEST_URI in
  445. *pwdchange) pwdChange;;
  446. *ssidchange) ssidChange;;
  447. *lanaddr) lanAddr;;
  448. *updatefw) updateFw;;
  449. *usbinit) usbInit;;
  450. *rebootnow) rebootNow;;
  451. *wan) wanSet;;
  452. *uptime) upTime;;
  453. *iwscan) iwScan;;
  454. *sgddns) sgDdns;;
  455. *sgvpn) sgOpenvpn;;
  456. *) logThis 'bad action'; headerPrint 405;
  457. echo 'no such thing'; exit 1;;
  458. esac
  459. fi
  460. ## if URL is not (just) /admin
  461. #if [[ "${REQUEST_METHOD^^}" == "GET" ]]; then
  462. if [[ "${REQUEST_URI/\/admin\//}" ]]; then
  463. headerPrint '301' '/admin/'
  464. fi
  465. #fi
  466. headerPrint '200'
  467. htmlHead
  468. read sgver < /etc/superglue_version
  469. read devmod < /etc/superglue_model
  470. read openwrt < /etc/openwrt_version
  471. . /opt/lib/scripts/jshn-helper.sh
  472. ## this does not work when iface isn't configured
  473. IFS=','
  474. wan=( $(ifaceStat wan) )
  475. IFS=$_IFS
  476. sgvpn=$(doUci get sgopenvpnenable)
  477. ddomain=$(doUci get sgddnsdomain)
  478. dsubdomain=$(doUci get sgddnssubdomain)
  479. wanifname=${wan[3]}
  480. wanproto=$(doUci get wanproto)
  481. wanipaddr=${wan[0]}
  482. wangw=${wan[2]}
  483. wandns=${wan[5]}
  484. wanuptime=${wan[4]}
  485. wanssid=$(doUci get wanssid)
  486. wankey=$(doUci get wankey)
  487. %>
  488. <body>
  489. <h1>Superglue control panel</h1>
  490. <img src='http://<% _echo "${HTTP_HOST}" %>/resources/img/superglueLogo.png' class='logo'>
  491. <section class='inert'>
  492. <span style='display:block;'><% printf "System version: %s | Device: %s | OpenWRT: %s" "$sgver" "$devmod" "$openwrt" %></span>
  493. <span style='display:block;' id='uptime'><% uptime %></span>
  494. </section>
  495. <section>
  496. <h2>Internet connection</h2>
  497. <form method='post' action='/admin/wan' name='wan' class='elem' id='wanconf'>
  498. <div style='display:inline-flex'>
  499. <div style='display:inline-block;'>
  500. <select name='wanifname' id='wanifname' style='display:block'>
  501. <option value='eth0' id='eth' <% ( [[ $wanifname =~ ('eth') ]] && _echo 'selected' ) %> >Wired (WAN port)</option>
  502. <option value='wlan1' id='wlan' <% ( [[ $wanifname =~ ('wlan') ]] && _echo 'selected' ) %> >Wireless (WiFi)</option>
  503. </select>
  504. <fieldset id='wanwifi' <% ( [[ $wanifname =~ ('wlan') ]] && _echo "class='show elem'" || _echo "class='hide elem'" ) %>>
  505. <select name='wanssid' id='wanssid' class='elem' style='display:block'>
  506. <% if [[ -z $wanssid ]]; then
  507. _echo "<option id='scan' disabled>select a network..</option>"
  508. else
  509. _echo "<option id=$wanssid selected>$wanssid</option>"
  510. fi %>
  511. </select>
  512. <input type='password' name='wankey' placeholder='passphrase' value='<% _echo $wankey %>'>
  513. </fieldset>
  514. <span class='help'>help</span>
  515. </div>
  516. <div style='display:inline-block;'>
  517. <select name='wanproto' id='wanproto' style='display:block'>
  518. <option value='dhcp' name='dhcp' id='dhcp' <% ([[ "$wanproto" == 'dhcp' ]] && _echo 'selected') %>>Automatic (DHCP)</option>
  519. <option value='static' name='stat' id='stat' <% ([[ "$wanproto" == 'static' ]] && _echo 'selected') %>>Manual (Static IP)</option>
  520. </select>
  521. <fieldset id='wanaddr' class='elem'>
  522. <input type='text' name='wanipaddr' id='wanipaddr' value='<% _echo $wanipaddr %>' <% ( [[ $wanproto =~ ('dhcp') ]] && _echo "readonly" ) %> placeholder='ip address'>
  523. <input type='text' name='wangw' id='wangw' value='<% _echo $wangw %>' <% ( [[ $wanproto =~ ('dhcp') ]] && _echo "readonly" ) %> placeholder='gateway/router'>
  524. <input type='text' name='wandns' id='wandns' value='<% _echo $wandns %>' <% ( [[ $wanproto =~ ('dhcp') ]] && _echo "readonly" ) %> placeholder='dns server'>
  525. </fieldset>
  526. </div>
  527. </div>
  528. <div>
  529. <input type='hidden' name='iface' value='wan' class='inline'>
  530. <input type='submit' id='wansubmit' value='Apply' class='inline'>
  531. </form>
  532. <form method='post' action='/admin/sgvpn' class='inline'>
  533. <input type='hidden' name='sgvpn' value='toggle' class='inline'>
  534. <input type='submit' value='<% [[ "$sgvpn" == '1' ]] && _echo 'Disable Superglue VPN service' || _echo 'Enable Superglue VPN service' %>' class='inline'>
  535. </form>
  536. </div>
  537. <span class='help'>help</span>
  538. </section>
  539. <section>
  540. <h2>Domain name</h2>
  541. <form method='post' action='/admin/sgddns' name='sgddns' id='sgddns'>
  542. <div style='display:inline-flex'>
  543. <div style='display:inline-block;'>
  544. <input type='text' name='sgddnssubdomain' placeholder='domain name' value='<% _echo $dsubdomain %>' class='inline'>
  545. </div>
  546. <div style='display:inline-block;'>
  547. <select name='sgddnsdomain' class='inline'>
  548. <option value='spgl.it' <% [[ "$ddomain" == 'spgl.it' ]] && _echo 'selected' %>>.spgl.it</option>
  549. <option value='spgl.cc' <% [[ "$ddomain" == 'spgl.cc' ]] && _echo 'selected' %>>.spgl.cc</option>
  550. <option value='superglue.it' <% [[ "$ddomain" == 'superglue.it' ]] && _echo 'selected' %>>.superglue.it</option>
  551. </select>
  552. </div>
  553. </div>
  554. <input type='submit' name='submit' value="Apply">
  555. <input type='hidden' name='sgddnsupdateurl' value='https://superglue.it/ddns/update'>
  556. </form>
  557. </section>
  558. <section>
  559. <h2>Local wireless network</h2>
  560. <form method='post' action='/admin/ssidchange'>
  561. <div style='display:inline-flex'>
  562. <div style='display:inline-block;'>
  563. <input type='text' name='lanssid' value='<% doUci get lanssid %>'>
  564. <input type='password' name='lankey' value='<% doUci get lankey %>'>
  565. </div>
  566. <div style='display:inline-block;'>
  567. <input type='text' name='lanipaddr' value='<% doUci get lanipaddr %>'>
  568. <input type='hidden' name='iface' value='lan'>
  569. </div>
  570. </div>
  571. <input type='submit' value='Apply' data-wait='Configuring..'>
  572. </form>
  573. <span class='help'>help</span>
  574. </section>
  575. <section>
  576. <h2>USB storage</h2>
  577. <% if findUsbstor; then %>
  578. <% if storageInfo; then %>
  579. <div>File storage: <% _echo "${_STOR[2]} used, ${_STOR[3]} available" %></div>
  580. <div>Swap: <% swapInfo && _echo "${_SWAP[1]}" || _echo '<b>n/a</b>' %></div>
  581. <% else %>
  582. <div>USB storage device must be initialized</div>
  583. <form method='post' action='/admin/usbinit'>
  584. <input type='hidden' name='dev' value='<% _echo $_USBDEV %>'>
  585. <input type='submit' value='Initialize'>
  586. </form>
  587. <% fi %>
  588. <% else %>
  589. <div><h3>USB storage device not found!</h3>Please check and try again</div>
  590. <% fi %>
  591. <span class='help'>help</span>
  592. </section>
  593. <section>
  594. <h2>Change password</h2>
  595. <form method='post' action='/admin/pwdchange'>
  596. <div style='display:inline-flex'>
  597. <div style='display:inline-block;'>
  598. <input type='text' name='usr' value='admin' readonly>
  599. </div>
  600. <div style='display:inline-block;'>
  601. <input type='password' name='pwd' placeholder='password' value=''>
  602. <input type='password' name='pwdd' placeholder='password again' value=''>
  603. </div>
  604. </div>
  605. <input type='submit' value='Apply'>
  606. </form>
  607. <span class='help'>help</span>
  608. </section>
  609. <section>
  610. <h2>Update firmware</h2>
  611. <form method='post' action='/admin/updatefw' enctype='multipart/form-data'>
  612. <div id='uploadbox'>
  613. <input id='uploadfile' placeholder='Select a file..' class='elem' disabled='disabled'>
  614. <input id='uploadbtn' class='elem' name='fwupload' type='file'>
  615. </div>
  616. <div style='display:inline-block;'>
  617. <input type='checkbox' name='fwreset' id='fw-resetbox' />
  618. <label for='fw-resetbox'>Reset all settings</label>
  619. </div>
  620. <input type='submit' value='Upload' data-wait='Uploading, do NOT interrupt!'>
  621. </form>
  622. <span class='help'>help</span>
  623. </section>
  624. <section>
  625. <h2></h2>
  626. <form action='/admin/rebootnow' method='post' class='inline'>
  627. <input type='hidden' name='reboot' value='now' class='inline'>
  628. <input type='submit' value='Reboot' class='inline'>
  629. </form>
  630. <form method='post' action='http://logout@<% _echo ${HTTP_HOST} %>/admin' class='inline'>
  631. <input type='submit' value='Logout' class='inline'>
  632. </form>
  633. </section>
  634. <div style='height:200px' name='big-spacer'></div>
  635. <section>
  636. <h2>Under the hood</h2>
  637. <h4>Memory</h4>
  638. <pre><% free %></pre>
  639. <hr>
  640. <h4>Storage</h4>
  641. <pre><% df -h %></pre>
  642. <hr>
  643. <h4>Recent log entries</h4>
  644. <pre><% tail -n10 /www/log/*.log %></pre>
  645. <hr>
  646. <h4>Environment</h4>
  647. <pre><% env %></pre>
  648. <hr>
  649. </section>
  650. <%
  651. footerBody
  652. exit 0
  653. %>