-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathatv.js
106 lines (94 loc) · 3.6 KB
/
atv.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
var webservers = require("./webserver");
var dns = require('./dns');
var ip = require("ip");
var logger = require('./logger');
var fs = require('fs');
var git = require('git-rev');
if(require.main === module)
start();
function startServers(data) {
try {
git.short(function (str) {
logger.notice('Starting Commit', str)
})
} catch(e) {
logger.error(e.stack || e);
}
try {
config = JSON.parse(data);
const LOCAL_IP = config.ip;
logger.notice("Starting PopcornTV");
dns.startDnsProxy(LOCAL_IP);
webservers.startWebServer(LOCAL_IP);
webservers.startSSLWebServer(LOCAL_IP);
} catch (err) {
logger.error('There is an error starting Popcorn TV, please post this on the Github page')
logger.error(err.stack || err);
process.exit();
}
}
function createCertificate(){
var pem = require('pem');
var path = require('path')
logger.notice('Creating SSL Certificates...');
var dir = path.join(__dirname, 'assets', 'certificates');
if (!fs.existsSync(dir)){
fs.mkdirSync(dir);
}
pem.createCertificate({days:7200, selfSigned:true, country: 'US', commonName: 'trailers.apple.com'}, function(err, keys){
try{
fs.writeFileSync(path.join(__dirname, 'assets', 'certificates', 'trailers.cer'), keys.certificate + '\n' + keys.serviceKey);
fs.writeFileSync(path.join(__dirname, 'assets', 'certificates', 'trailers.pem'), keys.certificate + '\n' + keys.serviceKey);
start();
} catch(e){
logger.warning('Unable to create certificates, generating them on the server, please wait...');
var http = require('https');
var cer = fs.createWriteStream(path.join(__dirname, 'assets', 'certificates', 'trailers.cer'));
var pem = fs.createWriteStream(path.join(__dirname, 'assets', 'certificates', 'trailers.pem'));
var request = http.get('', function(response) { // If you wish to use this, you must create your own file and website to generate the certificate.
response.pipe(cer);
response.pipe(pem);
cer.on('finish', function() {
start();
});
}).on('error', function(e){
logger.warning('Unable to create or download certificates, please report this bug on Github.')
logger.error(e.stack || e);
});
}
});
}
function start (){
if (!fs.existsSync(__dirname + '/assets/certificates/trailers.cer')){
createCertificate();
} else if (fs.existsSync(__dirname + '/config.json')) {
var data = fs.readFileSync(__dirname + '/config.json'), config;
startServers(data);
} else {
var myOptions = {
ip: ip.address(),
default_dns: '8.8.8.8'
};
var data = JSON.stringify(myOptions, null, 4);
fs.writeFile(__dirname + '/config.json', data, function(err) {
if (err) {
logger.error('There has been an error generating the Config, please report this error on the github page!');
logger.error(err.stack || err);
return;
}
var data = fs.readFileSync(__dirname + '/config.json'), config;
startServers(data);
});
}
}
function stop(){
dns.stop();
webservers.stop();
logger.notice('PopcornTV Stopped.');
}
// Proper uncaughException listener
process.addListener('uncaughtException', function (err) {
logger.error(err.stack || err);
});
module.exports.start = start;
module.exports.stop = stop;