-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
161 lines (132 loc) · 4.94 KB
/
index.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
const fs = require('fs');
const tar = require('tar-fs');
const http = require('http');
const https = require('https');
const httpServer = require("./httpserver.js");
const wsServer = require("./websocketserver.js");
const pjson = require('./package.json');
const cp = require('child_process');
const args = process.argv.slice(2);
const settings = require('./config.json');
for(let i in args){
if( args[i].indexOf("=") > -1 ){
const item = args[i].split("=")[0].replace("--", "");
const val = args[i].split("=")[1];
settings[item] = val;
}
}
wsServer.autoclose = settings.autoclose || false;
if(settings.browser == "edge") settings.browser = "msedge";
const port = settings.serverPort;
function createHttpServer(){
const server = http.createServer(function (req, res) {
if(req.url == "/ws"){
res.writeHead(426, {"Content-Type": 'text/plain'});
res.write('426 Upgrade Required'); //write a response to the client
res.end(); //end the response
}else{
httpServer.serve(req, res);
}
}).listen(port);
server.on('upgrade', function upgrade(request, socket, head) {
wsServer.handle(request, socket, head);
});
server.on('error', function(err){
console.log(err);
});
console.log("BurdIRC server is running on port " + port);
}
console.log("Checking for updates...");
function getUpdate(url,type){
const file = fs.createWriteStream("update.tar");
const request = https.get(url, function(response) {
response.pipe(file);
console.log("Extracting update file...");
setTimeout(function(){
fs.createReadStream('update.tar').pipe(tar.extract('./'));
console.log("Update complete!");
if(type == 2){
console.log("The app has been updated. You'll need to relaunch the it");
}else{
createHttpServer();
startGUI();
}
setTimeout(function(){
fs.unlink("update.tar", function(err){});
},1000);
},3000);
});
}
const upd = https.get('https://burdirc.haxed.net/updates.json', (resp) => {
let data = '';
resp.on('data', (chunk) => {
data += chunk;
});
resp.on('error', (err) => {
console.log("Error");
});
resp.on('end', () => {
try{
let json = JSON.parse(data);
if(json.version != pjson.version){
//update
console.log("Downloading update " + json.version + "...");
getUpdate(json.tarball, json.type);
}else{
console.log("Current version is the latest");
createHttpServer();
startGUI();
}
}catch(err){
console.log("Couldn't check for updates");
createHttpServer();
startGUI();
}
});
});
upd.on('error', (err) => {
console.log("Couldn't check for updates");
createHttpServer();
startGUI();
});
function startGUI(){
const start = (process.platform == 'darwin' ? 'open': process.platform == 'win32' ? 'start': 'xdg-open');
if(settings.appwindow){
if(process.platform == "win32"){
const browsers = [
"C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe",
process.env.USERPROFILE + "\\AppData\\Local\\Chrome\\Application\\chrome.exe",
process.env.USERPROFILE + "\\AppData\\Local\\Vivaldi\\Application\\vivaldi.exe",
"C:\\Users\\lueth\\AppData\\Local\\Vivaldi\\Application\\vivaldi.exe",
"C:\\Program Files\\BraveSoftware\\Brave-Browser\\Application\\brave.exe",
"C:\\Program Files\\Microsoft\\Edge\\Application\\msedge.exe",
"C:\\Program Files (x86)\\Microsoft\\Edge\\Application\\msedge.exe"
];
console.log("Looking for a browser to use...");
for(let i in browsers){
if(fs.existsSync(browsers[i])){
console.log("Found browser at " + browsers[i]);
cp.exec(start + " \"\" \"" + browsers[i] + "\" --app=http://localhost:" + port + "/index.html");
break;
}
}
}else{
if(fs.existsSync("/usr/bin/google-chrome")){
cp.exec("/usr/bin/google-chrome --app=http://localhost:" + port + "/index.html");
}else if(fs.existsSync("/usr/bin/chromium")){
cp.exec("/usr/bin/chromium --app=http://localhost:" + port + "/index.html");
}
}
/**
/usr/bin/google-chrome
/usr/bin/chromium
setTimeout(function(){
if(process.platform == "win32"){
cp.exec(start + " " + settings.browser + " --app=http://localhost:" + port + "/index.html");
}
},1000);
**/
}else{
console.log("Open the following URL in your web browser: http://localhost:" + port + "/");
}
}