Handle EXTERNAL_URL missing scheme (#1326)

Fixes #1314
This commit is contained in:
Jamil
2023-01-17 10:46:52 -08:00
committed by GitHub
parent 7ece067333
commit 421d56b0ea
2 changed files with 29 additions and 1 deletions

View File

@@ -1,4 +1,5 @@
[
plugins: [Phoenix.LiveView.HTMLFormatter],
subdirectories: ["apps/*"],
inputs: [
"*.{ex,exs}",

View File

@@ -1,6 +1,8 @@
defmodule FzHttp.Repo.Migrations.FixSitesNullableFields do
use Ecto.Migration
require Logger
def change do
dns = System.get_env("WIREGUARD_DNS", "1.1.1.1,1.0.0.1")
mtu = System.get_env("WIREGUARD_MTU", "1280")
@@ -10,7 +12,7 @@ defmodule FzHttp.Repo.Migrations.FixSitesNullableFields do
endpoint =
System.get_env(
"WIREGUARD_ENDPOINT",
URI.parse(System.get_env("EXTERNAL_URL", "https://localhost/")).host
host()
) <> ":" <> System.get_env("WIREGUARD_PORT", "51820")
execute("""
@@ -43,4 +45,29 @@ defmodule FzHttp.Repo.Migrations.FixSitesNullableFields do
WHERE endpoint IS NULL
""")
end
defp host do
external_url_var = System.get_env("EXTERNAL_URL")
substitute = "https://localhost/"
external_url =
if is_nil(external_url_var) || String.length(external_url_var) == 0 do
Logger.warn("EXTERNAL_URL is empty! Using #{substitute} as basis for WireGuard endpoint.")
substitute
else
external_url_var
end
parsed_host = URI.parse(external_url).host
if is_nil(parsed_host) do
Logger.warn(
"EXTERNAL_URL doesn't seem to contain a valid URL. Assuming https://#{external_url}."
)
external_url
else
parsed_host
end
end
end