[#534] Updating external instances reachability on incoming federation.

This commit is contained in:
Ivan Tashkinov 2019-01-24 17:37:23 +03:00
parent 20b54366ee
commit 8654a591f0
9 changed files with 49 additions and 7 deletions

View File

@ -24,7 +24,7 @@ defmodule Pleroma.Instances.Instance do
|> unique_constraint(:host)
end
def reachable?(url) do
def reachable?(url) when is_binary(url) do
!Repo.one(
from(i in Instance,
where:
@ -34,7 +34,9 @@ defmodule Pleroma.Instances.Instance do
)
end
def set_reachable(url) do
def reachable?(_), do: true
def set_reachable(url) when is_binary(url) do
Repo.update_all(
from(i in Instance, where: i.host == ^host(url)),
set: [
@ -44,7 +46,11 @@ defmodule Pleroma.Instances.Instance do
)
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()
host = host(url)
existing_record = Repo.get_by(Instance, %{host: host})
@ -67,6 +73,8 @@ defmodule Pleroma.Instances.Instance do
end
end
def set_unreachable(_, _), do: {0, :noop}
defp host(url_or_host) do
if url_or_host =~ ~r/^http/i do
URI.parse(url_or_host).host

View File

@ -3,7 +3,8 @@
# SPDX-License-Identifier: AGPL-3.0-only
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)
@keep_resp_headers @resp_cache_headers ++
~w(content-type content-disposition content-encoding content-range accept-ranges vary)

View File

@ -750,7 +750,8 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
[
{"Content-Type", "application/activity+json"},
{"signature", signature},
{"digest", digest}
{"digest", digest},
{"referer", Pleroma.Web.Endpoint.url()}
]
) do
Instances.set_reachable(inbox)

View File

@ -4,6 +4,7 @@
defmodule Pleroma.Web.ActivityPub.ActivityPubController do
use Pleroma.Web, :controller
alias Pleroma.{Activity, User, Object}
alias Pleroma.Web.ActivityPub.{ObjectView, UserView}
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(:relay_active? when action in [:relay])
plug(:set_requester_reachable when action in [:inbox])
def relay_active?(conn, _) do
if Keyword.get(Application.get_env(:pleroma, :instance), :allow_relay) do
@ -289,4 +291,9 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubController do
|> put_status(500)
|> json("error")
end
defp set_requester_reachable(conn, _) do
Pleroma.Web.ControllerHelper.set_requester_reachable(conn)
conn
end
end

View File

@ -10,4 +10,9 @@ defmodule Pleroma.Web.ControllerHelper do
|> put_status(status)
|> json(json)
end
def set_requester_reachable(conn) do
with [referer] <- get_req_header(conn, "referer"),
do: Pleroma.Instances.set_reachable(referer)
end
end

View File

@ -15,6 +15,8 @@ defmodule Pleroma.Web.OStatus.OStatusController do
alias Pleroma.Web.ActivityPub.ActivityPub
plug(Pleroma.Web.FederatingPlug when action in [:salmon_incoming])
plug(:set_requester_reachable when action in [:salmon_incoming])
action_fallback(:errors)
def feed_redirect(conn, %{"nickname" => nickname}) do
@ -201,4 +203,9 @@ defmodule Pleroma.Web.OStatus.OStatusController do
|> put_status(500)
|> text("Something went wrong")
end
defp set_requester_reachable(conn, _) do
Pleroma.Web.ControllerHelper.set_requester_reachable(conn)
conn
end
end

View File

@ -173,7 +173,10 @@ defmodule Pleroma.Web.Salmon do
poster.(
url,
feed,
[{"Content-Type", "application/magic-envelope+xml"}]
[
{"Content-Type", "application/magic-envelope+xml"},
{"referer", Pleroma.Web.Endpoint.url()}
]
) do
Instances.set_reachable(url)
Logger.debug(fn -> "Pushed to #{url}, code #{code}" end)

View File

@ -275,7 +275,8 @@ defmodule Pleroma.Web.Websub do
xml,
[
{"Content-Type", "application/atom+xml"},
{"X-Hub-Signature", "sha1=#{signature}"}
{"X-Hub-Signature", "sha1=#{signature}"},
{"referer", Pleroma.Web.Endpoint.url()}
]
) do
Instances.set_reachable(callback)

View File

@ -4,9 +4,11 @@
defmodule Pleroma.Web.Websub.WebsubController do
use Pleroma.Web, :controller
alias Pleroma.{Repo, User}
alias Pleroma.Web.{Websub, Federator}
alias Pleroma.Web.Websub.WebsubClientSubscription
require Logger
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
user = User.get_cached_by_nickname(nickname)
@ -92,4 +96,9 @@ defmodule Pleroma.Web.Websub.WebsubController do
|> send_resp(500, "Error")
end
end
defp set_requester_reachable(conn, _) do
Pleroma.Web.ControllerHelper.set_requester_reachable(conn)
conn
end
end