Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce security token and bind to 127.0.0.1 #46

Merged
merged 1 commit into from
Jul 25, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ Available commands:

Type `eslint_d --help` to see the supported `eslint` options.

`eslint_d` will select a free port automatically and store the port number in
`~/.eslint_d_port`.
`eslint_d` will select a free port automatically and store the port number
along with an access token in `~/.eslint_d`.

## Editor integration

Expand Down Expand Up @@ -83,7 +83,9 @@ the `eslint_d` server with netcat. This will also eliminate the node.js startup
time.

```bash
$ echo '. file.js' | nc localhost `cat ~/.eslint_d_port`
$ PORT=`cat ~/.eslint_d | cut -d" " -f1`
$ TOKEN=`cat ~/.eslint_d | cut -d" " -f2`
$ echo "$TOKEN $PWD file.js" | nc localhost $PORT
```

This runs `eslint` in under `50ms`!
Expand Down
24 changes: 12 additions & 12 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ var net = require('net');
var portfile = require('./portfile');

function connect(callback) {
var port = portfile.read();
if (port) {
var socket = net.connect(port, function () {
callback(null, socket);
var data = portfile.read();
if (data) {
var socket = net.connect(data.port, function () {
callback(null, socket, data.token);
});
socket.on('error', function () {
callback('Could not connect');
Expand All @@ -18,12 +18,12 @@ function connect(callback) {
}

exports.stop = function (callback) {
connect(function (err, socket) {
connect(function (err, socket, token) {
if (err) {
process.stdout.write(err + '\n');
return;
}
socket.end('stop', function () {
socket.end(token + ' stop', function () {
if (typeof callback === 'function') {
callback();
}
Expand All @@ -49,7 +49,7 @@ exports.lint = function (args, text) {
return;
}

function lint(socket) {
function lint(socket, token) {
var buf = '';
socket.on('data', function (chunk) {
buf += chunk;
Expand All @@ -68,25 +68,25 @@ exports.lint = function (args, text) {
}
}
});
socket.end(JSON.stringify({
socket.end(token + ' ' + JSON.stringify({
cwd: process.cwd(),
args: args,
text: text
}));
}
connect(function (err, socket) {
connect(function (err, socket, token) {
if (err) {
require('./launcher')(function () {
connect(function (err, socket) {
connect(function (err, socket, token) {
if (err) {
process.stdout.write(err + '\n');
} else {
lint(socket);
lint(socket, token);
}
});
});
} else {
lint(socket);
lint(socket, token);
}
});
};
6 changes: 3 additions & 3 deletions lib/launcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ var net = require('net');
var portfile = require('../lib/portfile');

function check(callback) {
var port = portfile.read();
if (!port) {
var data = portfile.read();
if (!data) {
callback(false);
return;
}
var socket = net.connect(port, function () {
var socket = net.connect(data.port, function () {
socket.end();
callback(true);
});
Expand Down
19 changes: 11 additions & 8 deletions lib/portfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,25 @@
var fs = require('fs');

var homeEnv = process.platform === 'win32' ? 'USERPROFILE' : 'HOME';
var portFile = process.env[homeEnv] + '/.eslint_d_port';
var dataFile = process.env[homeEnv] + '/.eslint_d';

exports.write = function (port) {
fs.writeFileSync(portFile, String(port));
exports.write = function (port, token) {
fs.writeFileSync(dataFile, port + ' ' + token);
};

exports.read = function () {
if (fs.existsSync(portFile)) {
var data = fs.readFileSync(portFile, 'utf8');
return Number(data);
if (fs.existsSync(dataFile)) {
var data = fs.readFileSync(dataFile, 'utf8').split(' ');
return {
port: Number(data[0]),
token: data[1]
};
}
return null;
};

exports.unlink = function () {
if (fs.existsSync(portFile)) {
fs.unlinkSync(portFile);
if (fs.existsSync(dataFile)) {
fs.unlinkSync(dataFile);
}
};
13 changes: 11 additions & 2 deletions lib/server.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
'use strict';

var crypto = require('crypto');
var net = require('net');
var portfile = require('./portfile');
var linter = require('./linter');

var token = crypto.randomBytes(8).toString('hex');


var server = net.createServer({
allowHalfOpen: true
Expand All @@ -17,6 +20,12 @@ var server = net.createServer({
con.end();
return;
}
var p = data.indexOf(' ');
if (p === -1 || data.substring(0, p) !== token) {
con.end();
return;
}
data = data.substring(p + 1);
if (data === 'stop') {
con.end();
server.close();
Expand All @@ -42,9 +51,9 @@ var server = net.createServer({
});
});

server.listen(function () {
server.listen(0, '127.0.0.1', function () {
var port = server.address().port;
portfile.write(port);
portfile.write(port, token);
});

process.on('exit', function () {
Expand Down