-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add reverse proxy * add feature flag to enable reverse proxy * update version * delete bundlex folder in Dockerfile * refactor timestamp adjuster * fix video assembler * upgrade deps
- Loading branch information
Showing
20 changed files
with
161 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
defmodule ExNVRWeb.Plug.ProxyAllow do | ||
@moduledoc """ | ||
Check if reverse proxy requests are allowed | ||
""" | ||
|
||
import ExNVRWeb.Controller.Helpers | ||
|
||
alias Plug.Conn | ||
|
||
def init(opts), do: opts | ||
|
||
def call(%Conn{} = conn, _opts) do | ||
if Application.get_env(:ex_nvr_web, :enable_reverse_proxy, false), | ||
do: conn, | ||
else: not_found(conn) | ||
end | ||
end |
21 changes: 21 additions & 0 deletions
21
apps/ex_nvr_web/lib/ex_nvr_web/plugs/proxy_path_rewriter.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
defmodule ExNVRWeb.Plug.ProxyPathRewriter do | ||
@moduledoc false | ||
|
||
alias Plug.Conn | ||
|
||
def init(opts), do: opts | ||
|
||
def call(%Conn{} = conn, _opts) do | ||
case Enum.count(conn.path_info) < 2 do | ||
true -> | ||
conn | ||
|
||
false -> | ||
{upstream, path_infos} = List.pop_at(conn.path_info, 1) | ||
|
||
conn | ||
|> Conn.put_req_header("x-host", upstream) | ||
|> Map.put(:path_info, path_infos) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
defmodule ExNVRWeb.ReverseProxy do | ||
@moduledoc false | ||
|
||
require Logger | ||
|
||
import Plug.Conn | ||
|
||
@private_ips [ | ||
{{10, 0, 0, 0}, {10, 255, 255, 255}}, | ||
{{172, 16, 0, 0}, {172, 31, 255, 255}}, | ||
{{192, 168, 0, 0}, {192, 168, 255, 255}} | ||
] | ||
|
||
def reverse_proxy(conn) do | ||
%URI{scheme: get_scheme(conn), host: validate_host(conn)} |> URI.to_string() | ||
end | ||
|
||
def handle_error(error, conn) do | ||
Logger.error("error occurred while trying to reverse proxy request: #{inspect(error)}") | ||
send_resp(conn, 500, "Internal Server Error") | ||
end | ||
|
||
defp validate_host(conn) do | ||
with [addr] <- get_req_header(conn, "x-host"), | ||
{:ok, ip_addr} <- :inet.parse_ipv4_address(to_charlist(addr)), | ||
true <- Enum.any?(@private_ips, &(elem(&1, 0) < ip_addr and elem(&1, 1) > ip_addr)) do | ||
:inet.ntoa(ip_addr) |> to_string() | ||
else | ||
_reason -> | ||
UUID.uuid4() | ||
end | ||
end | ||
|
||
defp get_scheme(conn) do | ||
case get_req_header(conn, "x-scheme") do | ||
[scheme] when scheme in ["http", "https"] -> scheme | ||
_other -> "http" | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
defmodule ExNVR.Umbrella.MixProject do | ||
use Mix.Project | ||
|
||
@version "0.15.2" | ||
@version "0.16.0" | ||
|
||
def project do | ||
[ | ||
|
Oops, something went wrong.