-
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add SSL based internet connectivity checker
This adds the ability to detect sneaky firewalls or anything else that may give false positives on internet connectivity by checking that a valid SSL connection can be made to a list of hosts. Signed-off-by: Connor Rigby <[email protected]>
- Loading branch information
1 parent
f98ea12
commit e34b05c
Showing
6 changed files
with
130 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
defmodule VintageNet.Connectivity.SSLConnect do | ||
@moduledoc """ | ||
Test connectivity by making a connection using SSL | ||
Connectivity with a remote host can be checked by making a SSL connection to | ||
it. The connection either works, the connection is refused, or it times out. | ||
The first two cases indicate connectivity. | ||
""" | ||
|
||
import VintageNet.Connectivity.TCPPing, only: [get_interface_address: 2] | ||
|
||
@connect_timeout 5_000 | ||
|
||
@doc """ | ||
Check connectivity with another device | ||
The "connect" is a SSL connection attempt from the specified interface to | ||
an IP address and port. Failures to connect don't necessarily mean that the | ||
Internet is down, but it's likely especially if the server that's specified | ||
in the configuration is highly available. | ||
""" | ||
@spec connect(VintageNet.ifname(), {String.t(), port}) :: :ok | {:error, :inet.posix()} | ||
def connect(ifname, {hostname, port}) do | ||
with {:ok, src_ip} <- get_interface_address(ifname, :inet), | ||
{:ok, ssl} <- | ||
:ssl.connect( | ||
to_charlist(hostname), | ||
port, | ||
[ | ||
verify: :verify_peer, | ||
cacerts: :public_key.cacerts_get(), | ||
active: false, | ||
ip: src_ip | ||
], | ||
@connect_timeout | ||
) do | ||
_ = :ssl.close(ssl) | ||
:ok | ||
else | ||
{:error, reason} -> | ||
{:error, reason} | ||
|
||
posix_error -> | ||
{:error, posix_error} | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
defmodule VintageNet.Connectivity.SSLConnectTest do | ||
use ExUnit.Case, async: true | ||
|
||
alias VintageNet.Connectivity.SSLConnect | ||
alias VintageNetTest.Utils | ||
|
||
test "connect to known host" do | ||
ifname = Utils.get_ifname_for_tests() | ||
|
||
assert SSLConnect.connect(ifname, {"google.com", 443}) == :ok | ||
assert SSLConnect.connect(ifname, {"github.com", 443}) == :ok | ||
assert SSLConnect.connect(ifname, {"superfakedomain", 443}) == {:error, :nxdomain} | ||
end | ||
end |