Refactor code
This commit is contained in:
		
							parent
							
								
									d278cfa353
								
							
						
					
					
						commit
						cf8c677c25
					
				@ -19,6 +19,7 @@ defmodule Chronoscope.NTS do
 | 
			
		||||
    |> Enum.map(fn {_, pid, _, _} -> GenServer.call(pid, :list) end)
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  @impl true
 | 
			
		||||
  def key_establishment(host, port) do
 | 
			
		||||
    name = "#{host}:#{port}"
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -37,12 +37,7 @@ defmodule Chronoscope.NTS.KeyEstablishmentClient do
 | 
			
		||||
    receive do
 | 
			
		||||
      {:ssl, _socket, response} ->
 | 
			
		||||
        :ssl.close(socket)
 | 
			
		||||
 | 
			
		||||
        case KeyEstablishmentResponse.parse(response) do
 | 
			
		||||
          {:ok, parsed_response} -> {:ok, Map.put(parsed_response, :cert_expiration, Certificate.expiration_date(peercert))}
 | 
			
		||||
          # todo - indicate errors in server response
 | 
			
		||||
          error -> error
 | 
			
		||||
        end
 | 
			
		||||
        parse_response(response, peercert)
 | 
			
		||||
 | 
			
		||||
      msg ->
 | 
			
		||||
        :ssl.close(socket)
 | 
			
		||||
@ -56,6 +51,13 @@ defmodule Chronoscope.NTS.KeyEstablishmentClient do
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  defp parse_response(response, peercert) do
 | 
			
		||||
    response
 | 
			
		||||
    |> KeyEstablishmentResponse.parse()
 | 
			
		||||
    |> Map.put(:cert_expiration, Certificate.expiration_date(peercert))
 | 
			
		||||
    |> then(&{:ok, &1})
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  defp handshake_failure_message(error) do
 | 
			
		||||
    cond do
 | 
			
		||||
      error =~ ~r/\{bad_cert,hostname_check_failed\}$/ ->
 | 
			
		||||
 | 
			
		||||
@ -17,7 +17,7 @@ defmodule Chronoscope.NTS.KeyEstablishmentResponse do
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  def parse(response) do
 | 
			
		||||
    {:ok, parse_response(response, %{})}
 | 
			
		||||
    parse_response(response, %{})
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  defp parse_response([], acc) do
 | 
			
		||||
 | 
			
		||||
@ -1,6 +1,12 @@
 | 
			
		||||
<.flash_group flash={@flash} />
 | 
			
		||||
<div class="left-[40rem] fixed inset-y-0 right-0 z-0 hidden lg:block xl:left-[50rem]">
 | 
			
		||||
  <svg viewBox="0 0 1480 957" fill="none" aria-hidden="true" class="absolute inset-0 h-full w-full" preserveAspectRatio="xMinYMid slice">
 | 
			
		||||
  <svg
 | 
			
		||||
    viewBox="0 0 1480 957"
 | 
			
		||||
    fill="none"
 | 
			
		||||
    aria-hidden="true"
 | 
			
		||||
    class="absolute inset-0 h-full w-full"
 | 
			
		||||
    preserveAspectRatio="xMinYMid slice"
 | 
			
		||||
  >
 | 
			
		||||
    <path fill="#EE7868" d="M0 0h1480v957H0z" />
 | 
			
		||||
    <path
 | 
			
		||||
      d="M137.542 466.27c-582.851-48.41-988.806-82.127-1608.412 658.2l67.39 810 3083.15-256.51L1535.94-49.622l-98.36 8.183C1269.29 281.468 734.115 515.799 146.47 467.012l-8.928-.742Z"
 | 
			
		||||
