From f6ac72f08d54bebef13f6e882bf730faed50a24e Mon Sep 17 00:00:00 2001 From: Thomas Citharel Date: Wed, 20 Apr 2022 18:35:18 +0200 Subject: [PATCH] Allow instance to have non-standard ports Signed-off-by: Thomas Citharel --- lib/federation/activity_pub/publisher.ex | 8 +++++--- lib/federation/activity_pub/relay.ex | 8 ++++---- lib/federation/activity_pub/utils.ex | 12 ++++++++++++ lib/federation/activity_stream/converter/actor.ex | 3 ++- 4 files changed, 23 insertions(+), 8 deletions(-) diff --git a/lib/federation/activity_pub/publisher.ex b/lib/federation/activity_pub/publisher.ex index 355d4ed14..f276cb741 100644 --- a/lib/federation/activity_pub/publisher.ex +++ b/lib/federation/activity_pub/publisher.ex @@ -8,7 +8,9 @@ defmodule Mobilizon.Federation.ActivityPub.Publisher do alias Mobilizon.Federation.ActivityPub.{Activity, Federator, Relay, Transmogrifier, Visibility} alias Mobilizon.Federation.HTTPSignatures.Signature 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 """ Publish an activity to all appropriated audiences inboxes @@ -77,7 +79,7 @@ defmodule Mobilizon.Federation.ActivityPub.Publisher do Tesla.Env.result() def publish_one(%{inbox: inbox, json: json, actor: actor, id: id}) do 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) date = Signature.generate_date_header() @@ -87,7 +89,7 @@ defmodule Mobilizon.Federation.ActivityPub.Publisher do signature = Signature.sign(actor, %{ "(request-target)": "post #{path}", - host: host, + host: create_full_domain_string(uri), "content-length": byte_size(json), digest: digest, date: date diff --git a/lib/federation/activity_pub/relay.ex b/lib/federation/activity_pub/relay.ex index 52ab3fe6d..00c011b11 100644 --- a/lib/federation/activity_pub/relay.ex +++ b/lib/federation/activity_pub/relay.ex @@ -14,9 +14,9 @@ defmodule Mobilizon.Federation.ActivityPub.Relay do alias Mobilizon.Federation.ActivityPub.{Actions, Activity, Transmogrifier} alias Mobilizon.Federation.ActivityPub.Actor, as: ActivityPubActor alias Mobilizon.Federation.WebFinger - alias Mobilizon.Service.Workers.Background - alias Mobilizon.GraphQL.API.Follows + alias Mobilizon.Service.Workers.Background + import Mobilizon.Federation.ActivityPub.Utils, only: [create_full_domain_string: 1] require Logger @@ -172,14 +172,14 @@ defmodule Mobilizon.Federation.ActivityPub.Relay do defp fetch_actor("http://" <> address), do: fetch_actor(address) defp fetch_actor(address) do - %URI{host: host} = URI.parse("http://" <> address) + %URI{host: host} = uri = URI.parse("http://" <> address) cond do String.contains?(address, "@") -> check_actor(address) !is_nil(host) -> - check_actor("relay@#{host}") + uri |> create_full_domain_string() |> check_actor() true -> {:error, :bad_url} diff --git a/lib/federation/activity_pub/utils.ex b/lib/federation/activity_pub/utils.ex index 1eaf7a388..c617f319e 100644 --- a/lib/federation/activity_pub/utils.ex +++ b/lib/federation/activity_pub/utils.ex @@ -781,4 +781,16 @@ defmodule Mobilizon.Federation.ActivityPub.Utils do params 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 diff --git a/lib/federation/activity_stream/converter/actor.ex b/lib/federation/activity_stream/converter/actor.ex index 27fe5037a..6991bc256 100644 --- a/lib/federation/activity_stream/converter/actor.ex +++ b/lib/federation/activity_stream/converter/actor.ex @@ -17,6 +17,7 @@ defmodule Mobilizon.Federation.ActivityStream.Converter.Actor do alias Mobilizon.Service.RichMedia.Parser alias Mobilizon.Web.Upload import Mobilizon.Federation.ActivityStream.Converter.Utils, only: [get_address: 1] + import Mobilizon.Federation.ActivityPub.Utils, only: [create_full_domain_string: 1] @behaviour Converter @@ -54,7 +55,7 @@ defmodule Mobilizon.Federation.ActivityStream.Converter.Actor do outbox_url: data["outbox"], following_url: data["following"], followers_url: data["followers"], - domain: URI.parse(data["id"]).host, + domain: data["id"] |> URI.new!() |> create_full_domain_string(), manually_approves_followers: data["manuallyApprovesFollowers"], type: data["type"], visibility: if(Map.get(data, "discoverable", false) == true, do: :public, else: :unlisted),