Fix socket path joins

A `config.socketRoot` without a trailing slash could lead to
EACCESS errors due to the path not existing.
This commit is contained in:
Jonas Hermsmeier 2017-11-16 18:18:07 +01:00
parent 70e03c119b
commit 8fc3ffe64e
No known key found for this signature in database
GPG key ID: 1B870F801A0CEE9F
3 changed files with 23 additions and 21 deletions

View file

@ -5,6 +5,7 @@ const net = require('net'),
EventParser = require('../entities/EventParser.js'), EventParser = require('../entities/EventParser.js'),
Message = require('js-message'), Message = require('js-message'),
fs = require('fs'), fs = require('fs'),
path = require('path'),
Queue = require('js-queue'); Queue = require('js-queue');
let Events = require('event-pubsub/es5'); let Events = require('event-pubsub/es5');
@ -82,9 +83,9 @@ function connect(){
options.path=client.path; options.path=client.path;
if (process.platform ==='win32' && !client.path.startsWith('\\\\.\\pipe\\')){ if (process.platform ==='win32' && !client.path.startsWith('\\\\.\\pipe\\')){
options.path = options.path.replace(/^\//, ''); options.path = options.path.replace(/^[\/\\]/, '');
options.path = options.path.replace(/\//g, '-'); options.path = options.path.replace(/[\/\\]/g, '-');
options.path= `\\\\.\\pipe\\${options.path}`; options.path = path.join('\\\\.\\pipe\\', options.path);
} }
client.socket = net.connect(options); client.socket = net.connect(options);

View file

@ -22,7 +22,7 @@ class Defaults{
constructor(){ constructor(){
this.appspace='app.'; this.appspace='app.';
this.socketRoot='/tmp/'; this.socketRoot=os.tmpdir();
this.id=os.hostname(); this.id=os.hostname();
this.encoding='utf8'; this.encoding='utf8';

View file

@ -3,7 +3,8 @@
const Defaults = require('../entities/Defaults.js'), const Defaults = require('../entities/Defaults.js'),
Client = require('../dao/client.js'), Client = require('../dao/client.js'),
Server = require('../dao/socketServer.js'), Server = require('../dao/socketServer.js'),
util = require('util'); util = require('util'),
path = require('path');
class IPC{ class IPC{
constructor(){ constructor(){
@ -102,18 +103,18 @@ function disconnect(id){
delete this.of[id]; delete this.of[id];
} }
function serve(path,callback){ function serve(socketPath,callback){
if(typeof path=='function'){ if(typeof socketPath=='function'){
callback=path; callback=socketPath;
path=false; socketPath=false;
} }
if(!path){ if(!socketPath){
socketPath=path.join(this.config.socketRoot, this.config.appspace + this.config.id);
this.log( this.log(
'Server path not specified, so defaulting to', 'Server path not specified, so defaulting to',
'ipc.config.socketRoot + ipc.config.appspace + ipc.config.id', 'ipc.config.socketRoot + ipc.config.appspace + ipc.config.id',
this.config.socketRoot+this.config.appspace+this.config.id socketPath
); );
path=this.config.socketRoot+this.config.appspace+this.config.id;
} }
if(!callback){ if(!callback){
@ -121,7 +122,7 @@ function serve(path,callback){
} }
this.server=new Server( this.server=new Server(
path, socketPath,
this.config, this.config,
log log
); );
@ -213,10 +214,10 @@ function serveNet(host,port,UDPType,callback){
); );
} }
function connect(id,path,callback){ function connect(id,socketPath,callback){
if(typeof path == 'function'){ if(typeof socketPath == 'function'){
callback=path; callback=socketPath;
path=false; socketPath=false;
} }
if(!callback){ if(!callback){
@ -231,13 +232,13 @@ function connect(id,path,callback){
return; return;
} }
if(!path){ if(!socketPath){
socketPath=path.join(this.config.socketRoot, this.config.appspace + id);
this.log( this.log(
'Service path not specified, so defaulting to', 'Service path not specified, so defaulting to',
'ipc.config.socketRoot + ipc.config.appspace + id', 'ipc.config.socketRoot + ipc.config.appspace + id',
(this.config.socketRoot+this.config.appspace+id).data socketPath
); );
path=this.config.socketRoot+this.config.appspace+id;
} }
if(this.of[id]){ if(this.of[id]){
@ -255,7 +256,7 @@ function connect(id,path,callback){
this.of[id] = new Client(this.config,this.log); this.of[id] = new Client(this.config,this.log);
this.of[id].id = id; this.of[id].id = id;
this.of[id].path = path; this.of[id].path = socketPath;
this.of[id].connect(); this.of[id].connect();