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

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
renatomassaro committed Feb 15, 2018
1 parent 569b26c commit 0ea9745
Show file tree
Hide file tree
Showing 12 changed files with 400 additions and 58 deletions.
2 changes: 2 additions & 0 deletions lib/network/model/connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ defmodule Helix.Network.Model.Connection do
@type public_ftp :: t_of_type(:public_ftp)
@type bank_login :: t_of_type(:bank_login)
@type wire_transfer :: t_of_type(:wire_transfer)
@type virus_collect :: t_of_type(:virus_collect)
@type cracker_bruteforce :: t_of_type(:cracker_bruteforce)

@type meta :: map | nil
Expand All @@ -37,6 +38,7 @@ defmodule Helix.Network.Model.Connection do
| :public_ftp
| :bank_login
| :wire_transfer
| :virus_collect
| :cracker_bruteforce

@type close_reasons :: :normal | :force
Expand Down
84 changes: 51 additions & 33 deletions lib/process/executable.ex
Original file line number Diff line number Diff line change
Expand Up @@ -274,31 +274,65 @@ defmodule Helix.Process.Executable do

# Defaults: in case these functions were not defined, we assume the
# process is not interested on this (optional) data.

@spec get_bounce_id(Server.t, Server.t, params, meta) ::
%{bounce_id: Bounce.id | nil}
@doc false
defp get_bounce_id(_, _, _, %{bounce: bounce = %Bounce{}}),
do: %{bounce_id: bounce.bounce_id}
defp get_bounce_id(_, _, _, %{bounce: bounce_id = %Bounce.ID{}}),
do: %{bounce_id: bounce_id}
defp get_bounce_id(_, _, _, _),
do: %{bounce_id: nil}

@spec get_source_connection(Server.t, Server.t, params, meta) ::
{:create, Connection.type}
| nil
@doc false
defp get_source_connection(_, _, _, _),
do: nil

@spec get_target_connection(Server.t, Server.t, params, meta) ::
{:create, Connection.type}
| :same_origin
| nil
@doc false
defp get_target_connection(_, _, _, _),
do: nil

@spec get_source_file(Server.t, Server.t, params, meta) ::
%{src_file_id: File.id | nil}
@doc false
defp get_source_file(_, _, _, _),
do: %{src_file_id: nil}

@spec get_target_file(Server.t, Server.t, params, meta) ::
%{tgt_file_id: File.id | nil}
@doc false
defp get_target_file(_, _, _, _),
do: %{tgt_file_id: nil}

@spec get_source_bank_account(Server.t, Server.t, params, meta) ::
%{
src_atm_id: Server.t | nil,
src_acc_number: BankAccount.account | nil
}
@doc false
defp get_source_bank_account(_, _, _, _),
do: %{src_atm_id: nil, src_acc_number: nil}

@spec get_target_bank_account(Server.t, Server.t, params, meta) ::
%{
tgt_atm_id: Server.t | nil,
tgt_acc_number: BankAccount.account | nil
}
@doc false
defp get_target_bank_account(_, _, _, _),
do: %{tgt_atm_id: nil, tgt_acc_number: nil}

@spec get_target_process(Server.t, Server.t, params, meta) ::
%{tgt_process_id: Process.t | nil}
@doc false
defp get_target_process(_, _, _, _),
do: %{tgt_process_id: nil}
end
Expand Down Expand Up @@ -406,10 +440,6 @@ defmodule Helix.Process.Executable do

quote do

@spec get_source_connection(term, term, term, term) ::
{:create, Connection.type}
| nil
@doc false
defp get_source_connection(unquote_splicing(args)) do
unquote(block)
end
Expand All @@ -431,11 +461,6 @@ defmodule Helix.Process.Executable do

quote do

@spec get_target_connection(term, term, term, term) ::
{:create, Connection.type}
| :same_origin
| nil
@doc false
defp get_target_connection(unquote_splicing(args)) do
unquote(block)
end
Expand All @@ -452,9 +477,6 @@ defmodule Helix.Process.Executable do

quote do

@spec get_source_file(term, term, term, term) ::
%{src_file_id: File.id | nil}
@doc false
defp get_source_file(unquote_splicing(args)) do
file_id = unquote(block)

Expand All @@ -473,9 +495,6 @@ defmodule Helix.Process.Executable do

quote do

