From eab3f9483b3659b1ad3572393d24652cac71c8b6 Mon Sep 17 00:00:00 2001 From: Alex Auvolat Date: Fri, 12 Oct 2018 22:11:45 +0200 Subject: fixes to connection logic --- shard/lib/application.ex | 7 ++++--- shard/lib/net/addr.ex | 32 ++++++++++++++++++++++-------- shard/lib/net/manager.ex | 4 ++-- shardweb/assets/package-lock.json | 16 +++++++++++---- shardweb/lib/templates/layout/app.html.eex | 8 -------- 5 files changed, 42 insertions(+), 25 deletions(-) diff --git a/shard/lib/application.ex b/shard/lib/application.ex index 0daf48e..74b0dfd 100644 --- a/shard/lib/application.ex +++ b/shard/lib/application.ex @@ -14,14 +14,15 @@ defmodule Shard.Application do # Define workers and child supervisors to be supervised children = [ { DynamicSupervisor, strategy: :one_for_one, name: Shard.DynamicSupervisor }, - - # Applications & data store - Shard.Manager, # Networking + SNet.Addr, SNet.Manager, SNet.TCPServer, + # Applications & data store + Shard.Manager, + # Keys & identities Shard.Keys, { Task, fn -> Shard.Keys.get_any_identity end }, diff --git a/shard/lib/net/addr.ex b/shard/lib/net/addr.ex index 630f95a..c1d2f05 100644 --- a/shard/lib/net/addr.ex +++ b/shard/lib/net/addr.ex @@ -1,4 +1,25 @@ defmodule SNet.Addr do + use Agent + + require Logger + + def start_link(_) do + Agent.start_link(__MODULE__, :init, [], name: __MODULE__) + end + + def init() do + Application.ensure_all_started(:inets) + Application.ensure_all_started(:ssl) + case :httpc.request('http://api.ipify.org') do + {:ok, {_, _, body}} -> + {:ok, addr} = :inet.parse_address body + Logger.info "Public IP address: #{body}" + [addr] + _ -> + Logger.info "Could not get public IP address" + [] + end + end def get_if_inet4 do {:ok, ifs} = :inet.getifaddrs @@ -12,20 +33,15 @@ defmodule SNet.Addr do end def get_pub_inet4 do - Application.ensure_all_started(:inets) - Application.ensure_all_started(:ssl) - {:ok, {_, _, body}} = :httpc.request('http://api.ipify.org') - {:ok, addr} = :inet.parse_address body - addr + Agent.get(__MODULE__, &(&1)) end def get_all_inet4 do - addrset = for x <- get_if_inet4(), into: %MapSet{}, do: x - addrset = MapSet.put(addrset, get_pub_inet4()) + addrset = for x <- get_if_inet4() ++ get_pub_inet4(), into: %MapSet{}, do: x MapSet.to_list addrset end def is_local?({:inet, ip, port}) do - port == Application.get_env(:shard, :port) and (ip == {127,0,0,1} or ip in get_if_inet4()) + port == Application.get_env(:shard, :port) and (ip == {127,0,0,1} or ip in get_all_inet4()) end end diff --git a/shard/lib/net/manager.ex b/shard/lib/net/manager.ex index 624d7e6..dd80347 100644 --- a/shard/lib/net/manager.ex +++ b/shard/lib/net/manager.ex @@ -72,7 +72,7 @@ defmodule SNet.Manager do [] -> my_port = Application.get_env(:shard, :port) {:ok, pid} = SNet.TCPConn.start_link(%{connect_to: peer_info, my_port: my_port, auth: auth}) - :ets.insert(:connections, {peer_info, pid, nil}) + :ets.insert(:connections, {peer_info, pid, auth}) pid end end @@ -93,7 +93,7 @@ defmodule SNet.Manager do Return the list of all connected peers """ def list_connections() do - for [x] <- :ets.match(:connections, :"$1"), do: x + :ets.tab2list(:connections) end @doc""" diff --git a/shardweb/assets/package-lock.json b/shardweb/assets/package-lock.json index 3e06420..78fc2ff 100644 --- a/shardweb/assets/package-lock.json +++ b/shardweb/assets/package-lock.json @@ -32,7 +32,8 @@ "ansi-color": { "version": "0.2.1", "resolved": "https://registry.npmjs.org/ansi-color/-/ansi-color-0.2.1.tgz", - "integrity": "sha1-PnXAN0dSF1RO12Oo21cJ+prlv5o=" + "integrity": "sha1-PnXAN0dSF1RO12Oo21cJ+prlv5o=", + "dev": true }, "ansi-regex": { "version": "2.1.1", @@ -1982,6 +1983,7 @@ "version": "1.1.0", "resolved": "https://registry.npmjs.org/copycat-brunch/-/copycat-brunch-1.1.0.tgz", "integrity": "sha1-EOF7hZCeDhKZgd+5AwuYZkg9Zr0=", + "dev": true, "requires": { "loggy": "~0.2.2", "mkdirp": "^0.5.1", @@ -1992,6 +1994,7 @@ "version": "0.2.2", "resolved": "http://registry.npmjs.org/loggy/-/loggy-0.2.2.tgz", "integrity": "sha1-ftyFcGqC12HOnO+Gjxr7rYQWVCc=", + "dev": true, "requires": { "ansi-color": "~0.2.1", "date-utils": "~1.2.17", @@ -2070,7 +2073,8 @@ "date-utils": { "version": "1.2.21", "resolved": "https://registry.npmjs.org/date-utils/-/date-utils-1.2.21.tgz", - "integrity": "sha1-YfsWzcEnSzyayq/+n8ad+HIKK2Q=" + "integrity": "sha1-YfsWzcEnSzyayq/+n8ad+HIKK2Q=", + "dev": true }, "debug": { "version": "2.6.9", @@ -3489,7 +3493,8 @@ "growl": { "version": "1.8.1", "resolved": "https://registry.npmjs.org/growl/-/growl-1.8.1.tgz", - "integrity": "sha1-Sy3sjZB+k9szZiTc7AGDUC+MlCg=" + "integrity": "sha1-Sy3sjZB+k9szZiTc7AGDUC+MlCg=", + "dev": true }, "has": { "version": "1.0.3", @@ -4121,7 +4126,8 @@ "minimist": { "version": "0.0.8", "resolved": "https://registry.npmjs.org/minimist/-/minimist-0.0.8.tgz", - "integrity": "sha1-hX/Kv8M5fSYluCKCYuhqp6ARsF0=" + "integrity": "sha1-hX/Kv8M5fSYluCKCYuhqp6ARsF0=", + "dev": true }, "mixin-deep": { "version": "1.3.1", @@ -4148,6 +4154,7 @@ "version": "0.5.1", "resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-0.5.1.tgz", "integrity": "sha1-MAV0OOrGz3+MR2fzhkjWaX11yQM=", + "dev": true, "requires": { "minimist": "0.0.8" } @@ -4626,6 +4633,7 @@ "version": "0.1.0", "resolved": "https://registry.npmjs.org/quickly-copy-file/-/quickly-copy-file-0.1.0.tgz", "integrity": "sha1-0UBj/2WfaGl+fL/ugXoHu2NSN8I=", + "dev": true, "requires": { "mkdirp": "~0.5.0" } diff --git a/shardweb/lib/templates/layout/app.html.eex b/shardweb/lib/templates/layout/app.html.eex index 6cedcaf..9c5a358 100644 --- a/shardweb/lib/templates/layout/app.html.eex +++ b/shardweb/lib/templates/layout/app.html.eex @@ -17,9 +17,6 @@ " rel="stylesheet"> - - " rel="stylesheet"> - " rel="stylesheet" type="text/css"> @@ -204,11 +201,6 @@ - - - - - <%= render_gon_script(@conn) %> -- cgit v1.2.3