fix remote agent ip

This commit is contained in:
Graham McIntire 2026-01-24 13:23:03 -06:00
parent e6054129b2
commit af71d3f4f9
No known key found for this signature in database

View file

@ -52,8 +52,9 @@ defmodule ToweropsWeb.AgentChannel do
# Subscribe to assignment changes for this agent
Phoenix.PubSub.subscribe(Towerops.PubSub, "agent:#{agent_token.id}:assignments")
# Update last_seen_at on join
_ = Agents.update_agent_token_heartbeat(agent_token.id, nil, %{})
# Update last_seen_at and IP on join
remote_ip = get_remote_ip(socket)
_ = Agents.update_agent_token_heartbeat(agent_token.id, remote_ip, %{})
# Send initial job list
send(self(), :send_jobs)
@ -454,4 +455,27 @@ defmodule ToweropsWeb.AgentChannel do
end
defp parse_float(_), do: nil
defp get_remote_ip(socket) do
case socket.transport_pid do
nil ->
nil
pid when is_pid(pid) ->
case :ranch.get_addr(pid) do
{ip, _port} -> format_ip(ip)
_ -> nil
end
end
rescue
_ -> nil
end
defp format_ip({a, b, c, d}), do: "#{a}.#{b}.#{c}.#{d}"
defp format_ip({a, b, c, d, e, f, g, h}),
do:
"#{Integer.to_string(a, 16)}:#{Integer.to_string(b, 16)}:#{Integer.to_string(c, 16)}:#{Integer.to_string(d, 16)}:#{Integer.to_string(e, 16)}:#{Integer.to_string(f, 16)}:#{Integer.to_string(g, 16)}:#{Integer.to_string(h, 16)}"
defp format_ip(_), do: nil
end