Skip to content

Commit

Permalink
CodeNarcServer listens only to localhost (#59)
Browse files Browse the repository at this point in the history
Related to #56
  • Loading branch information
nvuillam authored Jul 1, 2020
1 parent 5352842 commit dfc67bd
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 8 deletions.
14 changes: 8 additions & 6 deletions groovy/src/main/com/nvuillam/CodeNarcServer.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand All @@ -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
Expand Down Expand Up @@ -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) })
}

Expand Down
Binary file modified lib/java/CodeNarcServer.jar
Binary file not shown.
2 changes: 1 addition & 1 deletion lib/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)"
},
{
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down

0 comments on commit dfc67bd

Please sign in to comment.