-def fetch_nodeinfo(domain: str, path: str = None) -> dict:
- logger.debug("domain='%s',path='%s' - CALLED!", domain, path)
- domain_helper.raise_on(domain)
-
- if not isinstance(path, str) and path is not None:
- raise ValueError(f"Parameter path[]='{type(path)}' is not 'str'")
-
- logger.debug("Fetching nodeinfo from domain='%s' ...", domain)
- nodeinfo = fetch_wellknown_nodeinfo(domain)
-
- logger.debug("nodeinfo[%s](%d='%s'", type(nodeinfo), len(nodeinfo), nodeinfo)
- if "error_message" not in nodeinfo and "json" in nodeinfo and len(nodeinfo["json"]) > 0:
- logger.debug("Found nodeinfo[json]()=%d - EXIT!", len(nodeinfo['json']))
- return nodeinfo["json"]
-
- # No CSRF by default, you don't have to add network.api_headers by yourself here
- headers = tuple()
- data = dict()
-
- try:
- logger.debug("Checking CSRF for domain='%s'", domain)
- headers = csrf.determine(domain, dict())
- except network.exceptions as exception:
- logger.warning("Exception '%s' during checking CSRF (nodeinfo,%s) - EXIT!", type(exception), __name__)
- instances.set_last_error(domain, exception)
- return {
- "status_code" : 500,
- "error_message": f"exception[{type(exception)}]='{str(exception)}'",
- "exception" : exception,
- }
-
- request_paths = [
- "/nodeinfo/2.1.json",
- "/nodeinfo/2.1",
- "/nodeinfo/2.0.json",
- "/nodeinfo/2.0",
- "/nodeinfo/1.0",
- "/api/v1/instance",
- ]
-
- for request in request_paths:
- logger.debug("request='%s'", request)
- http_url = f"http://{domain}{path}"
- https_url = f"https://{domain}{path}"
-
- logger.debug("path[%s]='%s',request='%s',http_url='%s',https_url='%s'", type(path), path, request, http_url, https_url)
- if path is None or path in [request, http_url, https_url]:
- logger.debug("Fetching request='%s' from domain='%s' ...", request, domain)
- if path in [http_url, https_url]:
- logger.debug("domain='%s',path='%s' has protocol in path, splitting ...", domain, path)
- components = urlparse(path)
- path = components.path
-
- data = network.get_json_api(
- domain,
- request,
- headers,
- (config.get("nodeinfo_connection_timeout"), config.get("nodeinfo_read_timeout"))
- )
-
- logger.debug("data[]='%s'", type(data))
- if "error_message" not in data and "json" in data:
- logger.debug("Success: request='%s'", request)
- instances.set_detection_mode(domain, "STATIC_CHECK")
- instances.set_nodeinfo_url(domain, request)
- break
-
- logger.warning("Failed fetching nodeinfo from domain='%s',status_code='%s',error_message='%s'", domain, data['status_code'], data['error_message'])
-
- logger.debug("data()=%d - EXIT!", len(data))
- return data
-
-def fetch_wellknown_nodeinfo(domain: str) -> dict:
- logger.debug("domain='%s' - CALLED!", domain)
- domain_helper.raise_on(domain)
-
- # No CSRF by default, you don't have to add network.api_headers by yourself here
- headers = tuple()
-
- try:
- logger.debug("Checking CSRF for domain='%s'", domain)
- headers = csrf.determine(domain, dict())
- except network.exceptions as exception:
- logger.warning("Exception '%s' during checking CSRF (fetch_wellknown_nodeinfo,%s) - EXIT!", type(exception), __name__)
- instances.set_last_error(domain, exception)
- return {
- "status_code" : 500,
- "error_message": type(exception),
- "exception" : exception,
- }
-
- logger.debug("Fetching .well-known info for domain='%s'", domain)
- data = network.get_json_api(
- domain,
- "/.well-known/nodeinfo",
- headers,
- (config.get("nodeinfo_connection_timeout"), config.get("nodeinfo_read_timeout"))
- )
-
- if "error_message" not in data:
- nodeinfo = data["json"]
- instances.set_success(domain)
-
- logger.debug("Found entries: nodeinfo()=%d,domain='%s'", len(nodeinfo), domain)
- if "links" in nodeinfo:
- logger.debug("Found nodeinfo[links]()=%d record(s)", len(nodeinfo["links"]))
- for link in nodeinfo["links"]:
- logger.debug("link[%s]='%s'", type(link), link)
- if not isinstance(link, dict) or not "rel" in link:
- logger.warning("link[]='%s' is not 'dict' or no element 'rel' found", type(link))
- elif link["rel"] in nodeinfo_identifier:
- # Default is that 'href' has a complete URL, but some hosts don't send that
- url = link["href"]
- components = urlparse(link["href"])
-
- logger.debug("components[%s]='%s'", type(components), components)
- if components.scheme == "" and components.netloc == "":
- logger.debug("link[href]='%s' has no scheme and host name in it, prepending from domain='%s'", link['href'], domain)
- url = f"https://{domain}{url}"
- components = urlparse(url)
-
- if not utils.is_domain_wanted(components.netloc):
- logger.debug("components.netloc='%s' is not wanted - SKIPPED!", components.netloc)
- continue
-
- logger.debug("Fetching nodeinfo from url='%s' ...", url)
- data = network.fetch_api_url(
- url,
- (config.get("connection_timeout"), config.get("read_timeout"))
- )
-
- logger.debug("link[href]='%s',data[]='%s'", link["href"], type(data))
- if "error_message" not in data and "json" in data:
- logger.debug("Found JSON data()=%d", len(data))
- instances.set_detection_mode(domain, "AUTO_DISCOVERY")
- instances.set_nodeinfo_url(domain, link["href"])
- instances.set_success(domain)
- break
- else:
- instances.set_last_error(domain, data)
- else:
- logger.warning("Unknown 'rel' value: domain='%s',link[rel]='%s'", domain, link["rel"])
- else:
- logger.warning("nodeinfo does not contain 'links': domain='%s'", domain)
-
- logger.debug("Returning data[]='%s' - EXIT!", type(data))
- return data
-