10000 ui: do not crash on wrong password in onvif page by gBillal · Pull Request #586 · evercam/ex_nvr · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

ui: do not crash on wrong password in onvif page #586

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions nerves_fw/.env.sample
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
export NERVES_HUB_PRODUCT_KEY=nerves_hub_key
export NERVES_HUB_PRODUCT_SECRET=nerves_hub_secret

export SECRET_KEY_BASE=phoenix_secret_key_base

# remote configurer
export REMOTE_CONFIGURER_URL=https://configure.me.url/:id/config
export REMOTE_CONFIGURER_TOKEN=api_token
export REMOTE_CONFIGURER_VERSION=1.0

# Nerves hub
export NERVES_HUB_PRODUCT_KEY=nerves_hub_key
export NERVES_HUB_PRODUCT_SECRET=nerves_hub_secret
export NH_TOKEN=nerves_hub_token
export NERVES_HUB_URI=nerves_hub_url
export NERVES_HUB_ORG=nerves_hub_org

# Elixir env variables
export MIX_ENV=prod
export MIX_TARGET=ex_nvr_rpi4
2 changes: 1 addition & 1 deletion ui/lib/ex_nvr_web/live/onvif/stream_profile.ex
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ defmodule ExNVRWeb.Onvif.StreamProfile do
field={@update_form[:gov_length]}
id={"encoder_config_gov_#{@id}"}
type="number"
label="Gov Length"
label="Group of Pictures"
min={@view_encoder_options.gov_length_min}
max={@view_encoder_options.gov_length_max}
/>
Expand Down
81 changes: 47 additions & 34 deletions ui/lib/ex_nvr_web/live/onvif_discovery_live.ex
FC22
Original file line number Diff line number Diff line change
Expand Up @@ -36,24 +36,30 @@ defmodule ExNVRWeb.OnvifDiscoveryLive do
end

def handle_event("discover", %{"discover_settings" => params}, socket) do
with {:ok, validated_params} <- validate_discover_params(params),
discovered_devices <-
ExNVR.Devices.discover(
probe_timeout: to_timeout(second: validated_params[:timeout]),
ip_address: validated_params[:ip_addr]
) do
onvif_devices =
Enum.map(
discovered_devices,
&get_onvif_device(&1, validated_params.username, validated_params.password)
)

socket
|> assign_discovery_form(params)
|> assign_discovered_devices(onvif_devices)
|> then(&{:noreply, &1})
else
{:error, %Changeset{} = changeset} ->
case validate_discover_params(params) do
{:ok, params} ->
{onvif_devices, socket} =
ExNVR.Devices.discover(
probe_timeout: to_timeout(second: params[:timeout]),
ip_address: params[:ip_addr]
)
|> Enum.map_reduce(socket, fn probe, socket ->
case get_onvif_device(probe, params.username, params.password) do
{:ok, device} ->
{device, socket}

{:error, reason, device} ->
error = "Failed to connect to device: #{device.address} - #{reason}"
{device, put_flash(socket, :error, error)}
end
end)

socket
|> assign_discovery_form(params)
|> assign_discovered_devices(onvif_devices)
|> then(&{:noreply, &1})

{:error, changeset} ->
{:noreply, assign_discovery_form(socket, changeset)}
end
end
Expand Down Expand Up @@ -113,7 +119,7 @@ defmodule ExNVRWeb.OnvifDiscoveryLive do
type: :ip,
vendor: selected_device.manufacturer,
model: selected_device.model,
mac: device_details.network_interface.info.hw_address,
mac: device_details.network_interface && device_details.network_interface.info.hw_address,
url: selected_device.address,
stream_config: stream_config,
credentials: %{username: selected_device.username, password: selected_device.password}
Expand Down Expand Up @@ -157,12 +163,19 @@ defmodule ExNVRWeb.OnvifDiscoveryLive do
{:noreply, socket}
end

defp assign_discovery_form(socket, params \\ nil) do
assign(
socket,
:discover_form,
to_form(params || @default_discovery_settings, as: :discover_settings)
)
defp assign_discovery_form(socket, params \\ nil)

defp assign_discovery_form(socket, %Ecto.Changeset{} = changeset) do
assign(socket, :discover_form, to_form(changeset, as: :discover_settings))
end

defp assign_discovery_form(socket, params) do
params =
Map.new(params || @default_discovery_settings, fn {key, value} ->
{to_string(key), value}
end)

assign(socket, :discover_form, to_form(params, as: :discover_settings))
end

defp assign_discovered_devices(socket, devices \\ []) do
Expand All @@ -184,22 +197,20 @@ defmodule ExNVRWeb.OnvifDiscoveryLive do
|> Changeset.apply_action(:create)
end

defp get_onvif_device(probe, username, password)
when not is_nil(username) and not is_nil(password) do
case Onvif.Device.init(probe, username, password) do
{:ok, device} -> device
_error -> get_onvif_device(probe, nil, nil)
defp get_onvif_device(probe, username, password) do
case Onvif.Device.init(probe, username || "", password || "") do
{:ok, device} -> {:ok, device}
{:error, reason} -> {:error, reason, get_onvif_device(probe)}
end
end

defp get_onvif_device(probe, _username, _password) do
defp get_onvif_device(probe) do
%Onvif.Device{
address: List.first(probe.address),
manufacturer: scope_value(probe.scopes, "hardware"),
model: scope_value(probe.scopes, "name"),
username: "",
password: "",
scopes: probe.scopes
scopes: probe.scopes,
auth_type: :no_auth
}
end

Expand All @@ -217,6 +228,8 @@ defmodule ExNVRWeb.OnvifDiscoveryLive do
end
end

defp get_profiles(%{onvif_device: %{media_ver20_service_path: nil}} = details), do: details

defp get_profiles(details) do
case Media.Ver20.GetProfiles.request(details.onvif_device) do
{:ok, profiles} ->
Expand Down
4 changes: 4 additions & 0 deletions ui/test/ex_nvr_web/live/onvif_discovery_live_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ defmodule ExNVRWeb.OnvifDiscoveryLiveTest do
test "render found devices", %{conn: conn} do
expect(Onvif.Discovery, :probe, fn _params -> @probes end)

expect(Onvif.Device, :init, 2, fn _probe, "", "" ->
{:error, "Invalid Credentials"}
end)

{:ok, lv, _html} = live(conn, ~p"/onvif-discovery")

html =
Expand Down
0