# Disabling external network access

**URL:** <https://forum.elixirforum.com/t/disabling-external-network-access/63202>\
**Category:** Questions\
**Tags:** http, networking\
**Created:** [April 29, 2024, 8:35am UTC](https://forum.elixirforum.com/t/disabling-external-network-access/63202 "2024-04-29T08:35:49Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![teamon](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/teamon/32/23732_2.png) [@teamon](https://forum.elixirforum.com/u/teamon)\
**Post date:** [April 29, 2024, 8:35am UTC](https://forum.elixirforum.com/t/disabling-external-network-access/63202/1 "2024-04-29T08:35:49Z")

</div>

I have a fairly large codebase with many files and many tests.  
For whatever reasons that happened over the years, some tests execute code that does call an external APIs. It’s not easy to spot these tests (they pop up only when something breaks), these calls often come from some deep code using different http libraries.

I wonder if there is a way to somehow setup beam to filter TCP traffic - disable all network access except for e.g. localhost:5432 (postgres) during tests. It could be useful especially on the CI (Locally one can disconnect internet and then try to find these bad tests)

tl;dr - Is there any way to block (or log) all external TCP traffic on the beam level?

---

<div class="post-metadata">

**Author:** ![garazdawi](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/garazdawi/32/5853_2.png) [@garazdawi](https://forum.elixirforum.com/u/garazdawi)\
**Post date:** [April 29, 2024, 8:50am UTC](https://forum.elixirforum.com/t/disabling-external-network-access/63202/2 "2024-04-29T08:50:06Z")

</div>

You can configure the inet dns resolver to never resolve anything. That way only ip addresses will be possible to use.

For example:

```elixir
echo "{lookup,[]}." > ./erl_inetrc
ERL_INETRC=./erl_inetrc iex
Erlang/OTP 27 [RELEASE CANDIDATE 2] [erts-14.2.3] [source-f7fa414e3e] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] [jit:ns]

Interactive Elixir (1.17.0-dev) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> :gen_tcp.connect(~c"localhost",80,[])
{:error, :nxdomain}

```

You can read more about how to configure inetrc here: [Inet Configuration — OTP 29.0.2 (erts 17.0.2)](https://www.erlang.org/doc/apps/erts/inet_cfg)

---

<div class="post-metadata">

**Author:** ![dimitarvp](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/dimitarvp/32/38664_2.png) [@dimitarvp](https://forum.elixirforum.com/u/dimitarvp)\
**Post date:** [April 29, 2024, 9:04am UTC](https://forum.elixirforum.com/t/disabling-external-network-access/63202/3 "2024-04-29T09:04:09Z")

</div>

You can also try [Overview — extrace v0.6.0](https://hexdocs.pm/extrace/readme.html) with some of the lower-level functions in `:inet` but I am not sure that would work at all.

---

<div class="post-metadata">

**Author:** ![teamon](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/teamon/32/23732_2.png) [@teamon](https://forum.elixirforum.com/u/teamon)\
**Post date:** [April 29, 2024, 10:12am UTC](https://forum.elixirforum.com/t/disabling-external-network-access/63202/4 "2024-04-29T10:12:05Z")

</div>

Thanks!

I forgot about the amazing `dbg` - I’ve put this into `test/test_helper.ex`

```elixir
require Logger

:dbg.start()

:dbg.tracer(
  :process,
  {fn {:trace, _pid, :call, {:gen_tcp, :connect, [host, port | _]}}, n ->
     case {host, port} do
       {~c"localhost", 5432} -> :ok
       {~c"localhost", 6379} -> :ok
       _ -> Logger.error("\n\n ****\n\nConnection to #{host}:#{port} not allowed\n\n**** \n")
     end

     n + 1
   end, 0}
)

:dbg.tp(:gen_tcp, :connect, [])
:dbg.p(:all, :c)

```

and it will log unwanted connections during tests.
