From e3ff9507f9037f860d83cf7d00310efb18b64fae Mon Sep 17 00:00:00 2001
From: =?utf8?q?Roland=20H=C3=A4der?= <roland@mxchange.org>
Date: Tue, 20 Jun 2023 15:37:10 +0200
Subject: [PATCH] Continued: - added argument --software to command
 fetch_blocks - added some missing checks on parameter 'domain' - removed
 unused imports

---
 fba/boot.py             | 3 ++-
 fba/commands.py         | 6 ++++++
 fba/csrf.py             | 5 +++++
 fba/federation.py       | 2 ++
 fba/models/blocks.py    | 1 -
 fba/models/instances.py | 1 -
 fba/network.py          | 4 ++++
 7 files changed, 19 insertions(+), 3 deletions(-)

diff --git a/fba/boot.py b/fba/boot.py
index 67d02a1..ed80085 100644
--- a/fba/boot.py
+++ b/fba/boot.py
@@ -59,7 +59,8 @@ def init_parser():
         "fetch_blocks",
         help="Fetches blocks from registered instances (run command fetch_instances first!).",
     )
-    parser.add_argument("--domain", help="Instance name (aka. domain) to fetch blocks from")
+    parser.add_argument("--domain", help="Instance name (aka. domain)")
+    parser.add_argument("--software", help="Name of software, e.g. 'lemmy'")
     parser.set_defaults(command=commands.fetch_blocks)
 
     ### Fetch blocks from chaos.social ###
diff --git a/fba/commands.py b/fba/commands.py
index f10fb56..190a92d 100644
--- a/fba/commands.py
+++ b/fba/commands.py
@@ -153,6 +153,12 @@ def fetch_blocks(args: argparse.Namespace):
         fba.cursor.execute(
             "SELECT domain, software, origin, nodeinfo_url FROM instances WHERE domain = ?", [args.domain]
         )
+    elif args.software is not None and args.software != "":
+        # Re-check single software
+        # DEBUG: print(f"DEBUG: Querying database for args.software='{args.software}' ...")
+        fba.cursor.execute(
+            "SELECT domain, software, origin, nodeinfo_url FROM instances WHERE software = ?", [args.software]
+        )
     else:
         # Re-check after "timeout" (aka. minimum interval)
         fba.cursor.execute(
diff --git a/fba/csrf.py b/fba/csrf.py
index 537314e..4eedcc7 100644
--- a/fba/csrf.py
+++ b/fba/csrf.py
@@ -16,6 +16,7 @@
 
 import bs4
 import reqto
+import validators
 
 from fba import config
 from fba import network
@@ -28,6 +29,10 @@ def determine(domain: str, headers: dict) -> dict:
         raise ValueError(f"Parameter domain[]='{type(domain)}' is not 'str'")
     elif domain == "":
         raise ValueError("Parameter 'domain' is empty")
+    elif not validators.domain(domain.split("/")[0]):
+        raise ValueError(f"domain='{domain}' is not a valid domain")
+    elif domain.endswith(".arpa"):
+        raise ValueError(f"domain='{domain}' is a domain for reversed IP addresses, 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(headers, dict):
diff --git a/fba/federation.py b/fba/federation.py
index 5e01148..e583949 100644
--- a/fba/federation.py
+++ b/fba/federation.py
@@ -82,6 +82,8 @@ def fetch_instances(domain: str, origin: str, software: str, command: str, path:
         return
     elif not validators.domain(domain.split("/")[0]):
         raise ValueError(f"domain='{domain}' is not a valid domain")
+    elif domain.endswith(".arpa"):
+        raise ValueError(f"domain='{domain}' is a domain for reversed IP addresses, please don't crawl them!")
     elif domain.endswith(".tld"):
         raise ValueError(f"domain='{domain}' is a fake domain")
 
diff --git a/fba/models/blocks.py b/fba/models/blocks.py
index b23f4e0..2111164 100644
--- a/fba/models/blocks.py
+++ b/fba/models/blocks.py
@@ -14,7 +14,6 @@
 # You should have received a copy of the GNU Affero General Public License
 # along with this program.  If not, see <https://www.gnu.org/licenses/>.
 
-import sys
 import time
 import validators
 
diff --git a/fba/models/instances.py b/fba/models/instances.py
index e1a1143..0ed262e 100644
--- a/fba/models/instances.py
+++ b/fba/models/instances.py
@@ -15,7 +15,6 @@
 # along with this program.  If not, see <https://www.gnu.org/licenses/>.
 
 import json
-import sys
 import time
 
 import requests
diff --git a/fba/network.py b/fba/network.py
index 1378859..79ac8f7 100644
--- a/fba/network.py
+++ b/fba/network.py
@@ -201,6 +201,10 @@ def send_bot_post(domain: str, blocklist: dict):
         raise ValueError(f"Parameter domain[]='{type(domain)}' is not 'str'")
     elif domain == "":
         raise ValueError("Parameter 'domain' is empty")
+    elif not validators.domain(domain.split("/")[0]):
+        raise ValueError(f"domain='{domain}' is not a valid domain")
+    elif domain.endswith(".arpa"):
+        raise ValueError(f"domain='{domain}' is a domain for reversed IP addresses, 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(blocklist, dict):
-- 
2.39.5