Skip to content

Commit bbd1d20

Browse files
slashmilitonini
authored andcommittedJul 24, 2017
run server using unix domain socket (#16)
try to reduce the security risk as it is pointed out #14
1 parent 903d4f4 commit bbd1d20

File tree

4 files changed

+22
-13
lines changed

4 files changed

+22
-13
lines changed
 

‎.travis.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ language: elixir
22
elixir:
33
- 1.4.2
44
otp_release:
5-
- 18.0
5+
- 19.0
66
sudo: false
77
install: mix local.hex --force
88
script:

‎README.md

+5-3
Original file line numberDiff line numberDiff line change
@@ -37,22 +37,24 @@ Example for a completion request:
3737
COMP { "def", [ context: Elixir, imports: [Enum], aliases: [{MyList, List}] ] }
3838
```
3939

40-
## Read/Write through network socket
40+
## Read/Write through unix domain socket
4141
```
4242
$ cd elixir_project
4343
$ elixir path/to/alchemist-server/run.exs --env=dev --listen
44-
ok|localhost:55580
44+
ok|localhost:/tmp/alchemist-server-1500797742260403000.sock
4545
```
4646
In this mode, when a client connects to the port, it
4747
responds by sending information back to the opened connection
4848

4949
Example for a completion request:
5050

5151
```
52-
$ nc localhost 55580
52+
$ nc -U /tmp/alchemist-server-1500797742260403000.sock
5353
COMP { "def", [ context: Elixir, imports: [Enum], aliases: [{MyList, List}] ] }
5454
```
5555

56+
Unix domain socket is introduced since OTP 19.
57+
5658
# API
5759

5860
## Completion

‎lib/server/socket.exs

+11-3
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,22 @@ defmodule Alchemist.Server.Socket do
99

1010
env = Keyword.get(opts, :env)
1111
port = Keyword.get(opts, :port, 0)
12+
socket_file = opts
13+
|> Keyword.get( :socket_file, socket_file())
14+
|> String.to_charlist
1215

1316
children = [
1417
supervisor(Task.Supervisor, [[name: Alchemist.Server.Socket.TaskSupervisor]]),
15-
worker(Task, [__MODULE__, :accept, [env, port]])
18+
worker(Task, [__MODULE__, :accept, [env, port, socket_file]])
1619
]
1720

1821
opts = [strategy: :one_for_one, name: Alchemist.Server.Socket.Supervisor]
1922
Supervisor.start_link(children, opts)
2023
end
2124

22-
def accept(env, port) do
25+
def accept(env, port, socket_file) do
2326
{:ok, socket} = :gen_tcp.listen(port,
24-
[:binary, packet: :line, active: false, reuseaddr: true])
27+
[:binary, packet: :line, active: false, reuseaddr: true, ifaddr: {:local, socket_file}])
Has a conversation. Original line has a conversation.
2528
{:ok, port} = :inet.port(socket)
2629
IO.puts "ok|localhost:#{port}"
2730
loop_acceptor(socket, env)
@@ -58,4 +61,9 @@ defmodule Alchemist.Server.Socket do
5861
defp write_line(line, socket) do
5962
:gen_tcp.send(socket, line)
6063
end
64+
65+
defp socket_file do
66+
sock_id = :erlang.system_time()
Has conversations. Original line has conversations.
67+
"/tmp/alchemist-server-#{sock_id}.sock"
68+
end
6169
end

‎test/server/socket_test.exs

+5-6
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,12 @@ defmodule Alchemist.Server.SocketTest do
77
alias Alchemist.Server.Socket, as: ServerSocket
88

99
setup do
10-
ServerSocket.start([env: "dev", port: 55293])
11-
:ok
12-
end
13-
14-
setup do
10+
sock_id = :erlang.system_time()
11+
{:ok, server_pid} = ServerSocket.start([env: "dev", socket_file: "/tmp/alchemist-server-#{sock_id}.sock"])
12+
Process.sleep(100)
13+
socket_file = String.to_charlist("/tmp/alchemist-server-#{sock_id}.sock")
1514
opts = [:binary, packet: :line, active: false]
16-
{:ok, socket} = :gen_tcp.connect('localhost', 55293, opts)
15+
{:ok, socket} = :gen_tcp.connect({:local, socket_file}, 0, opts)
1716
{:ok, socket: socket}
1817
end
1918

0 commit comments

Comments
 (0)
Please sign in to comment.