Skip to content

Update example and bump ex_stun to 0.1.0 #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .formatter.exs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Used by "mix format"
[
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
inputs: ["{mix,.formatter}.exs", "{config,lib,test, example}/**/*.{ex,exs}"]
]
45 changes: 45 additions & 0 deletions example/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Simple ICE Example

Run as:

```bash
elixir peer.exs <signalling-ip> <signalling-port>
```

`signalling-ip` defaults to 127.0.0.1
`signalling-port` defaults to 4000

You can use our simple [signalling server](../signalling_server)

## Protocol

peer.exs sends following messages:

* `credentials`

```json
{
"type": "credentials",
"ufrag": "someufrag",
"passwd": "somepasswd"
}
```

* `candidate`

```json
{
"type": "candidate",
"cand": "somecandidate"
}
```

* `end_of_candidates`

```json
{
"type": "end_of_candidates"
}
```


64 changes: 45 additions & 19 deletions example/peer.exs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
Mix.install([{:gun, "~> 2.0.1"}, {:ex_ice, path: "../"}, {:jason, "~> 1.4.0"}])
Mix.install([{:gun, "~> 2.0.1"}, {:ex_ice, path: "../", force: true}, {:jason, "~> 1.4.0"}])

require Logger
Logger.configure(level: :info)

defmodule Peer do
use GenServer
Expand Down Expand Up @@ -31,7 +34,8 @@ defmodule Peer do
receive do
{:gun_upgrade, ^conn, stream, _, _} ->
Logger.info("Connected to the signalling server")
{:ok, %{conn: conn, stream: stream, ice: nil}}
Process.send_after(self(), :ws_ping, 1000)
{:ok, %{conn: conn, stream: stream, ice: nil, timer: nil}}

other ->
Logger.error("Couldn't connect to the signalling server: #{inspect(other)}")
Expand All @@ -48,21 +52,41 @@ defmodule Peer do
{:stop, :normal, state}
end

@impl true
def handle_info(:ws_ping, state) do
Process.send_after(self(), :ws_ping, 1000)
:gun.ws_send(state.conn, state.stream, :ping)
{:noreply, state}
end

@impl true
def handle_info({:gun_ws, _, _, {:text, msg}}, state) do
state = handle_ws_msg(Jason.decode!(msg), state)
{:noreply, state}
end

@impl true
def handle_info({:gun_ws, _, _, {:close, code}}, _state) do
Logger.info("Signalling connection closed with code: #{code}. Exiting")
exit(:ws_down)
end

@impl true
def handle_info({:ex_ice, _pid, msg}, state) do
state = handle_ice_msg(msg, state)
{:noreply, state}
end

@impl true
def handle_info(:send_ping, state) do
ref = Process.send_after(self(), :send_ping, 1000)
:ok = ICEAgent.send_data(state.ice, "ping")
{:noreply, %{state | timer: ref}}
end

@impl true
def handle_info(msg, state) do
Logger.warn("Received unknown msg: #{inspect(msg)}")
Logger.warning("Received unknown msg: #{inspect(msg)}")
{:noreply, state}
end

Expand All @@ -79,14 +103,17 @@ defmodule Peer do
stun_servers: ["stun:stun.l.google.com:19302"]
)

:ok = ICEAgent.run(pid)
{:ok, ufrag, passwd} = ICEAgent.get_local_credentials(pid)

msg = %{type: "credentials", ufrag: ufrag, passwd: passwd} |> Jason.encode!()
:gun.ws_send(state.conn, state.stream, {:text, msg})

:ok = ICEAgent.gather_candidates(pid)
%{state | ice: pid}
end

defp handle_ws_msg(%{"type" => "credentials", "ufrag" => ufrag, "passwd" => passwd}, state) do
:ok =
ICEAgent.set_remote_credentials(state.ice, Base.decode64!(ufrag), Base.decode64!(passwd))

:ok = ICEAgent.set_remote_credentials(state.ice, ufrag, passwd)
state
end

Expand All @@ -100,34 +127,33 @@ defmodule Peer do
state
end

def handle_ice_msg({:local_credentials, ufrag, passwd}, state) do
msg =
%{type: "credentials", ufrag: Base.encode64(ufrag), passwd: Base.encode64(passwd)}
|> Jason.encode!()

