Skip to content
This repository has been archived by the owner on Jun 11, 2023. It is now read-only.

Rewrite Log service #410

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
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
8 changes: 4 additions & 4 deletions events.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
"emits": [
"Log.Created",
"Log.Modified",
"Log.Deleted"
"Log.Destroyed"
]
},
"Process.Process":{
Expand Down Expand Up @@ -176,7 +176,7 @@
"publishable": [
"Log.Created",
"Log.Modified",
"Log.Deleted",
"Log.Destroyed",
"Network.Bounce.Created",
"Network.Bounce.CreateFailed",
"Network.Bounce.Removed",
Expand All @@ -188,7 +188,7 @@
"Process.Completed",
"Process.Killed",
"File.Added",
"File.Deleted",
"File.Destroyed",
"File.Uploaded",
"Story.Email.Sent",
"Story.Reply.Sent",
Expand All @@ -207,7 +207,7 @@
"DownloadCracker": {
"filters": [
"File.Downloaded",
"File.Deleted",
"File.Destroyed",
"Process.Created"
],
"emits": [
Expand Down
1 change: 1 addition & 0 deletions lib/balance/software.ex
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,6 @@ balance Software do
|> mul(modules.overflow.version)

bruteforce + overflow
# |> float_div(2)
end
end
38 changes: 26 additions & 12 deletions lib/event/dispatcher.ex
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,33 @@ defmodule Helix.Event.Dispatcher do
##############################################################################

# All
event LogEvent.Forge.Processed
event LogEvent.Recover.Processed
event LogEvent.Log.Created
event LogEvent.Log.Deleted
event LogEvent.Log.Modified
event LogEvent.Log.Destroyed
event LogEvent.Log.Recovered
event LogEvent.Log.Revised

# Custom handlers
event LogEvent.Forge.Processed,
LogHandler.Log,
:forge_processed

event LogEvent.Recover.Processed,
LogHandler.Log,
:recover_processed

event LogEvent.Log.Revised,
ProcessHandler.TOP,
:object_handler

event LogEvent.Log.Recovered,
ProcessHandler.TOP,
:object_handler

event LogEvent.Log.Destroyed,
ProcessHandler.TOP,
:object_handler

##############################################################################
# Process events
Expand Down Expand Up @@ -209,8 +233,6 @@ defmodule Helix.Event.Dispatcher do
event SoftwareEvent.File.Transfer.Processed
event SoftwareEvent.Firewall.Started
event SoftwareEvent.Firewall.Stopped
event SoftwareEvent.LogForge.LogCreate.Processed
event SoftwareEvent.LogForge.LogEdit.Processed
event SoftwareEvent.Virus.Collect.Processed
event SoftwareEvent.Virus.Collected
event SoftwareEvent.Virus.Installed
Expand Down Expand Up @@ -249,14 +271,6 @@ defmodule Helix.Event.Dispatcher do
ProcessHandler.Cracker,
:firewall_stopped

event SoftwareEvent.LogForge.LogCreate.Processed,
LogHandler.Log,
:log_forge_conclusion

event SoftwareEvent.LogForge.LogEdit.Processed,
LogHandler.Log,
:log_forge_conclusion

event SoftwareEvent.Virus.Collect.Processed,
SoftwareHandler.Virus,
:handle_collect
Expand Down
99 changes: 72 additions & 27 deletions lib/event/loggable/flow.ex
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ defmodule Helix.Event.Loggable.Flow do
alias Helix.Event
alias Helix.Event.Loggable.Utils, as: LoggableUtils
alias Helix.Entity.Model.Entity
alias Helix.Log.Model.Log
alias Helix.Log.Model.LogType
alias Helix.Log.Action.Log, as: LogAction
alias Helix.Network.Model.Bounce
alias Helix.Network.Model.Network
Expand All @@ -32,9 +34,7 @@ defmodule Helix.Event.Loggable.Flow do
alias Helix.Server.Model.Server

@type log_entry ::
{Server.id, Entity.id, log_msg}

@type log_msg :: String.t
{Server.id, Entity.id, Log.info}

@doc """
Top-level macro for events wanting to implement the Loggable protocol.
Expand Down Expand Up @@ -66,13 +66,12 @@ defmodule Helix.Event.Loggable.Flow do
"""
defmacro log_map(map) do
quote do
map = unquote(map)
unquote(map)

# Put default values (if not specified)
map
|> Map.put_new(:network_id, nil)
|> Map.put_new(:endpoint_id, nil)
|> Map.put_new(:msg_endpoint, nil)
|> Map.put_new(:data_both, %{})
|> Map.put_new(:opts, %{})
end
end
Expand Down Expand Up @@ -119,8 +118,11 @@ defmodule Helix.Event.Loggable.Flow do
gateway_id: gateway_id,
endpoint_id: endpoint_id,
network_id: network_id,
msg_gateway: msg_gateway,
msg_endpoint: msg_endpoint,
type_gateway: type_gateway,
data_gateway: data_gateway,
type_endpoint: type_endpoint,
data_endpoint: data_endpoint,
data_both: data_both,
opts: opts
})
do
Expand All @@ -147,25 +149,36 @@ defmodule Helix.Event.Loggable.Flow do
end
|> customize_last_ip(opts)

msg_gateway = String.replace(msg_gateway, "$first_ip", first_ip)
msg_endpoint = String.replace(msg_endpoint, "$last_ip", last_ip)
data_gateway =
data_gateway
|> replace_ips(first_ip, last_ip)
|> Map.merge(data_both)

data_endpoint =
data_endpoint
|> replace_ips(first_ip, last_ip)
|> Map.merge(data_both)

log_gateway = build_entry(gateway_id, entity_id, msg_gateway)
log_endpoint = build_entry(endpoint_id, entity_id, msg_endpoint)
log_gateway = {type_gateway, LogType.new(type_gateway, data_gateway)}
log_endpoint = {type_endpoint, LogType.new(type_endpoint, data_endpoint)}

bounce_logs =
entry_gateway = build_entry(gateway_id, entity_id, log_gateway)
entry_endpoint = build_entry(endpoint_id, entity_id, log_endpoint)

bounce_entries =
if skip_bounce? do
[]
else
build_bounce_entries(
bounce,
{gateway_id, network_id, gateway_ip},
{endpoint_id, network_id, endpoint_ip},
entity_id
entity_id,
network_id
)
end

[log_gateway, log_endpoint, bounce_logs] |> List.flatten()
[entry_gateway, entry_endpoint, bounce_entries] |> List.flatten()
end

@doc """
Expand All @@ -178,10 +191,13 @@ defmodule Helix.Event.Loggable.Flow do
event: _,
server_id: server_id,
entity_id: entity_id,
msg_server: msg_server
type: type,
data: data
})
do
[build_entry(server_id, entity_id, msg_server)]
log_type = LogType.new(type, data)

[build_entry(server_id, entity_id, {type, log_type})]
end

@doc """
Expand Down Expand Up @@ -249,7 +265,7 @@ defmodule Helix.Event.Loggable.Flow do
defdelegate format_ip(ip),
to: LoggableUtils

@spec build_entry(Server.id, Entity.id, log_msg) ::
@spec build_entry(Server.id, Entity.id, Log.info) ::
log_entry
@doc """
Returns data required to insert the log
Expand All @@ -263,16 +279,23 @@ defmodule Helix.Event.Loggable.Flow do

Messages follow the format "Connection bounced from hop (n-1) to (n+1)"
"""
def build_bounce_entries(nil, _, _, _),
def build_bounce_entries(nil, _, _, _, _),
do: []
def build_bounce_entries(bounce_id = %Bounce.ID{}, gateway, endpoint, entity) do
def build_bounce_entries(
bounce_id = %Bounce.ID{}, gateway, endpoint, entity, network
) do
bounce_id
|> BounceQuery.fetch()
|> build_bounce_entries(gateway, endpoint, entity)
|> build_bounce_entries(gateway, endpoint, entity, network)
end

def build_bounce_entries(
bounce = %Bounce{}, gateway = {_, _, _}, endpoint = {_, _, _}, entity_id)
do
bounce = %Bounce{},
gateway = {_, _, _},
endpoint = {_, _, _},
entity_id,
network_id
) do
full_path = [gateway | bounce.links] ++ [endpoint]
length_hop = length(full_path)

Expand All @@ -298,8 +321,10 @@ defmodule Helix.Event.Loggable.Flow do
{_, _, ip_prev} = bounce_map[idx - 1]
{_, _, ip_next} = bounce_map[idx + 1]

msg = "Connection bounced from #{ip_prev} to #{ip_next}"
entry = build_entry(server_id, entity_id, msg)
data = %{ip_prev: ip_prev, ip_next: ip_next, network_id: network_id}
log_info = {:connection_bounced, LogType.new(:connection_bounced, data)}

entry = build_entry(server_id, entity_id, log_info)

{idx + 1, acc ++ [entry]}
end
Expand All @@ -321,8 +346,8 @@ defmodule Helix.Event.Loggable.Flow do
do: save([log_entry])
def save(logs) do
logs
|> Enum.map(fn {server_id, entity_id, msg} ->
{:ok, _, events} = LogAction.create(server_id, entity_id, msg)
|> Enum.map(fn {server_id, entity_id, log_type} ->
{:ok, _, events} = LogAction.create(server_id, entity_id, log_type)
events
end)
|> List.flatten()
Expand Down Expand Up @@ -378,4 +403,24 @@ defmodule Helix.Event.Loggable.Flow do
do: censor_ip(ip)
defp customize_last_ip(ip, _),
do: ip

defp replace_ips(params, first_ip, last_ip) do
params
|> Enum.reduce([], fn {k, v}, acc ->
new_v =
case v do
"$first_ip" ->
first_ip

"$last_ip" ->
last_ip

_ ->
v
end

[{k, new_v} | acc]
end)
|> Enum.into(%{})
end
end
2 changes: 1 addition & 1 deletion lib/event/loggable/utils.ex
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ defmodule Helix.Event.Loggable.Utils do
end
end)
|> elem(0)
|> Enum.reverse
|> Enum.reverse()
|> List.to_string()
end

Expand Down
1 change: 1 addition & 0 deletions lib/event/notificable/notificable.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ defprotocol Helix.Event.Notificable do
| %{account_id: Account.id, server_id: Server.id}
| %{account_id: Entity.id, server_id: Server.id}
| Server.id
| :no_one

@spec get_notification_info(Event.t) ::
{Notification.class, Notification.code}
Expand Down
2 changes: 0 additions & 2 deletions lib/factor/client.ex
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ defmodule Helix.Factor.Client do
defmacro get_factors(params, do: block) do
quote do

@spec get_factors(map) ::
map
def get_factors(unquote(params)) do
var!(relay) = %{}
var!(factors) = %{}
Expand Down
32 changes: 30 additions & 2 deletions lib/factor/factor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,13 @@ defmodule Helix.Factor do
|> String.downcase()
|> String.to_atom()

# Generates typespecs for each fact
for key <- keys do
quote do
@spec unquote(key)(params, relay) :: {unquote(key), relay}
end
end

quote do

@doc """
Expand Down Expand Up @@ -381,8 +388,6 @@ defmodule Helix.Factor do
fname = :"fact_#{name}"
quote do

@spec unquote(fname)(term, term) ::
{unquote(fname), term}
def unquote(fname)(unquote(params), var!(relay) = unquote(relay)) do
unquote(block)
end
Expand Down Expand Up @@ -521,6 +526,29 @@ defmodule Helix.Factor do
end
end

@doc """
Used when `fact/3` was called without the required relay(s). It will append
the `append_relay` to the current relay, and execute the fact again.
"""
defmacro set_relay(params, current_relay, append_relay) do
{fname, _} = __CALLER__.function

quote do

# `set_relay` called again within the same fact; probably an infinite loop
if unquote(current_relay)[:__loop_checker] == unquote(fname),
do: raise "infinite loop detected"

new_relay =
unquote(current_relay)
|> Map.merge(unquote(append_relay))
|> Map.merge(%{__loop_checker: unquote(fname)})

apply(__MODULE__, unquote(fname), [unquote(params), new_relay])

end
end

@spec get_child_module(parent :: atom, child_name :: atom) ::
child_module :: atom
docp """
Expand Down
4 changes: 4 additions & 0 deletions lib/hell/hack.ex
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,12 @@ defmodule HELL.Hack.Experience do
"Elixir.Helix.Process.Model.Processable" => [
{:kill, 3},
{:complete, 2},
{:retarget, 2},
{:source_connection_closed, 3},
{:target_connection_closed, 3},
{:target_log_revised, 3},
{:target_log_recovered, 3},
{:target_log_destroyed, 3},
{:after_read_hook, 1}
],
"Elixir.Helix.Process.Public.View.ProcessViewable" => [
Expand Down
Loading