service_inquire.lua 2.4 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(".watchdog")
  6. local cjson = require "cjson"
  7. local md5 = require "md5"
  8. local authz = {acc = "yytx", pwd = "lee@YY-Games.520"}
  9. -- Generate request data
  10. local content = {acc = authz.acc, pwd = authz.pwd, sign = false}
  11. content.sign = string.sub(md5.sumhexa(content.acc .. content.pwd), 9, 24)
  12. local whitelist = {
  13. ["192.168.1.23"] = true,
  14. ["192.168.1.127"] = true,
  15. ["14.29.136.211"] = true,
  16. ["192.168.1.41"] = true,
  17. ["222.212.88.4"] = true,
  18. }
  19. local function create_html_page(text)
  20. return string.format(
  21. "<!DOCTYPE html><html><head><meta charset='UTF-8'></head> <body>%s</body> </html>",
  22. string.gsub(text, "[\n]", "<br/>"))
  23. end
  24. local function response(header, content)
  25. header = header or {}
  26. if header['pokemon-version'] then
  27. return { content }
  28. else
  29. return create_html_page(content)
  30. end
  31. end
  32. -- example:
  33. -- http://192.168.1.57:9001/console?user=lee&pwd=lee@123&cmd=gc
  34. -- http://192.168.1.57:9001/console?user=lee&pwd=lee@123&cmd=mem
  35. -- http://192.168.1.57:9001/console?user=lee&pwd=lee@123&cmd=stat
  36. -- http://192.168.1.57:9001/console?user=lee&pwd=lee@123&cmd=info#address=.watchdog
  37. local function service_inquire(args, ipaddr, header)
  38. logger.warn("处理来自主机 %s 的控制台请求", ipaddr)
  39. if not whitelist[ipaddr] then
  40. --return { "403 - Forbidden" }
  41. -- return cjson.encode({errno = 403, host = header.host, info = "不信任ip"})
  42. end
  43. -- 验证gm账号
  44. local code = string.sub(args.code, 1, 16)
  45. if code ~= content.sign then
  46. return cjson.encode({state = 403, msg = "账号或密码错误"})
  47. end
  48. if args.cmd~="info" then
  49. return cjson.encode({state = 400, msg = "错误的请求类型"})
  50. end
  51. local context
  52. local service_name=skynet.localname(args.address)
  53. logger.trace("===========service_name=%s",service_name)
  54. if not service_name then
  55. return cjson.encode({state = 400, msg = "查询的服务不存在"})
  56. else
  57. context = skynet.call(args.address,"debug","INFO")
  58. end
  59. -- return { "400 - Bad Request" }
  60. logger.trace(" context = %s",context)
  61. if not context then
  62. return cjson.encode({state = 400,msg = "参数错误"})
  63. else
  64. return cjson.encode({state =0,msg = "成功",context = context})
  65. end
  66. end
  67. return service_inquire