From cd4a027eac9a198ef6b1a7ec7e3a3abf74683385 Mon Sep 17 00:00:00 2001 From: WebFreak001 Date: Fri, 11 Aug 2017 06:26:52 +0200 Subject: [PATCH] Undo some changes in main --- src/common/messages.d | 2 +- src/server/main.d | 49 ++++++++++++++++++++++++++++++++++++------- 2 files changed, 42 insertions(+), 9 deletions(-) diff --git a/src/common/messages.d b/src/common/messages.d index 411c70b..3287758 100644 --- a/src/common/messages.d +++ b/src/common/messages.d @@ -167,7 +167,7 @@ struct AutocompleteResponse ulong symbolIdentifier; /** - * Creates an empty acknoledgement response + * Creates an empty acknowledgement response */ static AutocompleteResponse ack() { diff --git a/src/server/main.d b/src/server/main.d index 3a01e7c..1bc688d 100644 --- a/src/server/main.d +++ b/src/server/main.d @@ -131,7 +131,7 @@ int main(string[] args) socket = new TcpSocket(AddressFamily.INET); socket.blocking = true; socket.setOption(SocketOptionLevel.SOCKET, SocketOption.REUSEADDR, true); - socket.bind(new InternetAddress("127.0.0.1", port)); + socket.bind(new InternetAddress("localhost", port)); info("Listening on port ", port); } else @@ -182,21 +182,40 @@ int main(string[] args) // No relative paths version (Posix) chdir("/"); + version (LittleEndian) + immutable expectedClient = IPv4Union([1, 0, 0, 127]); + else + immutable expectedClient = IPv4Union([127, 0, 0, 1]); + serverLoop: while (true) { auto s = socket.accept(); s.blocking = true; + if (useTCP) + { + // Only accept connections from localhost + IPv4Union actual; + InternetAddress clientAddr = cast(InternetAddress) s.remoteAddress(); + actual.i = clientAddr.addr; + // Shut down if somebody tries connecting from outside + if (actual.i != expectedClient.i) + { + fatal("Connection attempted from ", clientAddr.toAddrString()); + return 1; + } + } + scope (exit) { s.shutdown(SocketShutdown.BOTH); s.close(); } - sw.reset(); - sw.start(); - auto bytesReceived = s.receive(buffer); + + auto requestWatch = StopWatch(AutoStart.yes); + size_t messageLength; // bit magic! (cast(ubyte*) &messageLength)[0..size_t.sizeof] = buffer[0..size_t.sizeof]; @@ -205,12 +224,18 @@ int main(string[] args) immutable b = s.receive(buffer[bytesReceived .. $]); if (b == Socket.ERROR) { - warning("Socket recieve failed"); - continue serverLoop; + bytesReceived = Socket.ERROR; + break; } bytesReceived += b; } + if (bytesReceived == Socket.ERROR) + { + warning("Socket recieve failed"); + break; + } + AutocompleteRequest request; msgpack.unpack(buffer[size_t.sizeof .. bytesReceived], request); @@ -259,12 +284,20 @@ int main(string[] args) else if (request.kind & RequestKind.localUse) s.trySendResponse(findLocalUse(request, cache), "Could not find local usage"); - sw.stop(); - info("Request processed in ", sw.peek().to!("msecs", float), " milliseconds"); + info("Request processed in ", requestWatch.peek().to!("msecs", float), " milliseconds"); } return 0; } +/// IP v4 address as bytes and a uint +union IPv4Union +{ + /// the bytes + ubyte[4] b; + /// the uint + uint i; +} + /// Lazily evaluates a response with an exception handler and sends it to a socket or logs msg if evaluating response fails. void trySendResponse(Socket socket, lazy AutocompleteResponse response, string msg) {