@spec get_target_file(term, term, term, term) ::
%{tgt_file_id: File.id | nil}
@doc false
defp get_target_file(unquote_splicing(args)) do
file_id = unquote(block)

Expand All @@ -494,14 +513,10 @@ defmodule Helix.Process.Executable do

quote do

@spec get_source_bank_account(term, term, term, term) ::
%{
src_atm_id: Server.t | nil,
src_acc_number: BankAccount.account | nil
}
@doc false
defp get_source_bank_account(unquote_splicing(args)) do
{atm_id, account_number} = unquote(block)
{atm_id, account_number} =
unquote(block)
|> get_bank_data()

%{
src_atm_id: atm_id,
Expand All @@ -521,14 +536,10 @@ defmodule Helix.Process.Executable do

quote do

@spec get_target_bank_account(term, term, term, term) ::
%{
tgt_atm_id: Server.t | nil,
tgt_acc_number: BankAccount.account | nil
}
@doc false
defp get_target_bank_account(unquote_splicing(args)) do
{atm_id, account_number} = unquote(block)
{atm_id, account_number} =
unquote(block)
|> get_bank_data()

%{
tgt_atm_id: atm_id,
Expand All @@ -539,6 +550,16 @@ defmodule Helix.Process.Executable do
end
end

@spec get_bank_data(BankAccount.t) :: {Server.id, BankAccount.account}
@spec get_bank_data(tuple) :: tuple
@spec get_bank_data(nil) :: {nil, nil}
def get_bank_data(%BankAccount{atm_id: atm_id, account_number: number}),
do: {atm_id, number}
def get_bank_data(result = {_, _}),
do: result
def get_bank_data(nil),
do: {nil, nil}

@doc """
Returns the process' `tgt_process_id`, as defined on the `target_process`
section of the Process.Executable.
Expand All @@ -548,9 +569,6 @@ defmodule Helix.Process.Executable do

quote do

@spec get_target_process(term, term, term, term) ::
%{tgt_process_id: Process.t | nil}
@doc false
defp get_target_process(unquote_splicing(args)) do
process_id = unquote(block)

Expand All @@ -573,7 +591,7 @@ defmodule Helix.Process.Executable do

quote do

@spec get_resources(term, term, term, term) ::
@spec get_resources(Server.t, Server.t, params, meta) ::
unquote(process).resources
@doc false
defp get_resources(unquote_splicing(args)) do
Expand Down
44 changes: 44 additions & 0 deletions lib/software/action/flow/virus.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
defmodule Helix.Software.Action.Flow.Virus do

alias Helix.Event
alias Helix.Network.Model.Tunnel
alias Helix.Network.Query.Network, as: NetworkQuery
alias Helix.Process.Model.Process
alias Helix.Server.Model.Server
alias Helix.Software.Model.File
alias Helix.Software.Model.Virus

alias Helix.Software.Process.Virus.Collect, as: VirusCollectProcess

@internet_id NetworkQuery.internet().network_id

@type viruses :: [{File.t, Server.t}]

@spec start_collect(
Server.t, viruses, Tunnel.bounce_id, Virus.payment_info, Event.relay)
::
[Process.t]
| no_return
def start_collect(gateway, viruses, bounce_id, {bank_acc, wallet}, relay) do
Enum.reduce(viruses, [], fn {virus, target}, acc ->

params =
%{
wallet: wallet,
bank_account: bank_acc
}

meta =
%{
virus: virus,
network_id: @internet_id,
bounce: bounce_id
}

{:ok, process} =
VirusCollectProcess.execute(gateway, target, params, meta, relay)

acc ++ [process]
end)
end
end
42 changes: 42 additions & 0 deletions lib/software/event/virus/collect.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
defmodule Helix.Software.Event.Virus.Collect do

import Helix.Event

event Processed do

alias Helix.Process.Model.Process
alias Helix.Universe.Bank.Model.BankAccount
alias Helix.Universe.Bank.Query.Bank, as: BankQuery
alias Helix.Software.Model.File
alias Helix.Software.Query.File, as: FileQuery

alias Helix.Software.Process.Virus.Collect, as: VirusCollectProcess

event_struct [:file, :bank_account, :wallet]

@type t ::
%__MODULE__{
file: File.t,
bank_account: BankAccount.t | nil,
wallet: term | nil
}

def new(process = %Process{}, %VirusCollectProcess{wallet: nil}) do
bank_account =
BankQuery.fetch_account(process.src_atm_id, process.src_acc_number)

do_new(process, bank_account, nil)
end

def new(process = %Process{}, %VirusCollectProcess{wallet: wallet}),
do: do_new(process, nil, wallet)

defp do_new(process, bank_account, wallet) do
%__MODULE__{
file: FileQuery.fetch(process.src_file_id),
bank_account: bank_account,
wallet: wallet
}
end
end
end
9 changes: 3 additions & 6 deletions lib/software/henforcer/virus.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,13 @@ defmodule Helix.Software.Henforcer.Virus do

alias Helix.Entity.Henforcer.Entity, as: EntityHenforcer
alias Helix.Entity.Model.Entity
alias Helix.Universe.Bank.Model.BankAccount
alias Helix.Software.Henforcer.File, as: FileHenforcer
alias Helix.Software.Henforcer.Storage, as: StorageHenforcer
alias Helix.Software.Model.File
alias Helix.Software.Model.Storage
alias Helix.Software.Model.Virus
alias Helix.Software.Query.Virus, as: VirusQuery

@type payment_info :: {BankAccount.t | nil, term | nil}

@type virus_exists_relay :: %{virus: Virus.t}
@type virus_exists_relay_partial :: %{}
@type virus_exists_error ::
Expand Down Expand Up @@ -72,7 +69,7 @@ defmodule Helix.Software.Henforcer.Virus do
@type can_collect_all_relay_partial :: map
@type can_collect_all_error :: can_collect_error

@spec can_collect_all?(Entity.t, [File.id], payment_info) ::
@spec can_collect_all?(Entity.t, [File.id], Virus.payment_info) ::
{true, can_collect_all_relay}
| can_collect_all_error
@doc """
Expand Down Expand Up @@ -105,7 +102,7 @@ defmodule Helix.Software.Henforcer.Virus do
| is_active_error
| valid_payment_error

@spec can_collect?(Entity.t, File.id, payment_info) ::
@spec can_collect?(Entity.t, File.id, Virus.payment_info) ::
{true, can_collect_relay}
| can_collect_error
@doc """
Expand Down Expand Up @@ -133,7 +130,7 @@ defmodule Helix.Software.Henforcer.Virus do
@type valid_payment_error ::
{false, {:payment, :invalid}, valid_payment_relay_partial}

@spec valid_payment?(File.t, payment_info) ::
@spec valid_payment?(File.t, Virus.payment_info) ::
{true, valid_payment_relay}
| valid_payment_error
@doc """
Expand Down
8 changes: 8 additions & 0 deletions lib/software/model/virus.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ defmodule Helix.Software.Model.Virus do

alias Ecto.Changeset
alias Helix.Entity.Model.Entity
alias Helix.Universe.Bank.Model.BankAccount
alias Helix.Software.Model.File
alias __MODULE__, as: Virus

Expand All @@ -21,6 +22,13 @@ defmodule Helix.Software.Model.Virus do
is_active?: boolean
}

@typep wallet :: term

@type payment_info ::
{BankAccount.t, wallet}
| {nil, wallet}
| {BankAccount.t, nil}

@type changeset :: %Changeset{data: %__MODULE__{}}
@type id :: File.id

Expand Down
14 changes: 10 additions & 4 deletions lib/software/process/file/transfer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ process Helix.Software.Process.File.Transfer do
%{dlk: resource_usage}
| %{ulk: resource_usage}

@type process_type :: :file_download | :file_upload
@type transfer_type :: :download | :upload
@type connection_type :: :ftp | :public_ftp

Expand Down Expand Up @@ -197,15 +198,20 @@ process Helix.Software.Process.File.Transfer do
Defines how FileTransferProcess should be executed.
"""

alias Helix.Network.Model.Bounce
alias Helix.Network.Model.Network
alias Helix.Software.Model.File
alias Helix.Software.Process.File.Transfer, as: FileTransferProcess

@type params :: FileTransferProcess.creation_params

@type meta :: %{
:file => File.t,
optional(atom) => term
}
@type meta ::
%{
file: File.t,
type: FileTransferProcess.process_type,
network_id: Network.id,
bounce: Bounce.idt | nil
}

resources(_, _, params, meta) do
%{
Expand Down
Loading

0 comments on commit 0ea9745

Please sign in to comment.