Each ip should have timestamp for when it was last seen?

I am working on elixir to create dynamic firewall for iPs, so need a concrete solution for that. I did not get any solution to timestamp with ip address.
Expand hardcoded list of ip to list of IPs provide by agent, each ip should have timestamp for when it was last seen???
I am new to elixir and the community?? If anyone could help me that would be great.

Thanks! :smiley:

Hi, maybe looking at this project could give you some ideas/inspiration …

hammer rate-limiter

how to merge timestamp with ip_address??

It is easy to store a map with ip as keys and timestamp as values…

You could use ets table as well if You need high read access.

Maybe You don’t know how to get the timestamp? Or where to store the information?

1 Like

yes!! i am newbie on Elixir. So was seeking for the help to continue with the project. For the timestamp i have used DateTime.utc_now(). But don’t have the proper idea on how to store it along with the ip address.

Maybe in an Agent…

https://hexdocs.pm/elixir/1.12/Agent.html

Or a normal GenServer process. Using OTP is an advanced topic.

Is there a way i can add ip address with timestamp to the list containing hardcoded list of ip addresses??
Any help could be a great relief to me?? I am roaming around google and have not found the concrete solution.

Your question is not well defined. What are you after, exactly?

I want to add ip address on the list of ip addres.

Before trying to add on the list, You need to know where to store it…

You should learn how to write a gen_server, and your second question will be solved as well.

A ticking gen_server, with api to add ips to the list.

You won’t understand until You write your first gen_server.

right now i am storing it in file and directly accessing from application.ex. however i am right now stucked on trying to filter unique ip based on date for last 7 days for each hour and removing it. I have the following ip with date on list:
192.168.44.152 2021-12-16 12:44:11.822908Z
192.168.44.163 2021-12-16 12:44:11.822924Z
192.168.44.164 2021-12-16 12:44:11.822931Z
192.168.44.165 2021-12-16 12:44:11.822937Z
192.168.44.166 2021-12-16 12:44:11.822943Z
192.168.44.167 2021-12-16 12:44:11.822962Z
192.168.44.168 2021-12-16 12:44:11.822984Z
192.168.44.169 2021-12-16 12:44:11.822991Z
192.168.44.151 2021-12-16 12:44:11.823005Z
192.168.44.153 2021-12-16 12:44:11.823012Z
This is in the file right now. I need to filter this. How can i trace the unique ip not seen in last 7 days every hour and remove. it if available??

You should filter the list with that kind of pseudo code,
filter where start < DateTime.utc_now + 7 days, then use Enum.uniq_by…

first, this list should be transformed into something usable…

list 
|> String.split("\n", trim: true) 
|> Enum.map(fn x -> 
    String.split(x, " ") 
end) 
|> Enum.map(fn [a, b, c] -> 
    {:ok, dt, _} = DateTime.from_iso8601(b<>"T"<>c)
    {a, dt} 
end) 
[
  {"192.168.44.152", ~U[2021-12-16 12:44:11.822908Z]},
  {"192.168.44.163", ~U[2021-12-16 12:44:11.822924Z]},
  {"192.168.44.164", ~U[2021-12-16 12:44:11.822931Z]},
  {"192.168.44.165", ~U[2021-12-16 12:44:11.822937Z]},
  {"192.168.44.166", ~U[2021-12-16 12:44:11.822943Z]},
  {"192.168.44.167", ~U[2021-12-16 12:44:11.822962Z]},
  {"192.168.44.168", ~U[2021-12-16 12:44:11.822984Z]},
  {"192.168.44.169", ~U[2021-12-16 12:44:11.822991Z]},
  {"192.168.44.151", ~U[2021-12-16 12:44:11.823005Z]},
  {"192.168.44.153", ~U[2021-12-16 12:44:11.823012Z]}
]

A list with ip first, then datetime…

Then, apply the pseudo code to get unique ip…

input
|> Enum.filter(...)
|> Enum.uniq_by(...)
|> Enum.map(&elem(&1, 0))

Then, create a ticking gen_server, which executes this code each hour.

2 Likes

Thank you for the help.

@DineshShrestha for this part you might want to take a look at this answer

3 Likes

** (MatchError) no match of right hand side value: {:error, :missing_offset}
(stdlib 3.15.2) erl_eval.erl:450: :erl_eval.expr/5
(stdlib 3.15.2) erl_eval.erl:123: :erl_eval.exprs/5
(elixir 1.12.3) lib/enum.ex:1582: Enum."-map/2-lists^map/1-0-"/2

How to resolve this error? I tried your solution, you mentioned to transform the list to usable form.