@ -59,7 +65,8 @@
 | 
			
		||||
            href="https://hexdocs.pm/phoenix/overview.html"
 | 
			
		||||
            class="group relative rounded-2xl px-6 py-4 text-sm font-semibold leading-6 text-zinc-900 sm:py-6"
 | 
			
		||||
          >
 | 
			
		||||
            <span class="absolute inset-0 rounded-2xl bg-zinc-50 transition group-hover:bg-zinc-100 sm:group-hover:scale-105"></span>
 | 
			
		||||
            <span class="absolute inset-0 rounded-2xl bg-zinc-50 transition group-hover:bg-zinc-100 sm:group-hover:scale-105">
 | 
			
		||||
            </span>
 | 
			
		||||
            <span class="relative flex items-center gap-4 sm:flex-col">
 | 
			
		||||
              <svg viewBox="0 0 24 24" fill="none" aria-hidden="true" class="h-6 w-6">
 | 
			
		||||
                <path d="m12 4 10-2v18l-10 2V4Z" fill="#18181B" fill-opacity=".15" />
 | 
			
		||||
@ -78,7 +85,8 @@
 | 
			
		||||
            href="https://github.com/phoenixframework/phoenix"
 | 
			
		||||
            class="group relative rounded-2xl px-6 py-4 text-sm font-semibold leading-6 text-zinc-900 sm:py-6"
 | 
			
		||||
          >
 | 
			
		||||
            <span class="absolute inset-0 rounded-2xl bg-zinc-50 transition group-hover:bg-zinc-100 sm:group-hover:scale-105"></span>
 | 
			
		||||
            <span class="absolute inset-0 rounded-2xl bg-zinc-50 transition group-hover:bg-zinc-100 sm:group-hover:scale-105">
 | 
			
		||||
            </span>
 | 
			
		||||
            <span class="relative flex items-center gap-4 sm:flex-col">
 | 
			
		||||
              <svg viewBox="0 0 24 24" aria-hidden="true" class="h-6 w-6">
 | 
			
		||||
                <path
 | 
			
		||||
@ -95,7 +103,8 @@
 | 
			
		||||
            href={"https://github.com/phoenixframework/phoenix/blob/v#{Application.spec(:phoenix, :vsn)}/CHANGELOG.md"}
 | 
			
		||||
            class="group relative rounded-2xl px-6 py-4 text-sm font-semibold leading-6 text-zinc-900 sm:py-6"
 | 
			
		||||
          >
 | 
			
		||||
            <span class="absolute inset-0 rounded-2xl bg-zinc-50 transition group-hover:bg-zinc-100 sm:group-hover:scale-105"></span>
 | 
			
		||||
            <span class="absolute inset-0 rounded-2xl bg-zinc-50 transition group-hover:bg-zinc-100 sm:group-hover:scale-105">
 | 
			
		||||
            </span>
 | 
			
		||||
            <span class="relative flex items-center gap-4 sm:flex-col">
 | 
			
		||||
              <svg viewBox="0 0 24 24" fill="none" aria-hidden="true" class="h-6 w-6">
 | 
			
		||||
                <path d="M12 1v6M12 17v6" stroke="#18181B" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" />
 | 
			
		||||
 | 
			
		||||
