Allow instance to have non-standard ports

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel 2022-04-20 18:35:18 +02:00
parent 19052ba48b
commit f6ac72f08d
No known key found for this signature in database
GPG key ID: A061B9DDE0CA0773
4 changed files with 23 additions and 8 deletions

View file

@ -8,7 +8,9 @@ defmodule Mobilizon.Federation.ActivityPub.Publisher do
alias Mobilizon.Federation.ActivityPub.{Activity, Federator, Relay, Transmogrifier, Visibility} alias Mobilizon.Federation.ActivityPub.{Activity, Federator, Relay, Transmogrifier, Visibility}
alias Mobilizon.Federation.HTTPSignatures.Signature alias Mobilizon.Federation.HTTPSignatures.Signature
require Logger require Logger
import Mobilizon.Federation.ActivityPub.Utils, only: [remote_actors: 1]
import Mobilizon.Federation.ActivityPub.Utils,
only: [remote_actors: 1, create_full_domain_string: 1]
@doc """ @doc """
Publish an activity to all appropriated audiences inboxes Publish an activity to all appropriated audiences inboxes
@ -77,7 +79,7 @@ defmodule Mobilizon.Federation.ActivityPub.Publisher do
Tesla.Env.result() Tesla.Env.result()
def publish_one(%{inbox: inbox, json: json, actor: actor, id: id}) do def publish_one(%{inbox: inbox, json: json, actor: actor, id: id}) do
Logger.info("Federating #{id} to #{inbox}") Logger.info("Federating #{id} to #{inbox}")
%URI{host: host, path: path} = URI.parse(inbox) %URI{path: path} = uri = URI.new!(inbox)
digest = Signature.build_digest(json) digest = Signature.build_digest(json)
date = Signature.generate_date_header() date = Signature.generate_date_header()
@ -87,7 +89,7 @@ defmodule Mobilizon.Federation.ActivityPub.Publisher do
signature = signature =
Signature.sign(actor, %{ Signature.sign(actor, %{
"(request-target)": "post #{path}", "(request-target)": "post #{path}",
host: host, host: create_full_domain_string(uri),
"content-length": byte_size(json), "content-length": byte_size(json),
digest: digest, digest: digest,
date: date date: date

View file

@ -14,9 +14,9 @@ defmodule Mobilizon.Federation.ActivityPub.Relay do
alias Mobilizon.Federation.ActivityPub.{Actions, Activity, Transmogrifier} alias Mobilizon.Federation.ActivityPub.{Actions, Activity, Transmogrifier}
alias Mobilizon.Federation.ActivityPub.Actor, as: ActivityPubActor alias Mobilizon.Federation.ActivityPub.Actor, as: ActivityPubActor
alias Mobilizon.Federation.WebFinger alias Mobilizon.Federation.WebFinger
alias Mobilizon.Service.Workers.Background
alias Mobilizon.GraphQL.API.Follows alias Mobilizon.GraphQL.API.Follows
alias Mobilizon.Service.Workers.Background
import Mobilizon.Federation.ActivityPub.Utils, only: [create_full_domain_string: 1]
require Logger require Logger
@ -172,14 +172,14 @@ defmodule Mobilizon.Federation.ActivityPub.Relay do
defp fetch_actor("http://" <> address), do: fetch_actor(address) defp fetch_actor("http://" <> address), do: fetch_actor(address)
defp fetch_actor(address) do defp fetch_actor(address) do
%URI{host: host} = URI.parse("http://" <> address) %URI{host: host} = uri = URI.parse("http://" <> address)
cond do cond do
String.contains?(address, "@") -> String.contains?(address, "@") ->
check_actor(address) check_actor(address)
!is_nil(host) -> !is_nil(host) ->
check_actor("relay@#{host}") uri |> create_full_domain_string() |> check_actor()
true -> true ->
{:error, :bad_url} {:error, :bad_url}

View file

@ -781,4 +781,16 @@ defmodule Mobilizon.Federation.ActivityPub.Utils do
params params
end end
end end
@schemes_with_no_port ["http", "https"]
def create_full_domain_string(%URI{host: host, port: nil}), do: host
def create_full_domain_string(%URI{host: host, port: port}) do
if port in Enum.map(@schemes_with_no_port, &URI.default_port/1) do
host
else
"#{host}:#{port}"
end
end
end end

View file

@ -17,6 +17,7 @@ defmodule Mobilizon.Federation.ActivityStream.Converter.Actor do
alias Mobilizon.Service.RichMedia.Parser alias Mobilizon.Service.RichMedia.Parser
alias Mobilizon.Web.Upload alias Mobilizon.Web.Upload
import Mobilizon.Federation.ActivityStream.Converter.Utils, only: [get_address: 1] import Mobilizon.Federation.ActivityStream.Converter.Utils, only: [get_address: 1]
import Mobilizon.Federation.ActivityPub.Utils, only: [create_full_domain_string: 1]
@behaviour Converter @behaviour Converter
@ -54,7 +55,7 @@ defmodule Mobilizon.Federation.ActivityStream.Converter.Actor do
outbox_url: data["outbox"], outbox_url: data["outbox"],
following_url: data["following"], following_url: data["following"],
followers_url: data["followers"], followers_url: data["followers"],
domain: URI.parse(data["id"]).host, domain: data["id"] |> URI.new!() |> create_full_domain_string(),
manually_approves_followers: data["manuallyApprovesFollowers"], manually_approves_followers: data["manuallyApprovesFollowers"],
type: data["type"], type: data["type"],
visibility: if(Map.get(data, "discoverable", false) == true, do: :public, else: :unlisted), visibility: if(Map.get(data, "discoverable", false) == true, do: :public, else: :unlisted),