-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasteroid.js
executable file
·247 lines (204 loc) · 6.38 KB
/
asteroid.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#!/usr/bin/node
/****************************************************************************************
Asteroid Server
Author: Steven Sokol ([email protected])
****************************************************************************************/
// Since we run this as a service...
process.chdir('/opt/asteroid');
// Web Stack
var express = require("express");
var http = require('http');
var https = require('https');
var sockio = require("socket.io");
// Other requirements
var fs = require('fs');
var url = require('url');
var path = require('path');
var bp = require('body-parser');
var strip = require('strip-ansi')
var net = require('net');
// Configuration file
var configFile = '/etc/asterisk/asteroid.conf';
if (!fs.existsSync(configFile)) {
console.log("No configuration file found at: " + configFile)
process.exit(1);
}
var config = require(configFile);
var app = express();
var server = http.createServer(app);
var privateKey = fs.readFileSync('sslcert/key.pem', 'utf8');
var certificate = fs.readFileSync('sslcert/cert.pem', 'utf8');
var credentials = {key: privateKey, cert: certificate, passphrase: config.passphrase};
var base = "/etc/asterisk";
var webroot = path.join(__dirname, "/web/");
// Web server configuration options
app.use(bp.json());
app.use("/", express.static(webroot));
// Load a file from the server
app.get("/file/load", function(req, res) {
var urlParts = url.parse(req.url, true);
var query = urlParts.query;
if (query.filepath) {
var fname = path.join(base, query.filepath);
console.log(fname);
fs.readFile(fname, function(err, data) {
if (data) {
res.send(200, JSON.stringify({"name": path.basename(fname), "filepath": path.basename(fname), "type": path.extname(fname), "text": data.toString()}));
} else {
res.send(404, "File not found.");
}
});
} else {
res.send(500, "Invalid request. No filepath value provided.");
}
});
// Retrieve a list of files from the server
app.get("/file/list", function(req, res) {
var urlParts = url.parse(req.url, true);
var query = urlParts.query;
fs.readdir(base, function(err, files) {
var ret = {};
var done = 0;
ret.path = base;
ret.files = [];
var checkDone = function() {
if (done == files.length) {
res.send(200, JSON.stringify(ret));
return true;
}
return false;
}
var getStat = function(fname) {
fs.stat(fname, function(err, stats) {
if (stats) {
if (stats.isDirectory()) {
ret.files.push({"name": path.basename(fname), "type": null, "directory": true});
} else {
ret.files.push({"name": path.basename(fname), "filepath": path.basename(fname), "type": path.extname(fname), "directory": false});
}
} else {
console.log(err);
}
done++;
checkDone();
});
}
if (err) {
res.send(404, "Requested path not found.");
} else {
for (var i = 0; i < files.length; i++) {
var file = files[i];
if (file[0] === ".") {
done++;
if (checkDone() === false) continue;
} else {
getStat(base + "/" + file);
}
}
}
});
});
app.post("/file/save", function(req, res) {
var base = "/etc/asterisk/";
var fname = req.body.filepath;
var text = req.body.text;
var commit = req.body.commit;
if ((fname) && (text)) {
fname = path.join(base, fname); // super dangerous
console.dir("Saving: " + fname + " (commit = " + commit + ")");
fs.open(fname, "w", function(err, fd) {
var bt = new Buffer(text);
if ((err) || (!fd)) {
res.send(501, "Error in write: " + err);
console.log("Cannot write file - invalid fd or something.");
console.log(err);
return
}
fs.write(fd, bt, 0, bt.length, 0, function(err, written, buf) {
if (err) {
res.send(501, "Error in write: " + err);
return;
}
fs.close(fd, function() {
res.send(200, "File" + fname + " saved.");
});
cli.write("core reload\0");
});
});
} else {
res.send(500, "Missing parameters required to save.");
}
});
app.get("/apps/list", function(req, res) {
});
app.get("/apps/load", function(req, res) {
});
// Create http and ws servers
var httpsServer = https.createServer(credentials, app);
httpsServer.listen(443);
var io = sockio.listen(httpsServer);
// Create http redirect server
var redirect = express();
redirect.use(function requireHTTPS(req, res, next) {
if (!req.secure) {
return res.redirect('https://' + req.headers.host + req.url);
}
next();
});
var httpServer = http.createServer(redirect);
httpServer.listen(80);
// Asterisk Console Listener / Writer
// TODO: Move to a separate module / file?
var cli = null;
var silent = false;
var connectCLI = function() {
setTimeout(function() {
cli = net.createConnection("/var/run/asterisk/asterisk.ctl");
if (!cli) {
return reconnectCLI();
}
cli.on("connect", function() {
console.log("Connected to the Asterisk CLI socket.");
cli.write("core set verbose atleast 5\0");
cli.write("core set debug atleast 5\0");
cli.write("logger mute silent\0");
});
cli.on("data", function(data) {
var text = data.toString('ascii');
var channel = "console";
io.sockets.emit(channel, text);
});
cli.on("end", function() {
io.sockets.emit("console", " == Asterisk disconnected!");
connectCLI();
});
}, 1000);
};
io.set('log level', 1);
io.sockets.on('connection', function (socket) {
console.log("Remote client connection.");
socket.emit("console", "Connected to Asterisk via web CLI interface.");
// TODO: process command requests from clients
socket.on("command", function(data) {
var command = data.command;
console.dir(data);
if (data[0] === "!") {
console.log("Illegal attempt to do rooty stuff.");
io.sockets.emit("console", "You're a very naughty boy. No root access from here.");
} else {
cli.write(data + "\0");
}
});
// TODO: process command requests from clients
socket.on("completion", function(data) {
var command = data.command;
console.dir(data);
if (data[0] === "!") {
console.log("Illegal attempt to do rooty stuff.");
io.sockets.emit("console", "You're a very naughty boy. No root access from here.");
} else {
cli.write(data + "\t\0");
}
});
});
connectCLI();