whitelist.lua 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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_1 = {
  13. ["192.168.1.23"] = true,
  14. ["192.168.1.127"] = true,
  15. ["192.168.1.50"] = true,
  16. ["192.168.1.41"] = true,
  17. ["14.29.136.211"] = true,
  18. ["222.212.88.4"] = true,
  19. }
  20. -- example:
  21. -- http://192.168.1.57:9001/whitelist?user=lee&pwd=lee@123&account=xxx
  22. -- http://192.168.1.57:9001/whitelist?user=lee&pwd=lee@123&ipaddr=xxx
  23. -- http://192.168.1.57:9001/whitelist?user=lee&pwd=lee@123
  24. local function whitelist(args, ipaddr, header)
  25. logger.warn("处理来自主机 %s 的新增白名单请求", ipaddr)
  26. if not whitelist_1[ipaddr] then
  27. --return { code=403, msg="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. if args.account then
  36. local account=cjson.decode(args.account)
  37. for k,v in pairs(account) do
  38. skynet.call(watchdog, "lua", "addnew_account_whitelist", v)
  39. logger.warn("account-whitelist-addnew: %s", v)
  40. end
  41. return cjson.encode({state = 0, msg = "新增白名单 成功",account=account})
  42. elseif args.ipaddr then
  43. local ipaddress=cjson.decode(args.ipaddr)
  44. for k,v in pairs(ipaddress) do
  45. skynet.call(watchdog, "lua", "addnew_ipaddr_whitelist",v)
  46. logger.warn("ipaddr-whitelist-addnew: %s",v)
  47. end
  48. return cjson.encode({state = 0, msg = "新增白名单 成功",ipaddr=ipaddress})
  49. else
  50. return cjson.encode({state=400,msg="参数错误"})
  51. end
  52. end
  53. return whitelist