[关闭]
@zwh8800 2017-08-23T02:18:44.000000Z 字数 4950 阅读 192557

用 thttpd+lua+cgi+jQuery mobile 搭建自动关机服务器

blog 归档 服务器


最近猎豹 wifi 共享精灵推出了一个 wifi 控制关机的功能. 我因为没有笔记本一直都用台式机 + linux nat + 无线 ap 来实现 wifi 共享 (见上次用虚拟机建立 NAT 曲线共享上网的经历忘了记录了). 所以看到这个功能之后一直眼红的不行, 在床上看视频到两三点再下去关电脑实在是破坏幸福感的事情. 所以嘛, 就又到了自己造轮子的时间.


首先分析一下需求, 很简单就是在手机上打开一个网页可以远程控制我的台式机关机.

所以必须要有一个 web 服务器, 另外一门 web 脚本语言也是必不可少的, 而且前端网页应该尽量漂亮, 并且适配手机.

首先 web 服务器应当尽量小巧, 一开始我用 shttpd 来做, 用了一天之后发现它对 http 验证支持的不很好. 所以便放弃他选择 thttpd 了. thttpd 也很小巧而且功能更多, 并且自称性能上不弱于 Apache.

然后脚本语言, 脚本语言的话我只会 lua 和 javascript, 如果用 javascript 的话需要用到 nodejs, 可是我有点不习惯 nodejs 的异步模型和一层一层的函数嵌套. 所以还是选 lua 吧. 而且 thttpd 支持 cgi, 可以和 lua 很好的适配

ok, 下面开始搭建环境. 因为对性能的要求不高, 所以决定在 Cygwin 下运行. 下载编译 thttpd, 直接 wget->./configure –prefix/usr->make->make install 就 ok 了.

中间可能在编译 htpasswd.c 文件时提示 getline 重定义. 直接把 htpasswd.c 文件里的所有 getline 改名成_getline 即可.

安装的时候会提示没有 / usr/man/man1 文件夹, 直接 mkdir /usr/man/man1 就 ok

