You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

375 lines
11 KiB

var pathmodule = require('path');
var app = require('express')();
var http = require('http').Server(app);
var https = require('https');
var io = require('socket.io')(http);
var chokidar = require("chokidar");
var logpath = "/var/lab/playground-serverlogs";
var watcher = chokidar.watch(logpath, {
ignored: /[\/\\]\./,
awaitWriteFinish: true,
persistent: true
});
var async = require("async");
const { check, validationResult } = require('express-validator');
const urlExistSync = require("url-exist-sync");
var express = require('express');
app.use(express.json());
const axios = require('axios');
axios.defaults.timeout = 30000
const helmet = require('helmet');
app.use(helmet());
const cors = require('cors')
const whitelist = [
'http://localhost:8080',
'http://localhost:3080',
'http://localhost:3081',
'http://localhost:3082'
]
const corsOptions = {
credentials: true,
methods: ['GET', 'PUT', 'POST', 'DELETE', 'OPTIONS'],
optionsSuccessStatus: 200, // some legacy browsers (IE11, various SmartTVs) choke on 204
allowedHeaders: [
'Content-Type',
'Authorization',
'X-Requested-With',
'device-remember-token',
'Access-Control-Allow-Origin',
'Access-Control-Allow-Headers',
'Origin',
'Accept'
],
origin: function(origin, callback) {
if (whitelist.indexOf(origin) !== -1) {
callback(null, true)
} else {
callback(null, true)
//callback(new Error('Not allowed by CORS'))
}
}
}
// ***************************************************
// checktoken
// ***************************************************
async function checkToken(token) {
const agent = new https.Agent({
rejectUnauthorized: false,
});
const instance = axios.create({
baseURL: 'https://api.swarmlab.io',
withCredentials: true,
rejectUnauthorized: false,
crossdomain: true,
httpsAgent: agent,
headers: {
'Accept': 'application/json',
'Content-Type': 'multipart/form-data',
'Authorization': 'Bearer '+token
}
})
try {
var pipelines = {
"source":'ssologin'
}
var params = {
pipeline: pipelines
}
var options = {
headers: { 'content-type': 'application/x-www-form-urlencoded',Authorization: `Bearer ${token}` },
};
instance.defaults.timeout = 30000;
const res = await instance.post('/istokenvalidsso',params,options);
if(res.status == 200){
return res.data
}else{
console.log("noerror: " + res)
return res.status
}
}
catch (err) {
console.error("error: "+err);
var error = new Object();
error.action = '401'
return error
}
}
function convertDateToUTC(date) {
return new Date(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate(), date.getUTCHours(), date.getUTCMinutes(), date.getUTCSeconds(),date.getUTCMilliseconds());
}
// ***************************************************
// get pipelines
// ***************************************************
async function getpipelines(token,pipelinename) {
const agent = new https.Agent({
rejectUnauthorized: false,
});
const instance = axios.create({
baseURL: 'https://api.swarmlab.io',
withCredentials: true,
rejectUnauthorized: false,
crossdomain: true,
httpsAgent: agent,
headers: {
'Accept': 'application/json',
'Content-Type': 'multipart/form-data',
'Authorization': 'Bearer '+token
}
})
/*
var params = {
playbook: value
}
var options = {
params: params,
headers: { 'content-type': 'application/x-www-form-urlencoded',Authorization: `Bearer ${token}` },
};
const playbook = await api.GET('playbookCode',options);
return playbook
*/
try {
var pipelines = {
"querytokenFilter":'uWr4FKRqrmpCRkJ9WLuI0DNuDWOGTkfcSzyZkJirZvJwwFDffLWrraqzzSPLeuQqL3TF9',
"filter":pipelinename
}
//var params = {
// pipeline: pipelines
// }
var params = {
querytokenFilter:'uWr4FKRqrmpCRkJ9WLuI0DNuDWOGTkfcSzyZkJirZvJwwFDffLWrraqzzSPLeuQqL3TF9',
filter:pipelinename
}
var options = {
params: params,
headers: { 'content-type': 'application/x-www-form-urlencoded',Authorization: `Bearer ${token}` },
};
//https://api.swarmlab.io/gettutorlabrooms?sort=pipelinename%7Casc&page=1&per_page=5&filter=&type=scripts&tutor=yes
instance.defaults.timeout = 30000;
//const res = await instance.get('/getplaygrounds',params,options);
const res = await instance.get('/getplaygrounds',options);
if(res.status == 200){
return res.data
}else{
console.log("noerror: " + res)
return res.status
}
}
catch (err) {
console.error("error: "+err);
var error = new Object();
error.action = '401'
return error
}
}
global.online='ob';
function onWatcherReady(){
console.info('From here can you check for real changes, the initial scan has been completed.');
}
// Declare the listeners of the watcher
watcher
.on('add', function(path) {
console.log('File', path, 'has been added');
var pathfile = pathmodule.basename(path);
console.log('Filepath ', pathfile, ' check');
// /var/lab/playground-serverlogs/ondemand_playgroundnodejs_masterJPMqt1Jr-RgpMZFwJ-log
(async() => {
var token = "d2539e5a7ae1f9f1b0eb2b8f22ca467a86d28407"; // desto
var resdata = await getpipelines(token,pathfile)
console.log('info', JSON.stringify(resdata));
/*
if(isvalid.action == 'ok'){
console.log("Authserver ok ", s.id + ' - ' + token);
global.SOCKET.user = isvalid.user
global.SOCKET.scope = isvalid.scope // space delimeter
global.SOCKET.token = isvalid.token
s.auth = true;
}else{
console.log("Authserver no ", s.id + ' - ' + token);
}
*/
})()
})
.on('addDir', function(path) {
console.log('Directory', path, 'has been added');
})
.on('change', function(path) {
console.log('File', path, 'has been changed');
})
.on('unlink', function(path) {
console.log('File', path, 'has been removed');
})
.on('unlinkDir', function(path) {
console.log('Directory', path, 'has been removed');
})
.on('error', function(error) {
console.log('Error happened', error);
})
.on('ready', onWatcherReady)
.on('raw', function(event, path, details) {
// This event should be triggered everytime something happens.
console.log('Raw event info:', event, path, details);
});
// ***************************************************
// rest get
// ***************************************************
app.get('/run', [
//check('access_token').isLength({ min: 40 }),
//check('llo').isBase64()
],
cors(corsOptions), (req, res, next) => {
var RES = new Object();
RES.code = req.query["code"]
console.error('socket GET from client ' + RES.code);
RES.error = false
RES.error_msg = "ok"
res.json(RES)
});
// ***************************************************
// rest post
// ***************************************************
app.post('/run', [
//check('access_token').isLength({ min: 40 }),
//check('llo').isBase64()
],
cors(corsOptions), (req, res, next) => {
//console.log(JSON.stringify(req.headers));
//console.log(JSON.stringify(req.body));
console.log("mongo "+JSON.stringify(req.body));
console.log("LOG "+JSON.stringify(req.body[0].message));
console.log("PATH "+JSON.stringify(req.body[0].tailed_path));
for (var i = 0; i < req.body.length; i++){
var obj = req.body[i];
var now = new Date();
var reslog = new Object();
reslog.log = obj
//var n = {}
//n.year = now.getFullYear();
//n.month = now.getMonth();
//n.day = now.getDay();
//n.hours = now.getHours();
//n.minutes = now.getMinutes();
//n.seconds = now.getSeconds();
//n.milliseconds = now.getMilliseconds();
//reslog.date = n
reslog.date = convertDateToUTC(now)
console.log(reslog);
io.emit("logdata", reslog);
/*
for (var key in obj){
var value = obj[key];
//console.log("line " + key + ": " + value);
reslog.key = key
reslog.value = value
//io.emit("logdata", reslog);
}
*/
}
//io.in("iot").emit("message", RES);
console.error('socket POST from client');
var RES = new Object();
RES.error = false
RES.error_msg = "ok"
RES.msg = req.body[0].messsage
res.json(RES)
});
// ***************************************************
// rocket
// ***************************************************
io.origins('*:*') // for latest version
io.on('connection', s => {
console.error('socket connection');
// ------------------------------
// --- set
// ------------------------------
var global = new Object();
global.SOCKET = {};
global.SOCKET.error = {};
console.error('socket ...');
s.auth = false;
// ------------------------------
// --- authenticate
// ------------------------------
s.on('authenticate', function(data){
const token = data
console.log('invalid 1 ' + token);
(async() => {
var isvalid = await checkToken(token);
if(isvalid.action == 'ok'){
console.log("Authserver ok ", s.id + ' - ' + token);
global.SOCKET.user = isvalid.user
global.SOCKET.scope = isvalid.scope // space delimeter
global.SOCKET.token = isvalid.token
s.auth = true;
}else{
console.log("Authserver no ", s.id + ' - ' + token);
s.auth = false;
}
})()
});
setTimeout(function(){
if (!s.auth) {
console.log("Disconnecting timeout socket ", s.id);
s.disconnect('unauthorized');
}
}, 30000);
var id = s.id
s.on('log', obj => {
console.error('from client '+ s.id + ' obj ' + obj);
});
});
http.listen(3000, () => console.error('listening on http://localhost:3000/'));
console.error('socket.io example');