online.lua 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. local skynet = require "skynet"
  2. require "skynet.manager"
  3. local logger = require "logger"
  4. local stringify = require "stringify"
  5. local watchdog = skynet.localname(".watchdog")
  6. local cjson = require "cjson"
  7. local md5 = require "md5"
  8. local redisdriver = require "skynet.db.redis"
  9. local s_public = skynet.localname(".public") -- PK3-1384 增加区服平均等级展示
  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. ["14.29.136.211"] = true,
  18. ["192.168.1.41"] = true,
  19. ["222.212.88.4"] = true,
  20. }
  21. local redis
  22. local status = 0
  23. local function initialize()
  24. status = 1
  25. local conf = assert(option.redis)
  26. redis = redisdriver.connect(conf)
  27. redis:select(0)
  28. end
  29. -- http://192.168.1.57:9001/console?user=lee&pwd=lee@123&cmd=info#address=.watchdog
  30. --请求方式: http://服务器ip地址:端口号/online?code=xxx
  31. --示例: http://192.168.1.102:9002/online?code=xxx
  32. local function online(args, ipaddr, header)
  33. logger.warn("处理来自主机 %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 content
  44. content = skynet.call(args.address or ".watchdog", "debug", "INFO")
  45. logger.trace(" content = %s", content)
  46. if not content then
  47. return cjson.encode({state = 400, msg = "获取服务器失败"})
  48. else
  49. if status == 0 then
  50. initialize()
  51. end
  52. local ret = redis:KEYS("character:*")
  53. content.offline_avg_number = #ret
  54. local average_lv = skynet.call(s_public, "lua", "compensate", "gm_average_lv") -- PK3-1384 增加区服平均等级展示
  55. content.average_lv = average_lv
  56. return cjson.encode({state = 0, msg = "success",content=content})
  57. end
  58. end
  59. return online