96) -> werkzeug.Response | None:
97
98
99 redis_client = redisdb.client()
100
101 if network.is_link_local and not cfg['botdetection.ip_limit.filter_link_local']:
102 logger.debug("network %s is link-local -> not monitored by ip_limit method", network.compressed)
103 return None
104
105 if request.args.get('format', 'html') != 'html':
106 c = incr_sliding_window(redis_client, 'ip_limit.API_WINDOW:' + network.compressed, API_WINDOW)
107 if c > API_MAX:
108 return too_many_requests(network, "too many request in API_WINDOW")
109
110 if cfg['botdetection.ip_limit.link_token']:
111
112 suspicious = link_token.is_suspicious(network, request, True)
113
114 if not suspicious:
115
116 drop_counter(redis_client, 'ip_limit.SUSPICIOUS_IP_WINDOW' + network.compressed)
117 return None
118
119
120 c = incr_sliding_window(
121 redis_client, 'ip_limit.SUSPICIOUS_IP_WINDOW' + network.compressed, SUSPICIOUS_IP_WINDOW
122 )
123 if c > SUSPICIOUS_IP_MAX:
124 logger.error("BLOCK: too many request from %s in SUSPICIOUS_IP_WINDOW (redirect to /)", network)
125 return flask.redirect(flask.url_for('index'), code=302)
126
127 c = incr_sliding_window(redis_client, 'ip_limit.BURST_WINDOW' + network.compressed, BURST_WINDOW)
128 if c > BURST_MAX_SUSPICIOUS:
129 return too_many_requests(network, "too many request in BURST_WINDOW (BURST_MAX_SUSPICIOUS)")
130
131 c = incr_sliding_window(redis_client, 'ip_limit.LONG_WINDOW' + network.compressed, LONG_WINDOW)
132 if c > LONG_MAX_SUSPICIOUS:
133 return too_many_requests(network, "too many request in LONG_WINDOW (LONG_MAX_SUSPICIOUS)")
134
135 return None
136
137
138 c = incr_sliding_window(redis_client, 'ip_limit.BURST_WINDOW' + network.compressed, BURST_WINDOW)
139 if c > BURST_MAX:
140 return too_many_requests(network, "too many request in BURST_WINDOW (BURST_MAX)")
141
142 c = incr_sliding_window(redis_client, 'ip_limit.LONG_WINDOW' + network.compressed, LONG_WINDOW)
143 if c > LONG_MAX:
144 return too_many_requests(network, "too many request in LONG_WINDOW (LONG_MAX)")
145
146 return None