log.lua 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. require "luci.util"
  2. require "nixio.fs"
  3. require "luci.sys"
  4. require "luci.http"
  5. f = SimpleForm("logview")
  6. f.reset = false
  7. f.submit = false
  8. f:append(Template("shadowsocksr/log"))
  9. -- 自定义 log 函数
  10. function log(...)
  11. local result = os.date("%Y-%m-%d %H:%M:%S: ") .. table.concat({...}, " ")
  12. local f, err = io.open("/var/log/ssrplus.log", "a")
  13. if f and err == nil then
  14. f:write(result .. "\n")
  15. f:close()
  16. end
  17. end
  18. -- 创建备份与恢复表单
  19. fb = SimpleForm('backup-restore')
  20. fb.reset = false
  21. fb.submit = false
  22. s = fb:section(SimpleSection, translate("Backup and Restore"), translate("Backup or Restore Client and Server Configurations.") ..
  23. "<br><font style='color:red'><b>" ..
  24. translate("Note: Restoring configurations across different versions may cause compatibility issues.") ..
  25. "</b></font>")
  26. s.anonymous = true
  27. s:append(Template("shadowsocksr/backup_restore"))
  28. -- 定义备份目标文件和目录
  29. local backup_targets = {
  30. files = {
  31. "/etc/config/shadowsocksr"
  32. },
  33. dirs = {
  34. "/etc/ssrplus"
  35. }
  36. }
  37. local file_path = '/tmp/shadowsocksr_upload.tar.gz'
  38. local temp_dir = '/tmp/shadowsocksr_bak'
  39. local fd
  40. -- 处理文件上传
  41. luci.http.setfilehandler(function(meta, chunk, eof)
  42. if not fd and meta and meta.name == "ulfile" and chunk then
  43. -- 初始化上传处理
  44. luci.sys.call("rm -rf " .. temp_dir)
  45. nixio.fs.remove(file_path)
  46. fd = nixio.open(file_path, "w")
  47. luci.sys.call("echo '' > /var/log/ssrplus.log")
  48. end
  49. if fd and chunk then
  50. fd:write(chunk)
  51. end
  52. if eof and fd then
  53. fd:close()
  54. fd = nil
  55. if nixio.fs.access(file_path) then
  56. log(" * shadowsocksr 配置文件上传成功…") -- 使用自定义的 log 函数
  57. luci.sys.call("mkdir -p " .. temp_dir)
  58. if luci.sys.call("tar -xzf " .. file_path .. " -C " .. temp_dir) == 0 then
  59. -- 处理文件还原
  60. for _, target in ipairs(backup_targets.files) do
  61. local temp_file = temp_dir .. target
  62. if nixio.fs.access(temp_file) then
  63. luci.sys.call(string.format("cp -f '%s' '%s'", temp_file, target))
  64. log(" * 文件 " .. target .. " 还原成功…") -- 使用自定义的 log 函数
  65. end
  66. end
  67. -- 处理目录还原
  68. for _, target in ipairs(backup_targets.dirs) do
  69. local temp_dir_path = temp_dir .. target
  70. if nixio.fs.access(temp_dir_path) then
  71. luci.sys.call(string.format("cp -rf '%s'/* '%s/'", temp_dir_path, target))
  72. log(" * 目录 " .. target .. " 还原成功…") -- 使用自定义的 log 函数
  73. end
  74. end
  75. log(" * shadowsocksr 配置还原成功…") -- 使用自定义的 log 函数
  76. log(" * 重启 shadowsocksr 服务中…\n") -- 使用自定义的 log 函数
  77. luci.sys.call('/etc/init.d/shadowsocksr restart > /dev/null 2>&1 &')
  78. else
  79. log(" * shadowsocksr 配置文件解压失败,请重试!") -- 使用自定义的 log 函数
  80. end
  81. else
  82. log(" * shadowsocksr 配置文件上传失败,请重试!") -- 使用自定义的 log 函数
  83. end
  84. -- 清理临时文件
  85. luci.sys.call("rm -rf " .. temp_dir)
  86. nixio.fs.remove(file_path)
  87. end
  88. end)
  89. return f, fb