View Source DiodeClient (Diode Client v1.3.0)
DiodeClient secure end-to-end encrypted connections between any two machines. Connections are established either through direct peer-to-peer TCP connections or bridged via the Diode network. To learn more about the decentralized Diode network visit https://diode.io/
Example Usage of DiodeClient with a simple server:
DiodeClient.interface_add("example_server_interface")
address = DiodeClient.Base16.encode(DiodeClient.address())
{:ok, port} = DiodeClient.port_listen(5000)
spawn_link(fn ->
IO.puts("server #{address} started")
{:ok, ssl} = DiodeClient.port_accept(port)
peer = DiodeClient.Port.peer(ssl)
IO.puts("got a connection from #{Base.encode16(peer)}")
:ssl.controlling_process(ssl, self())
:ssl.setopts(ssl, [packet: :line, active: true])
for x <- 1..10 do
IO.puts("sending message #{x}")
:ssl.send(ssl, "Hello #{Base.encode16(peer)} this is message #{x}\n")
end
receive do
{:ssl_closed, _ssl} -> IO.puts("closed!")
end
end)
And the client. Here insert in the server address the address that has been printed above.
For example server_address = "0x389eba94b330140579cdce1feb1a6e905ff876e6"
# Client:
server_address = "0x389eba94b330140579cdce1feb1a6e905ff876e6"
DiodeClient.interface_add("example_client_interface")
spawn_link(fn ->
{:ok, ssl} = DiodeClient.port_connect(server_address, 5000)
:ssl.controlling_process(ssl, self())
:ssl.setopts(ssl, [packet: :line, active: true])
Enum.reduce_while(1..10, nil, fn _, _ ->
receive do
{:ssl, _ssl, msg} -> {:cont, IO.inspect(msg)}
other -> {:halt, IO.inspect(other)}
end
end)
:ssl.close(ssl)
IO.puts("closed!")
end)
Summary
Functions
Connect to a port on a remote host.
Functions
@spec interface_stop(atom()) :: :ok | {:error, :not_found}
Connect to a port on a remote host.
Parameters
destination
is the 20 byte diode address of the remote host.
port
is the diode port number to connect to on the remote host.
options
is a keyword list of options to pass to the connection.
valid options are:
:access
- Defaults to "rw".:local
- Whether to attempt a local connection. Can betrue
,false
, or:always
. Defaults totrue
.:always
can be used to enforce only local connections to a target and avoid relaying.
Returns {:ok, pid}
if the connection is successful, or {:error, reason}
if the connection fails.