diff --git a/src/arvan_client.py b/src/arvan_client.py index a4c8fea7bcff1a5449be5af6703ede944c0cd702..3923afe63cadece0a2ea68e59686b17460e62174 100644 --- a/src/arvan_client.py +++ b/src/arvan_client.py @@ -32,3 +32,14 @@ class ArvanClient: 'url_pattern': '**', }, headers=self.headers).json()) + def get_firewall_rules(self): + return requests.get(f'{ARVAN_BASE_URL}/domains/{self.domain}/firewall', headers=self.headers).json()['data'] + + def is_IP_blocked(self, ip): + is_blocked = False + for rule in self.get_firewall_rules()['rules']: + if rule['action'] == 'deny': + for source in rule['sources']: + if source == ip: + is_blocked = True + return is_blocked \ No newline at end of file diff --git a/src/main.py b/src/main.py index 043f66afde4b78eecb7c11661bdff54dd194662a..e093dca75342815e078157a98fc504721fccbe6e 100644 --- a/src/main.py +++ b/src/main.py @@ -32,8 +32,9 @@ def check_high_requests_ips_and_block(): for row in high_req_ips: print('CHECK IP', row['ip']) if row['request_count'] > BLOCK_IP_THRESHOLD and row['ip'] not in WHITE_LIST_IPS: - print(f'BLOCK IP {row["ip"]} WITH {row["request_count"]} requests') - arvan_client.block_ip(row['ip']) + if not arvan_client.is_IP_blocked(row['ip']): + print(f'BLOCK IP {row["ip"]} WITH {row["request_count"]} requests') + arvan_client.block_ip(row['ip']) sleep(CHECK_INTERVAL)