-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
180 lines (153 loc) · 5.85 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import fs from 'fs';
import util from 'util';
import child_process from 'child_process';
const exec = util.promisify(child_process.exec);
import dns from 'dns';
import os from 'os';
import { fileURLToPath } from 'url';
import { dirname } from 'path';
import colors from 'colors';
const _filename = fileURLToPath(import.meta.url);
const _dirname = dirname(_filename);
process.on('SIGHUP', async () => {
await destroyServer();
process.exit();
});
process.on('SIGINT', async () => {
await destroyServer();
process.exit();
});
process.on('uncaughtException', (error) => {
console.error(error);
pause();
});
const logo = `
██████╗ ███████╗ ███████╗████████╗██████╗ ███████╗ █████╗ ███╗ ███╗███████╗██████╗
██╔══██╗██╔════╝ ██╔════╝╚══██╔══╝██╔══██╗██╔════╝██╔══██╗████╗ ████║██╔════╝██╔══██╗
██████╔╝█████╗█████╗███████╗ ██║ ██████╔╝█████╗ ███████║██╔████╔██║█████╗ ██████╔╝
██╔══██╗██╔══╝╚════╝╚════██║ ██║ ██╔══██╗██╔══╝ ██╔══██║██║╚██╔╝██║██╔══╝ ██╔══██╗
██║ ██║███████╗ ███████║ ██║ ██║ ██║███████╗██║ ██║██║ ╚═╝ ██║███████╗██║ ██║
╚═╝ ╚═╝╚══════╝ ╚══════╝ ╚═╝ ╚═╝ ╚═╝╚══════╝╚═╝ ╚═╝╚═╝ ╚═╝╚══════╝╚═╝ ╚═╝
`;
const version = 'v1.0';
const by = 'By NightStranger\n';
let ip, conf;
main();
async function main() {
ip = await getIP();
printLogo();
if(setupConfig()) {
await destroyServer();
startServer();
}
}
async function startServer() {
try {
const { error, stdout, stderr } = exec('chcp 65001 && cd server && server.exe');
if (error) {
console.error(`error: ${error.message}`);
return;
}
if (stderr) {
console.error(`stderr: ${stderr}`);
return;
}
console.log(`Server started on "${ip}/live" (port: ${conf.port}). Stream key: "any"`.brightGreen);
console.log(`Set these settings in the OBS or another streaming program.`.brightCyan);
console.log(`You can also check if the server is running by following this link: http://${ip}:${conf.webport}.\n`.brightCyan);
console.log(`Restreaming to:`);
for(let i = 0; i < conf.streams.length; i++) {
let stream = conf.streams[i];
stream = stream.split("/")[2];
console.log(` ${stream}`);
}
} catch (err) {
pause();
console.log('Failed to start server:'.red);
console.log(err);
}
}
async function destroyServer() {
try {
const { error, stdout, stderr } = await exec('taskkill /f /im server.exe');
if (error) {
console.error(`error: ${error.message}`);
return;
}
if (stderr) {
console.error(`stderr: ${stderr}`);
return;
}
console.log(`Server is offline now`.red);
} catch (err) {
console.log(`No servers started.`);
pause();
}
}
function getIP() {
return new Promise((resolve, reject) => {
dns.lookup(os.hostname(), (err, add, fam) => {
resolve(add);
});
});
}
function setupConfig() {
try {
let config = loadConfig();
if(!config)
return false;
fs.writeFileSync('server/conf/nginx.conf', config);
return true;
} catch (err) {
console.log(`Failed to setup config: ${err}`.red);
pause();
}
}
function loadConfig() {
try {
let example = fs.readFileSync("conf.example");
let config;
if(!fs.existsSync("config.json")){
config = {
port: 1935,
webport: 80,
streams: [
"rtmp://a.rtmp.youtube.com/live2/YOUR_YOUTUBE_KEY",
"rtmp://live.twitch.tv/app/YOUR_TWITCH_KEY"
]
};
fs.writeFileSync("config.json", JSON.stringify(config, null, 4));
console.log(`Config file is created. Customize it for yourself and restart the program.`.brightCyan);
pause();
return false;
} else {
config = fs.readFileSync("config.json");
config = config.toString();
config = JSON.parse(config);
}
example = example.toString();
conf = config;
let push = '';
for(let i = 0; i < config.streams.length; i++) {
const stream = config.streams[i];
push += `push ${stream};\r`;
if(i != config.streams.length)
push += '';
}
example = example.replace("${port}", config.port);
example = example.replace("${webport}", config.webport);
example = example.replace("${push}", push);
return example;
} catch(err) {
console.log(`Failed to load config: ${err}`.red);
pause();
}
}
function printLogo() {
console.log(`\x1b[5m${logo}\x1b[0m`);
console.log(`${version}`.brightCyan);
console.log(`${by}`.brightMagenta);
}
function pause() {
setTimeout(() => {}, 1000000);
}