forked from masc-ucsc/liveos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
csearch_daemon.js
executable file
·77 lines (66 loc) · 2.61 KB
/
csearch_daemon.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
/*
* (C) Copyright 2015 Regents of the University of California and LiveOS Project.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the GNU Lesser General Public License
* (LGPL) version 2.1 which accompanies this distribution, and is available at
* http://www.gnu.org/licenses/lgpl-2.1.html
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* Contributors:
* Ethan Papp
*/
//importing external and internal libraries
var socket_io = require("socket.io");
var socket_io_client = require("socket.io-client");
var fs = require("fs");
var terminal_server = require("./server/lib/terminal_server.js");
var codesearch_server = require("./server/lib/codesearch_server/codesearch_server.js");
//check config files
var conf = JSON.parse(fs.readFileSync("etc/server.json"));
var port = conf.ports.csearch;
//defining constants and variables
var global_tokens = {};
global_tokens.keys = [];
global_tokens.map = {};
global_tokens.keys[0] = "JKJKd00_435jmkMM235345tp3456M2^^7820@_++4!";
global_tokens.map.daemons = 0;
//connecting to the main daemon
var os = new socket_io_client("http://localhost:" + conf.ports.main, {query: "token=JKJKd00_435jmkMM235345tp3456M2^^7820@_++4!"});
var fsio = new socket_io_client("http://localhost:" + conf.ports.main + "/file_server", {query: "token=JKJKd00_435jmkMM235345tp3456M2^^7820@_++4!"});
//starting io servers
var io = new socket_io();
var tserver = new terminal_server(__dirname);
var csearchs = new codesearch_server(__dirname, io, tserver, fsio);
//master daemon events
os.on("connect", function() {
os.emit("update_tokens_req", {name: "codesearch"});
});
os.on("update_tokens", function(obj) {
global_tokens = obj.tokens;
});
//authorization
io.set("authorization", function (req, callback) {
if(!req || !req._query || !req._query.token) {
console.log("Authorization: failed, no valid params");
return false;
}
if (req._query.token === undefined || req._query.token.length === 0) {
console.log("Authorization: failed, empty token");
return false;
}
if(global_tokens.keys.indexOf(req._query.token) != -1) {
console.log("Authorization: success");
return callback(null, true);
} else {
console.log("Authorization: failed, incorrect token");
return false;
}
});
//Start listening
io.listen(port);
console.log("Codesearch daemon started successfully on port " + port);