var fs = require('fs'); var express = require('express'); var http = require('http'); var app = express(); const cors = require('cors') const helmet = require('helmet'); var allowedOrigins = [ 'http://localhost:3080', 'http://localhost:3088', 'http://localhost:8085' ]; app.use(cors({ origin: function(origin, callback){ // allow requests with no origin // (like mobile apps or curl requests) if(!origin) return callback(null, true); if(allowedOrigins.indexOf(origin) === -1){ var msg = 'The CORS policy for this site does not '; return callback(new Error(msg), false); } return callback(null, true); } })); app.use( helmet({ frameguard: false, contentSecurityPolicy: false }) ); app.use(express.json()); var serverPort = 8085; var server = http.createServer(app); const { spawn, exec, execSync } = require('child_process') var static_options = { dotfiles: 'ignore', etag: false, extensions: ['html'], index: false, redirect: false, setHeaders: function (res, path, stat) { res.set('x-timestamp', Date.now()) } } app.use('/', express.static('client', static_options)) /* * ********************************************************** * create basic wg file server * ********************************************************** */ function wg_save_clientconf(res) { /* res.publickey res.endpoint res.serverip res.netmask res.privatekey res.allowedips res.interfaceip res.endpointfpath */ var endpointfpath = `${res.serverip}:${res.endpoint}` var peerconfig =` [Interface] PrivateKey = ${res.privatekey} ListenPort = 51820 [Peer] PublicKey = ${res.publickey} Endpoint = ${endpointfpath} AllowedIPs = ${res.allowedips} #AllowedIPs = 10.1.0.0/24, 0.0.0.0/0, ::/0 PersistentKeepalive = 30 ` try { fs.writeFileSync('./hybrid/connect/wg0.conf', peerconfig); return 'ok' } catch (error) { var datajson = error.toString() return datajson } } /* * ********************************************************** * create basic ifup linux * ********************************************************** */ function wg_save_ifup_linux(res) { var mypath = process.cwd() var DEV_NAME = res.dev_name var INTERFACE_IP = res.INTERFACE_IP var INTERFACE_IProute = res.INTERFACE_IProute /* res.publickey res.endpoint res.serverip res.netmask res.privatekey res.allowedips res.interfaceip res.endpointfpath */ try { const myExec = ` ip link set ${DEV_NAME} down ip link del ${DEV_NAME} sleep 1 ip link add ${DEV_NAME} type wireguard wg setconf ${DEV_NAME} /settingsclient/wg0.conf sleep 1 ip addr add ${INTERFACE_IP} dev ${DEV_NAME} sysctl -w net.ipv4.conf.all.rp_filter=2 ip link set mtu 1420 up dev ${DEV_NAME} ip link set up dev ${DEV_NAME} ip route add default via ${INTERFACE_IProute} metric 2 table 200 ip route show table 200 | grep -w ${INTERFACE_IProute} ip rule add table main suppress_prefixlength 0 ` fs.writeFileSync('./hybrid/connect/ifup_linux', myExec); const myExec1 = ` ip link set ${DEV_NAME} down ip link del ${DEV_NAME} ` fs.writeFileSync('./hybrid/connect/ifdown_linux', myExec1); return 'ok' } catch (error) { var datajson = error.toString() return datajson } } /* * ********************************************************** * create basic run docker * ********************************************************** */ function wg_save_connect_linux(res) { var mypath = process.cwd() var DEV_NAME = res.dev_name var INTERFACE_IP = res.INTERFACE_IP var INTERFACE_IProute = res.INTERFACE_IProute /* res.publickey res.endpoint res.serverip res.netmask res.privatekey res.allowedips res.interfaceip res.endpointfpath */ try { const myExec = ` docker exec -it swarmlabwg /bin/bash -c "ip link set swarmlab0 down; ip link del swarmlab0" sleep 1 docker stop swarmlabwg; docker container rm swarmlabwg sleep 1 docker pull hub.swarmlab.io:5480/venusclient:latest sleep 1 docker run -d \ --net=host \ --name=swarmlabwg \ --cap-add=NET_ADMIN \ --cap-add=SYS_MODULE \ -e PUID=1000 \ -e PGID=1000 \ -e TZ=Europe/Athens \ -p 51820:51820/udp \ -v ${mypath}/hybrid/connect:/settingsclient \ -v /lib/modules:/lib/modules \ --restart unless-stopped \ hub.swarmlab.io:5480/venus-alpine:latest ` //docker exec -it swarmlabwg /bin/bash -c "pm2-runtime start /app/swarmlab-venus/client_ecosystem.config.js" //hub.swarmlab.io:5480/venusclient:latest fs.writeFileSync('./hybrid/connect/swarmlabwg', myExec); fs.writeFileSync('./hybrid/connect/swarmlabstatus', 'wgstatusclient=off'); const myExec1 = ` docker exec -it swarmlabwg /bin/bash -c "ip link set swarmlab0 down; ip link del swarmlab0" sleep 1 docker stop swarmlabwg; docker container rm swarmlabwg sleep 1 ` fs.writeFileSync('./hybrid/connect/swarmlabwgdown', myExec1); return 'ok' } catch (error) { var datajson = error.toString() return datajson } } app.post('/hybrid_join_start', (req, res, next) => { var RES = new Object(); RES.publickey = req.body["publickey"] RES.endpoint = req.body["endpointport"] RES.serverip = req.body["serverip"] RES.netmask = req.body["netmask"] RES.privatekey = req.body["privatekey"] RES.allowedips = req.body["allowedips"] RES.interfaceip = req.body["peer_ip"] RES.endpointfpath = `${RES.serverip}:${RES.endpoint}` RES.dev_name = 'swarmlab0' RES.INTERFACE_IP = `${RES.interfaceip}/${RES.netmask}` RES.INTERFACE_IProute = `${RES.interfaceip}` try { // ***************************************** // *** save wg config file wg0.conf // ***************************************** RES.serverconf = wg_save_clientconf(RES) RES.serverrun = wg_save_ifup_linux(RES) RES.serverconnect = wg_save_connect_linux(RES) // ***************************************** // *** save wg ifup linux // ***************************************** RES.error = false RES.error_msg = "ok" RES.action = "ok" console.error('socket get from client' + JSON.stringify(RES)) res.json(RES) } catch (error) { var datajson = error.toString() RES.error = true RES.error_msg = datajson RES.action = "no" res.json(RES) } }); app.post('/hybrid_join_connect', (req, res, next) => { var RES = new Object(); RES.action = req.body["action"] try { if(RES.action == "connect"){ fs.writeFileSync('./hybrid/connect/swarmlabstatus', 'wgstatusclient=on'); var showexecrm = `/bin/sh ./hybrid/connect/swarmlabwg` execSync(showexecrm); var showexec = `docker ps --format '{"ID":"{{ .ID }}", "Image": "{{ .Image }}", "Names":"{{ .Names }}", "Ports":"{{.Ports}}", "Networks":"{{.Networks}}", "Status":"{{.Status}}","RunningFor":"{{.RunningFor}}","CreatedAt":"{{.CreatedAt}}"}' | jq . -s ` exec(showexec, (err, stdout, stderr) => { if (err) { console.error(`exec error: ${err}`); return; } var nn = [] var string = stdout.toString() var datajson = JSON.parse(string); var results = [] var filter = "swarmlabwg" var grep = new RegExp(filter); var datalenth = datajson.length for (var i=0 ; i < datalenth ; i++) { if(grep.test(datajson[i]['Names'])){ results.push(datajson[i]); } } RES.debug = datajson if(results){ RES.error = false RES.error_msg = "ok" RES.action = "execok" console.error('socket get from client ' + JSON.stringify(RES)) res.json(RES) }else{ RES.error = false RES.error_msg = "ok" RES.action = "execno" console.error('socket get from client ' + JSON.stringify(RES)) res.json(RES) } }); }else if( RES.action == "disconnect" ){ fs.writeFileSync('./hybrid/connect/swarmlabstatus', 'wgstatusclient=off'); var showexecrm1 = `/bin/sh ./hybrid/connect/swarmlabwgdown` execSync(showexecrm1); var showexec = `docker ps --format '{"ID":"{{ .ID }}", "Image": "{{ .Image }}", "Names":"{{ .Names }}", "Ports":"{{.Ports}}", "Networks":"{{.Networks}}", "Status":"{{.Status}}","RunningFor":"{{.RunningFor}}","CreatedAt":"{{.CreatedAt}}"}' | jq . -s ` exec(showexec, (err, stdout, stderr) => { if (err) { console.error(`exec error: ${err}`); return; } var nn = [] var string = stdout.toString() var datajson = JSON.parse(string); var results = [] var filter = "swarmlabwg" var grep = new RegExp(filter); var datalenth = datajson.length for (var i=0 ; i < datalenth ; i++) { if(grep.test(datajson[i]['Names'])){ results.push(datajson[i]); } } RES.debug = datajson if(results){ RES.error = false RES.error_msg = "ok" RES.action = "execok" console.error('socket get from client ' + JSON.stringify(RES)) res.json(RES) }else{ RES.error = false RES.error_msg = "ok" RES.action = "execno" console.error('socket get from client ' + JSON.stringify(RES)) res.json(RES) } }); } } catch (error) { var datajson = error.toString() RES.error = true RES.error_msg = datajson RES.action = "no" res.json(RES) } }); app.get('/getserviceshybridstatus', (req, res, next) => { var RES = new Object(); RES.bootstrapnameid = req.query["bootstrapnameid"] // swarmlab name swarmlabwg RES.bootstrapstackname = req.query["bootstrapstackname"] var showexec = `docker ps --format '{"Names":"{{ .Names }}", "Status":"{{.Status}}"}' | jq . -s ` exec(showexec, (err, stdout, stderr) => { if (err) { console.error(`exec error: ${err}`); return; } if (stdout) { var string = stdout.toString() var datajson = JSON.parse(string); var search = RES.bootstrapstackname var extenderror = new RegExp(search); var found = 'no'; for(var i = 0; i < datajson.length; i++) { var servicename = datajson[i].Names //console.log(JSON.stringify(search+' '+servicename)) if(extenderror.test(servicename)){ found = 'yes'; } } RES.error = false RES.error_msg = "ok" RES.test = datajson; RES.data = found; res.json(RES) }else{ var found = 'no' RES.error = false RES.error_msg = "ok" RES.data = found; res.json(RES) } }); }); server.listen(serverPort, function() { console.log('server up and running at %s port', serverPort); });