# Found info from node, such as nodeinfo URL, detection mode that needs to be
# written to database. Both arrays must be filled at the same time or else
-# update_nodeinfo() will fail
+# update_nodeinfos() will fail
nodeinfos = {
# Detection mode: AUTO_DISCOVERY or STATIC_CHECKS
"detection_mode": {},
# Found nodeinfo URL
"nodeinfo_url": {},
+ # Where to fetch peers (other instances)
+ "get_peers_url": {},
}
+# URL for fetching peers
+get_peers_url = "/api/v1/instance/peers"
+
+# Connect to database
connection = sqlite3.connect("blocks.db")
cursor = connection.cursor()
print("ERROR: failed SQL query:", domain, e)
sys.exit(255)
+ # NOISY-DEBUG: print("DEBUG: EXIT!")
+
def update_nodeinfos(domain: str):
- #print("DEBUG: Updating nodeinfo for domain:", domain)
- if domain not in nodeinfos["detection_mode"] or domain not in nodeinfos["nodeinfo_url"]:
- print(f"WARNING: domain {domain} has no pending nodeinfo!")
- raise
+ # NOISY-DEBUG: print("DEBUG: Updating nodeinfo for domain:", domain)
+ sql_string = ''
+ fields = list()
+ for key in nodeinfos:
+ # NOISY-DEBUG: print("DEBUG: key:", key)
+ if domain in nodeinfos[key]:
+ # NOISY-DEBUG: print(f"DEBUG: Adding '{nodeinfos[key][domain]}' for key='{key}' ...")
+ fields.append(nodeinfos[key][domain])
+ sql_string += f" {key} = ?,"
+
+ sql_string = sql_string[:-1]
+ fields.append(domain)
+ # NOISY-DEBUG: print(f"DEBUG: sql_string='{sql_string}',fields()={len(fields)}")
+
+ sql = "UPDATE instances SET" + sql_string + " WHERE domain = ? LIMIT 1"
+ # NOISY-DEBUG: print("DEBUG: sql:", sql)
try:
- cursor.execute("UPDATE instances SET detection_mode = ?, nodeinfo_url = ? WHERE domain = ? LIMIT 1", [
- nodeinfos["detection_mode"][domain],
- nodeinfos["nodeinfo_url"][domain],
- domain
- ])
+ # NOISY-DEBUG: print("DEBUG: Executing SQL:", sql)
+ cursor.execute(sql, fields)
+ # NOISY-DEBUG: print(f"DEBUG: Success! (rowcount={cursor.rowcount })")
if cursor.rowcount == 0:
print("WARNING: Did not update any rows:", domain)
except BaseException as e:
- print("ERROR: failed SQL query:", domain, e)
+ print(f"ERROR: failed SQL query: domain='{domain}',sql='{sql}',exception='{e}'")
sys.exit(255)
- # NOISY-DEBUG: print("DEBUG: Deleting nodeinfos for domain:", domain)
- del nodeinfos["detection_mode"][domain]
- del nodeinfos["nodeinfo_url"][domain]
+ # NOISY-DEBUG: # NOISY-DEBUG: print("DEBUG: Deleting nodeinfos for domain:", domain)
+ for key in nodeinfos:
+ try:
+ # NOISY-DEBUG: print("DEBUG: Deleting key:", key)
+ del nodeinfos[key][domain]
+ except:
+ pass
+
+ # NOISY-DEBUG: print("DEBUG: EXIT!")
def update_last_error(domain: str, res: any):
# NOISY-DEBUG: print("DEBUG: domain,res.status_code:", domain, res.status_code, res.reason)
print("ERROR: failed SQL query:", domain, e)
sys.exit(255)
- # NOISY-DEBUG: print("DEBUG: Deleting pending_errors for domain:", domain)
+ # NOISY-DEBUG: print(f"DEBUG: Deleting domain='{domain}' from pending_errors")
del pending_errors[domain]
+ # NOISY-DEBUG: print("DEBUG: EXIT!")
def update_last_nodeinfo(domain: str):
# NOISY-DEBUG: print("DEBUG: Updating last_nodeinfo for domain:", domain)
sys.exit(255)
connection.commit()
+ # NOISY-DEBUG: print("DEBUG: EXIT!")
def get_peers(domain: str) -> list:
# NOISY-DEBUG: print("DEBUG: Getting peers for domain:", domain)
peers = None
try:
- res = reqto.get(f"https://{domain}/api/v1/instance/peers", headers=headers, timeout=(config["connection_timeout"], config["read_timeout"]))
+ res = reqto.get(f"https://{domain}{get_peers_url}", headers=headers, timeout=(config["connection_timeout"], config["read_timeout"]))
if not res.ok or res.status_code >= 400:
print("WARNING: Cannot fetch peers:", domain)
else:
# NOISY-DEBUG: print("DEBUG: Querying API was successful:", domain, len(res.json()))
peers = res.json()
+ nodeinfos["get_peers_url"][domain] = get_peers_url
except:
print("WARNING: Some error during get():", domain)
print("ERROR: failed SQL query:", last_seen, blocker, blocked, block_level)
sys.exit(255)
+ # NOISY-DEBUG: print("DEBUG: EXIT!")
+
def block_instance(blocker: str, blocked: str, reason: str, block_level: str):
# NOISY-DEBUG: print("DEBUG: blocker,blocked,reason,block_level:", blocker, blocked, reason, block_level)
if blocker.find("@") > 0:
print("ERROR: failed SQL query:", blocker, blocked, reason, block_level, first_added, last_seen)
sys.exit(255)
+ # NOISY-DEBUG: print("DEBUG: EXIT!")
+
def add_instance(domain: str, origin: str, originator: str):
# NOISY-DEBUG: print("DEBUG: domain,origin:", domain, origin, originator)
if domain.find("@") > 0:
# NOISY-DEBUG: print("DEBUG: Updating nodeinfo for domain:", domain)
update_last_nodeinfo(domain)
+ # NOISY-DEBUG: print("DEBUG: EXIT!")
+
def send_bot_post(instance: str, blocks: dict):
message = instance + " has blocked the following instances:\n\n"
truncated = False