mirror of
https://github.com/livebook-dev/livebook.git
synced 2025-01-01 20:51:44 +08:00
Skip GenServer in Tailscale ZTA
This commit is contained in:
parent
9cbdae2786
commit
1332573aa4
1 changed files with 13 additions and 21 deletions
|
@ -1,37 +1,29 @@
|
||||||
defmodule Livebook.ZTA.Tailscale do
|
defmodule Livebook.ZTA.Tailscale do
|
||||||
use GenServer
|
|
||||||
require Logger
|
require Logger
|
||||||
|
|
||||||
defstruct [:address]
|
def child_spec(opts) do
|
||||||
|
%{id: __MODULE__, start: {__MODULE__, :start_link, [opts]}}
|
||||||
|
end
|
||||||
|
|
||||||
def start_link(opts) do
|
def start_link(opts) do
|
||||||
options = [address: opts[:identity_key]]
|
name = Keyword.fetch!(opts, :name)
|
||||||
GenServer.start_link(__MODULE__, options, Keyword.take(opts, [:name]))
|
address = Keyword.fetch!(opts, :identity_key)
|
||||||
end
|
|
||||||
|
|
||||||
def authenticate(name, conn, _opts) do
|
if not String.starts_with?(address, "http") and
|
||||||
remote_ip = to_string(:inet_parse.ntoa(conn.remote_ip))
|
|
||||||
tailscale_address = GenServer.call(name, :get_address)
|
|
||||||
user = authenticate_ip(remote_ip, tailscale_address)
|
|
||||||
{conn, user}
|
|
||||||
end
|
|
||||||
|
|
||||||
@impl true
|
|
||||||
def init(options) do
|
|
||||||
%{address: address} = state = struct!(__MODULE__, options)
|
|
||||||
|
|
||||||
if not String.starts_with?(state.address, "http") and
|
|
||||||
not File.exists?(address) do
|
not File.exists?(address) do
|
||||||
Logger.error("Tailscale socket does not exist: #{inspect(address)}")
|
Logger.error("Tailscale socket does not exist: #{inspect(address)}")
|
||||||
raise "invalid Tailscale ZTA configuration"
|
raise "invalid Tailscale ZTA configuration"
|
||||||
end
|
end
|
||||||
|
|
||||||
{:ok, state}
|
:persistent_term.put({__MODULE__, name}, address)
|
||||||
|
:ignore
|
||||||
end
|
end
|
||||||
|
|
||||||
@impl true
|
def authenticate(name, conn, _opts) do
|
||||||
def handle_call(:get_address, _from, state) do
|
remote_ip = to_string(:inet_parse.ntoa(conn.remote_ip))
|
||||||
{:reply, state.address, state}
|
tailscale_address = :persistent_term.get({__MODULE__, name})
|
||||||
|
user = authenticate_ip(remote_ip, tailscale_address)
|
||||||
|
{conn, user}
|
||||||
end
|
end
|
||||||
|
|
||||||
defp authenticate_ip(remote_ip, address) do
|
defp authenticate_ip(remote_ip, address) do
|
||||||
|
|
Loading…
Reference in a new issue