admin2.cgi 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746
  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. doUci commit openvpn &&
  351. /etc/init.d/openvpn start &&
  352. showMesg 'VPN service enabled' '5'
  353. else
  354. doUci set sgopenvpnenable '0' &&
  355. doUci commit openvpn &&
  356. /etc/init.d/openvpn stop &&
  357. showMesg 'VPN service disabled' '5'
  358. fi
  359. showMesg 'VPN configuration failed' '5' 'make sure your device is online'
  360. }
  361. doUci() {
  362. local _CMD=''
  363. local _ARG=''
  364. case $1 in
  365. get|set|commit) _CMD=$1;;
  366. *) logThis 'bad UCI command'; headerPrint 405; echo 'bad UCI command'; exit 1 ;;
  367. esac
  368. case $2 in
  369. lanssid) _ARG='wireless.@wifi-iface[0].ssid';;
  370. lanenc) _ARG='wireless.@wifi-iface[0].encryption';;
  371. lankey) _ARG='wireless.@wifi-iface[0].key';;
  372. wanwifacedis) _ARG='wireless.@wifi-iface[1].disabled';;
  373. wanssid) _ARG='wireless.@wifi-iface[1].ssid';;
  374. wanenc) _ARG='wireless.@wifi-iface[1].encryption';;
  375. wankey) _ARG='wireless.@wifi-iface[1].key';;
  376. lanipaddr) _ARG='network.lan.ipaddr';;
  377. wanifname) _ARG='network.wan.ifname';;
  378. wanproto) _ARG='network.wan.proto';;
  379. wanipaddr) _ARG='network.wan.ipaddr';;
  380. wanmask) _ARG='network.wan.netmask';;
  381. wangw) _ARG='network.wan.gateway';;
  382. wandns) _ARG='network.wan.dns';;
  383. sgddnsenable) _ARG='dyndns.superglue.enable';;
  384. sgddnsurl) _ARG='dyndns.superglue.updateurl';;
  385. sgddnsclient) _ARG='dyndns.superglue.client';;
  386. sgddnssubdomain) _ARG='dyndns.superglue.subdomain';;
  387. sgddnsdomain) _ARG='dyndns.superglue.domain';;
  388. sgopenvpnenable) _ARG='openvpn.superglue.enable';;
  389. *) if [[ $_CMD == 'commit' ]]; then
  390. _ARG=$2
  391. else
  392. logThis "bad UCI entry: $2"
  393. _ERR=1
  394. showMesg 'bad UCI entry'
  395. fi ;;
  396. esac
  397. if [[ $_CMD == 'get' ]]; then
  398. if [ ! -z $_ARG ]; then
  399. /sbin/uci -q get $_ARG || return $?
  400. fi
  401. fi
  402. if [[ $_CMD == 'set' ]]; then
  403. local _VAL=$3
  404. if [ -z $_VAL ]; then
  405. logThis "empty $_ARG value, removing record"
  406. /sbin/uci delete $_ARG || ( echo "uci delete $_ARG: error"; exit 1; )
  407. fi
  408. if [ ! -z $_ARG ]; then
  409. logThis "setting $_ARG value"
  410. /sbin/uci set $_ARG=$_VAL || ( echo "uci set $_ARG: error"; exit 1; )
  411. fi
  412. fi
  413. if [[ $_CMD == 'commit' ]]; then
  414. /sbin/uci commit $_ARG || echo "uci commit $_ARG: error"
  415. fi
  416. }
  417. ## call with argument to inject additional lines
  418. ## ie: htmlhead "<meta http-equiv='refresh' content='2;URL=http://${HTTP_REFERER}'>"
  419. htmlHead() {
  420. _echo "<!-- obnoxious code below, keep your ports tight -->
  421. <!doctype html>
  422. <html>
  423. <head>
  424. <meta http-equiv='Content-Type' content='text/html; charset=UTF-8' />
  425. <meta http-equiv='Cache-Control' content='no-cache, no-store, must-revalidate' />
  426. <meta http-equiv='Pragma' content='no-cache' />
  427. <meta http-equiv='Expires' content='0' />
  428. <link rel='icon' href='http://${HTTP_HOST}/resources/img/favicon.ico' type='image/x-icon' />
  429. <title>Superglue | Control panel</title>
  430. <link rel='stylesheet' type='text/css' href='http://${HTTP_HOST}/resources/admin/admin.css' />
  431. $@
  432. </head>"
  433. }
  434. footerBody() {
  435. _echo "</body>
  436. <script type='text/javascript' src='http://${HTTP_HOST}/resources/admin/admin.js'></script>
  437. </html>"
  438. }
  439. if [[ "${REQUEST_METHOD^^}" == "POST" ]]; then
  440. [[ $CONTENT_LENGTH -gt 0 ]] || err 'content length is zero, 301 back to referer' '301'
  441. case "${CONTENT_TYPE^^}" in
  442. APPLICATION/X-WWW-FORM-URLENCODED*) setQueryVars;;
  443. MULTIPART/FORM-DATA*) getQueryFile;;
  444. *) _ERR=1; _OUT='this is not a post';;
  445. esac
  446. case $REQUEST_URI in
  447. *pwdchange) pwdChange;;
  448. *ssidchange) ssidChange;;
  449. *lanaddr) lanAddr;;
  450. *updatefw) updateFw;;
  451. *usbinit) usbInit;;
  452. *rebootnow) rebootNow;;
  453. *wan) wanSet;;
  454. *uptime) upTime;;
  455. *iwscan) iwScan;;
  456. *sgddns) sgDdns;;
  457. *sgvpn) sgOpenvpn;;
  458. *) logThis 'bad action'; headerPrint 405;
  459. echo 'no such thing'; exit 1;;
  460. esac
  461. fi
  462. ## if URL is not (just) /admin
  463. #if [[ "${REQUEST_METHOD^^}" == "GET" ]]; then
  464. if [[ "${REQUEST_URI/\/admin\//}" ]]; then
  465. headerPrint '301' '/admin/'
  466. fi
  467. #fi
  468. headerPrint '200'
  469. htmlHead
  470. read sgver < /etc/superglue_version
  471. read devmod < /etc/superglue_model
  472. read openwrt < /etc/openwrt_version
  473. . /opt/lib/scripts/jshn-helper.sh
  474. ## this does not work when iface isn't configured
  475. IFS=','
  476. wan=( $(ifaceStat wan) )
  477. IFS=$_IFS
  478. sgvpn=$(doUci get sgopenvpnenable)
  479. ddomain=$(doUci get sgddnsdomain)
  480. dsubdomain=$(doUci get sgddnssubdomain)
  481. wanifname=${wan[3]}
  482. wanproto=$(doUci get wanproto)
  483. wanipaddr=${wan[0]}
  484. wangw=${wan[2]}
  485. wandns=${wan[5]}
  486. wanuptime=${wan[4]}
  487. wanssid=$(doUci get wanssid)
  488. wankey=$(doUci get wankey)
  489. %>
  490. <body>
  491. <h1>Superglue control panel</h1>
  492. <img src='http://<% _echo "${HTTP_HOST}" %>/resources/img/superglueLogo.png' class='logo'>
  493. <section class='inert'>
  494. <span style='display:block;'><% printf "System version: %s | Device: %s | OpenWRT: %s" "$sgver" "$devmod" "$openwrt" %></span>
  495. <span style='display:block;' id='uptime'><% uptime %></span>
  496. </section>
  497. <section>
  498. <h2>Internet connection</h2>
  499. <form method='post' action='/admin/wan' name='wan' class='elem' id='wanconf'>
  500. <div style='display:inline-flex'>
  501. <div style='display:inline-block;'>
  502. <select name='wanifname' id='wanifname' style='display:block'>
  503. <option value='eth0' id='eth' <% ( [[ $wanifname =~ ('eth') ]] && _echo 'selected' ) %> >Wired (WAN port)</option>
  504. <option value='wlan1' id='wlan' <% ( [[ $wanifname =~ ('wlan') ]] && _echo 'selected' ) %> >Wireless (WiFi)</option>
  505. </select>
  506. <fieldset id='wanwifi' <% ( [[ $wanifname =~ ('wlan') ]] && _echo "class='show elem'" || _echo "class='hide elem'" ) %>>
  507. <select name='wanssid' id='wanssid' class='elem' style='display:block'>
  508. <% if [[ -z $wanssid ]]; then
  509. _echo "<option id='scan' disabled>select a network..</option>"
  510. else
  511. _echo "<option id=$wanssid selected>$wanssid</option>"
  512. fi %>
  513. </select>
  514. <input type='password' name='wankey' placeholder='passphrase' value='<% _echo $wankey %>'>
  515. </fieldset>
  516. <span class='help'>help</span>
  517. </div>
  518. <div style='display:inline-block;'>
  519. <select name='wanproto' id='wanproto' style='display:block'>
  520. <option value='dhcp' name='dhcp' id='dhcp' <% ([[ "$wanproto" == 'dhcp' ]] && _echo 'selected') %>>Automatic (DHCP)</option>
  521. <option value='static' name='stat' id='stat' <% ([[ "$wanproto" == 'static' ]] && _echo 'selected') %>>Manual (Static IP)</option>
  522. </select>
  523. <fieldset id='wanaddr' class='elem'>
  524. <input type='text' name='wanipaddr' id='wanipaddr' value='<% _echo $wanipaddr %>' <% ( [[ $wanproto =~ ('dhcp') ]] && _echo "readonly" ) %> placeholder='ip address'>
  525. <input type='text' name='wangw' id='wangw' value='<% _echo $wangw %>' <% ( [[ $wanproto =~ ('dhcp') ]] && _echo "readonly" ) %> placeholder='gateway/router'>
  526. <input type='text' name='wandns' id='wandns' value='<% _echo $wandns %>' <% ( [[ $wanproto =~ ('dhcp') ]] && _echo "readonly" ) %> placeholder='dns server'>
  527. </fieldset>
  528. </div>
  529. </div>
  530. <div>
  531. <input type='hidden' name='iface' value='wan' class='inline'>
  532. <input type='submit' id='wansubmit' value='Apply' class='inline'>
  533. </form>
  534. <form method='post' action='/admin/sgvpn' class='inline'>
  535. <input type='hidden' name='sgvpn' value='toggle' class='inline'>
  536. <input type='submit' value='<% [[ "$sgvpn" == '1' ]] && _echo 'Disable Superglue VPN service' || _echo 'Enable Superglue VPN service' %>' class='inline'>
  537. </form>
  538. </div>
  539. <span class='help'>help</span>
  540. </section>
  541. <section>
  542. <h2>Domain name</h2>
  543. <form method='post' action='/admin/sgddns' name='sgddns' id='sgddns'>
  544. <div style='display:inline-flex'>
  545. <div style='display:inline-block;'>
  546. <input type='text' name='sgddnssubdomain' placeholder='domain name' value='<% _echo $dsubdomain %>' class='inline'>
  547. </div>
  548. <div style='display:inline-block;'>
  549. <select name='sgddnsdomain' class='inline'>
  550. <option value='spgl.it' <% [[ "$ddomain" == 'spgl.it' ]] && _echo 'selected' %>>.spgl.it</option>
  551. <option value='spgl.cc' <% [[ "$ddomain" == 'spgl.cc' ]] && _echo 'selected' %>>.spgl.cc</option>
  552. <option value='superglue.it' <% [[ "$ddomain" == 'superglue.it' ]] && _echo 'selected' %>>.superglue.it</option>
  553. </select>
  554. </div>
  555. </div>
  556. <input type='submit' name='submit' value="Apply">
  557. <input type='hidden' name='sgddnsupdateurl' value='https://superglue.it/ddns/update'>
  558. </form>
  559. </section>
  560. <section>
  561. <h2>Local wireless network</h2>
  562. <form method='post' action='/admin/ssidchange'>
  563. <div style='display:inline-flex'>
  564. <div style='display:inline-block;'>
  565. <input type='text' name='lanssid' value='<% doUci get lanssid %>'>
  566. <input type='password' name='lankey' value='<% doUci get lankey %>'>
  567. </div>
  568. <div style='display:inline-block;'>
  569. <input type='text' name='lanipaddr' value='<% doUci get lanipaddr %>'>
  570. <input type='hidden' name='iface' value='lan'>
  571. </div>
  572. </div>
  573. <input type='submit' value='Apply' data-wait='Configuring..'>
  574. </form>
  575. <span class='help'>help</span>
  576. </section>
  577. <section>
  578. <h2>USB storage</h2>
  579. <% if findUsbstor; then %>
  580. <% if storageInfo; then %>
  581. <div>File storage: <% _echo "${_STOR[2]} used, ${_STOR[3]} available" %></div>
  582. <div>Swap: <% swapInfo && _echo "${_SWAP[1]}" || _echo '<b>n/a</b>' %></div>
  583. <% else %>
  584. <div>USB storage device must be initialized</div>
  585. <form method='post' action='/admin/usbinit'>
  586. <input type='hidden' name='dev' value='<% _echo $_USBDEV %>'>
  587. <input type='submit' value='Initialize'>
  588. </form>
  589. <% fi %>
  590. <% else %>
  591. <div><h3>USB storage device not found!</h3>Please check and try again</div>
  592. <% fi %>
  593. <span class='help'>help</span>
  594. </section>
  595. <section>
  596. <h2>Change password</h2>
  597. <form method='post' action='/admin/pwdchange'>
  598. <div style='display:inline-flex'>
  599. <div style='display:inline-block;'>
  600. <input type='text' name='usr' value='admin' readonly>
  601. </div>
  602. <div style='display:inline-block;'>
  603. <input type='password' name='pwd' placeholder='password' value=''>
  604. <input type='password' name='pwdd' placeholder='password again' value=''>
  605. </div>
  606. </div>
  607. <input type='submit' value='Apply'>
  608. </form>
  609. <span class='help'>help</span>
  610. </section>
  611. <section>
  612. <h2>Update firmware</h2>
  613. <form method='post' action='/admin/updatefw' enctype='multipart/form-data'>
  614. <div id='uploadbox'>
  615. <input id='uploadfile' placeholder='Select a file..' class='elem' disabled='disabled'>
  616. <input id='uploadbtn' class='elem' name='fwupload' type='file'>
  617. </div>
  618. <div style='display:inline-block;'>
  619. <input type='checkbox' name='fwreset' id='fw-resetbox' />
  620. <label for='fw-resetbox'>Reset all settings</label>
  621. </div>
  622. <input type='submit' value='Upload' data-wait='Uploading, do NOT interrupt!'>
  623. </form>
  624. <span class='help'>help</span>
  625. </section>
  626. <section>
  627. <h2></h2>
  628. <form action='/admin/rebootnow' method='post' class='inline'>
  629. <input type='hidden' name='reboot' value='now' class='inline'>
  630. <input type='submit' value='Reboot' class='inline'>
  631. </form>
  632. <form method='post' action='http://logout@<% _echo ${HTTP_HOST} %>/admin' class='inline'>
  633. <input type='submit' value='Logout' class='inline'>
  634. </form>
  635. </section>
  636. <div style='height:200px' name='big-spacer'></div>
  637. <section>
  638. <h2>Under the hood</h2>
  639. <h4>Memory</h4>
  640. <pre><% free %></pre>
  641. <hr>
  642. <h4>Storage</h4>
  643. <pre><% df -h %></pre>
  644. <hr>
  645. <h4>Recent log entries</h4>
  646. <pre><% tail -n10 /www/log/*.log %></pre>
  647. <hr>
  648. <h4>Environment</h4>
  649. <pre><% env %></pre>
  650. <hr>
  651. </section>
  652. <%
  653. footerBody
  654. exit 0
  655. %>