Cloud Native应用交付

  • 首页
  • 关于本站
  • 个人介绍
  • Downloads
  • Repo
    • Github
    • Container
  • F5
    • F5 Python SDK
    • F5-container
    • F5-LBaaS
  • 社交
    • 联系我
    • 微信/微博
    • 公众号
    • 打赏赞助
行至水穷处 坐看云起时
Cloud Native Application Services: cnadn.net
  1. 首页
  2. F5-Tech tips
  3. 正文

[FW]基于IP+URI的防秒杀irule

2018年01月22日 10558点热度 3人点赞 0条评论

IRULE

THE TABLE COMMAND

So that we can rate-limit traffic the iRule command 'table' is used. The table command (as the name suggests) provides the ability to create, delete, and append tables, along with being able to define timeouts for each table entry.

Each table that's created contains the following columns.

  • Key - This is a key that is assigned to the table entry and reference during table look up.
  • Value - This is a value that is assigned to the key.
  • Timeout - This is the timeout for the key.
  • Lifetime - This is the lifetime for the key. The difference between the timeout and the lifetime value is that at the point of table look up the timeout value is reset.
  • Touch Time - This value indicates when the key entry was last touched. This value is mainly used internally.
  • Create Time - This value indicates when the key entry was created. This value is mainly used internally.

TABLE EXAMPLE

Below shows an example of the table that is created.

Table : 8.8.8.8:/database/lookup

KEY VALUE TIMEOUT LIFETIME TOUCH TIME CREATE TIME
1 8.8.8.8:/database/lookup indefinite  1 1223830112 1223830112
2 8.8.8.8:/database/lookup indefinite  1 1223830110 1223830110
3 8.8.8.8:/database/lookup indefinite  1 1223830107 1223830107

 

EXAMPLE

Within our example we will rate-limit on a per IP, per URI basis.

Below is the iRule:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# Function : RateLimit HTTP GET requests per IP, per URI
# Created : 29/10/12
 
when RULE_INIT {
    set static::maxRate 3
    set static::windowSecs 1 
}
 
when HTTP_REQUEST {
    if { ([HTTP::method] eq "GET") and ([class match [string tolower [HTTP::uri]] starts_with DATAGROUP-RATELIMIT-URI] ) } {
 
        # whitelist
        if { [class match [IP::client_addr] equals DATAGROUP-RATELIMIT-WHITELIST] }{
           return
        }
 
        # set variables
        set limiter [string tolower [HTTP::uri]]
        set clientip_limitervar [IP::client_addr]:$limiter
        set get_count [table key -count -subtable $clientip_limitervar]
 
        # main condition
        if { $get_count < $static::maxRate } {
            incr get_count 1
             table set -subtable $clientip_limitervar $get_count $clientip_limitervar indefinite $static::windowSecs
        } else {
            log local0. "$clientip_limitervar has exceeded the number of requests allowed."
            drop
            return
        }
    }
}

 

HOW IT WORKS

The way in which this iRule works is by creating a new table, named IP + URI and appending subsequent requests to the same table.
Each entry has a lifetime value and is removed once reached.

Finally the table is then counted to ensure that the total amount of requests for a given IP and URI has not exceeded the configured threshold.

DATAGROUPS

Within this iRule 2 datagroups are used.

DATAGROUP-RATELIMIT-WHITELIST - (Type:Address) - Contains addresses that are not to be rate-limited.
DATAGROUP-RATELIMIT-URI - (Type:String) - Contains URIs that are to be rate-limited. If all URIs should be rate-limited then just add an / to this datagroup.

CUSTOMIZATION

In order to customize this iRule review the following 2 variables:

1
2
set limiter [string tolower [HTTP::uri]]
set clientip_limitervar [IP::client_addr]:$limiter

 

RATELIMIT USING DIFFERENT ATTRIBUTES

If you wanted to ratelimit on a per IP, per hostname basis then then the line would be:

1
set limiter [string tolower [HTTP::host]]

 

RATELIMIT USING A SINGLE ATTRIBUTE

Likewise if you wanted to only rate-limit of a per IP basis then the ':limiter' would be removed from the 'clientip_limitvar' variable.

RATELIMIT CONNECTIONS

If you want to rate limit traffic that is not HTTP based or the traffic is encrypted (SSL) then the following iRule can be used (click here).

GOOD TO KNOW

MEMORY

It is worth nothing that the F5 places NO limits on the amount of memory that can be consumed when using the table command. Because of this it is recommended that once you have implemented this iRule that the memory of the device is monitored via the use of the command "show sys mem" within TMSH.

BUGS

