This repository has been archived by the owner on May 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
server.js
228 lines (192 loc) · 6.68 KB
/
server.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
/*
* PlugBuild socket router
* Copyright © 2012 Stephen Oliver <mrsteveman1@gmail.com>
* Copyright © 2012 Jason Plum <max@warheads.net>
*/
var path = require('path');
var os = require('os');
var fs = require('fs');
var util = require('util');
var crypto = require('crypto');
var express = require('express');
var net = require('net');
var MemoryStore = express.session.MemoryStore;
var sessionStore = new MemoryStore();
var io;
// testing use of a global variable for the structured store
var package_store;
var builder_state = {};
/***************************************************************/
// set up builder connection, will this reconnect if the builder restarts?
var BHOST = 'localhost';
var BPORT = 2122;
var build_buffer = ''; // buffer for messages from the builder
var builder = new net.Socket();
function sendToBuilder(command){
builder.write(JSON.stringify(command));
}
builder.setEncoding('utf8');
builder.on('error', function(error) {
console.log('Error connecting to builder: ' + error);
});
builder.on('data', function(data) {
if( data.toString().charCodeAt(data.length-1) != 0 ) {
// append
build_buffer+=data.toString();
}else{
// append & parse
build_buffer+= data.toString().substr(0,data.length-1);
var message = JSON.parse(build_buffer);
build_buffer = '';
switch (message.command) {
case "echo":
console.log('Got successful echo: ' + message.session);
io.sockets.in(message.session).emit('console',message);
break;
case "dump":
var package_snapshot = message.dump;
package_store = package_snapshot;
console.log('Received initial package snapshot');
break;
case "console":
io.sockets.emit('console',message.console);
console.log('Package update: ' + package.package);
break;
case "update":
var type = message.type;
if (type == 'package') {
var package = message.package;
var name = package['name'];
package_store[name] = package;
io.sockets.emit('package',package);
console.log('Package update: ' + package.package);
}
else if (type == 'builder') {
var bdata = message.builder;
var fqn = bdata['fqn'];
builder_state[fqn] = bdata;
io.sockets.emit('builder',bdata);
console.log('Builder: ' + bdata.fqn + ', state: ' + bdata.state);
}
//
break;
default:
// wtf eh?
console.log('Unhandled message: ' + message);
break;
}
}
});
builder.on('close', function() {
console.log('Builder socket exited!');
});
builder.connect(BPORT, BHOST, function() {
console.log('Builder connected');
sendToBuilder({ command: "dump" });
});
/***************************************************************/
// create a webserver with https and client cert auth
var app = module.exports = express.createServer({
key: fs.readFileSync('/etc/plugbuild/server.key'),
cert: fs.readFileSync('/etc/plugbuild/server.crt'),
ca: fs.readFileSync('/etc/plugbuild/cacert.pem'),
requestCert: true,
rejectUnauthorized: true
});
app.configure(function(){
app.use(express.cookieParser());
app.use(express.session({
store : sessionStore,
secret : 'ununewh8wq3',
key : 'plugsid'
}));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use("/static", express.static(__dirname + '/static'));
// disable layout
app.set("view options", {layout: false});
app.set('views', __dirname + '/views'); //set the views dir
app.register('.html', {
compile: function(str, options){
return function(locals){
return str;
};
}
});
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
// the only actual web route, just serves the core page which then makes a bunch of static requests
app.get('/', function(req, res){
res.render('core.html');
var user = req.connection.getPeerCertificate().subject.CN;
console.log('New web connection from: ' + user);
// store in session?
req.session.userCN = user;
req.session.touch();
});
var parseCookie = require('connect').utils.parseCookie;
/***************************************************************/
// piggyback socket.io on to the webserver
io = require('socket.io').listen(app);
io.set('log level',1);
io.set('authorization', function (data, accept) {
// cookie?
if( data.headers.cookie ) {
data.cookie = parseCookie(data.headers.cookie);
data.sessionID = data.cookie['plugsid'];
// get the data from the store.
sessionStore.get(data.sessionID, function (err,session) {
if ( err || !session ) {
// no seesion, reject
accept('Session Error',false);
}else{
// save to data & accept
data.session = session;
accept(null,true);
}
});
} else {
accept('No cookie transmitted',false);
}
});
io.sockets.on('connection',function(socket){
console.log('socket connection: '+socket.handshake.session.userCN);
socket.join(socket.handshake.sessionID);
socket.emit('init', { user: socket.handshake.session.userCN, packages: package_store, builders: builder_state } );
socket.on('echo',function(data){
console.log(data);
sendToBuilder({
command : 'echo',
user : socket.handshake.session.userCN,
session : socket.handshake.sessionID,
consoleline : data.consoleline
});
});
socket.on('test',function(data){
console.log(data);
sendToBuilder({
command : 'echo',
user : socket.handshake.session.userCN,
session : socket.handshake.sessionID,
data : data
});
});
});
io.sockets.on('disconnect',function(){
console.log('socket disconnected');
});
setInterval(function() {
var status = {};
var freemem = os.freemem();
var totalmem = os.totalmem();
status.freemem = freemem;
status.memused = totalmem - freemem;
status.loadavg = os.loadavg();
status.uptime = os.uptime();
status.memory_total = totalmem;
io.sockets.emit('status', status);
}, 10000);
/***************************************************************/
// start the webserver
app.listen(7050);