Skip to content

Commit

Permalink
Add a service socket to enable out of band access to the process comm…
Browse files Browse the repository at this point in the history
…andline
  • Loading branch information
Xaekai committed Jul 16, 2016
1 parent aaa21aa commit 4010ee9
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 0 deletions.
6 changes: 6 additions & 0 deletions config.template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -237,3 +237,9 @@ setuid:
# concurrent updates), then run `node lib/channel-storage/migrate.js`.
channel-storage:
type: 'file'

# Allows for external services to access the system commandline
# Useful for setups where stdin isn't available such as when using PM2
service-socket:
enabled: false
socket: 'service.sock'
27 changes: 27 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ process.stdin.on("data", function (data) {
}
});

var net = require('net');
function handleLine(line) {
if (line === "/reload") {
Logger.syslog.log("Reloading config");
Expand Down Expand Up @@ -75,5 +76,31 @@ function handleLine(line) {
}
} else if (line.indexOf("/reload-partitions") === 0) {
sv.reloadPartitionMap();
} else if (line.indexOf("/globalban") === 0) {
var args = line.split(" "); args.shift();
if (args.length === 2 && net.isIPv4(args[0]) || net.isIPv6(args[0])) {
require("./lib/database").globalBanIP(args[0], args[1], function (err, res) {
if (!err) {
Logger.eventlog.log("[acp] " + "SYSTEM" + " global banned " + args[0]);
}
})
}
} else if (line.indexOf("/unglobalban") === 0) {
var args = line.split(" "); args.shift();
if (args.length === 1 && net.isIPv4(args[0]) || net.isIPv6(args[0])) {
require("./lib/database").globalUnbanIP(args[0], function (err, res) {
if (!err) {
Logger.eventlog.log("[acp] " + "SYSTEM" + " un-global banned " + args[0]);
}
})
}
}
}

// Go Go Gadget Service Socket
if (Config.get("service-socket.enabled")) {
Logger.syslog.log("Opening service socket");
var ServiceSocket = require('./lib/servsock');
var server = new ServiceSocket;
server.init(handleLine, Config.get("service-socket.socket"));
}
56 changes: 56 additions & 0 deletions servcmd.sh.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#!/usr/bin/env node
/*
** CyTube Service Socket Commandline
*/

var Config = require("./lib/config");
Config.load("config.yaml");

if(!Config.get("service-socket.enabled")){
console.error('The Service Socket is not enabled.');
process.exit(1);
}

const SOCKETFILE = Config.get("service-socket.socket");
var net = require('net');

var client = net.createConnection(SOCKETFILE)
.on('connect', () => {
console.log("Connected.");
})
.on('data', (msg) => {
msg = msg.toString();

if(msg === '__disconnect'){
console.log('Server shutting down. Terminating.')
return cleanup();
}

// Generic message handler
console.info('Server:', data)
})
.on('error', (data) => {
console.error('Unable to connect to Service Socket.');
process.exit(1);
})
;

var inputbuffer = "";
process.stdin.on("data", (data) => {
inputbuffer += data;
if (inputbuffer.indexOf("\n") !== -1) {
var line = inputbuffer.substring(0, inputbuffer.indexOf("\n"));
inputbuffer = inputbuffer.substring(inputbuffer.indexOf("\n") + 1);
// Let the client escape
if(line === 'exit'){ return cleanup(); }
if(line === 'quit'){ return cleanup(); }
client.write(line);
}
});

function cleanup(){
console.log('\n',"Terminating.",'\n');
client.end();
process.exit(0);
}
process.on('SIGINT', cleanup);
4 changes: 4 additions & 0 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ var defaults = {
},
"channel-storage": {
type: "file"
},
"service-socket": {
enabled: false,
socket: "service.sock"
}
};

Expand Down
53 changes: 53 additions & 0 deletions src/servsock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
var fs = require('fs');
var net = require('net');

export default class ServiceSocket {

constructor() {
this.connections = {};
}

init(handler, socket){
this.handler = handler;
this.socket = socket;

fs.stat(this.socket, (err, stats) => {
if (err) {
return this.openServiceSocket();
}
fs.unlink(this.socket, (err) => {
if(err){
console.error(err); process.exit(0);
}
return this.openServiceSocket();
});
});
}

openServiceSocket(){
this.server = net.createServer((stream) => {
let id = Date.now();
this.connections[id] = stream;
stream.on('end', () => {
delete this.connections[id];
});
stream.on('data', (msg) => {
this.handler(msg.toString());
});
}).listen(this.socket);
process.on('exit', this.closeServiceSocket);
}

closeServiceSocket() {
if(Object.keys(this.connections).length){
let clients = Object.keys(this.connections);
while(clients.length){
let client = clients.pop();
this.connections[client].write('__disconnect');
this.connections[client].end();
}
}
this.server.close();
}

}

0 comments on commit 4010ee9

Please sign in to comment.