There are a number of bugs that can cause excessive memory usage or TMM instability when using the table/session command.
Below are the main F5 bug IDs, ref : BIG-IP cumulative hotfix version 10.2.4

ID381096 - Fixed a TMM connflow memory leak caused by iRule commands that temporarily suspend execution
ID363612 - Memory utilization for TCL string cache has been optimized. Ref : sol13889
ID374923 - Fixed a defect which could cause TMM to restart and leave a core when using the table command in an iRule on an SSL-enabled virtual server.

All of the above are fixed within HF5. However it is recommended to upgrade to the latest hotfix, which at time of writing this is HF7.

MIRRORING

When session or tables are used within iRules Session DB mirroring is still performed, even if mirroring is not enabled on the virtual server.

 

https://www.fir3net.com/Loadbalancers/F5-BIG-IP/f5-ltm-ratelimiting.html

相关文章

  • 密码保护:F5OS 容器的network mode以及IP分配
  • 密码保护:F5OS docker-compose.yml
  • 密码保护:F5OS 底层容器、网络及k8s状态
  • 二进制flannel部署,非cni网络模式下与k8s CIS结合方案
  • F5 BIG-IP链接Istio 增强入口服务能力
本作品采用 知识共享署名-非商业性使用 4.0 国际许可协议 进行许可
标签: irule 秒杀 防秒杀
最后更新:2018年01月22日

纳米

linjing.io

打赏 点赞
< 上一篇
下一篇 >

文章评论

razz evil exclaim smile redface biggrin eek confused idea lol mad twisted rolleyes wink cool arrow neutral cry mrgreen drooling persevering
取消回复

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理。

页面AI聊天助手
文章目录
  • IRULE
    • THE TABLE COMMAND
    • TABLE EXAMPLE
  • EXAMPLE
    • HOW IT WORKS
    • CUSTOMIZATION
  • GOOD TO KNOW

纳米

linjing.io

☁️迈向Cloud Native ADC ☁️

认证获得:
TOGAF: ID 152743
Kubernetes: CKA #664
Microsoft: MCSE MCDBA
Cisco: CCNP
Juniper: JNCIS
F5:
F5 Certified Solution Expert, Security
F5 Certified Technology Specialist, LTM/GTM/APM/ASM
F5 Certified BIG-IP Administrator
  • 点击查看本博技术要素列表
  • 归档
    分类
    • AI
    • Automation
    • Avi Networks
    • Cisco ACI
    • CISCO资源
    • F5 with ELK
    • F5-Tech tips
    • F5技术
    • Juniper
    • Linux
    • NGINX
    • SDN
    • ServiceMesh
    • WEB编程
    • WINDOWS相关
    • 业界文章
    • 交换机技术
    • 化云为雨/Openstack
    • 协议原理
    • 容器/k8s
    • 我的工作
    • 我的生活
    • 网站技术
    • 路由器技术
    • 项目案例
    标签聚合
    DNS k8s irule flannel gtm api nginx network F5 neutron envoy bigip istio openstack docker
    最近评论
    汤姆 发布于 8 个月前(09月10日) 嗨,楼主,里面的json怎么下载啊,怎么收费啊?
    汤姆 发布于 8 个月前(09月09日) 大佬,kib的页面可以分享下吗?谢谢
    zhangsha 发布于 1 年前(05月12日) 资料发给我下,谢谢纳米同志!!!!lyx895@qq.com
    李成才 发布于 1 年前(01月02日) 麻烦了,谢谢大佬
    纳米 发布于 1 年前(01月02日) 你好。是的,因为以前下载系统插件在一次升级后将所有的下载生成信息全弄丢了。所以不少文件无法下载。DN...
    浏览次数
    • Downloads - 183,744 views
    • 联系我 - 118,966 views
    • 迄今为止最全最深入的BIGIP-DNS/GTM原理及培训资料 - 116,404 views
    • Github - 103,609 views
    • F5常见log日志解释 - 79,756 views
    • 从传统ADC迈向CLOUD NATIVE ADC - 下载 - 74,604 views
    • Sniffer Pro 4 70 530抓包软件 中文版+视频教程 - 74,320 views
    • 迄今为止最全最深入的BIGIP-DNS/GTM原理及培训资料 - 67,770 views
    • 关于本站 - 60,860 views
    • 这篇文档您是否感兴趣 - 55,485 views
    链接表
    • F5SE创新
    • Jimmy Song‘s Blog
    • SDNlab
    • Service Mesh社区
    • 三斗室
    • 个人profile
    • 云原生社区

    COPYRIGHT © 2023 Cloud Native 应用交付. ALL RIGHTS RESERVED.

    Theme Kratos Made By Seaton Jiang

    京ICP备14048088号-1

    京公网安备 11010502041506号