• openresty 动态黑白名单



    openresty 动态黑白名单

                

                     

                                      

    动态黑白名单

               

                       

    1. 客户端请求发送到openresty,openresty需要验证黑白名单;
    2. 可将黑白名单存放在redis中,openresty直接与redis交互,验证黑白名单;
    3. openresty也可将黑白名单加载到共享内存,在内存中验证黑白名单;
    4. 验证通过后,执行后续请求;
    5. 验证不通过,向客户端返回403(request forbidden)错误

             

                      

                                      

    使用示例

               

    创建redis 容器

    docker run -it -d --net fixed --ip 172.18.0.81 --name redis-black redis
    

               

    default.conf

    1. server {
    2. listen 80;
    3. server_name localhost;
    4. location / {
    5. root /usr/local/openresty/nginx/html;
    6. index index.html index.htm;
    7. }
    8. location /test {
    9. access_by_lua_block {
    10. local redis = require 'resty.redis';
    11. local red = redis:new();
    12. red:set_timeouts(1000, 1000, 1000);
    13. local ok, err = red:connect("172.18.0.81", 6379);
    14. if not ok then
    15. ngx.log(ngx.ERR, "failed to connect: ", err)
    16. return
    17. end
    18. local remote_addr = ngx.var.remote_addr;
    19. ngx.log(ngx.ERR, "remote_addr ==> ", remote_addr);
    20. ngx.log(ngx.ERR, "red:sismember('balck-list', remote_addr)");
    21. res, err = red:sismember('black-list', remote_addr);
    22. ngx.log(ngx.ERR, "是否在黑名单 ==> ", res);
    23. if res == 1 then
    24. ngx.log(ngx.ERR, "输出 403");
    25. ngx.exit(ngx.HTTP_FORBIDDEN);
    26. else
    27. ngx.log(ngx.ERR, "输出 200");
    28. ngx.exit(ngx.OK);
    29. end
    30. }
    31. echo "test";
    32. content_by_lua_block {
    33. ngx.say("test");
    34. }
    35. }
    36. location /add {
    37. content_by_lua_block {
    38. local redis = require 'resty.redis';
    39. local red = redis:new();
    40. red:set_timeouts(1000, 1000, 1000);
    41. local ok, err = red:connect("172.18.0.81", 6379);
    42. if not ok then
    43. ngx.say("failed to connect: ", err)
    44. return
    45. end
    46. local members = red:smembers("black-list");
    47. if members then
    48. ngx.say("添加前黑名单 ==> ", members);
    49. end
    50. local ip = ngx.var.arg_ip;
    51. ngx.say("red:sadd('balck-list', ip)");
    52. local res, err = red:sadd('black-list', ip);
    53. if not res then
    54. ngx.say("failed to sadd: ", err)
    55. return
    56. end
    57. local members = red:smembers("black-list");
    58. if members then
    59. ngx.say("添加后黑名单 ==> ", members);
    60. end
    61. }
    62. }
    63. location /delete {
    64. content_by_lua_block {
    65. local redis = require 'resty.redis';
    66. local red = redis:new();
    67. red:set_timeouts(1000, 1000, 1000);
    68. local ok, err = red:connect("172.18.0.81", 6379);
    69. if not ok then
    70. ngx.say("failed to connect: ", err)
    71. return
    72. end
    73. local members = red:smembers("black-list");
    74. if members then
    75. ngx.say("删除前黑名单 ==> ", members);
    76. end
    77. local ip = ngx.var.arg_ip;
    78. ngx.say("red:srem('balck-list', ip)");
    79. local res, err = red:srem('black-list', ip);
    80. if not res then
    81. ngx.say("failed to srem: ", err)
    82. return
    83. end
    84. local members = red:smembers("black-list");
    85. if members then
    86. ngx.say("删除后黑名单 ==> ", members);
    87. end
    88. }
    89. }
    90. error_page 500 502 503 504 /50x.html;
    91. location = /50x.html {
    92. root /usr/local/openresty/nginx/html;
    93. }
    94. }

               

    创建openresty 容器

    1. docker run -it -d --net fixed --ip 172.18.0.82 -p 6001:80 \
    2. -v /Users/huli/lua/openresty/black/default.conf:/etc/nginx/conf.d/default.conf \
    3. --name open-black lihu12344/openresty

               

    使用测试

    1. # 初始黑名单为空
    2. huli@hudeMacBook-Pro black % curl localhost:6001/test
    3. test
    4. # 添加黑名单
    5. huli@hudeMacBook-Pro black % curl --location --request GET 'localhost:6001/add?ip=172.18.0.1'
    6. 添加前黑名单 ==> gtlx
    7. red:sadd('balck-list', ip)
    8. 添加后黑名单 ==> 172.18.0.1gtlx
    9. huli@hudeMacBook-Pro black % curl localhost:6001/test
    10. <head>403 Forbidden
    11. 403 Forbidden


    12. openresty/1.21.4.1
    13. # 删除黑名单
    14. huli@hudeMacBook-Pro black % curl --location --request GET 'localhost:6001/delete?ip=172.18.0.1'
    15. 删除前黑名单 ==> 172.18.0.1gtlx
    16. red:srem('balck-list', ip)
    17. 删除后黑名单 ==> gtlx
    18. huli@hudeMacBook-Pro black % curl localhost:6001/test
    19. test

                  

                          

  • 相关阅读:
    Spring 事务
    P95陷阱
    服务器怎么远程连接控制
    11-注意力机制
    全球名校AI课程库(3)| Stanford斯坦福 · 深度学习与计算机视觉课程『CS231n』
    这几种好用的英语图片翻译方法,你们千万不能错过
    DDOS直接攻击系统资源
    函数 RFC 函数 BAPI
    HOW TO ADD P-VALUES ONTO A GROUPED GGPLOT USING THE GGPUBR R PACKAGE
    串的匹配 (Brute - Force 算法)
  • 原文地址:https://blog.csdn.net/weixin_43931625/article/details/126057418