diff --git a/groovy/src/main/com/nvuillam/CodeNarcServer.groovy b/groovy/src/main/com/nvuillam/CodeNarcServer.groovy index c72b3ce6..af6bb1a1 100644 --- a/groovy/src/main/com/nvuillam/CodeNarcServer.groovy +++ b/groovy/src/main/com/nvuillam/CodeNarcServer.groovy @@ -8,6 +8,7 @@ package com.nvuillam // Java Http Server import com.sun.net.httpserver.HttpServer +import com.sun.net.httpserver.HttpExchange import java.net.InetSocketAddress import java.io.PrintStream @@ -67,9 +68,11 @@ class CodeNarcServer { // Launch HttpServer to receive CodeNarc linting request via Http /* groovylint-disable-next-line UnusedPrivateMethod */ private void initialize() { - // Create a server who accepts only calls from localhost - InetSocketAddress socketAddr = new InetSocketAddress(PORT) - def server = HttpServer.create(socketAddr, 0) + // Create a server who accepts only calls from localhost ( https://stackoverflow.com/questions/50770747/how-to-configure-com-sun-net-httpserver-to-accept-only-requests-from-localhost ) + + InetAddress localHost = InetAddress.getLoopbackAddress() + InetSocketAddress sockAddr = new InetSocketAddress(localHost, PORT) + HttpServer server = HttpServer.create(sockAddr, 0) Timer timer = new Timer() TimerTask currentTimerTask @@ -102,7 +105,6 @@ class CodeNarcServer { server.createContext('/request') { http -> System.setOut(new StorePrintStream(System.out)) println "INIT: Hit from Host: ${http.remoteAddress.hostName} on port: ${http.remoteAddress.holder.port}" - // Restart idle timer currentTimerTask.cancel() timer = new Timer() @@ -118,7 +120,7 @@ class CodeNarcServer { def bodyObj = jsonSlurper.parseText(body) requestKey = bodyObj.requestKey - manageRequestKey = (requestKey != null) + manageRequestKey = (requestKey != null && requestKey != 'undefined' ) if (manageRequestKey) { // Cancel already running request if necessary @@ -204,7 +206,7 @@ class CodeNarcServer { // Create executor & start server with a timeOut if inactive server.setExecutor(ex); // set up a custom executor for the server server.start(); // start the server - println "LISTENING on ${this.getHostString(socketAddr)}:${PORT}, hit Ctrl+C to exit." + println "LISTENING on ${this.getHostString(sockAddr)}:${PORT}, hit Ctrl+C to exit." currentTimerTask = timer.runAfter(this.maxIdleTime, { timerData -> stopServer(ex, server) }) } diff --git a/lib/java/CodeNarcServer.jar b/lib/java/CodeNarcServer.jar index 1df38520..560ee292 100644 Binary files a/lib/java/CodeNarcServer.jar and b/lib/java/CodeNarcServer.jar differ diff --git a/lib/options.js b/lib/options.js index 326c4ea0..7f2f89c5 100644 --- a/lib/options.js +++ b/lib/options.js @@ -179,7 +179,7 @@ module.exports = optionator({ { option: "serverhost", type: "String", - default: "http://" + require("ip").address(), + default: "http://localhost", //"http://" + require("ip").address(), description: "If use of CodeNarc server, host where is the CodeNarc server (default: localhost)" }, { diff --git a/package.json b/package.json index 02ab2815..6c128f04 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,7 @@ "test:coverage": "nyc npm run test", "test:debug": "mocha --reporter spec --inspect-brk \"lib/test/**/*.test.js\"", "dev:build-config": "node script-build-config-all.js", - "dev:lint-install-local": "npm run lint:fix && npm link", + "dev:lint-install-local": "npm run lint:fix && npm link --force", "dev:lint-install-local-copy-vscode": "npm run dev:lint-install-local && node script-deploy-in-vscode.js" }, "repository": {