From 6e9af773a87b5e8bb4c13915a4e557db8d232fab Mon Sep 17 00:00:00 2001 From: Mike Cifelli Date: Sat, 4 May 2024 15:25:44 -0400 Subject: [PATCH] Simplify self-signed certificate verification --- lib/chronoscope/gemini/connection_client.ex | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/lib/chronoscope/gemini/connection_client.ex b/lib/chronoscope/gemini/connection_client.ex index 00683ff..8299a22 100644 --- a/lib/chronoscope/gemini/connection_client.ex +++ b/lib/chronoscope/gemini/connection_client.ex @@ -29,19 +29,15 @@ defmodule Chronoscope.Gemini.ConnectionClient do defp tls_options(host) do host |> :tls_certificate_check.options() - |> Keyword.put(:verify_fun, {verify_fun(host), []}) + |> Keyword.put(:verify_fun, {&verify_fun/3, [check_hostname: String.to_charlist(host)]}) end - defp verify_fun(hostname) do - hostname_charlist = String.to_charlist(hostname) + defp verify_fun(certificate, {:bad_cert, :selfsigned_peer}, state) do + :ssl_verify_hostname.verify_fun(certificate, :valid_peer, state) + end - fn - certificate, {:bad_cert, :selfsigned_peer}, _state -> - :ssl_verify_hostname.verify_fun(certificate, :valid_peer, check_hostname: hostname_charlist) - - certificate, event, _state -> - :ssl_verify_hostname.verify_fun(certificate, event, check_hostname: hostname_charlist) - end + defp verify_fun(certificate, event, state) do + :ssl_verify_hostname.verify_fun(certificate, event, state) end defp make_request(socket, url) do