thttpd 支持命令行启动同时也支持配置文件. 具体可以 man thttpd 来查看. 在源码包里的./contrib/redhat-rpm 文件夹有一个例子的配置文件. 我们把它改一下放到用户目录里:

  1. # This section overrides defaults
  2. dir=/home/Administrator/webroot
  3. #chroot
  4. user=Administrator# default = nobody
  5. logfile=/var/log/thttpd.log
  6. pidfile=/var/run/thttpd.pid
  7. # This section _documents_ defaults in effect
  8. # port=80
  9. # nosymlink# default = !chroot
  10. # novhost
  11. cgipat=/*.cgi
  12. # nothrottles
  13. # host=0.0.0.0
  14. # charset=iso-8859-1

然后就是安装 lua 了, Cygwin 源里直接就有, 装上即可.

在用户目录里建立一个 webroot 文件夹, 网站放这里.

建立两个文件, 一个叫 index.cgi, 一个叫 shutdown.cgi 内容如下:

  1. #!/bin/lua
  2. local Z_SHUTDOWN_FILE = "/tmp/zshutdown"
  3. print("Content-type: text/html")
  4. print()
  5. local is_shutting_down
  6. local time_to_shutdown
  7. local file = io.open(Z_SHUTDOWN_FILE, "r") --read模式, 可查看文件是否存在
  8. if file == nil then
  9. is_shutting_down = false
  10. else
  11. is_shutting_down = true
  12. local t1 = assert(file:read("*n"))
  13. time_to_shutdown = 30 - os.difftime(os.time(), t1)
  14. if time_to_shutdown < 0 then
  15. is_shutting_down = false
  16. os.remove(Z_SHUTDOWN_FILE)
  17. end
  18. end
  19. print[=[
  20. <!Doctype html>
  21. <html xmlns=https://www.w3.org/1999/xhtml>
  22. <head>
  23. <meta charset="utf-8">
  24. <title>zzZ的自动关机</title>
  25. <meta name="viewport" content="user-scalable=no, width=device-width, initial-scale=1.0, maximum-scale=1.0">
  26. <meta http-equiv="mobile-agent" content="format=html5;">
  27. <link rel="stylesheet" href="https://code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.min.css">
  28. <script src="https://code.jquery.com/jquery-1.8.3.min.js"></script>
  29. <script src="https://code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.min.js"></script>
  30. </head>
  31. <body>
  32. <div data-role="page" id="main">
  33. <div data-role="header" data-position="fixed">
  34. <h1>zzZ的自动关机</h1>
  35. </div>
  36. <div style="text-align:center;" data-role="content">
  37. ]=]
  38. if is_shutting_down then
  39. print[=[
  40. <p>您的电脑将在<span id="time" style="color:red">]=] print(time_to_shutdown); print[=[</span>秒后自动关机</p>
  41. <script>
  42. var t = ]=] print(time_to_shutdown); print[=[;
  43. function decrease()
  44. { if (t > 0)$("#time").text(--t); }
  45. setInterval("decrease()", 1000);
  46. </script>
  47. <p>按动按钮来取消关机:</p>
  48. <a href="shutdown.cgi" data-rel="dialog" data-role="button" data-inline="true" data-icon="info">取消关机</a>
  49. ]=]
  50. else
  51. print[=[
  52. <p>按动按钮来进行关机:</p>
  53. <a href="#shutdown_confirm" data-rel="dialog" data-role="button" data-inline="true" data-icon="info">关机</a>
  54. ]=]
  55. end
  56. print[=[
  57. </div>
  58. <div data-role="footer" data-position="fixed">
  59. <h1>Powered by zzZ</h1>
  60. </div>
  61. </div>
  62. <div data-role="page" id="shutdown_confirm">
  63. <div data-role="header">
  64. <h1>真的要进行关机?</h1>
  65. </div>
  66. <div data-role="content">
  67. <center>
  68. <a href="shutdown.cgi" data-rel="dialog" data-role="button" data-inline="true" data-icon="info">确认</a>
  69. <a href="#main" data-role="button" data-inline="true" data-icon="delete">取消</a>
  70. </center>
  71. </div>
  72. <div data-role="footer">
  73. <h1>Powered by zzZ</h1>
  74. </div>
  75. </div>
  76. </body>
  77. </html>
  78. ]=]
  1. #!/bin/lua
  2. local Z_SHUTDOWN_FILE = "/tmp/zshutdown"
  3. print("Content-type: text/html")
  4. print()
  5. local is_shutting_down
  6. local file = io.open(Z_SHUTDOWN_FILE, "r") --read模式, 可查看文件是否存在
  7. if file == nil then
  8. is_shutting_down = false
  9. file = io.open(Z_SHUTDOWN_FILE, "w") --write模式, 打开文件并写入
  10. file:write(os.time())
  11. file:close()
  12. os.execute('shutdown -s 30')
  13. else
  14. is_shutting_down = true
  15. os.remove(Z_SHUTDOWN_FILE)
  16. os.execute('shutdown -a')
  17. end
  18. print[=[
  19. <!Doctype html>
  20. <html xmlns=https://www.w3.org/1999/xhtml>
  21. <head>
  22. <meta charset="utf-8">
  23. <title>关机命令已接受</title>
  24. <meta name="viewport" content="user-scalable=no, width=device-width, initial-scale=1.0, maximum-scale=1.0">
  25. <meta http-equiv="mobile-agent" content="format=html5;">
  26. <link rel="stylesheet" href="https://code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.min.css">
  27. <script src="https://code.jquery.com/jquery-1.8.3.min.js"></script>
  28. <script src="https://code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.min.js"></script>
  29. </head>
  30. <body>
  31. <div data-role="page" id="main">
  32. <div data-role="header" data-position="fixed">
  33. <h1>
  34. ]=]
  35. if is_shutting_down then
  36. print "关机已取消"
  37. else
  38. print "关机命令已接受"
  39. end
  40. print[=[
  41. </h1>
  42. </div>
  43. <div data-role="content">
  44. <center>
  45. ]=]
  46. if is_shutting_down then
  47. print[=[
  48. <p>关机命令已取消</p>
  49. <a href="javascript:location.reload(true)" data-role="button" data-inline="true" data-icon="info">返回</a>
  50. ]=]
  51. else
  52. print[=[
  53. <p>您的电脑将在<span id="time" style="color:red">30</span>秒后自动关机</p>
  54. <script>
  55. var t = 30;
  56. function decrease()
  57. { if (t > 0)$("#time").text(--t); }
  58. setInterval("decrease()", 1000);
  59. </script>
  60. <a href="javascript:location.reload(true)" data-role="button" data-inline="true" data-icon="info">返回</a>
  61. ]=]
  62. end
  63. print[=[
  64. </center>
  65. </div>
  66. <div data-role="footer" data-position="fixed">
  67. <h1>Powered by zzZ</h1>
  68. </div>
  69. </div>
  70. </body>
  71. </html>
  72. ]=]

ok 了, 效果如图:
image_1bl0277mi94vhuh2v11d3gar49.png-219.7kB
image_1bl028649hrj10b9p66155e14jtm.png-153.8kB
image_1bl028n31qgj1ikq13e81ktcqam13.png-130kB
image_1bl029cp41lakb6l1f1m1tfmrf91g.png-136.7kB
image_1bl02a0ek97d1m54foe9suips1t.png-177.8kB
image_1bl02ajhhg2v1vqc1kmbmrm1ilh2a.png-124.4kB

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注