-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
-
- # 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)
- instances.set_software(domain, None)
- instances.set_detection_mode(domain, None)
- instances.set_nodeinfo_url(domain, None)
- 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.json",
- "/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' - Setting detection_mode=STATIC_CHECK ...", 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)
-
- # "rel" identifiers (no real URLs)
- nodeinfo_identifier = [
- "https://nodeinfo.diaspora.software/ns/schema/2.1",
- "http://nodeinfo.diaspora.software/ns/schema/2.1",
- "https://nodeinfo.diaspora.software/ns/schema/2.0",
- "http://nodeinfo.diaspora.software/ns/schema/2.0",
- "https://nodeinfo.diaspora.software/ns/schema/1.1",
- "http://nodeinfo.diaspora.software/ns/schema/1.1",
- "https://nodeinfo.diaspora.software/ns/schema/1.0",
- "http://nodeinfo.diaspora.software/ns/schema/1.0",
- ]
-
- # 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"))
- )
-
- logger.debug("data[]='%s'", type(data))
- if "error_message" not in data:
- nodeinfo = data["json"]
-
- logger.debug("Marking domain='%s' as successfully handled ...", domain)
- 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 niid in nodeinfo_identifier:
- data = dict()
-
- logger.debug("Checking niid='%s' ...", niid)
- for link in nodeinfo["links"]:
- logger.debug("link[%s]='%s'", type(link), link)
- if not isinstance(link, dict) or not "rel" in link:
- logger.debug("link[]='%s' is not 'dict' or no element 'rel' found - SKIPPED!", type(link))
- continue
- elif link["rel"] != niid:
- logger.debug("link[re]='%s' does not matched niid='%s' - SKIPPED!", link["rel"], niid)
- continue
- elif "href" not in link:
- logger.warning("link[rel]='%s' has no element 'href' - SKIPPED!", link["rel"])
- continue
-
- # Default is that 'href' has a complete URL, but some hosts don't send that
- logger.debug("link[rel]='%s' matches niid='%s'", link["rel"], niid)
- url = link["href"]
- components = urlparse(link["href"])
-
- logger.debug("components[%s]='%s'", type(components), components)
- if components.scheme == "" and components.netloc == "":
- logger.warning("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)
- elif components.netloc == "":
- logger.warning("link[href]='%s' has no netloc set, setting domain='%s'", link["href"], domain)
- url = f"{components.scheme}://{domain}{components.path}"
- 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,link[href]='%s' - Setting detection_mode=AUTO_DISCOVERY ...", len(data), link["href"])
- instances.set_detection_mode(domain, "AUTO_DISCOVERY")
- instances.set_nodeinfo_url(domain, link["href"])
-
- logger.debug("Marking domain='%s' as successfully handled ...", domain)
- instances.set_success(domain)
- break
- else:
- logger.debug("Setting last error for domain='%s',data[]='%s'", domain, type(data))
- instances.set_last_error(domain, data)
-
- logger.debug("data()=%d", len(data))
- if "error_message" not in data and "json" in data:
- logger.debug("Auto-discovery successful: domain='%s'", domain)
- break
- else:
- logger.warning("nodeinfo does not contain 'links': domain='%s'", domain)
-
- logger.debug("Returning data[]='%s' - EXIT!", type(data))
- return data
-