-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpastebeam.erl
More file actions
357 lines (338 loc) · 16.9 KB
/
pastebeam.erl
File metadata and controls
357 lines (338 loc) · 16.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
%% # PasteBEAM Service
%%
%% Reference implementation.
%%
%% ## Logging Notes
%%
%% We are currently logging with just io:format for the sake of
%% simplicity.
%%
%% Potentially dangerous data for ANSI Terminals should be formatted
%% with ~p. Safe human readable strings should be probably formatted
%% with ~ts.
-module(pastebeam).
-export([start/0, start/1, start/3, default_public_params/0]).
-define(DEFAULT_PORT, 6969).
-define(DEFAULT_POSTS_ROOT, "./posts/").
-define(POST_ID_BYTE_SIZE, 32).
-doc "Parameters of the server visible to the clients via PARAMS\r\n command".
-record(public_params, { challenge_leading_zeros :: integer(),
challenge_timeout_ms :: timeout(),
challenge_byte_size :: integer(),
max_limit_per_ip :: integer(),
post_byte_size_limit :: integer() }).
-spec default_public_params() -> #public_params {}.
default_public_params() ->
#public_params { challenge_leading_zeros = 6,
challenge_timeout_ms = 60*1000,
challenge_byte_size = 32,
max_limit_per_ip = 5,
post_byte_size_limit = 4*1024 }.
-record(session_params, { public_params :: #public_params{},
sock :: gen_tcp:socket(),
addr :: addr(),
posts_root :: file:name_all()}).
-spec start() -> pid().
start() ->
start(?DEFAULT_PORT, ?DEFAULT_POSTS_ROOT, default_public_params()).
-spec start(Port) -> pid() when
Port :: inet:port_number().
start(Port) ->
start(Port, ?DEFAULT_POSTS_ROOT, default_public_params()).
-spec start(Port, PostsRoot, PublicParams) -> pid() when
Port :: inet:port_number(),
PostsRoot :: file:name_all(),
PublicParams :: #public_params{}.
start(Port, PostsRoot, PublicParams) ->
ok = filelib:ensure_dir(PostsRoot),
Options = [binary, {packet, line}, {active, false}, {reuseaddr, true}],
Server = spawn(fun () -> server(PostsRoot, PublicParams, #{}, #{}) end),
{ok, LSock} = gen_tcp:listen(Port, Options),
%% TODO: the accepter should be created and monitored by the server
_IgnoringAccepter = spawn(fun () -> accepter(LSock, Server) end),
Server.
%% The only reason this is a macro is because record_info/2 is
%% absolutely dumb. It's some sort of compile time expression that
%% does not accept runtime parameters.
-define(RECORD_FIELD_INDICES(RecordName),
lists:zip(
record_info(fields, RecordName),
lists:seq(2, record_info(size, RecordName)))).
-spec server(PostsRoot, PublicParams, Connections, Limits) -> no_return() when
PostsRoot :: file:name_all(),
PublicParams :: #public_params{},
Connections :: #{ Pid :: pid() => {Socket :: gen_tcp:socket(), Addr :: addr()} },
Limits :: #{ IP :: term() => integer() }.
server(PostsRoot, PublicParams, Connections, Limits) ->
#public_params{ max_limit_per_ip = MAX_LIMIT_PER_IP } = PublicParams,
receive
{public_params} ->
io:format("INFO: ~p\n", [record_info(fields, public_params)]),
server(PostsRoot, PublicParams, Connections, Limits);
{public_params, Field} ->
case proplists:get_value(Field, ?RECORD_FIELD_INDICES(public_params)) of
undefined ->
io:format("ERROR: invalid public parameter ~p, avaliable parameters are ~p\n",
[Field, record_info(fields, public_params)]);
Index ->
io:format("INFO: ~p\n", [{Field, element(Index, PublicParams)}])
end,
server(PostsRoot, PublicParams, Connections, Limits);
{public_params, Field, Value} ->
case proplists:get_value(Field, ?RECORD_FIELD_INDICES(public_params)) of
undefined ->
io:format("ERROR: invalid public parameter ~p, avaliable parameters are ~p\n",
[Field, record_info(fields, public_params)]),
server(PostsRoot, PublicParams, Connections, Limits);
Index ->
NewPublicParams = setelement(Index, PublicParams, Value),
NewPublicParamsPropList = lists:zip(
record_info(fields, public_params),
lists:nthtail(1, tuple_to_list(NewPublicParams))),
io:format("INFO: updated ~p: ~p\n", [Field, NewPublicParamsPropList]),
server(PostsRoot, NewPublicParams, Connections, Limits)
end;
{connected, Sock} ->
case inet:peername(Sock) of
{ok, Addr} ->
{IP, _Port} = Addr,
Limit = maps:get(IP, Limits, 0) + 1,
if
Limit > MAX_LIMIT_PER_IP ->
io:format("~p: ERROR: too many connections\n", [Addr]),
gen_tcp:send(Sock, <<"TOO MANY CONNECTIONS\r\n">>),
gen_tcp:close(Sock),
server(PostsRoot, PublicParams, Connections, Limits);
true ->
SessionParams = #session_params { sock = Sock,
addr = Addr,
public_params = PublicParams,
posts_root = PostsRoot },
{Pid, _Ref} = spawn_monitor(fun () -> session(command, SessionParams) end),
NewLimits = maps:put(IP, Limit, Limits),
NewConnections = maps:put(Pid, {Sock, Addr}, Connections),
server(PostsRoot, PublicParams, NewConnections, NewLimits)
end;
{error, Posix} ->
io:format("ERROR: could not get a remote address of a connection: ~p\n", [Posix]),
server(PostsRoot, PublicParams, Connections, Limits)
end;
{'DOWN', _Ref, process, Pid, Reason} ->
case maps:get(Pid, Connections, undefined) of
{Sock, Addr} ->
case Reason of
normal ->
io:format("~p: exited normally\n", [Addr]);
Reason ->
io:format("~p: ERROR: exited with reason: ~p\n", [Addr, Reason]),
gen_tcp:send(Sock, <<"500\r\n">>)
end,
gen_tcp:close(Sock),
NewConnections = maps:remove(Pid, Connections),
{IP, _Port} = Addr,
Limit = case maps:get(IP, Limits, 0) of
Value when Value > 0 -> Value - 1;
Value -> Value
end,
NewLimits = maps:put(IP, Limit, Limits),
server(PostsRoot, PublicParams, NewConnections, NewLimits);
undefined ->
io:format("WARNING: process ~p went down, but it was not associated with any sockets. Weird...\n", [Pid]),
server(PostsRoot, PublicParams, Connections, Limits)
end;
Message ->
io:format("WARNING: Unknown message ~p\n", [Message]),
server(PostsRoot, PublicParams, Connections, Limits)
end.
-type addr() :: {inet:ip_address(), inet:port_number()} |
inet:returned_non_ip_address().
-type session_state() :: command |
{challenge, Content :: binary()} |
{accepted, Content :: binary(), Challenge :: binary()} |
{post, Content :: binary()} |
{get, Id :: unicode:chardata()}.
-spec session(State, Params) -> ok when
State :: session_state(),
Params :: #session_params{}.
session(command, Params) ->
#session_params { sock = Sock, addr = Addr, public_params = PublicParams } = Params,
gen_tcp:send(Sock, <<"HI\r\n">>),
io:format("~p: connected\n", [Addr]),
case gen_tcp:recv(Sock, 0) of
{ok, <<"CRASH\r\n">>} ->
throw(crash);
{ok, <<"PARAMS\r\n">>} ->
PublicParamsValues = lists:nthtail(1, tuple_to_list(PublicParams)),
PublicParamsPropList = lists:zip(record_info(fields, public_params), PublicParamsValues),
lists:foreach(
fun ({Key, Value}) ->
gen_tcp:send(Sock, io_lib:bformat(<<"~p ~p\r\n">>, [Key, Value]))
end,
PublicParamsPropList),
ok;
{ok, <<"POST\r\n">>} ->
gen_tcp:send(Sock, <<"OK\r\n">>),
io:format("~p: wants to make a post\n", [Addr]),
session({post, <<"">>}, Params);
{ok, <<"GET ", Id/binary>>} ->
io:format("~p: wants to get a post\n", [Addr]),
session({get, string:trim(Id)}, Params);
{ok, Command} ->
io:format("~p: ERROR: invalid command: ~p\n", [Addr, Command]),
gen_tcp:send(Sock, "INVALID COMMAND\r\n"),
ok;
{error, Reason} ->
exit(Reason)
end;
session({post, Content}, Params) ->
#session_params { sock = Sock, addr = Addr, public_params = PublicParams } = Params,
#public_params { post_byte_size_limit = POST_BYTE_SIZE_LIMIT } = PublicParams,
case gen_tcp:recv(Sock, 0) of
{ok, <<"SUBMIT\r\n">>} ->
io:format("~p: submitted the post of size ~p bytes\n", [Addr, byte_size(Content)]),
session({challenge, Content}, Params);
{ok, Line} ->
%% Is line a valid UTF-8?
case unicode:characters_to_list(Line, utf8) of
{error, _, _} ->
io:format("~p: ERROR: invalid utf8\n", [Addr]),
gen_tcp:send(Sock, <<"INVALID UTF8\r\n">>),
ok;
{incomplete, _, _} ->
io:format("~p: ERROR: incomplete utf8\n", [Addr]),
gen_tcp:send(Sock, <<"INVALID UTF8\r\n">>), % For the user it's all invalid utf8, no distinction
ok;
_Line ->
%% Does the line end with \r\n?
case binary:longest_common_suffix([Line, <<"\r\n">>]) of
2 ->
%% Does the line overflow the post size limit?
PostSize = byte_size(Content) + byte_size(Line),
if
PostSize >= POST_BYTE_SIZE_LIMIT ->
io:format("~p: ERROR: post is too big\n", [Addr]),
gen_tcp:send(Sock, <<"TOO BIG\r\n">>),
ok;
true ->
%% All good, adding the line
gen_tcp:send(Sock, <<"OK\r\n">>),
session({post, <<Content/binary, Line/binary>>}, Params)
end;
_ ->
io:format("~p: ERROR: bad line ending\n", [Addr]),
gen_tcp:send(Sock, <<"BAD LINE ENDING\r\n">>),
ok
end
end;
{error, Reason} ->
exit(Reason)
end;
session({challenge, Content}, Params) ->
#session_params { addr = Addr, sock = Sock, public_params = PublicParams } = Params,
#public_params { challenge_byte_size = CHALLENGE_BYTE_SIZE,
challenge_leading_zeros = CHALLENGE_LEADING_ZEROS } = PublicParams,
Challenge = base64:encode(crypto:strong_rand_bytes(CHALLENGE_BYTE_SIZE)),
gen_tcp:send(Sock, io_lib:bformat(<<"CHALLENGE sha256 ~p ~ts\r\n">>, [CHALLENGE_LEADING_ZEROS, Challenge])),
io:format("~p: has been challenged with prefix ~ts\n", [Addr, Challenge]),
session({accepted, Content, Challenge}, Params);
session({accepted, Content, Challenge}, Params) ->
#session_params { addr = Addr,
sock = Sock,
posts_root = PostsRoot,
public_params = PublicParams } = Params,
#public_params { challenge_timeout_ms = CHALLENGE_TIMEOUT_MS,
challenge_leading_zeros = CHALLENGE_LEADING_ZEROS} = PublicParams,
case gen_tcp:recv(Sock, 0, CHALLENGE_TIMEOUT_MS) of
{ok, <<"ACCEPTED ", Prefix/binary>>} ->
io:format("~p: accepted the challenge\n", [Addr]),
Blob = <<Prefix/binary,
Content/binary,
Challenge/binary,
<<"\r\n">>/binary>>,
Hash = binary:encode_hex(crypto:hash(sha256, Blob)),
LeadingZeros = count_leading_zeros(Hash),
if
LeadingZeros >= CHALLENGE_LEADING_ZEROS ->
io:format("~p: completed the challenge with hash: ~ts\n", [Addr, Hash]),
Id = random_valid_post_id(),
io:format("~p: assigned post id: ~ts\n", [Addr, Id]),
PostPath = io_lib:format("~ts/~ts", [PostsRoot, Id]),
%% TODO: try to regenerate the Id several times until you find the one that is not taken
false = filelib:is_regular(PostPath), %% Very unlikely to happen, but still
ok = file:write_file(PostPath, Content),
gen_tcp:send(Sock, [<<"SENT ">>, Id, <<"\r\n">>]),
ok;
true ->
io:format("~p: ERROR: failed the challenge with hash: ~ts\n", [Addr, Hash]),
gen_tcp:send(Sock, <<"CHALLENGED FAILED\r\n">>),
ok
end;
{ok, _} ->
io:format("~p: ERROR: failed the challenge: Invalid Command\n", [Addr]),
gen_tcp:send(Sock, <<"INVALID COMMAND\r\n">>),
ok;
{error, timeout} ->
io:format("~p: ERROR: failed the challenge: Timeout\n", [Addr]),
gen_tcp:send(Sock, <<"TOO SLOW\r\n">>),
ok;
{error, Reason} ->
exit(Reason)
end;
session({get, Id}, Params) ->
#session_params { addr = Addr, sock = Sock, posts_root = PostsRoot } = Params,
case is_valid_post_id(Id) of
true ->
%% PostPath is safe to log with ~ts since it's made out of
%% PublicParams#params.posts_root which we trust and Id which is verified with
%% is_valid_post_id/1.
PostPath = io_lib:format("~ts/~ts", [PostsRoot, Id]),
case file:read_file(PostPath) of
{ok, Binary} ->
io:format("~p: sending out post ~ts\n", [Addr, PostPath]),
gen_tcp:send(Sock, Binary),
ok;
{error, enoent} ->
io:format("~p: ERROR: could not read post file ~ts: doesn't exists\n", [Addr, PostPath]),
gen_tcp:send(Sock, <<"404\r\n">>),
ok;
{error, Reason} ->
exit(Reason)
end;
false ->
%% Id is invalid post ID submitted by user! Always log such things with ~p!
io:format("~p: ERROR: invalid Post ID: ~p\n", [Addr, Id]),
gen_tcp:send(Sock, <<"404\r\n">>), %% Do not let the user know that the id is invalid. It's all "not found" for them.
ok
end.
-spec random_valid_post_id() -> binary().
random_valid_post_id() ->
binary:encode_hex(crypto:strong_rand_bytes(?POST_ID_BYTE_SIZE)).
-spec is_hex_digit(X :: integer()) -> boolean().
is_hex_digit(X) -> (($0 =< X) and (X =< $9)) or (($A =< X) and (X =< $F)).
-spec count_leading_zeros(Digest :: binary(), Acc :: integer()) -> integer().
count_leading_zeros(<<"0", Digest/binary>>, Acc) ->
count_leading_zeros(Digest, Acc + 1);
count_leading_zeros(_Digest, Acc) ->
Acc.
-spec count_leading_zeros(Digest :: binary()) -> integer().
count_leading_zeros(Digest) ->
count_leading_zeros(Digest, 0).
-spec is_valid_post_id(Id) -> boolean() when
Id :: binary().
is_valid_post_id(Id) ->
IdList = binary_to_list(Id),
(length(IdList) == ?POST_ID_BYTE_SIZE*2) and lists:all(fun is_hex_digit/1, IdList).
-spec accepter(LSock, Server) -> no_return() when
LSock :: gen_tcp:socket(),
Server :: pid().
accepter(LSock, Server) ->
{ok, Sock} = gen_tcp:accept(LSock),
Server ! {connected, Sock},
accepter(LSock, Server).
%% TODO: Delete the posts by requiring the user to provide the CHALLENGE and ACCEPTED strings.
%% TODO: Maybe post ids should be uuids?
%% TODO: Some sort of heartbeat mechanism while the client is doing POW challenge.
%% TODO: Should we support TLS connections?
%% TODO: server process should support hot reloading itself by a message
%% https://stackoverflow.com/a/11971978
%% TODO: dynamically increase the challenge as the load increases