parent
8abe5d60fa
commit
10b60b0206
4 changed files with 90 additions and 74 deletions
|
@ -1,18 +1,18 @@
|
|||
from collections import defaultdict
|
||||
from datetime import datetime
|
||||
from datetime import timedelta
|
||||
from typing import Dict
|
||||
from typing import Dict, Tuple
|
||||
|
||||
import requests
|
||||
|
||||
from app import app
|
||||
from app.extensions import db
|
||||
from app.models.alarms import Alarm, AlarmState
|
||||
from app.models.mirrors import Origin
|
||||
from app.terraform import BaseAutomation
|
||||
|
||||
|
||||
def check_origin(domain_name: str):
|
||||
start_date = (datetime.utcnow() - timedelta(days=2)).strftime("%Y-%m-%dT%H%%3A%M")
|
||||
start_date = (datetime.utcnow() - timedelta(days=1)).strftime("%Y-%m-%dT%H%%3A%M")
|
||||
end_date = datetime.utcnow().strftime("%Y-%m-%dT%H%%3A%M")
|
||||
api_url = f"https://api.ooni.io/api/v1/measurements?domain={domain_name}&since={start_date}&until={end_date}"
|
||||
result = defaultdict(lambda: {"anomaly": 0, "confirmed": 0, "failure": 0, "ok": 0})
|
||||
|
@ -22,7 +22,7 @@ def check_origin(domain_name: str):
|
|||
def _check_origin(api_url: str, result: Dict):
|
||||
print(f"Processing {api_url}")
|
||||
req = requests.get(api_url).json()
|
||||
if not req['results']:
|
||||
if 'results' not in req or not req['results']:
|
||||
return result
|
||||
for r in req['results']:
|
||||
not_ok = False
|
||||
|
@ -72,9 +72,14 @@ def set_ooni_alarm(origin_id: int, country: str, state: AlarmState, text: str):
|
|||
alarm.update_state(state, text)
|
||||
|
||||
|
||||
with app.app_context():
|
||||
origins = Origin.query.filter(Origin.destroyed == None).all()
|
||||
for origin in origins:
|
||||
ooni = threshold_origin(origin.domain_name)
|
||||
for country in ooni:
|
||||
set_ooni_alarm(origin.id, country.lower(), ooni[country]["state"], ooni[country]["message"])
|
||||
class BlockOONIAutomation(BaseAutomation):
|
||||
short_name = "block_ooni"
|
||||
description = "Import origin and/or proxy reachability results from OONI"
|
||||
|
||||
def automate(self, full: bool = False) -> Tuple[bool, str]:
|
||||
origins = Origin.query.filter(Origin.destroyed == None).all()
|
||||
for origin in origins:
|
||||
ooni = threshold_origin(origin.domain_name)
|
||||
for country in ooni:
|
||||
set_ooni_alarm(origin.id, country.lower(), ooni[country]["state"], ooni[country]["message"])
|
||||
return True, ""
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue