-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathserver.js
182 lines (142 loc) · 6.27 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
var readline = require('readline');
var path = require('path');
var fs = require('fs');
var watch = require('node-watch');
var https = require('https');
var socket = require("socket.io");
//setup certificates
var privateKey = "-----BEGIN RSA PRIVATE KEY-----\nMIICXAIBAAKBgQDHuz6myrzBGVkcxPhYU8kPHSoie9+pTPOKChmBiuoaFjsPfZii\nnlea0WxNC91NWekR9MubJ1cXJlq4/6gphGbwZmR3iwVwW9nNLXGCxUSxCSDbgay7\neMci4O/hLfF4SSp9ZA2fTfXpMuAiVZGFMsOvvIeytut240t9o90OXz7iSQIDAQAB\nAoGAUTZ5yyg0h+/epCwTLxcicdrR/yOPVi/L9x0UMfBiMClJ6oqPPdBUnsb42nsw\niPs+Ieb/wx7D8s3NpduObRNBJ1Xn1rRA5klxoF0/aKF4gssKJxBHnAL884DahF+D\ni0f850Ky+/xgsZXidOS1RCflRknGFvCtsJ0kApdKQvHQ4mECQQD9S2cXSrrYKTD6\n3J+jEUXKz+cC5MtmFM9HtxLukZLMJ3R+j6RuUONFU33mVK8T2Z972cutmTHG8t/W\n9PNv9hf7AkEAyd1hpbAn2bCrbOpyJmq2cv2MaW2rkkO/mWUF8nHadtyddWru0E5G\nWFZbWs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FICATE-----\n";
var credentials = {key: privateKey, cert: certificate};
//setup readline
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
//var usableFileEndings = "none";
var config = {}
function AskUsedLanguage(){
rl.question("What language do you code in? (possible options: py/cs/js/java) ", (answer) => {
if(answer === "py" || answer === "cs" || answer === "js" || answer === "java"){
//write new config
config = {language: answer}
fs.writeFileSync("./config.json", JSON.stringify(config), {flag: "w"})
StartServer()
}else{
console.log("That is not a correct language/file ending!");
AskUsedLanguage();
}
})
}
//Starting basic HTTPS server (socket.io server isn't started yet, it starts in StartServer)
var app = require('express')(); //launch express
var httpsServer = https.createServer(credentials, app);
httpsServer.listen(50000);
if(fs.existsSync("./config.json")){
config = JSON.parse(fs.readFileSync("./config.json"))
StartServer();
}else{
console.log("No 'config.json' file detected! Entering first time setup...");
AskUsedLanguage();
}
//Force https
app.use(function(req, res, next) {
if(!req.secure) {
return res.redirect(['https://', req.get('Host'), req.url].join(''));
}
next();
});
app.get("/", function(req, res){
res.sendFile(__dirname + "/index.html")
})
function StartServer(){
console.log("Using language '" + config.language + "' files")
var watcher = watch(".", {recursive: true})
var walkSync = function(dir, fileEnding, filelist) {
var path = path || require('path');
var fs = fs || require('fs'),
files = fs.readdirSync(dir);
filelist = filelist || [];
files.forEach(function(file) {
if (fs.statSync(path.join(dir, file)).isDirectory()) {
filelist = walkSync(path.join(dir, file), fileEnding, filelist);
}
else {
if(file.endsWith(fileEnding)){
filelist.push(path.join(dir, file));
}
}
});
return filelist;
};
function UploadFiles(){
var filesData = {}
var allFilePaths = walkSync("./", `.${config.language}`)
allFilePaths.forEach(function(filePath){
var fileName = path.basename(filePath)
if(!fileName.startsWith("TemporaryGeneratedFile_") && fileName !== "AssemblyInfo.cs"){
filesData[fileName] = fs.readFileSync(filePath, 'utf8')
}
})
io.emit("upLoadDir", filesData)
}
var currentId;
var enableFileWatcher = true;
watcher.on("change", function(event, filename){
if(enableFileWatcher && filename.endsWith(`.${config.language}`)){
//if the user is still connected
console.log("Detected file change, uploading to webkit...");
UploadFiles()
enableFileWatcher = false;
setTimeout(function(){
enableFileWatcher = true
}, 500)
}
})
var io = socket(httpsServer)
io.on("connection", function(socket){
//console.log("a user connected")
var madeAllowDownloadDecision = false;
var allowDownload = false;
currentId = socket.id
//emit to user the current dir
io.emit("showDirs", {dirs: ["Source (location of the .exe)"]})
//emit the config data
io.emit("GetConfig", config);
//if the web client has set a new language
socket.on("SetLanguage", function(data){
config.language = data;
fs.writeFileSync("./config.json", JSON.stringify(config), {flag: "w"})
console.log("Now using language '" + config.language + "' files")
})
//once the user confirms the dir he chose, we send it back to him
socket.on("dirWasSet", function(data){
//io.sockets.connected[currentId].emit("dirWasSet", {dirName: [JSON.parse(data).dirName]})
io.emit("dirWasSet", {dirName: [JSON.parse(data).dirName]})
})
//here the user chooses whether to upload or download the code, we handle these events
//user wants to upload files from the working station to the webkit
socket.on("upLoadDir", function(data){
if(!madeAllowDownloadDecision){
madeAllowDownloadDecision = true;
allowDownload = false;
}
UploadFiles()
})
//user wants to download files from the webkit to our working station
socket.on("downLoadDir", function(data){
if(!madeAllowDownloadDecision){
madeAllowDownloadDecision = true;
allowDownload = true;
}
//console.log(`got downLoadDir, allowDownload = ${allowDownload}`);
if(madeAllowDownloadDecision && allowDownload){
data = JSON.parse(data)
Object.keys(data).forEach(function(key) {
var val = data[key];
fs.appendFileSync("./" + key, val, {flag: "w"})
});
}
})
})
}