server.lua 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. local skynet = require "skynet"
  2. require "skynet.manager"
  3. local logger = require "logger"
  4. local stringify = require "stringify"
  5. local watchdog = skynet.localname(".ws_watchdog")
  6. local usercenter = skynet.localname(".usercenter")
  7. local skynet_sleep = skynet.sleep
  8. local cjson = require "cjson"
  9. local md5 = require "md5"
  10. local authz = {acc = "yytx", pwd = "lee@YY-Games.520"}
  11. -- Generate request data
  12. local content = {acc = authz.acc, pwd = authz.pwd, sign = false}
  13. content.sign = string.sub(md5.sumhexa(content.acc .. content.pwd), 9, 24)
  14. local whitelist = {
  15. ["192.168.1.23"] = true,
  16. ["192.168.1.127"] = true,
  17. ["192.168.1.50"] = true,
  18. ["14.29.136.211"] = true,
  19. ["192.168.1.41"] = true,
  20. ["222.212.88.4"] = true,
  21. }
  22. --请求方式: http://服务器ip地址:端口号/server?code=xxx&ntype=操作类型
  23. --示例: http://192.168.106.25:9002/server?code=a7fd102b76268069&ntype=1&error=错误描述
  24. local function server(args, ipaddr, header)
  25. logger.trace("处理来自主机 %s 的服务器维护请求", ipaddr)
  26. if not whitelist[ipaddr] then
  27. -- return { "403 - Forbidden" }
  28. -- return cjson.encode({errno = 403, host = header.host, info = "不信任ip"})
  29. end
  30. -- 验证gm账号
  31. -- local code = string.sub(args.code, 1, 16)
  32. -- if code ~= content.sign then
  33. -- return cjson.encode({state = 403, msg = "账号或密码错误"})
  34. -- end
  35. logger.trace("args:"..stringify(args))
  36. local ntype=tonumber(args.ntype)
  37. local error=args.error
  38. if ntype==1 then
  39. local status=skynet.call(watchdog,"lua","getstatus")
  40. if status then
  41. return cjson.encode({state = 0, msg= "维护状态"})
  42. else
  43. return cjson.encode({state = 0, msg= "正常状态"})
  44. end
  45. elseif ntype==2 then
  46. -- 通知 watchdog, 进入维护状态
  47. skynet.call(watchdog, "lua", "maintain")
  48. -- 通知所有在线玩家下线(标准下线流程)
  49. skynet.call(usercenter, "lua", "maintain",15)
  50. logger.warn("maintain (%s, %s)", args.code, ipaddr)
  51. return cjson.encode({state = 0,msg = "维护 成功"})
  52. elseif ntype==3 then
  53. skynet.call(watchdog,"lua","open")
  54. logger.warn("open (%s, %s)", args.code, ipaddr)
  55. -- return { "200 - OK" }
  56. return cjson.encode({state = 0, msg= "开放 成功"})
  57. elseif ntype==4 then
  58. -- 通知 watchdog, 进入维护状态
  59. skynet.call(watchdog, "lua", "maintain")
  60. -- 通知所有在线玩家下线(标准下线流程)
  61. skynet.call(usercenter, "lua", "maintain",15,error)
  62. logger.warn("maintain (%s, %s)", args.code, ipaddr)
  63. skynet_sleep(10)
  64. skynet.call(watchdog,"lua","open")
  65. return cjson.encode({state = 0, msg= "设置成功"})
  66. end
  67. end
  68. return server