-def update_block_reason(reason: str, blocker: str, blocked: str, block_level: str):
- # DEBUG: print(f"DEBUG: reason='{reason}',blocker={blocker},blocked={blocked},block_level={block_level} - CALLED!")
- if type(reason) != str and reason != None:
- raise ValueError(f"Parameter reason[]='{type(reason)}' is not 'str'")
- elif type(blocker) != str:
- raise ValueError(f"Parameter blocker[]='{type(blocker)}' is not 'str'")
- elif type(blocked) != str:
- raise ValueError(f"Parameter blocked[]='{type(blocked)}' is not 'str'")
- elif type(block_level) != str:
- raise ValueError(f"Parameter block_level[]='{type(block_level)}' is not 'str'")
-
- # DEBUG: print("DEBUG: Updating block reason:", reason, blocker, blocked, block_level)
- try:
- cursor.execute(
- "UPDATE blocks SET reason = ?, last_seen = ? WHERE blocker = ? AND blocked = ? AND block_level = ? AND reason IN ('','unknown') LIMIT 1",
- (
- reason,
- time.time(),
- blocker,
- blocked,
- block_level
- ),
- )
-
- # DEBUG: print(f"DEBUG: cursor.rowcount={cursor.rowcount}")
- if cursor.rowcount == 0:
- # DEBUG: print(f"DEBUG: Did not update any rows: blocker='{blocker}',blocked='{blocked}',block_level='{block_level}',reason='{reason}' - EXIT!")
- return
-
- except BaseException as e:
- print(f"ERROR: failed SQL query: reason='{reason}',blocker='{blocker}',blocked='{blocked}',block_level='{block_level}',exception[{type(e)}]:'{str(e)}'")
- sys.exit(255)
-
- # DEBUG: print("DEBUG: EXIT!")
-
-def update_last_seen(blocker: str, blocked: str, block_level: str):
- # DEBUG: print("DEBUG: Updating last_seen for:", blocker, blocked, block_level)
- try:
- cursor.execute(
- "UPDATE blocks SET last_seen = ? WHERE blocker = ? AND blocked = ? AND block_level = ? LIMIT 1",
- (
- time.time(),
- blocker,
- blocked,
- block_level
- )
- )
-
- # DEBUG: print(f"DEBUG: cursor.rowcount={cursor.rowcount}")
- if cursor.rowcount == 0:
- # DEBUG: print(f"DEBUG: Did not update any rows: blocker='{blocker}',blocked='{blocked}',block_level='{block_level}' - EXIT!")
- return
-
- except BaseException as e:
- print(f"ERROR: failed SQL query: blocker='{blocker}',blocked='{blocked}',block_level='{block_level}',exception[{type(e)}]:'{str(e)}'")
- sys.exit(255)
-
- # DEBUG: print("DEBUG: EXIT!")
-
-def is_instance_blocked(blocker: str, blocked: str, block_level: str) -> bool:
- # DEBUG: print(f"DEBUG: blocker={blocker},blocked={blocked},block_level={block_level} - CALLED!")
- if type(blocker) != str:
- raise ValueError(f"Parameter blocker[]={type(blocker)} is not of type 'str'")
- elif blocker == "":
- raise ValueError("Parameter 'blocker' cannot be empty")
- elif type(blocked) != str:
- raise ValueError(f"Parameter blocked[]={type(blocked)} is not of type 'str'")
- elif blocked == "":
- raise ValueError("Parameter 'blocked' cannot be empty")
- elif type(block_level) != str:
- raise ValueError(f"Parameter block_level[]={type(block_level)} is not of type 'str'")
- elif block_level == "":
- raise ValueError("Parameter 'block_level' cannot be empty")
-
- cursor.execute(
- "SELECT * FROM blocks WHERE blocker = ? AND blocked = ? AND block_level = ? LIMIT 1",
- (
- blocker,
- blocked,
- block_level
- ),
- )
-
- is_blocked = cursor.fetchone() != None
-
- # DEBUG: print(f"DEBUG: is_blocked='{is_blocked}' - EXIT!")
- return is_blocked
-
-def block_instance(blocker: str, blocked: str, reason: str, block_level: str):
- # DEBUG: print("DEBUG: blocker,blocked,reason,block_level:", blocker, blocked, reason, block_level)
- if type(blocker) != str:
- raise ValueError(f"Parameter blocker[]={type(blocker)} is not 'str'")
- elif blocker == "":
- raise ValueError(f"Parameter 'blocker' cannot be empty")
- elif not validators.domain(blocker.split("/")[0]):
- raise ValueError(f"Bad blocker='{blocker}'")
- elif type(blocked) != str:
- raise ValueError(f"Parameter blocked[]={type(blocked)} is not 'str'")
- elif blocked == "":
- raise ValueError(f"Parameter 'blocked' cannot be empty")
- elif not validators.domain(blocked.split("/")[0]):
- raise ValueError(f"Bad blocked='{blocked}'")
- elif is_blacklisted(blocker):
- raise Exception(f"blocker='{blocker}' is blacklisted but function invoked")
- elif is_blacklisted(blocked):
- raise Exception(f"blocked='{blocked}' is blacklisted but function invoked")
-
- print(f"INFO: New block: blocker='{blocker}',blocked='{blocked}', reason='{reason}', block_level='{block_level}'")
- try:
- cursor.execute(
- "INSERT INTO blocks (blocker, blocked, reason, block_level, first_seen, last_seen) VALUES(?, ?, ?, ?, ?, ?)",
- (
- blocker,
- blocked,
- reason,
- block_level,
- time.time(),
- time.time()
- ),
- )
-
- connection.commit()
- except BaseException as e:
- print(f"ERROR: failed SQL query: blocker='{blocker}',blocked='{blocked}',reason='{reason}',block_level='{block_level}',exception[{type(e)}]:'{str(e)}'")
- sys.exit(255)
-
- # DEBUG: print("DEBUG: EXIT!")
-
-def is_instance_registered(domain: str) -> bool:
- # DEBUG: print(f"DEBUG: domain={domain} - CALLED!")
- if type(domain) != str:
- raise ValueError(f"Parameter domain[]={type(domain)} is not 'str'")
- elif domain == "":
- raise ValueError(f"Parameter 'domain' cannot be empty")
-
- # NOISY-DEBUG: # DEBUG: print(f"DEBUG: domain='{domain}' - CALLED!")
- if not cache.is_cache_initialized("is_registered"):
- # NOISY-DEBUG: # DEBUG: print(f"DEBUG: Cache for 'is_registered' not initialized, fetching all rows ...")
- try:
- cursor.execute("SELECT domain FROM instances")
-
- # Check Set all
- cache.set_all_cache_key("is_registered", cursor.fetchall(), True)
- except BaseException as e:
- print(f"ERROR: failed SQL query: domain='{domain}',exception[{type(e)}]:'{str(e)}'")
- sys.exit(255)
-
- # Is cache found?
- registered = cache.is_cache_key_set("is_registered", domain)
-
- # NOISY-DEBUG: # DEBUG: print(f"DEBUG: registered='{registered}' - EXIT!")
- return registered
-
-def add_instance(domain: str, origin: str, originator: str, path: str = None):
- # DEBUG: print(f"DEBUG: domain={domain},origin={origin},originator={originator},path={path} - CALLED!")
- if type(domain) != str:
- raise ValueError(f"Parameter domain[]={type(domain)} is not 'str'")
- elif domain == "":
- raise ValueError(f"Parameter 'domain' cannot be empty")
- elif type(origin) != str and origin != None:
- raise ValueError(f"origin[]={type(origin)} is not 'str'")
- elif type(originator) != str:
- raise ValueError(f"originator[]={type(originator)} is not 'str'")
- elif originator == "":
- raise ValueError(f"originator cannot be empty")
- elif not validators.domain(domain.split("/")[0]):
- raise ValueError(f"Bad domain name='{domain}'")
- elif origin is not None and not validators.domain(origin.split("/")[0]):
- raise ValueError(f"Bad origin name='{origin}'")
- elif is_blacklisted(domain):
- raise Exception(f"domain='{domain}' is blacklisted, but method invoked")
-
- # DEBUG: print("DEBUG: domain,origin,originator,path:", domain, origin, originator, path)
- software = determine_software(domain, path)
- # DEBUG: print("DEBUG: Determined software:", software)
-
- print(f"INFO: Adding instance domain='{domain}' (origin='{origin}',software='{software}')")
- try:
- cursor.execute(
- "INSERT INTO instances (domain, origin, originator, hash, software, first_seen) VALUES (?, ?, ?, ?, ?, ?)",
- (
- domain,
- origin,
- originator,
- get_hash(domain),
- software,
- time.time()
- ),
- )
-
- cache.set_cache_key("is_registered", domain, True)
-
- if has_pending_instance_data(domain):
- # DEBUG: print(f"DEBUG: domain='{domain}' has pending nodeinfo being updated ...")
- set_instance_data("last_status_code" , domain, None)
- set_instance_data("last_error_details", domain, None)
- update_instance_data(domain)
- remove_pending_error(domain)
-
- if domain in pending_errors:
- # DEBUG: print("DEBUG: domain has pending error being updated:", domain)
- update_last_error(domain, pending_errors[domain])
- remove_pending_error(domain)
-
- except BaseException as e:
- print(f"ERROR: failed SQL query: domain='{domain}',exception[{type(e)}]:'{str(e)}'")
- sys.exit(255)
- else:
- # DEBUG: print("DEBUG: Updating nodeinfo for domain:", domain)
- update_last_nodeinfo(domain)
-
- # DEBUG: print("DEBUG: EXIT!")
-
-def send_bot_post(instance: str, blocks: dict):
- # DEBUG: print(f"DEBUG: instance={instance},blocks()={len(blocks)} - CALLED!")
- message = instance + " has blocked the following instances:\n\n"
- truncated = False
-
- if len(blocks) > 20:
- truncated = True
- blocks = blocks[0 : 19]
-
- for block in blocks:
- if block["reason"] == None or block["reason"] == '':
- message = message + block["blocked"] + " with unspecified reason\n"
- else:
- if len(block["reason"]) > 420:
- block["reason"] = block["reason"][0:419] + "[…]"
-
- message = message + block["blocked"] + ' for "' + block["reason"].replace("@", "@\u200b") + '"\n'
-
- if truncated:
- message = message + "(the list has been truncated to the first 20 entries)"