forked from johnkeates/gsd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utls.js
85 lines (71 loc) · 2.31 KB
/
utls.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
var restify = require('restify');
var exec = require('child_process').exec;
function executeCommand(command, callback){
exec(command,
function (error, stdout, stderr) {
console.log('stdout: ' + stdout);
console.log('stderr: ' + stderr);
if (error !== null) {
console.log('exec error: ' + error);
}
callback();
});
}
function merge(joinedCliCommands) {
var sources = [].slice.call(arguments, 1);
var variables = [];
sources.forEach(function (source) {
for (var key in source) {
variables[key] = source[key];
}
});
output = [];
for (var key in variables) {
if (joinedCliCommands.indexOf(key)==-1){
output.push(key,variables[key]);
}else{
output.push(key + variables[key]);
}
}
output = output.filter(function(n){return n});
return output;
}
function saveconfig(config){
fs.writeFile("config.json", JSON.stringify(config, null, 4), function(err) {
if(err) {
console.log(err);
} else {
console.log("JSON saved to " + outputFilename);
}
});
}
function unknownMethodHandler(req, res) {
if (req.method.toLowerCase() === 'options') {
console.log('received an options method request');
var allowHeaders = ['Accept', 'Accept-Version', 'Content-Type', 'Api-Version', 'Origin', 'X-Requested-With', 'Options', 'X-Access-Token']; // added Origin & X-Requested-With
res.header('Access-Control-Allow-Credentials', true);
res.header('Access-Control-Allow-Headers', allowHeaders.join(', '));
res.header('Access-Control-Allow-Methods', res.methods.join(', '));
res.header('Access-Control-Allow-Origin', req.headers.origin);
return res.send(204);
}
else
return res.send(new restify.MethodNotAllowedError());
}
function getIPAddress() {
var interfaces = require('os').networkInterfaces();
for (var devName in interfaces) {
var iface = interfaces[devName];
for (var i = 0; i < iface.length; i++) {
var alias = iface[i];
if (alias.family === 'IPv4' && alias.address !== '127.0.0.1' && !alias.internal)
return alias.address;
}
}
return '0.0.0.0';
}
exports.getIPAddress = getIPAddress;
exports.executeCommand = executeCommand;
exports.saveconfig = saveconfig;
exports.merge = merge;
exports.unknownMethodHandler = unknownMethodHandler;