]> git.mxchange.org Git - fba.git/blobdiff - fba/models/instances.py
Continued:
[fba.git] / fba / models / instances.py
index d2555063131e14cee95062574667fd8361a233ad..36191bb5b516d76e85b64c8053c485a699d3e1ae 100644 (file)
@@ -28,6 +28,7 @@ from fba.helpers import blacklist
 from fba.helpers import cache
 from fba.helpers import config
 from fba.helpers import domain as domain_helper
+from fba.helpers import tidyup
 
 from fba.http import federation
 from fba.http import network
@@ -36,10 +37,11 @@ from fba.models import error_log
 
 logging.basicConfig(level=logging.INFO)
 logger = logging.getLogger(__name__)
+#logger.setLevel(logging.DEBUG)
 
 # 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_data() will fail
+# update() will fail
 _pending = {
     # Detection mode
     # NULL means all detection methods have failed (maybe still reachable instance)
@@ -48,6 +50,10 @@ _pending = {
     "nodeinfo_url"       : {},
     # Found total peers
     "total_peers"        : {},
+    # Found total blocks
+    "total_blocks"       : {},
+    # Obfuscated domains
+    "obfuscated_blocks"  : {},
     # Last fetched instances
     "last_instance_fetch": {},
     # Last updated
@@ -62,13 +68,15 @@ _pending = {
     "last_error_details" : {},
     # Wether obfuscation has been used
     "has_obfuscation"    : {},
+    # Determined software
+    "software"           : {},
 }
 
 def _set_data(key: str, domain: str, value: any):
     logger.debug("key='%s',domain='%s',value[]='%s' - CALLED!", key, domain, type(value))
     domain_helper.raise_on(domain)
     if not isinstance(key, str):
-        raise ValueError("Parameter key[]='{type(key)}' is not 'str'")
+        raise ValueError(f"Parameter key[]='{type(key)}' is not of type 'str'")
     elif key == "":
         raise ValueError("Parameter 'key' is empty")
     elif not key in _pending:
@@ -95,7 +103,7 @@ def has_pending(domain: str) -> bool:
     logger.debug("has='%s' - EXIT!", has)
     return has
 
-def update_data(domain: str):
+def update(domain: str):
     logger.debug("domain='%s' - CALLED!", domain)
     domain_helper.raise_on(domain)
     if not has_pending(domain):
@@ -113,20 +121,21 @@ def update_data(domain: str):
             fields.append(_pending[key][domain])
             sql_string += f" {key} = ?,"
 
-    logger.debug("sql_string()=%d", len(sql_string))
+    logger.debug("sql_string(%d)='%s'", len(sql_string), sql_string)
     if sql_string == "":
-        raise ValueError(f"No fields have been set, but method invoked, domain='{domain}'")
+        raise ValueError(f"No fields have been set, but function invoked, domain='{domain}'")
 
     # Set last_updated to current timestamp
     fields.append(time.time())
 
     # For WHERE statement
+    logger.debug("Setting domain='%s' for WHERE statement ...", domain)
     fields.append(domain)
 
     logger.debug("sql_string='%s',fields()=%d", sql_string, len(fields))
     sql_string = "UPDATE instances SET" + sql_string + " last_updated = ? WHERE domain = ? LIMIT 1"
 
-    logger.debug("Executing SQL: '%s'", sql_string)
+    logger.debug("Executing SQL: sql_string='%s',fields()=%d", sql_string, len(fields))
     database.cursor.execute(sql_string, fields)
 
     logger.debug("rowcount=%d", database.cursor.rowcount)
@@ -150,25 +159,25 @@ def add(domain: str, origin: str, command: str, path: str = None, software: str
     domain_helper.raise_on(domain)
 
     if not isinstance(origin, str) and origin is not None:
-        raise ValueError(f"origin[]='{type(origin)}' is not 'str'")
+        raise ValueError(f"origin[]='{type(origin)}' is not of type 'str'")
     elif origin == "":
         raise ValueError("Parameter 'origin' is empty")
     elif not isinstance(command, str):
-        raise ValueError(f"command[]='{type(command)}' is not 'str'")
+        raise ValueError(f"command[]='{type(command)}' is not of type 'str'")
     elif command == "":
         raise ValueError("Parameter 'command' is empty")
     elif not isinstance(path, str) and path is not None:
-        raise ValueError(f"path[]='{type(path)}' is not 'str'")
+        raise ValueError(f"path[]='{type(path)}' is not of type 'str'")
     elif path == "":
         raise ValueError("Parameter 'path' is empty")
     elif not isinstance(software, str) and software is not None:
-        raise ValueError(f"software[]='{type(software)}' is not 'str'")
+        raise ValueError(f"software[]='{type(software)}' is not of type 'str'")
     elif software == "":
         raise ValueError("Parameter 'software' is empty")
     elif origin is not None and not validators.domain(origin.split("/")[0]):
         raise ValueError(f"Bad origin name='{origin}'")
     elif blacklist.is_blacklisted(domain):
-        raise Exception(f"domain='{domain}' is blacklisted, but method invoked")
+        raise Exception(f"domain='{domain}' is blacklisted, but function invoked")
     elif domain.find("/profile/") > 0 or domain.find("/users/") > 0 or (is_registered(domain.split("/")[0]) and domain.find("/c/") > 0):
         raise Exception(f"domain='{domain}' is a single user")
     elif domain.find("/tag/") > 0:
@@ -208,7 +217,7 @@ def add(domain: str, origin: str, command: str, path: str = None, software: str
     logger.debug("Checking if domain='%s' has pending updates ...", domain)
     if has_pending(domain):
         logger.debug("Flushing updates for domain='%s' ...", domain)
-        update_data(domain)
+        update(domain)
 
     logger.debug("EXIT!")
 
@@ -264,9 +273,13 @@ def set_success(domain: str):
 
     logger.debug("EXIT!")
 
-def is_registered(domain: str) -> bool:
-    logger.debug("domain='%s' - CALLED!", domain)
-    domain_helper.raise_on(domain)
+def is_registered(domain: str, skip_raise = False) -> bool:
+    logger.debug("domain='%s',skip_raise='%s' - CALLED!", domain, skip_raise)
+    if not isinstance(skip_raise, bool):
+        raise ValueError(f"skip_raise[]='{type(skip_raise)}' is not type of 'bool'")
+
+    if not skip_raise:
+        domain_helper.raise_on(domain)
 
     logger.debug("domain='%s' - CALLED!", domain)
     if not cache.key_exists("is_registered"):
@@ -287,21 +300,29 @@ def is_recent(domain: str, column: str = "last_instance_fetch") -> bool:
     domain_helper.raise_on(domain)
 
     if not isinstance(column, str):
-        raise ValueError(f"Parameter column[]='{type(column)}' is not 'str'")
-    elif column not in ["last_instance_fetch", "last_blocked"]:
+        raise ValueError(f"Parameter column[]='{type(column)}' is not of type 'str'")
+    elif not column.startswith("last_"):
         raise ValueError(f"Parameter column='{column}' is not expected")
     elif not is_registered(domain):
         logger.debug("domain='%s' is not registered, returning False - EXIT!", domain)
         return False
 
+    key = "recheck_instance"
+    if column == "last_blocked":
+        key = "recheck_block"
+
     # Query database
     database.cursor.execute(f"SELECT {column} FROM instances WHERE domain = ? LIMIT 1", [domain])
 
     # Fetch row
-    fetched = database.cursor.fetchone()[0]
+    row = database.cursor.fetchone()
+
+    fetched = float(row[column]) if row[column] is not None else 0.0
+
+    diff = (time.time() - fetched)
 
-    logger.debug("fetched[%s]='%s'", type(fetched), fetched)
-    recently = isinstance(fetched, float) and time.time() - fetched <= config.get("recheck_instance")
+    logger.debug("fetched[%s]='%s',key='%s',diff=%f", type(fetched), fetched, key, diff)
+    recently = bool(diff < config.get(key))
 
     logger.debug("recently='%s' - EXIT!", recently)
     return recently
@@ -309,26 +330,21 @@ def is_recent(domain: str, column: str = "last_instance_fetch") -> bool:
 def deobfuscate(char: str, domain: str, blocked_hash: str = None) -> tuple:
     logger.debug("char='%s',domain='%s',blocked_hash='%s' - CALLED!", char, domain, blocked_hash)
 
-    if not isinstance(domain, str):
-        raise ValueError(f"Parameter domain[]='{type(domain)}' is not 'str'")
-    elif domain == "":
-        raise ValueError("Parameter 'domain' is empty")
-    elif domain.lower() != domain:
-        raise ValueError(f"Parameter domain='{domain}' must be all lower-case")
-    elif domain.endswith(".arpa"):
-        raise ValueError(f"domain='{domain}' is a domain for reversed IP addresses, please don't crawl them!")
-    elif domain.endswith(".onion"):
-        raise ValueError(f"domain='{domain}' is a TOR domain, please don't crawl them!")
-    elif domain.endswith(".tld"):
-        raise ValueError(f"domain='{domain}' is a fake domain, please don't crawl them!")
-    elif not isinstance(char, str):
-        raise ValueError(f"Parameter char[]='{type(char)}' is not 'str'")
+    if not isinstance(char, str):
+        raise ValueError(f"Parameter char[]='{type(char)}' is not of type 'str'")
     elif char == "":
         raise ValueError("Parameter 'char' is empty")
     elif not char in domain:
         raise ValueError(f"char='{char}' not found in domain='{domain}' but function invoked")
+    elif not isinstance(domain, str):
+        raise ValueError(f"Parameter domain[]='{type(domain)}'")
+    elif domain == "":
+        raise ValueError("Parameter 'domain' is empty")
     elif not isinstance(blocked_hash, str) and blocked_hash is not None:
-        raise ValueError(f"Parameter blocked_hash[]='{type(blocked_hash)}' is not 'str'")
+        raise ValueError(f"Parameter blocked_hash[]='{type(blocked_hash)}' is not of type 'str'")
+
+    # Init row
+    row = None
 
     logger.debug("blocked_hash[]='%s'", type(blocked_hash))
     if isinstance(blocked_hash, str):
@@ -343,10 +359,20 @@ def deobfuscate(char: str, domain: str, blocked_hash: str = None) -> tuple:
         if row is None:
             logger.debug("blocked_hash='%s' not found, trying domain='%s' ...", blocked_hash, domain)
             return deobfuscate(char, domain)
-    else:
-        logger.debug("Looking up domain='%s' ...", domain)
+    elif not domain.startswith("*."):
+        logger.debug("domain='%s' - BEFORE!", domain)
+        domain = tidyup.domain(domain)
+        logger.debug("domain='%s' - AFTER!", domain)
+
+        if domain == "":
+            logger.warning("domain is empty after tidyup - EXIT!")
+            return None
+
+        search = domain.replace(char, "_")
+
+        logger.debug("Looking up domain='%s',search='%s' ...", domain, search)
         database.cursor.execute(
-            "SELECT domain, origin, nodeinfo_url FROM instances WHERE domain LIKE ? ORDER BY rowid LIMIT 1", [domain.replace(char, "_")]
+            "SELECT domain, origin, nodeinfo_url FROM instances WHERE domain LIKE ? OR 'https://' || domain LIKE ? ORDER BY rowid LIMIT 1", [search, search]
         )
 
         row = database.cursor.fetchone()
@@ -376,18 +402,42 @@ def set_total_peers(domain: str, peers: list):
     domain_helper.raise_on(domain)
 
     if not isinstance(peers, list):
-        raise ValueError(f"Parameter peers[]='{type(peers)}' is not 'list': '%s'")
+        raise ValueError(f"Parameter peers[]='{type(peers)}' is not of type 'list'")
 
     # Set timestamp
     _set_data("total_peers", domain, len(peers))
     logger.debug("EXIT!")
 
+def set_total_blocks(domain: str, blocks: list):
+    logger.debug("domain='%s',blocks()=%d - CALLED!", domain, len(blocks))
+    domain_helper.raise_on(domain)
+
+    if not isinstance(blocks, list):
+        raise ValueError(f"Parameter blocks[]='{type(blocks)}' is not of type 'list'")
+
+    # Set timestamp
+    _set_data("total_blocks", domain, len(blocks))
+    logger.debug("EXIT!")
+
+def set_obfuscated_blocks(domain: str, obfuscated: int):
+    logger.debug("domain='%s',obfuscated=%d - CALLED!", domain, obfuscated)
+    domain_helper.raise_on(domain)
+
+    if not isinstance(obfuscated, int):
+        raise ValueError(f"Parameter obfuscated[]='{type(obfuscated)}' is not of type 'int'")
+    elif obfuscated < 0:
+        raise ValueError(f"Parameter obfuscated={obfuscated} is not valid")
+
+    # Set timestamp
+    _set_data("obfuscated_blocks", domain, obfuscated)
+    logger.debug("EXIT!")
+
 def set_nodeinfo_url(domain: str, url: str):
     logger.debug("domain='%s',url='%s' - CALLED!", domain, url)
     domain_helper.raise_on(domain)
 
-    if not isinstance(url, str):
-        raise ValueError("Parameter url[]='{type(url)}' is not 'list'")
+    if not isinstance(url, str) and url is not None:
+        raise ValueError(f"Parameter url[]='{type(url)}' is not of type 'str'")
     elif url == "":
         raise ValueError("Parameter 'url' is empty")
 
@@ -399,8 +449,8 @@ def set_detection_mode(domain: str, mode: str):
     logger.debug("domain='%s',mode='%s' - CALLED!", domain, mode)
     domain_helper.raise_on(domain)
 
-    if not isinstance(mode, str):
-        raise ValueError("Parameter mode[]='{type(mode)}' is not 'list'")
+    if not isinstance(mode, str) and mode is not None:
+        raise ValueError(f"Parameter mode[]='{type(mode)}' is not of type 'str'")
     elif mode == "":
         raise ValueError("Parameter 'mode' is empty")
 
@@ -409,12 +459,79 @@ def set_detection_mode(domain: str, mode: str):
     logger.debug("EXIT!")
 
 def set_has_obfuscation(domain: str, status: bool):
-    logger.debug("domain(%d)='%s',status='%s' - CALLED!", len(domain), domain, status)
+    logger.debug("domain='%s',status='%s' - CALLED!", domain, status)
     domain_helper.raise_on(domain)
 
     if not isinstance(status, bool):
-        raise ValueError(f"Parameter status[]='{type(status)}' is not 'bool'")
+        raise ValueError(f"Parameter status[]='{type(status)}' is not of type 'bool'")
 
     # Set timestamp
     _set_data("has_obfuscation", domain, status)
     logger.debug("EXIT!")
+
+def set_software(domain: str, software: str):
+    logger.debug("domain='%s',software='%s' - CALLED!", domain, software)
+    domain_helper.raise_on(domain)
+
+    if not isinstance(software, str) and software is not None:
+        raise ValueError(f"Parameter software[]='{type(software)}' is not of type 'str'")
+    elif software == "":
+        raise ValueError("Parameter 'software' is empty")
+
+    # Set timestamp
+    _set_data("software", domain, software)
+    logger.debug("EXIT!")
+
+def valid(value: str, column: str) -> bool:
+    logger.debug("value='%s' - CALLED!", value)
+    if not isinstance(value, str):
+        raise ValueError(f"Parameter value[]='{type(value)}' is not of type 'str'")
+    elif value == "":
+        raise ValueError("Parameter 'value' is empty")
+    elif not isinstance(column, str):
+        raise ValueError(f"Parameter column[]='{type(column)}' is not of type 'str'")
+    elif column == "":
+        raise ValueError("Parameter 'column' is empty")
+
+    # Query database
+    database.cursor.execute(
+        f"SELECT {column} FROM instances WHERE {column} = ? LIMIT 1", [value]
+    )
+
+    is_valid = database.cursor.fetchone() is not None
+
+    logger.debug("is_valid='%s' - EXIT!", is_valid)
+    return is_valid
+
+def translate_idnas(rows: list, column: str):
+    logger.debug("rows[]='%s' - CALLED!", type(rows))
+    if not isinstance(rows, list):
+        raise ValueError("rows[]='{type(rows)}' is not of type 'list'")
+    elif len(rows) == 0:
+        raise ValueError("Parameter 'rows' is an empty list")
+    elif not isinstance(column, str):
+        raise ValueError(f"column='{type(column)}' is not of type 'str'")
+    elif column == "":
+        raise ValueError("Parameter 'column' is empty")
+    elif column not in ["domain", "origin"]:
+        raise ValueError(f"column='{column}' is not supported")
+
+    logger.info("Checking/converting %d domain names ...", len(rows))
+    for row in rows:
+        logger.debug("row[]='%s'", type(row))
+
+        translated = row[column].encode("idna").decode("utf-8")
+        logger.debug("translated='%s',row[%s]='%s'", translated, column, row[column])
+
+        if translated != row[column]:
+            logger.info("Translated row[%s]='%s' to '%s'", column, row[column], translated)
+            if is_registered(translated, True):
+                logger.warning("Deleting row[%s]='%s' as translated='%s' already exist", column, row[column], translated)
+                database.cursor.execute(f"DELETE FROM instances WHERE {column} = ? LIMIT 1", [row[column]])
+            else:
+                database.cursor.execute(f"UPDATE instances SET {column} = ? WHERE {column} = ? LIMIT 1", [translated, row[column]])
+
+            logger.debug("Invoking commit() ...")
+            database.connection.commit()
+
+    logger.debug("EXIT!")