aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--shard/lib/app/chat.ex53
-rw-r--r--shard/lib/app/identity.ex22
-rw-r--r--shard/lib/cli/cli.ex28
-rw-r--r--shard/lib/data/merklesearchtree.ex7
-rw-r--r--shard/lib/keys.ex28
-rw-r--r--shardweb/assets/js/app.js3
-rw-r--r--shardweb/lib/shard_web/channels/room_channel.ex37
-rw-r--r--shardweb/lib/shard_web/controllers/room_controller.ex15
-rw-r--r--shardweb/lib/shard_web/templates/room/show.html.eex13
9 files changed, 130 insertions, 76 deletions
diff --git a/shard/lib/app/chat.ex b/shard/lib/app/chat.ex
index e61d648..c7df62e 100644
--- a/shard/lib/app/chat.ex
+++ b/shard/lib/app/chat.ex
@@ -201,33 +201,37 @@ defmodule SApp.Chat do
end
defp init_merge(state, new_root, source_peer) do
- # TODO: make the merge asynchronous
-
- Logger.info("Starting merge for #{inspect state.manifest}, merging root: #{new_root|>Base.encode16}")
+ if new_root == nil do
+ state
+ else
+ # TODO: make the merge asynchronous
+
+ Logger.info("Starting merge for #{inspect state.manifest}, merging root: #{new_root|>Base.encode16}")
- prev_last = for {x, true} <- MST.last(state.mst, nil, 100), into: MapSet.new, do: x
+ prev_last = for {x, true} <- MST.last(state.mst, nil, 100), into: MapSet.new, do: x
- mgmst = %{state.mst | root: new_root}
- mgmst = put_in(mgmst.store.prefer_ask, [source_peer])
- mst = MST.merge(state.mst, mgmst)
+ mgmst = %{state.mst | root: new_root}
+ mgmst = put_in(mgmst.store.prefer_ask, [source_peer])
+ mst = MST.merge(state.mst, mgmst)
- correct = for {x, true} <- MST.last(mst, nil, 100) do
- if not MapSet.member? prev_last, x do
- msg_callback(state, x)
- {pk, bin, sign} = x
- Shard.Keys.verify(pk, bin, sign)
- else
- true
+ correct = for {x, true} <- MST.last(mst, nil, 100) do
+ if not MapSet.member? prev_last, x do
+ msg_callback(state, x)
+ {pk, bin, sign} = x
+ Shard.Keys.verify(pk, bin, sign)
+ else
+ true
+ end
end
- end
- if Enum.all? correct do
- GenServer.cast(state.page_store, {:set_roots, [mst.root]})
- Shard.Manager.save_state(state.id, mst.root)
- %{state | mst: mst}
- else
- Logger.warn("Incorrect signatures somewhere while merging, dropping merged data")
- state
+ if Enum.all? correct do
+ GenServer.cast(state.page_store, {:set_roots, [mst.root]})
+ Shard.Manager.save_state(state.id, mst.root)
+ %{state | mst: mst}
+ else
+ Logger.warn("Incorrect signatures somewhere while merging, dropping merged data")
+ state
+ end
end
end
@@ -236,11 +240,10 @@ defmodule SApp.Chat do
{:noreply, %{ state | subs: new_subs }}
end
- defp msg_callback(state, {pk, msgbin, _sign}) do
- {ts, msg} = SData.term_unbin msgbin
+ defp msg_callback(state, {pk, msgbin, sign}) do
for pid <- state.subs do
if Process.alive?(pid) do
- send(pid, {:chat_recv, state.channel, {ts, pk, msg}})
+ send(pid, {:chat_recv, state.channel, {pk, msgbin, sign}})
end
end
end
diff --git a/shard/lib/app/identity.ex b/shard/lib/app/identity.ex
index 7e97897..204dfb1 100644
--- a/shard/lib/app/identity.ex
+++ b/shard/lib/app/identity.ex
@@ -43,10 +43,7 @@ defmodule SApp.Identity do
end
def default_nick(pk) do
- nick_suffix = pk
- |> binary_part(0, 4)
- |> Base.encode16
- |> String.downcase
+ nick_suffix = Shard.Keys.pk_display pk
"Anon" <> nick_suffix
end
@@ -56,6 +53,19 @@ defmodule SApp.Identity do
Shard.Manager.find_proc id
end
+ def get_nick(pk) do
+ case find_proc pk do
+ nil ->
+ if Shard.Keys.valid_identity_pk? pk do
+ Shard.Manifest.start %Manifest{pk: pk}
+ end
+ default_nick pk
+ pid ->
+ info = GenServer.call(pid, :get_info)
+ info.nick
+ end
+ end
+
def handle_call(:manifest, _from, state) do
{:replyl, state.manifest, state}
end
@@ -85,11 +95,12 @@ defmodule SApp.Identity do
def handle_cast({:interested, peer_id}, state) do
Shard.Manager.send(peer_id, {state.id, nil, {:update, SData.SignRev.signed(state.state)}})
+ {:noreply, state}
end
def handle_cast({:msg, peer_id, _shard_id, nil, msg}, state) do
state = case msg do
- {:update, signed} ->
+ {:update, signed} when signed != nil ->
case SData.SignRev.merge(state.state, signed, state.pk) do
{true, st2} ->
Shard.Manager.save_state(state.id, st2)
@@ -99,6 +110,7 @@ defmodule SApp.Identity do
{false, _} ->
state
end
+ _ -> state
end
{:noreply, state}
end
diff --git a/shard/lib/cli/cli.ex b/shard/lib/cli/cli.ex
index 3778b2d..2e35e45 100644
--- a/shard/lib/cli/cli.ex
+++ b/shard/lib/cli/cli.ex
@@ -12,13 +12,7 @@ defmodule SCLI do
GenServer.cast(chpid, {:subscribe, self()})
end
- pk = case Shard.Keys.list_identities do
- [pk1|_] -> pk1
- _ ->
- IO.puts "Generating a new identity..."
- Shard.Keys.new_identity
- end
-
+ pk = Shard.Keys.get_any_identity
run(%State{room_pid: nil, id_pid: nil, pk: pk})
end
@@ -63,8 +57,10 @@ defmodule SCLI do
defp handle_messages() do
receive do
- {:chat_recv, chan, {ts, nick, msg}} ->
- IO.puts "#{ts |> DateTime.from_unix! |> DateTime.to_iso8601} ##{chan} <#{nick}> #{msg}"
+ {:chat_recv, chan, {pk, msgbin, _sign}} ->
+ {ts, msg} = SData.term_unbin msgbin
+ nick = SApp.Identity.get_nick pk
+ IO.puts "#{ts |> DateTime.from_unix! |> DateTime.to_iso8601} ##{chan} <#{nick} #{Shard.Keys.pk_display pk}> #{msg}"
handle_messages()
{:chat_send, _, _} ->
# do nothing
@@ -95,16 +91,10 @@ defmodule SCLI do
else
GenServer.call(state.room_pid, {:read_history, nil, 25})
|> Enum.each(fn {{pk, msgbin, _sign}, true} ->
- {ts, msg} = SData.term_unbin msgbin
- nick = case SApp.Identity.find_proc pk do
- nil ->
- SApp.Identity.default_nick pk
- pid ->
- info = GenServer.call(pid, :get_info)
- info.nick
- end
- IO.puts "#{ts |> DateTime.from_unix! |> DateTime.to_iso8601} <#{nick} #{pk|>binary_part(0, 4)|>Base.encode16|>String.downcase}> #{msg}"
- end)
+ {ts, msg} = SData.term_unbin msgbin
+ nick = SApp.Identity.get_nick pk
+ IO.puts "#{ts |> DateTime.from_unix! |> DateTime.to_iso8601} <#{nick} #{Shard.Keys.pk_display pk}> #{msg}"
+ end)
end
state
end
diff --git a/shard/lib/data/merklesearchtree.ex b/shard/lib/data/merklesearchtree.ex
index 49d54a5..e646774 100644
--- a/shard/lib/data/merklesearchtree.ex
+++ b/shard/lib/data/merklesearchtree.ex
@@ -301,8 +301,11 @@ defmodule SData.MerkleSearchTree do
case root do
nil -> []
_ ->
- %Page{ level: _, low: low, list: lst } = Store.get(s.store, root)
- last_aux(s, low, lst, top_bound, num)
+ case Store.get(s.store, root) do
+ nil -> []
+ %Page{ level: _, low: low, list: lst } ->
+ last_aux(s, low, lst, top_bound, num)
+ end
end
end
diff --git a/shard/lib/keys.ex b/shard/lib/keys.ex
index 0dc3154..de054e1 100644
--- a/shard/lib/keys.ex
+++ b/shard/lib/keys.ex
@@ -48,11 +48,21 @@ defmodule Shard.Keys do
Agent.get(__MODULE__, &(&1))
end
+ def get_any_identity() do
+ Agent.get(__MODULE__, fn _ ->
+ case list_identities() do
+ [x|_] -> x
+ [] -> new_identity()
+ end
+ end)
+ end
+
@doc"""
Generate a new keypair for a user identity, and start an Identity Shard for it.
"""
def new_identity() do
{pk, sk} = gen_keypair(Application.get_env(:shard, :identity_suffix))
+ Logger.info "New identity: #{pk|>Base.encode16}"
:dets.insert @key_db, {pk, sk}
SApp.Identity.start_link(pk)
pk
@@ -86,7 +96,7 @@ defmodule Shard.Keys do
If correct, returns {:ok, original_message}
"""
def open(pk, signed) do
- if check_suffix(pk, Application.get_env(:shard, :identity_suffix)) do
+ if valid_identity_pk? pk do
Sign.open(signed, pk)
else
{:error, :invalid_pk_suffix}
@@ -115,10 +125,24 @@ defmodule Shard.Keys do
Returns :ok if the signature was correct.
"""
def verify(pk, bin, sign) do
- if check_suffix(pk, Application.get_env(:shard, :identity_suffix)) do
+ if valid_identity_pk? pk do
Sign.verify_detached(sign, bin, pk)
else
{:error, :invalid_pk_suffix}
end
end
+
+ @doc"""
+ Check if a public key is a valid identity pk. Requirement: have the correct suffix.
+ """
+ def valid_identity_pk?(pk) do
+ check_suffix(pk, Application.get_env(:shard, :identity_suffix))
+ end
+
+ def pk_display(pk) do
+ pk
+ |> binary_part(0, 4)
+ |> Base.encode16
+ |> String.downcase
+ end
end
diff --git a/shardweb/assets/js/app.js b/shardweb/assets/js/app.js
index 59a0acd..c98ec1b 100644
--- a/shardweb/assets/js/app.js
+++ b/shardweb/assets/js/app.js
@@ -28,7 +28,7 @@ if (room_name != undefined)
channel.on('shout', function (payload) { // listen to the 'shout' event
var li = document.createElement("li"); // creaet new list item DOM element
var name = payload.name || 'guest'; // get name from payload or set default
- li.innerHTML = '<b>' + name + '</b>: ' + payload.message; // set li contents
+ li.innerHTML = '<b>' + name + '</b> <small>' + payload.pk16 + '</small>: ' + payload.message; // set li contents
console.log(ul.scrollTop + ' ' + ul.scrollHeight + ' ' + ul.clientHeight);
var must_scroll = (ul.scrollTop >= ul.scrollHeight - ul.clientHeight - 10);
@@ -49,7 +49,6 @@ if (room_name != undefined)
msg.addEventListener('keypress', function (event) {
if (event.keyCode == 13 && msg.value.length > 0) { // don't sent empty msg.
channel.push('shout', { // send the message to the server on "shout" channel
- name: name.value, // get value of "name" of person sending the message
message: msg.value // get message text (value) from msg input field.
});
msg.value = ''; // reset the message input field for next message.
diff --git a/shardweb/lib/shard_web/channels/room_channel.ex b/shardweb/lib/shard_web/channels/room_channel.ex
index bb41a74..2935c45 100644
--- a/shardweb/lib/shard_web/channels/room_channel.ex
+++ b/shardweb/lib/shard_web/channels/room_channel.ex
@@ -6,17 +6,17 @@ defmodule ShardWeb.RoomChannel do
def join("room:" <> room_name, payload, socket) do
if authorized?(payload) do
- list = for {_chid, manifest, chpid} <- Shard.Manager.list_shards,
- %SApp.Chat.Manifest{channel: chan} = manifest,
- do: {chan, chpid}
+ list = for {_chid, %SApp.Chat.Manifest{channel: chan}, chpid} <- Shard.Manager.list_shards,
+ do: {chan, chpid}
pid = case List.keyfind(list, room_name, 0) do
nil ->
- {:ok, pid} = DynamicSupervisor.start_child(Shard.DynamicSupervisor, {SApp.Chat, room_name})
+ {:ok, pid} = Shard.Manifest.start %SApp.Chat.Manifest{channel: room_name}
pid
{_, pid} ->
pid
end
socket = assign(socket, :pid, pid)
+ socket = assign(socket, :pk, Shard.Keys.get_any_identity) # TODO same as in session!
GenServer.cast(pid, {:subscribe, self()})
send(self(), :after_join)
@@ -29,16 +29,25 @@ defmodule ShardWeb.RoomChannel do
def handle_info(:after_join, socket) do
GenServer.call(socket.assigns.pid, {:read_history, nil, 100})
- |> Enum.each(fn {{_ts, nick, msg}, true} -> push(socket, "shout", %{
- name: nick,
- message: msg,
- }) end)
+ |> Enum.each(fn {{pk, msgbin, _sign}, true} ->
+ {_ts, msg} = SData.term_unbin msgbin
+ nick = SApp.Identity.get_nick pk
+ push(socket, "shout", %{
+ name: nick,
+ pk16: Shard.Keys.pk_display(pk),
+ message: msg,
+ })
+ end)
{:noreply, socket}
end
- def handle_info({:chat_recv, _chan, {_ts, from, msg}}, socket) do
+ def handle_info({:chat_recv, _chan, {pk, msgbin, _sign}}, socket) do
+ {_ts, msg} = SData.term_unbin msgbin
+ nick = SApp.Identity.get_nick pk
Logger.info("#{inspect self()} :chat_recv #{inspect msg}")
- push socket, "shout", %{"name" => from, "message" => msg}
+ push socket, "shout", %{"name" => nick,
+ "pk16" => Shard.Keys.pk_display(pk),
+ "message" => msg}
{:noreply, socket}
end
@@ -55,9 +64,13 @@ defmodule ShardWeb.RoomChannel do
# It is also common to receive messages from the client and
# broadcast to everyone in the current topic (room:lobby).
def handle_in("shout", payload, socket) do
+ pk = socket.assigns.pk
+ nick = SApp.Identity.get_nick pk
+ payload = Map.put(payload, "name", nick)
+ payload = Map.put(payload, "pk16", Shard.Keys.pk_display pk)
+
broadcast socket, "shout", payload
- Shard.Identity.set_nickname(payload["name"])
- GenServer.cast(socket.assigns.pid, {:chat_send, payload["message"]})
+ GenServer.cast(socket.assigns.pid, {:chat_send, pk, payload["message"]})
{:noreply, socket}
end
diff --git a/shardweb/lib/shard_web/controllers/room_controller.ex b/shardweb/lib/shard_web/controllers/room_controller.ex
index 4d9adb4..48ba2a4 100644
--- a/shardweb/lib/shard_web/controllers/room_controller.ex
+++ b/shardweb/lib/shard_web/controllers/room_controller.ex
@@ -1,12 +1,25 @@
defmodule ShardWeb.RoomController do
use ShardWeb, :controller
+ require Logger
import PhoenixGon.Controller
def show(conn, %{"room" => room}) do
+ {pk, conn} = case get_session(conn, :pk) do
+ nil ->
+ pk = Shard.Keys.get_any_identity
+ conn = put_session(conn, :pk, pk)
+ {pk, conn}
+ x ->
+ {x, conn}
+ end
+
+ name = SApp.Identity.get_nick pk
+
conn = put_gon(conn, chat_room: room)
render conn, "show.html",
room: room,
- name: Shard.Identity.get_nickname
+ pk: pk,
+ name: name
end
end
diff --git a/shardweb/lib/shard_web/templates/room/show.html.eex b/shardweb/lib/shard_web/templates/room/show.html.eex
index 5f28cc5..bc1641a 100644
--- a/shardweb/lib/shard_web/templates/room/show.html.eex
+++ b/shardweb/lib/shard_web/templates/room/show.html.eex
@@ -1,11 +1,8 @@
<ul class="nav nav-tabs">
- <%= for shard <- shard_list() do %>
- <%= case shard do %>
- <%= {_, %SApp.Chat.Manifest{channel: name}, _} -> %>
- <li class="<%= if name == @room do "active" else "" end %>">
- <a href="<%= room_path(@conn, :show, name) %>">#<%= name %></a>
- </li>
- <% end %>
+ <%= for {_, %SApp.Chat.Manifest{channel: name}, _} <- shard_list() do %>
+ <li class="<%= if name == @room do "active" else "" end %>">
+ <a href="<%= room_path(@conn, :show, name) %>">#<%= name %></a>
+ </li>
<% end %>
<li>
<a href="#" onclick="if(new_room=prompt('Enter name of room to join, without preceding # sign:'))window.location.href='/room/'+new_room;">Join room</a>
@@ -17,7 +14,7 @@
<div class="row">
<div class="col-xs-3">
- <input type="text" value="<%= @name %>" id="name" class="form-control" placeholder="Your Name">
+ <strong><%= @name %></strong>
</div>
<div class="col-xs-9">
<input type="text" id="msg" class="form-control" placeholder="Your Message" autofocus>