2019-07-09 23:13:23 -06:00
|
|
|
# Pleroma: A lightweight social networking server
|
2021-01-12 23:49:20 -07:00
|
|
|
# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
|
2019-07-09 23:13:23 -06:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2019-01-23 08:37:25 -07:00
|
|
|
defmodule Pleroma.Instances.Instance do
|
|
|
|
@moduledoc "Instance."
|
|
|
|
|
2022-11-06 15:49:39 -07:00
|
|
|
@cachex Pleroma.Config.get([:cachex, :provider], Cachex)
|
|
|
|
|
2019-02-09 08:16:26 -07:00
|
|
|
alias Pleroma.Instances
|
2019-01-23 08:37:25 -07:00
|
|
|
alias Pleroma.Instances.Instance
|
2019-03-04 19:52:23 -07:00
|
|
|
alias Pleroma.Repo
|
2021-07-17 13:06:35 -06:00
|
|
|
alias Pleroma.User
|
|
|
|
alias Pleroma.Workers.BackgroundWorker
|
2019-01-23 08:37:25 -07:00
|
|
|
|
|
|
|
use Ecto.Schema
|
|
|
|
|
2019-02-09 08:20:18 -07:00
|
|
|
import Ecto.Query
|
|
|
|
import Ecto.Changeset
|
2019-01-23 08:37:25 -07:00
|
|
|
|
2020-09-04 22:38:07 -06:00
|
|
|
require Logger
|
|
|
|
|
2019-01-23 08:37:25 -07:00
|
|
|
schema "instances" do
|
|
|
|
field(:host, :string)
|
2019-03-20 06:59:27 -06:00
|
|
|
field(:unreachable_since, :naive_datetime_usec)
|
2020-07-07 03:13:38 -06:00
|
|
|
field(:favicon, :string)
|
2022-11-06 15:49:39 -07:00
|
|
|
field(:metadata_updated_at, :naive_datetime)
|
|
|
|
field(:nodeinfo, :map, default: %{})
|
2022-11-26 12:22:56 -07:00
|
|
|
field(:has_request_signatures, :boolean)
|
2019-01-23 08:37:25 -07:00
|
|
|
|
|
|
|
timestamps()
|
|
|
|
end
|
|
|
|
|
2019-01-28 05:25:06 -07:00
|
|
|
defdelegate host(url_or_host), to: Instances
|
2019-01-24 09:15:23 -07:00
|
|
|
|
|
|
|
def changeset(struct, params \\ %{}) do
|
2019-01-23 08:37:25 -07:00
|
|
|
struct
|
2022-11-26 12:22:56 -07:00
|
|
|
|> cast(params, [
|
|
|
|
:host,
|
|
|
|
:unreachable_since,
|
|
|
|
:favicon,
|
|
|
|
:nodeinfo,
|
|
|
|
:metadata_updated_at,
|
|
|
|
:has_request_signatures
|
|
|
|
])
|
2019-01-24 09:15:23 -07:00
|
|
|
|> validate_required([:host])
|
2019-01-23 08:37:25 -07:00
|
|
|
|> unique_constraint(:host)
|
|
|
|
end
|
|
|
|
|
2019-02-03 02:41:27 -07:00
|
|
|
def filter_reachable([]), do: %{}
|
2019-01-24 09:15:23 -07:00
|
|
|
|
2019-01-28 05:25:06 -07:00
|
|
|
def filter_reachable(urls_or_hosts) when is_list(urls_or_hosts) do
|
2019-01-24 09:15:23 -07:00
|
|
|
hosts =
|
2019-01-28 05:25:06 -07:00
|
|
|
urls_or_hosts
|
2019-01-24 09:15:23 -07:00
|
|
|
|> Enum.map(&(&1 && host(&1)))
|
|
|
|
|> Enum.filter(&(to_string(&1) != ""))
|
|
|
|
|
2019-02-03 02:41:27 -07:00
|
|
|
unreachable_since_by_host =
|
2019-01-24 09:15:23 -07:00
|
|
|
Repo.all(
|
|
|
|
from(i in Instance,
|
2019-02-03 02:41:27 -07:00
|
|
|
where: i.host in ^hosts,
|
|
|
|
select: {i.host, i.unreachable_since}
|
2019-01-24 09:15:23 -07:00
|
|
|
)
|
|
|
|
)
|
2019-02-03 02:41:27 -07:00
|
|
|
|> Map.new(& &1)
|
2019-01-24 09:15:23 -07:00
|
|
|
|
2019-02-03 02:41:27 -07:00
|
|
|
reachability_datetime_threshold = Instances.reachability_datetime_threshold()
|
|
|
|
|
|
|
|
for entry <- Enum.filter(urls_or_hosts, &is_binary/1) do
|
|
|
|
host = host(entry)
|
|
|
|
unreachable_since = unreachable_since_by_host[host]
|
|
|
|
|
|
|
|
if !unreachable_since ||
|
|
|
|
NaiveDateTime.compare(unreachable_since, reachability_datetime_threshold) == :gt do
|
|
|
|
{entry, unreachable_since}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|> Enum.filter(& &1)
|
|
|
|
|> Map.new(& &1)
|
2019-01-24 09:15:23 -07:00
|
|
|
end
|
|
|
|
|
2019-01-28 05:25:06 -07:00
|
|
|
def reachable?(url_or_host) when is_binary(url_or_host) do
|
2019-01-23 08:37:25 -07:00
|
|
|
!Repo.one(
|
|
|
|
from(i in Instance,
|
|
|
|
where:
|
2019-01-28 05:25:06 -07:00
|
|
|
i.host == ^host(url_or_host) and
|
2019-01-25 05:10:21 -07:00
|
|
|
i.unreachable_since <= ^Instances.reachability_datetime_threshold(),
|
2019-01-23 08:37:25 -07:00
|
|
|
select: true
|
|
|
|
)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2020-12-07 13:18:51 -07:00
|
|
|
def reachable?(url_or_host) when is_binary(url_or_host), do: true
|
2019-01-24 07:37:23 -07:00
|
|
|
|
2019-01-28 05:25:06 -07:00
|
|
|
def set_reachable(url_or_host) when is_binary(url_or_host) do
|
|
|
|
with host <- host(url_or_host),
|
2019-01-24 09:15:23 -07:00
|
|
|
%Instance{} = existing_record <- Repo.get_by(Instance, %{host: host}) do
|
|
|
|
{:ok, _instance} =
|
|
|
|
existing_record
|
2019-01-28 01:03:52 -07:00
|
|
|
|> changeset(%{unreachable_since: nil})
|
2019-01-24 09:15:23 -07:00
|
|
|
|> Repo.update()
|
|
|
|
end
|
2019-01-23 08:37:25 -07:00
|
|
|
end
|
|
|
|
|
2019-01-28 05:25:06 -07:00
|
|
|
def set_reachable(_), do: {:error, nil}
|
2019-01-24 07:37:23 -07:00
|
|
|
|
2019-01-28 05:25:06 -07:00
|
|
|
def set_unreachable(url_or_host, unreachable_since \\ nil)
|
2019-01-24 07:37:23 -07:00
|
|
|
|
2019-01-28 05:25:06 -07:00
|
|
|
def set_unreachable(url_or_host, unreachable_since) when is_binary(url_or_host) do
|
2019-08-10 11:38:31 -06:00
|
|
|
unreachable_since = parse_datetime(unreachable_since) || NaiveDateTime.utc_now()
|
2019-01-28 05:25:06 -07:00
|
|
|
host = host(url_or_host)
|
2019-01-23 08:37:25 -07:00
|
|
|
existing_record = Repo.get_by(Instance, %{host: host})
|
|
|
|
|
2019-01-28 01:03:52 -07:00
|
|
|
changes = %{unreachable_since: unreachable_since}
|
2019-01-23 08:37:25 -07:00
|
|
|
|
2019-01-28 01:03:52 -07:00
|
|
|
cond do
|
|
|
|
is_nil(existing_record) ->
|
2019-01-24 09:15:23 -07:00
|
|
|
%Instance{}
|
|
|
|
|> changeset(Map.put(changes, :host, host))
|
|
|
|
|> Repo.insert()
|
2019-01-28 01:03:52 -07:00
|
|
|
|
|
|
|
existing_record.unreachable_since &&
|
|
|
|
NaiveDateTime.compare(existing_record.unreachable_since, unreachable_since) != :gt ->
|
2019-01-28 05:25:06 -07:00
|
|
|
{:ok, existing_record}
|
2019-01-28 01:03:52 -07:00
|
|
|
|
|
|
|
true ->
|
|
|
|
existing_record
|
|
|
|
|> changeset(changes)
|
|
|
|
|> Repo.update()
|
2019-01-23 08:37:25 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-01-28 05:25:06 -07:00
|
|
|
def set_unreachable(_, _), do: {:error, nil}
|
2019-08-10 11:38:31 -06:00
|
|
|
|
2020-10-15 16:32:20 -06:00
|
|
|
def get_consistently_unreachable do
|
|
|
|
reachability_datetime_threshold = Instances.reachability_datetime_threshold()
|
|
|
|
|
|
|
|
from(i in Instance,
|
|
|
|
where: ^reachability_datetime_threshold > i.unreachable_since,
|
|
|
|
order_by: i.unreachable_since,
|
|
|
|
select: {i.host, i.unreachable_since}
|
|
|
|
)
|
|
|
|
|> Repo.all()
|
|
|
|
end
|
|
|
|
|
2019-08-10 11:38:31 -06:00
|
|
|
defp parse_datetime(datetime) when is_binary(datetime) do
|
|
|
|
NaiveDateTime.from_iso8601(datetime)
|
|
|
|
end
|
|
|
|
|
|
|
|
defp parse_datetime(datetime), do: datetime
|
2020-07-07 03:13:38 -06:00
|
|
|
|
2022-11-06 15:49:39 -07:00
|
|
|
def needs_update(nil), do: true
|
|
|
|
|
|
|
|
def needs_update(%Instance{metadata_updated_at: nil}), do: true
|
|
|
|
|
|
|
|
def needs_update(%Instance{metadata_updated_at: metadata_updated_at}) do
|
2020-07-07 03:13:38 -06:00
|
|
|
now = NaiveDateTime.utc_now()
|
2022-11-06 15:49:39 -07:00
|
|
|
NaiveDateTime.diff(now, metadata_updated_at) > 86_400
|
|
|
|
end
|
2020-07-07 03:13:38 -06:00
|
|
|
|
2022-11-06 15:49:39 -07:00
|
|
|
def local do
|
|
|
|
%Instance{
|
|
|
|
host: Pleroma.Web.Endpoint.host(),
|
|
|
|
favicon: Pleroma.Web.Endpoint.url() <> "/favicon.png",
|
2023-03-17 09:33:28 -06:00
|
|
|
nodeinfo: Pleroma.Web.Nodeinfo.Nodeinfo.get_nodeinfo("2.1")
|
2022-11-06 15:49:39 -07:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_metadata(%URI{host: host} = uri) do
|
2022-11-08 04:03:43 -07:00
|
|
|
Logger.debug("Checking metadata for #{host}")
|
2022-11-06 15:49:39 -07:00
|
|
|
existing_record = Repo.get_by(Instance, %{host: host})
|
|
|
|
|
|
|
|
if reachable?(host) do
|
|
|
|
do_update_metadata(uri, existing_record)
|
2020-07-07 03:13:38 -06:00
|
|
|
else
|
2022-11-06 15:49:39 -07:00
|
|
|
{:discard, :unreachable}
|
|
|
|
end
|
|
|
|
end
|
2020-07-07 03:13:38 -06:00
|
|
|
|
2022-11-06 15:49:39 -07:00
|
|
|
defp do_update_metadata(%URI{host: host} = uri, existing_record) do
|
|
|
|
if existing_record do
|
|
|
|
if needs_update(existing_record) do
|
|
|
|
Logger.info("Updating metadata for #{host}")
|
|
|
|
favicon = scrape_favicon(uri)
|
|
|
|
nodeinfo = scrape_nodeinfo(uri)
|
|
|
|
|
2022-11-08 03:39:01 -07:00
|
|
|
existing_record
|
|
|
|
|> changeset(%{
|
|
|
|
host: host,
|
|
|
|
favicon: favicon,
|
|
|
|
nodeinfo: nodeinfo,
|
|
|
|
metadata_updated_at: NaiveDateTime.utc_now()
|
|
|
|
})
|
|
|
|
|> Repo.update()
|
2020-07-07 04:07:30 -06:00
|
|
|
else
|
2022-11-06 15:49:39 -07:00
|
|
|
{:discard, "Does not require update"}
|
|
|
|
end
|
|
|
|
else
|
|
|
|
favicon = scrape_favicon(uri)
|
|
|
|
nodeinfo = scrape_nodeinfo(uri)
|
|
|
|
|
|
|
|
Logger.info("Creating metadata for #{host}")
|
|
|
|
|
2022-11-08 06:54:43 -07:00
|
|
|
%Instance{}
|
|
|
|
|> changeset(%{
|
|
|
|
host: host,
|
|
|
|
favicon: favicon,
|
|
|
|
nodeinfo: nodeinfo,
|
|
|
|
metadata_updated_at: NaiveDateTime.utc_now()
|
|
|
|
})
|
|
|
|
|> Repo.insert()
|
2020-07-07 03:13:38 -06:00
|
|
|
end
|
2022-11-06 15:49:39 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
def get_favicon(%URI{host: host}) do
|
|
|
|
existing_record = Repo.get_by(Instance, %{host: host})
|
|
|
|
|
|
|
|
if existing_record do
|
|
|
|
existing_record.favicon
|
|
|
|
else
|
2020-09-04 22:38:07 -06:00
|
|
|
nil
|
2022-11-06 15:49:39 -07:00
|
|
|
end
|
2020-07-07 03:13:38 -06:00
|
|
|
end
|
|
|
|
|
2022-11-06 15:49:39 -07:00
|
|
|
defp scrape_nodeinfo(%URI{} = instance_uri) do
|
|
|
|
with true <- Pleroma.Config.get([:instances_nodeinfo, :enabled]),
|
|
|
|
{_, true} <- {:reachable, reachable?(instance_uri.host)},
|
|
|
|
{:ok, %Tesla.Env{status: 200, body: body}} <-
|
|
|
|
Tesla.get(
|
|
|
|
"https://#{instance_uri.host}/.well-known/nodeinfo",
|
|
|
|
headers: [{"Accept", "application/json"}]
|
|
|
|
),
|
|
|
|
{:ok, json} <- Jason.decode(body),
|
|
|
|
{:ok, %{"links" => links}} <- {:ok, json},
|
|
|
|
{:ok, %{"href" => href}} <-
|
|
|
|
{:ok,
|
|
|
|
Enum.find(links, &(&1["rel"] == "http://nodeinfo.diaspora.software/ns/schema/2.0"))},
|
|
|
|
{:ok, %Tesla.Env{body: data}} <-
|
|
|
|
Pleroma.HTTP.get(href, [{"accept", "application/json"}], []),
|
|
|
|
{:length, true} <- {:length, String.length(data) < 50_000},
|
|
|
|
{:ok, nodeinfo} <- Jason.decode(data) do
|
|
|
|
nodeinfo
|
|
|
|
else
|
|
|
|
{:reachable, false} ->
|
|
|
|
Logger.debug(
|
|
|
|
"Instance.scrape_nodeinfo(\"#{to_string(instance_uri)}\") ignored unreachable host"
|
|
|
|
)
|
2020-12-07 12:09:34 -07:00
|
|
|
|
2022-11-06 15:49:39 -07:00
|
|
|
nil
|
2020-12-07 12:09:34 -07:00
|
|
|
|
2022-11-06 15:49:39 -07:00
|
|
|
{:length, false} ->
|
|
|
|
Logger.debug(
|
|
|
|
"Instance.scrape_nodeinfo(\"#{to_string(instance_uri)}\") ignored too long body"
|
2020-09-04 22:38:07 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
nil
|
2022-11-06 15:49:39 -07:00
|
|
|
|
|
|
|
_ ->
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defp scrape_favicon(%URI{} = instance_uri) do
|
|
|
|
with true <- Pleroma.Config.get([:instances_favicons, :enabled]),
|
|
|
|
{_, true} <- {:reachable, reachable?(instance_uri.host)},
|
|
|
|
{:ok, %Tesla.Env{body: html}} <-
|
|
|
|
Pleroma.HTTP.get(to_string(instance_uri), [{"accept", "text/html"}], []),
|
|
|
|
{_, [favicon_rel | _]} when is_binary(favicon_rel) <-
|
|
|
|
{:parse, html |> Floki.parse_document!() |> Floki.attribute("link[rel=icon]", "href")},
|
|
|
|
{_, favicon} when is_binary(favicon) <-
|
|
|
|
{:merge, URI.merge(instance_uri, favicon_rel) |> to_string()},
|
|
|
|
{:length, true} <- {:length, String.length(favicon) < 255} do
|
|
|
|
favicon
|
|
|
|
else
|
|
|
|
{:reachable, false} ->
|
|
|
|
Logger.debug(
|
|
|
|
"Instance.scrape_favicon(\"#{to_string(instance_uri)}\") ignored unreachable host"
|
|
|
|
)
|
|
|
|
|
|
|
|
nil
|
|
|
|
|
|
|
|
_ ->
|
|
|
|
nil
|
2020-07-07 03:13:38 -06:00
|
|
|
end
|
|
|
|
end
|
2021-07-17 13:06:35 -06:00
|
|
|
|
|
|
|
@doc """
|
|
|
|
Deletes all users from an instance in a background task, thus also deleting
|
|
|
|
all of those users' activities and notifications.
|
|
|
|
"""
|
|
|
|
def delete_users_and_activities(host) when is_binary(host) do
|
|
|
|
BackgroundWorker.enqueue("delete_instance", %{"host" => host})
|
|
|
|
end
|
|
|
|
|
|
|
|
def perform(:delete_instance, host) when is_binary(host) do
|
|
|
|
User.Query.build(%{nickname: "@#{host}"})
|
|
|
|
|> Repo.chunk_stream(100, :batches)
|
|
|
|
|> Stream.each(fn users ->
|
|
|
|
users
|
|
|
|
|> Enum.each(fn user ->
|
|
|
|
User.perform(:delete, user)
|
|
|
|
end)
|
|
|
|
end)
|
|
|
|
|> Stream.run()
|
|
|
|
end
|
2022-11-06 15:49:39 -07:00
|
|
|
|
|
|
|
def get_by_url(url_or_host) do
|
|
|
|
url = host(url_or_host)
|
|
|
|
Repo.get_by(Instance, host: url)
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_cached_by_url(url_or_host) do
|
|
|
|
url = host(url_or_host)
|
|
|
|
|
|
|
|
if url == Pleroma.Web.Endpoint.host() do
|
|
|
|
{:ok, local()}
|
|
|
|
else
|
|
|
|
@cachex.fetch!(:instances_cache, "instances:#{url}", fn _ ->
|
|
|
|
with %Instance{} = instance <- get_by_url(url) do
|
|
|
|
{:commit, {:ok, instance}}
|
|
|
|
else
|
|
|
|
_ -> {:ignore, nil}
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
end
|
2022-11-26 12:22:56 -07:00
|
|
|
|
|
|
|
def set_request_signatures(url_or_host) when is_binary(url_or_host) do
|
|
|
|
host = host(url_or_host)
|
|
|
|
existing_record = Repo.get_by(Instance, %{host: host})
|
|
|
|
changes = %{has_request_signatures: true}
|
|
|
|
|
|
|
|
cond do
|
|
|
|
is_nil(existing_record) ->
|
|
|
|
%Instance{}
|
|
|
|
|> changeset(Map.put(changes, :host, host))
|
|
|
|
|> Repo.insert()
|
|
|
|
|
|
|
|
true ->
|
|
|
|
existing_record
|
|
|
|
|> changeset(changes)
|
|
|
|
|> Repo.update()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def set_request_signatures(_), do: {:error, :invalid_input}
|
2019-01-23 08:37:25 -07:00
|
|
|
end
|