2020-09-27 05:52:52 +00:00
|
|
|
const { Worker } = require('worker_threads');
|
|
|
|
module.exports = function(s,config,lang,app){
|
2021-11-15 18:39:01 +00:00
|
|
|
const fetch = require('node-fetch')
|
2020-09-27 05:52:52 +00:00
|
|
|
const { modifyConfiguration, getConfiguration } = require('./system/utils.js')(config)
|
2021-11-15 18:39:01 +00:00
|
|
|
let customerServerList = !!config.p2pServerList;
|
2020-09-27 05:52:52 +00:00
|
|
|
var runningWorker;
|
|
|
|
config.machineId = config.p2pApiKey + '' + config.p2pGroupId
|
|
|
|
config.p2pTargetAuth = config.p2pTargetAuth || s.gid(30)
|
2022-06-22 23:13:15 +00:00
|
|
|
config.p2pShellAccess = config.p2pShellAccess || false
|
2021-11-06 18:28:33 +00:00
|
|
|
if(!config.workerStreamOutHandlers){
|
|
|
|
config.workerStreamOutHandlers = [
|
|
|
|
'Base64',
|
|
|
|
'FLV',
|
|
|
|
'MP4',
|
|
|
|
];
|
|
|
|
}
|
2021-11-15 18:39:01 +00:00
|
|
|
if(!customerServerList){
|
|
|
|
config.p2pServerList = {
|
2022-03-30 23:20:53 +00:00
|
|
|
"vancouver-1-v2": {
|
2021-11-15 18:39:01 +00:00
|
|
|
name: 'Vancouver-1',
|
|
|
|
host: 'p2p-vancouver-1.shinobi.cloud',
|
2022-03-30 23:20:53 +00:00
|
|
|
v2: true,
|
|
|
|
p2pPort: '81',
|
|
|
|
webPort: '80',
|
|
|
|
chartPort: '82',
|
2021-11-15 18:39:01 +00:00
|
|
|
maxNetworkSpeed: {
|
|
|
|
up: 5000,
|
|
|
|
down: 5000,
|
|
|
|
shared: true
|
|
|
|
},
|
|
|
|
location: {
|
|
|
|
lat: 49.284966,
|
|
|
|
lon: -123.1140607
|
|
|
|
}
|
2021-04-14 03:49:26 +00:00
|
|
|
},
|
2022-03-30 23:20:53 +00:00
|
|
|
"toronto-1-v2": {
|
2021-11-15 18:39:01 +00:00
|
|
|
name: 'Toronto-1',
|
|
|
|
host: 'p2p-toronto-1.shinobi.cloud',
|
2022-03-30 23:20:53 +00:00
|
|
|
v2: true,
|
|
|
|
p2pPort: '81',
|
|
|
|
webPort: '80',
|
|
|
|
chartPort: '82',
|
2021-11-15 18:39:01 +00:00
|
|
|
maxNetworkSpeed: {
|
|
|
|
up: 5000,
|
|
|
|
down: 5000,
|
|
|
|
shared: true
|
|
|
|
},
|
|
|
|
location: {
|
|
|
|
lat: 43.644773,
|
|
|
|
lon: -79.3862837
|
|
|
|
}
|
2020-09-27 05:52:52 +00:00
|
|
|
},
|
2022-03-30 23:20:53 +00:00
|
|
|
"paris-1-v2": {
|
2021-11-15 18:39:01 +00:00
|
|
|
name: 'Paris-1',
|
|
|
|
host: 'p2p-paris-1.shinobi.cloud',
|
2022-03-30 23:20:53 +00:00
|
|
|
v2: true,
|
|
|
|
p2pPort: '81',
|
|
|
|
webPort: '80',
|
|
|
|
chartPort: '82',
|
2021-11-15 18:39:01 +00:00
|
|
|
maxNetworkSpeed: {
|
|
|
|
up: 200,
|
|
|
|
down: 200,
|
|
|
|
shared: true
|
|
|
|
},
|
|
|
|
location: {
|
|
|
|
lat: 48.873877,
|
|
|
|
lon: 2.295533
|
|
|
|
}
|
2020-10-03 04:27:29 +00:00
|
|
|
}
|
2021-11-15 18:39:01 +00:00
|
|
|
}
|
|
|
|
// get latest
|
|
|
|
fetch('https://cdn.shinobi.video/configs/p2pServers.js')
|
|
|
|
.then(res => res.text())
|
|
|
|
.then((text) => {
|
|
|
|
try{
|
|
|
|
eval(`config.p2pServerList = ` + text)
|
|
|
|
}catch(err){
|
|
|
|
s.debugLog(err)
|
|
|
|
}
|
|
|
|
});
|
2020-09-27 05:52:52 +00:00
|
|
|
}
|
|
|
|
if(!config.p2pHostSelected)config.p2pHostSelected = 'paris-1'
|
2022-06-07 23:47:28 +00:00
|
|
|
const p2pServerKeys = Object.keys(config.p2pServerList)
|
|
|
|
const filteredList = {}
|
|
|
|
p2pServerKeys.forEach((keyName) => {
|
|
|
|
const connector = config.p2pServerList[keyName]
|
|
|
|
if(connector.v2 === !!config.useBetterP2P){
|
|
|
|
filteredList[keyName] = connector;
|
|
|
|
}
|
|
|
|
})
|
|
|
|
config.p2pServerList = filteredList;
|
2020-09-27 05:52:52 +00:00
|
|
|
const stopWorker = () => {
|
|
|
|
if(runningWorker){
|
|
|
|
runningWorker.postMessage({
|
|
|
|
f: 'exit'
|
2020-09-24 04:21:03 +00:00
|
|
|
})
|
|
|
|
}
|
2020-09-27 05:52:52 +00:00
|
|
|
}
|
|
|
|
const startWorker = () => {
|
|
|
|
stopWorker()
|
|
|
|
// set the first parameter as a string.
|
2022-03-28 23:02:05 +00:00
|
|
|
const pathToWorkerScript = __dirname + `/commander/${config.useBetterP2P ? 'workerv2' : 'worker'}.js`
|
2020-09-27 05:52:52 +00:00
|
|
|
const workerProcess = new Worker(pathToWorkerScript)
|
|
|
|
workerProcess.on('message',function(data){
|
|
|
|
switch(data.f){
|
|
|
|
case'debugLog':
|
|
|
|
s.debugLog(...data.data)
|
|
|
|
break;
|
|
|
|
case'systemLog':
|
|
|
|
s.systemLog(...data.data)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
})
|
|
|
|
setTimeout(() => {
|
|
|
|
workerProcess.postMessage({
|
|
|
|
f: 'init',
|
|
|
|
config: config,
|
|
|
|
lang: lang
|
|
|
|
})
|
|
|
|
},2000)
|
|
|
|
// workerProcess is an Emitter.
|
|
|
|
// it also contains a direct handle to the `spawn` at `workerProcess.spawnProcess`
|
|
|
|
return workerProcess
|
|
|
|
}
|
|
|
|
const beginConnection = () => {
|
2020-09-24 04:21:03 +00:00
|
|
|
if(config.p2pTargetGroupId && config.p2pTargetUserId){
|
2020-09-27 05:52:52 +00:00
|
|
|
runningWorker = startWorker()
|
2020-09-24 04:21:03 +00:00
|
|
|
}else{
|
|
|
|
s.knexQuery({
|
|
|
|
action: "select",
|
|
|
|
columns: "ke,uid",
|
|
|
|
table: "Users",
|
|
|
|
where: [],
|
|
|
|
limit: 1
|
|
|
|
},(err,r) => {
|
|
|
|
const firstUser = r[0]
|
|
|
|
config.p2pTargetUserId = firstUser.uid
|
|
|
|
config.p2pTargetGroupId = firstUser.ke
|
2020-09-27 05:52:52 +00:00
|
|
|
runningWorker = startWorker()
|
2020-09-24 04:21:03 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2020-09-27 05:52:52 +00:00
|
|
|
if(config.p2pEnabled){
|
|
|
|
beginConnection()
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* API : Superuser : Log delete.
|
|
|
|
*/
|
|
|
|
app.post(config.webPaths.superApiPrefix+':auth/p2p/save', function (req,res){
|
|
|
|
s.superAuth(req.params,async (resp) => {
|
|
|
|
const response = {ok: true};
|
|
|
|
const form = s.getPostData(req,'data',true)
|
|
|
|
form.p2pEnabled = form.p2pEnabled === '1' ? true : false
|
|
|
|
config = Object.assign(config,form)
|
|
|
|
const currentConfig = await getConfiguration()
|
|
|
|
const configError = await modifyConfiguration(Object.assign(currentConfig,form))
|
|
|
|
if(configError)s.systemLog(configError)
|
|
|
|
setTimeout(() => {
|
|
|
|
if(form.p2pEnabled){
|
|
|
|
s.systemLog('Starting P2P')
|
|
|
|
beginConnection()
|
|
|
|
}else{
|
|
|
|
s.systemLog('Stopping P2P')
|
|
|
|
stopWorker()
|
|
|
|
}
|
|
|
|
},2000)
|
|
|
|
s.closeJsonResponse(res,response)
|
|
|
|
},res,req)
|
|
|
|
})
|
2020-09-14 06:49:52 +00:00
|
|
|
}
|