@ -18,7 +18,10 @@ defmodule Chronoscope.NTS.CertificateTest do
 | 
			
		||||
        |> expiration_date()
 | 
			
		||||
        |> DateTime.from_iso8601()
 | 
			
		||||
 | 
			
		||||
      assert DateTime.diff(expiration, DateTime.utc_now(), :day) == 12
 | 
			
		||||
      expiration_time_in_minutes = DateTime.diff(expiration, DateTime.utc_now(), :minute)
 | 
			
		||||
 | 
			
		||||
      assert expiration_time_in_minutes > 12 * 24 * 60 - 5
 | 
			
		||||
      assert expiration_time_in_minutes < 12 * 24 * 60 + 5
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -5,95 +5,101 @@ defmodule Chronoscope.NTS.KeyEstablishmentResponseTest do
 | 
			
		||||
 | 
			
		||||
  describe "Chronoscope.NTS.KeyEstablishmentResponse.parse()" do
 | 
			
		||||
    test "handles empty response" do
 | 
			
		||||
      assert parse([]) == {:ok, %{}}
 | 
			
		||||
      assert parse([]) == %{}
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    test "ignores unkown record" do
 | 
			
		||||
      assert parse([0x80, 0x21, 0x00, 0x04, 0x00, 0x01, 0x02, 0x03]) == {:ok, %{}}
 | 
			
		||||
      assert parse([0x80, 0x21, 0x00, 0x04, 0x00, 0x01, 0x02, 0x03]) == %{}
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    test "handles end of message record" do
 | 
			
		||||
      assert parse([0x80, 0x00, 0x00, 0x00]) == {:ok, %{}}
 | 
			
		||||
      assert parse([0x80, 0x00, 0x00, 0x00]) == %{}
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    test "handles next protocol negotiation record" do
 | 
			
		||||
      assert parse([0x80, 0x01, 0x00, 0x02, 0x00, 0x00]) == {:ok, %{next_protocols: ["NTPv4"]}}
 | 
			
		||||
      assert parse([0x80, 0x01, 0x00, 0x02, 0x00, 0x00]) == %{next_protocols: ["NTPv4"]}
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    test "does not handle next protocol negotiation record without critical bit" do
 | 
			
		||||
      assert parse([0x00, 0x01, 0x00, 0x02, 0x00, 0x00]) == {:ok, %{}}
 | 
			
		||||
      assert parse([0x00, 0x01, 0x00, 0x02, 0x00, 0x00]) == %{}
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    test "handles empty next protocols" do
 | 
			
		||||
      assert parse([0x80, 0x01, 0x00, 0x00]) == {:ok, %{next_protocols: []}}
 | 
			
		||||
      assert parse([0x80, 0x01, 0x00, 0x00]) == %{next_protocols: []}
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    test "handles multiple next protocols" do
 | 
			
		||||
      assert parse([0x80, 0x01, 0x00, 0x06, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00]) ==
 | 
			
		||||
               {:ok, %{next_protocols: ["NTPv4", "UNASSIGNED", "NTPv4"]}}
 | 
			
		||||
      assert parse([0x80, 0x01, 0x00, 0x06, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00]) == %{
 | 
			
		||||
               next_protocols: ["NTPv4", "UNASSIGNED", "NTPv4"]
 | 
			
		||||
             }
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    test "handles aead algorithm negotiation record" do
 | 
			
		||||
      assert parse([0x80, 0x04, 0x00, 0x02, 0x00, 0x0F]) == {:ok, %{aead_algorithms: ["AEAD_AES_SIV_CMAC_256"]}}
 | 
			
		||||
      assert parse([0x80, 0x04, 0x00, 0x02, 0x00, 0x0F]) == %{aead_algorithms: ["AEAD_AES_SIV_CMAC_256"]}
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    test "handles aead algorithm negotiation record without critical bit" do
 | 
			
		||||
      assert parse([0x00, 0x04, 0x00, 0x02, 0x00, 0x1E]) == {:ok, %{aead_algorithms: ["AEAD_AES_128_GCM_SIV"]}}
 | 
			
		||||
      assert parse([0x00, 0x04, 0x00, 0x02, 0x00, 0x1E]) == %{aead_algorithms: ["AEAD_AES_128_GCM_SIV"]}
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    test "handles empty aead algorithms" do
 | 
			
		||||
      assert parse([0x80, 0x04, 0x00, 0x00]) == {:ok, %{aead_algorithms: []}}
 | 
			
		||||
      assert parse([0x80, 0x04, 0x00, 0x00]) == %{aead_algorithms: []}
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    test "handles multiple aead algorithms" do
 | 
			
		||||
      assert parse([0x80, 0x04, 0x00, 0x06, 0x00, 0x1E, 0x00, 0x01, 0x00, 0x0F]) ==
 | 
			
		||||
               {:ok, %{aead_algorithms: ["AEAD_AES_SIV_CMAC_256", "UNKNOWN", "AEAD_AES_128_GCM_SIV"]}}
 | 
			
		||||
      assert parse([0x80, 0x04, 0x00, 0x06, 0x00, 0x1E, 0x00, 0x01, 0x00, 0x0F]) == %{
 | 
			
		||||
               aead_algorithms: ["AEAD_AES_SIV_CMAC_256", "UNKNOWN", "AEAD_AES_128_GCM_SIV"]
 | 
			
		||||
             }
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    test "handles error record" do
 | 
			
		||||
      assert parse([0x80, 0x02, 0x00, 0x02, 0x00, 0x01]) == {:ok, %{error: "Bad Request"}}
 | 
			
		||||
      assert parse([0x80, 0x02, 0x00, 0x02, 0x00, 0x01]) == %{error: "Bad Request"}
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    test "handles unknown error record" do
 | 
			
		||||
      assert parse([0x80, 0x02, 0x00, 0x02, 0x00, 0x99]) == {:ok, %{error: "153"}}
 | 
			
		||||
      assert parse([0x80, 0x02, 0x00, 0x02, 0x00, 0x99]) == %{error: "153"}
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    test "does not handle error record without critical bit" do
 | 
			
		||||
      assert parse([0x00, 0x02, 0x00, 0x02, 0x00, 0x01]) == {:ok, %{}}
 | 
			
		||||
      assert parse([0x00, 0x02, 0x00, 0x02, 0x00, 0x01]) == %{}
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    test "handles warning record" do
 | 
			
		||||
      assert parse([0x80, 0x03, 0x00, 0x02, 0x00, 0x01]) == {:ok, %{warning: "1"}}
 | 
			
		||||
      assert parse([0x80, 0x03, 0x00, 0x02, 0x00, 0x01]) == %{warning: "1"}
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    test "does not handle warning record without critical bit" do
 | 
			
		||||
      assert parse([0x00, 0x03, 0x00, 0x02, 0x00, 0x01]) == {:ok, %{}}
 | 
			
		||||
      assert parse([0x00, 0x03, 0x00, 0x02, 0x00, 0x01]) == %{}
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    test "handles server record" do
 | 
			
		||||
      assert parse([0x80, 0x06, 0x00, 0x09, ?1, ?2, ?7, ?., ?0, ?., ?0, ?., ?1]) == {:ok, %{server: "127.0.0.1"}}
 | 
			
		||||
      assert parse([0x80, 0x06, 0x00, 0x09, ?1, ?2, ?7, ?., ?0, ?., ?0, ?., ?1]) == %{server: "127.0.0.1"}
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    test "handles server record without critical bit" do
 | 
			
		||||
      assert parse([0x00, 0x06, 0x00, 0x09, ?1, ?2, ?7, ?., ?0, ?., ?0, ?., ?1]) == {:ok, %{server: "127.0.0.1"}}
 | 
			
		||||
      assert parse([0x00, 0x06, 0x00, 0x09, ?1, ?2, ?7, ?., ?0, ?., ?0, ?., ?1]) == %{server: "127.0.0.1"}
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    test "handles port record" do
 | 
			
		||||
      assert parse([0x80, 0x07, 0x00, 0x02, 0x04, 0xCE]) == {:ok, %{port: 1230}}
 | 
			
		||||
      assert parse([0x80, 0x07, 0x00, 0x02, 0x04, 0xCE]) == %{port: 1230}
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    test "handles port record without critical bit" do
 | 
			
		||||
      assert parse([0x00, 0x07, 0x00, 0x02, 0x04, 0xCE]) == {:ok, %{port: 1230}}
 | 
			
		||||
      assert parse([0x00, 0x07, 0x00, 0x02, 0x04, 0xCE]) == %{port: 1230}
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    test "handles cookie record" do
 | 
			
		||||
      assert parse([0x80, 0x05, 0x00, 0x0E, ?c, ?h, ?o, ?c, ?o, ?l, ?a, ?t, ?e, ?_, ?c, ?h, ?i, ?p]) ==
 | 
			
		||||
               {:ok, %{cookies: [~c"chocolate_chip"], cookie_length: 14}}
 | 
			
		||||
      assert parse([0x80, 0x05, 0x00, 0x0E, ?c, ?h, ?o, ?c, ?o, ?l, ?a, ?t, ?e, ?_, ?c, ?h, ?i, ?p]) == %{
 | 
			
		||||
               cookies: [~c"chocolate_chip"],
 | 
			
		||||
               cookie_length: 14
 | 
			
		||||
             }
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    test "handles cookie record without critical bit" do
 | 
			
		||||
      assert parse([0x00, 0x05, 0x00, 0x0E, ?c, ?h, ?o, ?c, ?o, ?l, ?a, ?t, ?e, ?_, ?c, ?h, ?i, ?p]) ==
 | 
			
		||||
               {:ok, %{cookies: [~c"chocolate_chip"], cookie_length: 14}}
 | 
			
		||||
      assert parse([0x00, 0x05, 0x00, 0x0E, ?c, ?h, ?o, ?c, ?o, ?l, ?a, ?t, ?e, ?_, ?c, ?h, ?i, ?p]) == %{
 | 
			
		||||
               cookies: [~c"chocolate_chip"],
 | 
			
		||||
               cookie_length: 14
 | 
			
		||||
             }
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    test "sets cookie length to longest cookie" do
 | 
			
		||||
