forked from AkkomaGang/akkoma
[#534] Updating external instances reachability on incoming federation.
This commit is contained in:
parent
20b54366ee
commit
8654a591f0
9 changed files with 49 additions and 7 deletions
|
@ -24,7 +24,7 @@ def update_changeset(struct, params \\ %{}) do
|
||||||
|> unique_constraint(:host)
|
|> unique_constraint(:host)
|
||||||
end
|
end
|
||||||
|
|
||||||
def reachable?(url) do
|
def reachable?(url) when is_binary(url) do
|
||||||
!Repo.one(
|
!Repo.one(
|
||||||
from(i in Instance,
|
from(i in Instance,
|
||||||
where:
|
where:
|
||||||
|
@ -34,7 +34,9 @@ def reachable?(url) do
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
def set_reachable(url) do
|
def reachable?(_), do: true
|
||||||
|
|
||||||
|
def set_reachable(url) when is_binary(url) do
|
||||||
Repo.update_all(
|
Repo.update_all(
|
||||||
from(i in Instance, where: i.host == ^host(url)),
|
from(i in Instance, where: i.host == ^host(url)),
|
||||||
set: [
|
set: [
|
||||||
|
@ -44,7 +46,11 @@ def set_reachable(url) do
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
def set_unreachable(url, unreachable_since \\ nil) do
|
def set_reachable(_), do: {0, :noop}
|
||||||
|
|
||||||
|
def set_unreachable(url, unreachable_since \\ nil)
|
||||||
|
|
||||||
|
def set_unreachable(url, unreachable_since) when is_binary(url) do
|
||||||
unreachable_since = unreachable_since || DateTime.utc_now()
|
unreachable_since = unreachable_since || DateTime.utc_now()
|
||||||
host = host(url)
|
host = host(url)
|
||||||
existing_record = Repo.get_by(Instance, %{host: host})
|
existing_record = Repo.get_by(Instance, %{host: host})
|
||||||
|
@ -67,6 +73,8 @@ def set_unreachable(url, unreachable_since \\ nil) do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def set_unreachable(_, _), do: {0, :noop}
|
||||||
|
|
||||||
defp host(url_or_host) do
|
defp host(url_or_host) do
|
||||||
if url_or_host =~ ~r/^http/i do
|
if url_or_host =~ ~r/^http/i do
|
||||||
URI.parse(url_or_host).host
|
URI.parse(url_or_host).host
|
||||||
|
|
|
@ -3,7 +3,8 @@
|
||||||
# SPDX-License-Identifier: AGPL-3.0-only
|
# SPDX-License-Identifier: AGPL-3.0-only
|
||||||
|
|
||||||
defmodule Pleroma.ReverseProxy do
|
defmodule Pleroma.ReverseProxy do
|
||||||
@keep_req_headers ~w(accept user-agent accept-encoding cache-control if-modified-since if-unmodified-since if-none-match if-range range)
|
@keep_req_headers ~w(accept user-agent accept-encoding cache-control if-modified-since if-unmodified-since) ++
|
||||||
|
~w(if-none-match if-range range referer)
|
||||||
@resp_cache_headers ~w(etag date last-modified cache-control)
|
@resp_cache_headers ~w(etag date last-modified cache-control)
|
||||||
@keep_resp_headers @resp_cache_headers ++
|
@keep_resp_headers @resp_cache_headers ++
|
||||||
~w(content-type content-disposition content-encoding content-range accept-ranges vary)
|
~w(content-type content-disposition content-encoding content-range accept-ranges vary)
|
||||||
|
|
|
@ -750,7 +750,8 @@ defp do_publish_one(%{inbox: inbox, json: json, actor: actor, id: id}) do
|
||||||
[
|
[
|
||||||
{"Content-Type", "application/activity+json"},
|
{"Content-Type", "application/activity+json"},
|
||||||
{"signature", signature},
|
{"signature", signature},
|
||||||
{"digest", digest}
|
{"digest", digest},
|
||||||
|
{"referer", Pleroma.Web.Endpoint.url()}
|
||||||
]
|
]
|
||||||
) do
|
) do
|
||||||
Instances.set_reachable(inbox)
|
Instances.set_reachable(inbox)
|
||||||
|
|
|
@ -4,6 +4,7 @@
|
||||||
|
|
||||||
defmodule Pleroma.Web.ActivityPub.ActivityPubController do
|
defmodule Pleroma.Web.ActivityPub.ActivityPubController do
|
||||||
use Pleroma.Web, :controller
|
use Pleroma.Web, :controller
|
||||||
|
|
||||||
alias Pleroma.{Activity, User, Object}
|
alias Pleroma.{Activity, User, Object}
|
||||||
alias Pleroma.Web.ActivityPub.{ObjectView, UserView}
|
alias Pleroma.Web.ActivityPub.{ObjectView, UserView}
|
||||||
alias Pleroma.Web.ActivityPub.ActivityPub
|
alias Pleroma.Web.ActivityPub.ActivityPub
|
||||||
|
@ -18,6 +19,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubController do
|
||||||
|
|
||||||
plug(Pleroma.Web.FederatingPlug when action in [:inbox, :relay])
|
plug(Pleroma.Web.FederatingPlug when action in [:inbox, :relay])
|
||||||
plug(:relay_active? when action in [:relay])
|
plug(:relay_active? when action in [:relay])
|
||||||
|
plug(:set_requester_reachable when action in [:inbox])
|
||||||
|
|
||||||
def relay_active?(conn, _) do
|
def relay_active?(conn, _) do
|
||||||
if Keyword.get(Application.get_env(:pleroma, :instance), :allow_relay) do
|
if Keyword.get(Application.get_env(:pleroma, :instance), :allow_relay) do
|
||||||
|
@ -289,4 +291,9 @@ def errors(conn, _e) do
|
||||||
|> put_status(500)
|
|> put_status(500)
|
||||||
|> json("error")
|
|> json("error")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
defp set_requester_reachable(conn, _) do
|
||||||
|
Pleroma.Web.ControllerHelper.set_requester_reachable(conn)
|
||||||
|
conn
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -10,4 +10,9 @@ def json_response(conn, status, json) do
|
||||||
|> put_status(status)
|
|> put_status(status)
|
||||||
|> json(json)
|
|> json(json)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def set_requester_reachable(conn) do
|
||||||
|
with [referer] <- get_req_header(conn, "referer"),
|
||||||
|
do: Pleroma.Instances.set_reachable(referer)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -15,6 +15,8 @@ defmodule Pleroma.Web.OStatus.OStatusController do
|
||||||
alias Pleroma.Web.ActivityPub.ActivityPub
|
alias Pleroma.Web.ActivityPub.ActivityPub
|
||||||
|
|
||||||
plug(Pleroma.Web.FederatingPlug when action in [:salmon_incoming])
|
plug(Pleroma.Web.FederatingPlug when action in [:salmon_incoming])
|
||||||
|
plug(:set_requester_reachable when action in [:salmon_incoming])
|
||||||
|
|
||||||
action_fallback(:errors)
|
action_fallback(:errors)
|
||||||
|
|
||||||
def feed_redirect(conn, %{"nickname" => nickname}) do
|
def feed_redirect(conn, %{"nickname" => nickname}) do
|
||||||
|
@ -201,4 +203,9 @@ def errors(conn, _) do
|
||||||
|> put_status(500)
|
|> put_status(500)
|
||||||
|> text("Something went wrong")
|
|> text("Something went wrong")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
defp set_requester_reachable(conn, _) do
|
||||||
|
Pleroma.Web.ControllerHelper.set_requester_reachable(conn)
|
||||||
|
conn
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -173,7 +173,10 @@ defp send_to_user(url, feed, poster) when is_binary(url) do
|
||||||
poster.(
|
poster.(
|
||||||
url,
|
url,
|
||||||
feed,
|
feed,
|
||||||
[{"Content-Type", "application/magic-envelope+xml"}]
|
[
|
||||||
|
{"Content-Type", "application/magic-envelope+xml"},
|
||||||
|
{"referer", Pleroma.Web.Endpoint.url()}
|
||||||
|
]
|
||||||
) do
|
) do
|
||||||
Instances.set_reachable(url)
|
Instances.set_reachable(url)
|
||||||
Logger.debug(fn -> "Pushed to #{url}, code #{code}" end)
|
Logger.debug(fn -> "Pushed to #{url}, code #{code}" end)
|
||||||
|
|
|
@ -275,7 +275,8 @@ def publish_one(%{xml: xml, topic: topic, callback: callback, secret: secret}) d
|
||||||
xml,
|
xml,
|
||||||
[
|
[
|
||||||
{"Content-Type", "application/atom+xml"},
|
{"Content-Type", "application/atom+xml"},
|
||||||
{"X-Hub-Signature", "sha1=#{signature}"}
|
{"X-Hub-Signature", "sha1=#{signature}"},
|
||||||
|
{"referer", Pleroma.Web.Endpoint.url()}
|
||||||
]
|
]
|
||||||
) do
|
) do
|
||||||
Instances.set_reachable(callback)
|
Instances.set_reachable(callback)
|
||||||
|
|
|
@ -4,9 +4,11 @@
|
||||||
|
|
||||||
defmodule Pleroma.Web.Websub.WebsubController do
|
defmodule Pleroma.Web.Websub.WebsubController do
|
||||||
use Pleroma.Web, :controller
|
use Pleroma.Web, :controller
|
||||||
|
|
||||||
alias Pleroma.{Repo, User}
|
alias Pleroma.{Repo, User}
|
||||||
alias Pleroma.Web.{Websub, Federator}
|
alias Pleroma.Web.{Websub, Federator}
|
||||||
alias Pleroma.Web.Websub.WebsubClientSubscription
|
alias Pleroma.Web.Websub.WebsubClientSubscription
|
||||||
|
|
||||||
require Logger
|
require Logger
|
||||||
|
|
||||||
plug(
|
plug(
|
||||||
|
@ -18,6 +20,8 @@ defmodule Pleroma.Web.Websub.WebsubController do
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
|
|
||||||
|
plug(:set_requester_reachable when action in [:websub_incoming])
|
||||||
|
|
||||||
def websub_subscription_request(conn, %{"nickname" => nickname} = params) do
|
def websub_subscription_request(conn, %{"nickname" => nickname} = params) do
|
||||||
user = User.get_cached_by_nickname(nickname)
|
user = User.get_cached_by_nickname(nickname)
|
||||||
|
|
||||||
|
@ -92,4 +96,9 @@ def websub_incoming(conn, %{"id" => id}) do
|
||||||
|> send_resp(500, "Error")
|
|> send_resp(500, "Error")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
defp set_requester_reachable(conn, _) do
|
||||||
|
Pleroma.Web.ControllerHelper.set_requester_reachable(conn)
|
||||||
|
conn
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue