modify_time.lua 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. local skynet = require "skynet"
  2. require "skynet.manager"
  3. local logger = require "logger"
  4. local stringify = require "stringify"
  5. local usercenter = skynet.localname(".usercenter")
  6. local logind = skynet.localname(".loginserver")
  7. local redisdriver = require "skynet.db.redis"
  8. local namecenter = skynet.localname(".namecenter")
  9. local md5 = require "md5"
  10. local cjson = require "cjson"
  11. local authz = {acc = "yytx", pwd = "lee@YY-Games.520"}
  12. -- Generate request data
  13. local content = {acc = authz.acc, pwd = authz.pwd, sign = false}
  14. content.sign = string.sub(md5.sumhexa(content.acc .. content.pwd), 9, 24)
  15. local whitelist = {
  16. ["192.168.1.23"] = true,
  17. ["192.168.1.127"] = true,
  18. ["192.168.1.41"] = true,
  19. ["222.212.88.4"] = true,
  20. }
  21. --[[
  22. byname = zlf -- 玩家的名字
  23. ]]
  24. -- example:
  25. --[[
  26. type = 1/2 1: 获取当前时间 2:设置时间 3:指定格式修改时间
  27. time = utc 目标时间
  28. http://192.168.1.51:9001/set_time?user=yytx&pwd=lee@YY-Games.520&arg={"type":1,"time":0}
  29. http://192.168.1.51:9001/set_time?user=yytx&pwd=lee@YY-Games.520&arg={"type":2,"time":1531822162}
  30. http://192.168.1.51:9001/set_time?user=yytx&pwd=lee@YY-Games.520&arg={"type":3,"time":"2018-7-17 17:44:50"}
  31. ]]
  32. local function modify_time(args, ipaddr, header)
  33. logger.trace("处理来自主机 %s 的 服务器时间设置请求", ipaddr)
  34. if not whitelist[ipaddr] then
  35. --return { "403 - Forbidden" }
  36. -- return cjson.encode({errno = 403, host = header.host, info = "不信任ip"})
  37. end
  38. -- 验证gm账号
  39. local code = string.sub(args.code, 1, 16)
  40. if code ~= content.sign then
  41. return cjson.encode({state = 403, msg = "账号或密码错误"})
  42. end
  43. local par = cjson.decode(args.arg)
  44. if par.type == 1 then -- 获取系统时间
  45. -- 直接返回即可
  46. elseif par.type == 2 then -- 设置系统时间
  47. -- 检查时间
  48. if par.time <= os.time() then
  49. return cjson.encode({state = 403,msg = "errno less than now time"})
  50. end
  51. -- 转换时间格式
  52. local format = os.date("%Y-%m-%d %X", par.time)
  53. local key = string.format("sudo date -s '%s'", format)
  54. logger.trace(" === %s", key)
  55. local ret,ret2 = os.execute(key)
  56. logger.trace("ret = %s, ret2 = %s", ret, ret2)
  57. if not ret then
  58. -- return cjson.encode({errno = 403, host = header.host, info = "errno args.time"})
  59. return cjson.encode({state = 403, state = "errno abnormal"})
  60. end
  61. elseif par.type == 3 then -- 设置系统时间
  62. logger.trace(" === --- %s", par.time)
  63. local key = string.format("sudo date -s '%s'", par.time)
  64. logger.trace(" === %s", key)
  65. local ret,ret2 = os.execute(key)
  66. logger.trace("ret = %s, ret2 = %s", ret, ret2)
  67. if not ret then
  68. return cjson.encode({state = 403, msg = "errno args.time"})
  69. end
  70. else
  71. return cjson.encode({state = 403, msg = "errno ars.type"})
  72. end
  73. return cjson.encode({state = 0, msg = "set succeed",type = par.type, time = os.time(),format = os.date("%Y-%m-%d %X", os.time())})
  74. end
  75. return modify_time