Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -module(webserver).
- -export([start/1, loop/1]).
- -include("debug.hrl").
- start(_) ->
- Port = 8080,
- {ok, Sock} = gen_tcp:listen(Port, [{active, false}, {packet, line}, {reuseaddr, true}]),
- loop(Sock).
- loop(Sock) ->
- case gen_tcp:accept(Sock, 1000) of
- {ok, ConnSock} ->
- spawn_link(fun () -> handle(ConnSock) end);
- {error, timeout} ->
- socket_timeout;
- Err ->
- error({webserver_sock_accept,Err})
- end,
- receive
- code_change ->
- logger:log([webserver, code],{code_change}),
- ?MODULE:loop(Sock);
- Unknown ->
- logger:log([critical,webserver],{unhandled_receive, Unknown}),
- loop(Sock)
- after 0 ->
- loop(Sock)
- end.
- handle(Conn) ->
- Timeout = 20000 + (erlang:crc32(pid_to_list(self())) band 8191),
- Got = get_request(Conn),
- % ?DEBUG("Got request: ~p", [Got]),
- % muze byt {error,enotconn!}
- IP = case inet:peername(Conn) of
- {ok, {IPaddr, _Port}} ->
- IPaddr;
- _ ->
- {69,69,69,69}
- end,
- flood_activity(IP, Got, 0),
- {ok, {_Score, TextStatus}} = floodbot:status_of(IP),
- case TextStatus of
- ok ->
- send_request_to_lua(Got);
- flooding ->
- self() ! {flooding, right_now}
- end,
- Reply = handle_loop(Conn, IP, Timeout, Got),
- gen_tcp:send(Conn, create_response(Reply)),
- gen_tcp:close(Conn),
- exit(normal).
- handle_loop(Conn, IP, Timeout, Got) ->
- receive
- {flooding,_FloodInfo} ->
- "<h2>Flooding attempt detected from this address, sorry...<p>Please leave for a few minutes, otherwise you'll be kept locked out.</h2>";
- {http_reply, Body} -> Body;
- {flood_activity, Score} ->
- flood_activity(IP, Got, Score),
- handle_loop(Conn, IP, Timeout, Got);
- Dunno ->
- logger:log([socket, critical],{unhandled_receive, Dunno}),
- exit(normal)
- after Timeout ->
- server:send_to(luaserver, {webserver, self(), connection_aborted}),
- "IDLE"
- end.
- get_request(Conn) ->
- RevParts = get_request(Conn, []),
- lists:concat(lists:reverse(RevParts)).
- get_request(Conn, RevParts) ->
- Part = gen_tcp:recv(Conn, 0, 5000),
- case Part of
- {ok,"\r\n"} -> %konec requestu
- RevParts;
- {ok, Bytes} ->
- get_request(Conn, [Bytes | RevParts]);
- {error, _Err} ->
- server:send_to(luaserver, {webserver, self(), connection_aborted}),
- gen_tcp:close(Conn),
- % logger:log([socket, debug], {socket_error, self(), Err}),
- exit(normal);
- Any ->
- logger:log([socket,critical], {unknown_from_socket, Any}), %Really shouldn't happen
- exit(normal)
- end.
- send_request_to_lua(String) ->
- server:send_to(luaserver, {webserver, self(), new_connection, String}).
- create_response(Str) ->
- string_to_response(Str).
- string_to_response(Str) ->
- B = iolist_to_binary(Str),
- iolist_to_binary(
- io_lib:fwrite(
- "HTTP/1.0 200 OK\nContent-Type: text/html\nAccess-Control-Allow-Origin: *\nContent-Length: ~p\n\n~s",
- [size(B), B])).
- flood_activity(IP, Request, Score) ->
- server:send_to(floodbot, {self(), {flood_activity, Score, IP, Request}}).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement