[关闭]
@dooy 2016-11-25T07:40:53.000000Z 字数 3860 阅读 226

利用Nginx lua 判断IP地域属性再进行域名转型

Nginx 批改网


1.安装nginx+lua

参考 https://www.zybuluo.com/dooy/note/318900

2.下载ip库

http://www.ipip.net/download.html 下载IP库
将包中的 17monipdb.bat 发到/usr/local/nginx/lua/17monipdb.dat

3.配置

将2个代码文件都放到 /usr/local/nginx/lua/下 在配置nginx
首先在nginx.conf http 中加入

  1. lua_shared_dict config 1m;
  2. init_by_lua_file /usr/local/nginx/lua/ipmid.lua;

然后找到 location ~ .*\.php?$
在 他的区域里面加入
access_by_lua_file /usr/local/nginx/lua/istw.lua;
表示只有匹配php的时候才去查下IP;css 图片 html 这些就不去查下ip了

4.代码文件

有2个比较重要的文件ipmid.lua 和 istw.lua

4.1 ipmid.lua

  1. #!/usr/bin/env luajit
  2. local bit = require("bit")
  3. function byteToUint32(a,b,c,d)
  4. local _int = 0
  5. if a then
  6. _int = _int + bit.lshift(a, 24)
  7. end
  8. _int = _int + bit.lshift(b, 16)
  9. _int = _int + bit.lshift(c, 8)
  10. _int = _int + d
  11. if _int >= 0 then
  12. return _int
  13. else
  14. return _int + math.pow(2, 32)
  15. end
  16. end
  17. local ipBinaryFilePath = "/usr/local/nginx/lua/17monipdb.dat"
  18. function IpLocation(ipstr)
  19. local ip1,ip2,ip3,ip4 = string.match(ipstr, "(%d+).(%d+).(%d+).(%d+)")
  20. local ip_uint32 = byteToUint32(ip1, ip2, ip3, ip4)
  21. local file = io.open(ipBinaryFilePath)
  22. if file == nil then
  23. return nil
  24. end
  25. local str = file:read(4)
  26. local offset_len = byteToUint32(string.byte(str, 1), string.byte(str, 2),string.byte(str, 3),string.byte(str, 4))
  27. local indexBuffer = file:read(offset_len - 4)
  28. local tmp_offset = ip1 * 4
  29. local start_len = byteToUint32(string.byte(indexBuffer, tmp_offset + 4), string.byte(indexBuffer, tmp_offset + 3), string.byte(indexBuffer, tmp_offset + 2), string.byte(indexBuffer, tmp_offset + 1))
  30. local max_comp_len = offset_len - 1028
  31. start = start_len * 8 + 1024 + 1
  32. local find_uint32 = 0
  33. local index_offset = -1
  34. local index_length = -1
  35. local min= (start-1)/8
  36. local max= max_comp_len/8;
  37. local mid,yu= math.modf( (min+max)/2);
  38. local mark = -1;
  39. local cnt=0
  40. --if start >0 then return cnt.." mark="..mark.."\tmin="..min.."\tstart="..start.."\tend="..max_comp_len end
  41. while 1 do
  42. cnt=cnt+1
  43. start= mid*8+1
  44. find_uint32 = byteToUint32(string.byte(indexBuffer, start), string.byte(indexBuffer, start+1),string.byte(indexBuffer, start+2),string.byte(indexBuffer, start+3))
  45. if ip_uint32 > find_uint32 then
  46. min=mid
  47. elseif ip_uint32 < find_uint32 then
  48. max = mid
  49. else
  50. mark= mid
  51. break;
  52. end
  53. local mid_new,yu = math.modf( (min+max)/2);
  54. if mid == mid_new then
  55. mark= max;
  56. break;
  57. end
  58. mid = mid_new
  59. end
  60. start = mark*8+1;
  61. index_offset = byteToUint32(0, string.byte(indexBuffer, start+6),string.byte(indexBuffer, start+5),string.byte(indexBuffer, start+4))
  62. index_length = string.byte(indexBuffer, start+7)
  63. --[[
  64. while start < max_comp_len do
  65. find_uint32 = byteToUint32(string.byte(indexBuffer, start), string.byte(indexBuffer, start+1),string.byte(indexBuffer, start+2),string.byte(indexBuffer, start+3))
  66. if ip_uint32 <= find_uint32 then
  67. index_offset = byteToUint32(0, string.byte(indexBuffer, start+6),string.byte(indexBuffer, start+5),string.byte(indexBuffer, start+4))
  68. index_length = string.byte(indexBuffer, start+7)
  69. break
  70. end
  71. start = start + 8
  72. end
  73. --]]
  74. if index_offset == -1 or index_length == -1 then
  75. return "hello"
  76. end
  77. local offset = offset_len + index_offset - 1024
  78. file:seek("set", offset)
  79. local restr= file:read(index_length);
  80. return restr.."\tcnt="..cnt
  81. end
  82. --print(IpLocation("210.34.3.4"))

4.2 文件 istw.lua

  1. --ngx.log(ngx.ERR,"LOOKIP:", ngx.req.get_headers()["Host"], "\thost:",ngx.var.host,"\tmethod=", ngx.req.get_method() ,"\trq_uri=", ngx.var.request_uri );
  2. local _isgo= ngx.req.get_method()=="GET" and (ngx.var.request_uri=="/" or ngx.var.request_uri=="/index.php" ) and (ngx.var.host=="pigai.org" or ngx.var.host=="117.79.131.103" )
  3. if true ~= _isgo then
  4. return
  5. end
  6. local args = ngx.req.get_uri_args()
  7. local ip = ngx.var.remote_addr
  8. if args["ip"] then
  9. ip = args["ip"]
  10. end
  11. local ipstr= IpLocation(ip);
  12. ngx.log(ngx.ERR,"IpLocation:",ipstr )
  13. local pos,len= string.find(ipstr,"台湾")
  14. if pos ~= nil then
  15. ngx.log(ngx.ERR," to twpigai");
  16. ngx.redirect("http://tw.pigai.org", 302)
  17. --return ngx.exit(403)
  18. end
  19. local pos,len= string.find(ipstr,"香港")
  20. if pos ~= nil then
  21. ngx.log(ngx.ERR," to twpigai");
  22. ngx.redirect("http://tw.pigai.org", 302)
  23. --return ngx.exit(403)
  24. end
  25. local pos,len= string.find(ipstr,"澳门")
  26. if pos ~= nil then
  27. ngx.log(ngx.ERR," to twpigai");
  28. ngx.redirect("http://tw.pigai.org", 302)
  29. --return ngx.exit(403)
  30. end
  31. return
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注