@ -102,7 +108,7 @@ defmodule Chronoscope.NTS.KeyEstablishmentResponseTest do
 | 
			
		||||
                 [0x80, 0x05, 0x00, 0x03, ?c, ?c, ?c] ++
 | 
			
		||||
                 [0x80, 0x05, 0x00, 0x01, ?c]
 | 
			
		||||
             ) ==
 | 
			
		||||
               {:ok, %{cookies: [~c"c", ~c"ccc", ~c"c"], cookie_length: 3}}
 | 
			
		||||
               %{cookies: [~c"c", ~c"ccc", ~c"c"], cookie_length: 3}
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    test "handles full response" do
 | 
			
		||||
@ -115,15 +121,14 @@ defmodule Chronoscope.NTS.KeyEstablishmentResponseTest do
 | 
			
		||||
                 [0x80, 0x07, 0x00, 0x02, 0x04, 0xCE] ++
 | 
			
		||||
                 [0x80, 0x00, 0x00, 0x00]
 | 
			
		||||
             ) ==
 | 
			
		||||
               {:ok,
 | 
			
		||||
                %{
 | 
			
		||||
                  cookies: [~c"c"],
 | 
			
		||||
                  aead_algorithms: ["AEAD_AES_SIV_CMAC_256", "UNKNOWN", "AEAD_AES_128_GCM_SIV"],
 | 
			
		||||
                  cookie_length: 1,
 | 
			
		||||
                  next_protocols: ["NTPv4"],
 | 
			
		||||
                  port: 1230,
 | 
			
		||||
                  server: "127.0.0.1"
 | 
			
		||||
                }}
 | 
			
		||||
               %{
 | 
			
		||||
                 cookies: [~c"c"],
 | 
			
		||||
                 aead_algorithms: ["AEAD_AES_SIV_CMAC_256", "UNKNOWN", "AEAD_AES_128_GCM_SIV"],
 | 
			
		||||
                 cookie_length: 1,
 | 
			
		||||
                 next_protocols: ["NTPv4"],
 | 
			
		||||
                 port: 1230,
 | 
			
		||||
                 server: "127.0.0.1"
 | 
			
		||||
               }
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    test "doesn't read past end of message record" do
 | 
			
		||||
@ -136,11 +141,10 @@ defmodule Chronoscope.NTS.KeyEstablishmentResponseTest do
 | 
			
		||||
                 [0x80, 0x06, 0x00, 0x09, ?1, ?2, ?7, ?., ?0, ?., ?0, ?., ?1] ++
 | 
			
		||||
                 [0x80, 0x07, 0x00, 0x02, 0x04, 0xCE]
 | 
			
		||||
             ) ==
 | 
			
		||||
               {:ok,
 | 
			
		||||
                %{
 | 
			
		||||
                  aead_algorithms: ["AEAD_AES_128_GCM_SIV"],
 | 
			
		||||
                  next_protocols: ["NTPv4"]
 | 
			
		||||
                }}
 | 
			
		||||
               %{
 | 
			
		||||
                 aead_algorithms: ["AEAD_AES_128_GCM_SIV"],
 | 
			
		||||
                 next_protocols: ["NTPv4"]
 | 
			
		||||
               }
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user