:gun.ws_send(state.conn, state.stream, {:text, msg})
state
end

def handle_ice_msg({:new_candidate, cand}, state) do

msg = %{type: "candidate", cand: cand} |> Jason.encode!()
:gun.ws_send(state.conn, state.stream, {:text, msg})
state
end

def handle_ice_msg(:gathering_complete, state) do
def handle_ice_msg({:gathering_state_change, :complete} = msg, state) do
Logger.info("ICE: #{inspect(msg)}")
msg = %{type: "end_of_candidates"} |> Jason.encode!()
:gun.ws_send(state.conn, state.stream, {:text, msg})
state
end

def handle_ice_msg(:completed, state) do
Logger.info("ICE: :completed")
Logger.info("Starting sending...")
ref = Process.send_after(self(), :send_ping, 1000)
%{state | timer: ref}
end

def handle_ice_msg(other, state) do
Logger.info("ICE: #{inspect(other)}")
state
end
end

require Logger

{:ok, pid} = Peer.start_link()
ref = Process.monitor(pid)
Expand All @@ -137,5 +163,5 @@ receive do
Logger.info("Peer process closed. Exiting")

other ->
Logger.warn("Unexpected msg. Exiting. Msg: #{inspect(other)}")
Logger.warning("Unexpected msg. Exiting. Msg: #{inspect(other)}")
end
8 changes: 4 additions & 4 deletions lib/ice_agent.ex
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,7 @@ defmodule ExICE.ICEAgent do
def handle_info(:ta_timeout, %{remote_ufrag: nil, remote_pwd: nil} = state) do
# TODO we can do this better i.e.
# allow for executing gathering transactions
Logger.debug("Ta timer fired but there are no remote credentials. Scheduling next check")
ta_timer = Process.send_after(self(), :ta_timeout, @ta_timeout)
state = %{state | ta_timer: ta_timer}
state = update_ta_timer(state)
Expand Down Expand Up @@ -781,7 +782,7 @@ defmodule ExICE.ICEAgent do
state = put_in(state, [:checklist, conn_check_pair.id], conn_check_pair)
@conn_check_handler[state.role].update_nominated_flag(state, pair_id, nominate?)
else
{:error, reason} when reason in [:invalid_message_integrity, :invalid_fingerprint] ->
{:error, reason} when reason == :invalid_auth_attributes ->
Logger.debug("Ignoring conn check response, reason: #{reason}")

conn_check_pair = %CandidatePair{conn_check_pair | state: :failed}
Expand Down Expand Up @@ -1276,11 +1277,10 @@ defmodule ExICE.ICEAgent do

defp authenticate_msg(msg, local_pwd) do
with {:ok, key} <- Message.authenticate_st(msg, local_pwd),
true <- Message.check_fingerprint(msg) do
:ok <- Message.check_fingerprint(msg) do
{:ok, key}
else
:error -> {:error, :invalid_message_integrity}
false -> {:error, :invalid_fingerprint}
{:error, _reason} -> {:error, :invalid_auth_attributes}
end
end

Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ defmodule ExICE.MixProject do

defp deps do
[
{:ex_stun, github: "elixir-webrtc/ex_stun"},
{:ex_stun, "~> 0.1.0"},
{:excoveralls, "~> 0.14.6", only: :test, runtime: false},
{:ex_doc, "~> 0.27", only: :dev, runtime: false},
{:credo, "~> 1.6", only: [:dev, :test], runtime: false}
Expand Down
2 changes: 1 addition & 1 deletion mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"credo": {:hex, :credo, "1.6.5", "330ca591c12244ab95498d8f47994c493064b2689febf1236d43d596b4f2261d", [:mix], [{:bunt, "~> 0.2.0", [hex: :bunt, repo: "hexpm", optional: false]}, {:file_system, "~> 0.2.8", [hex: :file_system, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "101de53e6907397c3246ccd2cc9b9f0d3fc0b7805b8e1c1c3d818471fc85bafd"},
"earmark_parser": {:hex, :earmark_parser, "1.4.26", "f4291134583f373c7d8755566122908eb9662df4c4b63caa66a0eabe06569b0a", [:mix], [], "hexpm", "48d460899f8a0c52c5470676611c01f64f3337bad0b26ddab43648428d94aabc"},
"ex_doc": {:hex, :ex_doc, "0.28.4", "001a0ea6beac2f810f1abc3dbf4b123e9593eaa5f00dd13ded024eae7c523298", [:mix], [{:earmark_parser, "~> 1.4.19", [hex: :earmark_parser, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.14", [hex: :makeup_elixir, repo: "hexpm", optional: false]}, {:makeup_erlang, "~> 0.1", [hex: :makeup_erlang, repo: "hexpm", optional: false]}], "hexpm", "bf85d003dd34911d89c8ddb8bda1a958af3471a274a4c2150a9c01c78ac3f8ed"},
"ex_stun": {:git, "https://github.com/elixir-webrtc/ex_stun.git", "2feefe6f06135666404020e86324b5a715c0eaa1", []},
"ex_stun": {:hex, :ex_stun, "0.1.0", "252474bf4c8519fbf4bc0fbfc6a1b846a634b1478c65dbbfb4b6ab4e33c2a95a", [:mix], [], "hexpm", "629fc8be45b624a92522f81d85ba001877b1f0745889a2419bdb678790d7480c"},
"excoveralls": {:hex, :excoveralls, "0.14.6", "610e921e25b180a8538229ef547957f7e04bd3d3e9a55c7c5b7d24354abbba70", [:mix], [{:hackney, "~> 1.16", [hex: :hackney, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "0eceddaa9785cfcefbf3cd37812705f9d8ad34a758e513bb975b081dce4eb11e"},
"file_system": {:hex, :file_system, "0.2.10", "fb082005a9cd1711c05b5248710f8826b02d7d1784e7c3451f9c1231d4fc162d", [:mix], [], "hexpm", "41195edbfb562a593726eda3b3e8b103a309b733ad25f3d642ba49696bf715dc"},
"hackney": {:hex, :hackney, "1.18.1", "f48bf88f521f2a229fc7bae88cf4f85adc9cd9bcf23b5dc8eb6a1788c662c4f6", [:rebar3], [{:certifi, "~>2.9.0", [hex: :certifi, repo: "hexpm", optional: false]}, {:idna, "~>6.1.0", [hex: :idna, repo: "hexpm", optional: false]}, {:metrics, "~>1.0.0", [hex: :metrics, repo: "hexpm", optional: false]}, {:mimerl, "~>1.1", [hex: :mimerl, repo: "hexpm", optional: false]}, {:parse_trans, "3.3.1", [hex: :parse_trans, repo: "hexpm", optional: false]}, {:ssl_verify_fun, "~>1.1.0", [hex: :ssl_verify_fun, repo: "hexpm", optional: false]}, {:unicode_util_compat, "~>0.7.0", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm", "a4ecdaff44297e9b5894ae499e9a070ea1888c84afdd1fd9b7b2bc384950128e"},
Expand Down
2 changes: 1 addition & 1 deletion signalling_server/lib/peer_handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ defmodule SignallingServer.PeerHandler do
end

def handle_info(msg, state) do
Logger.warn("Unknown msg: #{inspect(msg)}")
Logger.warning("Unknown msg: #{inspect(msg)}")
{:ok, state}
end

Expand Down
4 changes: 2 additions & 2 deletions signalling_server/lib/room.ex
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ defmodule SignallingServer.Room do
if state.p2 do
send(state.p2, {:forward, msg})
else
Logger.warn("Not forwarding msg as there is no p2")
Logger.warning("Not forwarding msg as there is no p2")
end

{:reply, :ok, state}
Expand All @@ -62,7 +62,7 @@ defmodule SignallingServer.Room do
if state.p1 do
send(state.p1, {:forward, msg})
else
Logger.warn("Not forwarding msg as there is no p1")
Logger.warning("Not forwarding msg as there is no p1")
end

{:reply, :ok, state}
Expand Down
2 changes: 1 addition & 1 deletion signalling_server/lib/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ defmodule SignallingServer.Router do

get "/websocket" do
conn
|> WebSockAdapter.upgrade(SignallingServer.PeerHandler, [], timeout: 60_000)
|> WebSockAdapter.upgrade(SignallingServer.PeerHandler, [], timeout: 2_000)
|> halt()
end

Expand Down