From 8b25e52c87961982e14b47487a9214eaba60a8f4 Mon Sep 17 00:00:00 2001 From: zeus Date: Wed, 12 May 2021 02:11:34 +0300 Subject: [PATCH] alpine admin --- hybrid/scripts/check-wg-connection.sh | 98 +++++++++++++++ llo/connect-new.js | 171 +++++++++++++++++++++----- package.json | 1 + 3 files changed, 241 insertions(+), 29 deletions(-) create mode 100755 hybrid/scripts/check-wg-connection.sh diff --git a/hybrid/scripts/check-wg-connection.sh b/hybrid/scripts/check-wg-connection.sh new file mode 100755 index 0000000..eb8dc1b --- /dev/null +++ b/hybrid/scripts/check-wg-connection.sh @@ -0,0 +1,98 @@ +#!/bin/bash + + +for f in /sys/class/net/swlab*; do + if [ -L "$f" ]; then + #echo $f + f=$(basename $f) + f="${f#"${f%%[![:space:]]*}"}" + f="${f%"${f##*[![:space:]]}"}" + f=${f:5} + for fdir in ./hybrid/connect/$f*; do + if [[ -d "$fdir" && ! -L "$fdir" ]]; then + fdirfull=$fdir + fdirdir=$(basename $fdir) + fdir=$(basename $fdir) + fdir="${fdir#"${fdir%%[![:space:]]*}"}" + fdir="${fdir%"${fdir##*[![:space:]]}"}" + fdir=${fdir::10} + fdir="${fdir#"${fdir%%[![:space:]]*}"}" + #echo "$fdir = $f" + if [ "$fdir" = "$f" ]; then + #echo "----------------------" + wg='' + S="=" + while read -r name value + do + if [ "$name" = "AllowedIPs" ]; then + var=${value//\"/} + # remove leading whitespace characters + var="${var#"${var%%[![:space:]]*}"}" + # remove trailing whitespace characters + var="${var%"${var##*[![:space:]]}"}" + var=${var:1} + AllowedIPs="${var#"${var%%[![:space:]]*}"}" + #echo $privateKey + fi + if [ "$name" = "PublicKey" ]; then + var1=${value//\"/} + # remove leading whitespace characters + var1="${var1#"${var1%%[![:space:]]*}"}" + # remove trailing whitespace characters + var1="${var1%"${var1##*[![:space:]]}"}" + var1=${var1:1} + publickey="${var1#"${var1%%[![:space:]]*}"}" + #echo $publickey + fi + done < $fdirfull/wg0.conf + if [ ! -z $AllowedIPs ] && [ ! -z $publickey ]; then + #echo "$AllowedIPs = $publickey" + + + nextip(){ + IP=$1 + IP_HEX=$(printf '%.2X%.2X%.2X%.2X\n' `echo $IP | sed -e 's/\./ /g'`) + NEXT_IP_HEX=$(printf %.8X `echo $(( 0x$IP_HEX + 1 ))`) + NEXT_IP=$(printf '%d.%d.%d.%d\n' `echo $NEXT_IP_HEX | sed -r 's/(..)/0x\1 /g'`) + echo "$NEXT_IP" + } + + IP=$(echo $AllowedIPs | cut -d'/' -f1) + IP=$(nextip $IP) + #echo $IP + if ping -q -w 1 -c 1 $IP > /dev/null; then + setconfig=$(jq -c \ + --arg key0 'ip' --arg ip "${AllowedIPs}" \ + --arg key1 'publickey' --arg publickey $publickey \ + --arg key2 'stackid' --arg stackid $fdirdir \ + --arg key3 'connection' --arg connection 'yes' \ + '. | .[$key0]=$ip | .[$key1]=$publickey | .[$key2]=$stackid | .[$key3]=$connection' <<<'{}' + ) + else + setconfig=$(jq -c \ + --arg key0 'ip' --arg ip "${AllowedIPs}" \ + --arg key1 'publickey' --arg publickey $publickey \ + --arg key2 'stackid' --arg stackid $fdirdir \ + --arg key3 'connection' --arg connection 'no' \ + '. | .[$key0]=$ip | .[$key1]=$publickey | .[$key2]=$stackid | .[$key3]=$connection' <<<'{}' + ) + fi + + if [ -z "$JSON" ]; then + JSON=$setconfig + else + JSON=$JSON,$setconfig + fi + fi + fi + fi + done + fi +done + +echo [$JSON] + +#setconfig1=$(jq -c --arg key0 'interfaces' --arg interfaces "$JSON" \ +#'. | .[$key0]=$interfaces' <<<'{}' +#) +#echo $setconfig1 diff --git a/llo/connect-new.js b/llo/connect-new.js index aebac1e..a0e4a03 100644 --- a/llo/connect-new.js +++ b/llo/connect-new.js @@ -6,6 +6,7 @@ var http = require('http'); var app = express(); const cors = require('cors') const helmet = require('helmet'); +var MongoClient = require('mongodb').MongoClient; var allowedOrigins = [ 'http://localhost:3080', @@ -37,6 +38,14 @@ app.use(express.json()); var serverPort = 8085; var server = http.createServer(app); +const io = require("socket.io")(server, { + cors: { + origin: allowedOrigins, + //origin: [ "http://localhost:3088", "http://localhost:8085" ], + methods: ["GET", "POST"] + } +}); + const { spawn, exec, execSync } = require('child_process') var static_options = { @@ -79,8 +88,6 @@ function myexec(res) { } - - /* * ********************************************************** * create basic wg file server @@ -248,33 +255,6 @@ export NODE_PATH=$(npm root --quiet -g) try { var mongoserver = JSON.parse(fs.readFileSync('./hybrid/venus-stats/config.json', 'utf8')) - try { - var MongoClient = require('mongodb').MongoClient; - var mongourl = `mongodb://${mongoserver.mongo_user}:${mongoserver.mongo_pass}@${mongoserver.mongo_ip}:${mongoserver.mongo_port}`; - const OPTS = { - useNewUrlParser: true, - useUnifiedTopology: true - }; - - MongoClient.connect(mongourl, OPTS, function(err, db) { - if(err){ - console.log(err); - } else { - var dbo = db.db(mongoserver.mongo_db); - var limit = 2 - dbo.collection(mongoserver.mongo_coll).find({"stackid":res.bootstrapstackid},{sort:{_id:-1}}).limit(limit).toArray(function(err, result) { - if(err){ - console.log(err); - } else { - console.log(result); - db.close(); - } - }); - } - }); - } catch (err) { - console.error(err); - } const myExec = ` @@ -538,6 +518,139 @@ app.get('/getserviceshybridstatus', (req, res, next) => { }); +function myserverStatus(){ + var mongoserver = JSON.parse(fs.readFileSync('./hybrid/venus-stats/config.json', 'utf8')) + var mypath = process.cwd() + + var showexec = `./hybrid/scripts/check-wg-connection.sh` + log = spawn(showexec, { + shell: true, + cwd: mypath, + detached: false, + stdio: 'pipe' + }); + + log.stdout.on('data', function (data) { + var n = {} + n.data = data.toString() + n.data = n.data.replace(/(\r\n|\n|\r|\t)/g,""); + console.log(n); + console.log(n.data); + var interfaces = JSON.parse(n.data); + var datalenth = interfaces.length + for (var i=0 ; i < datalenth ; i++) + { + var intconnection = interfaces[i].connection; + var resprivatekey = interfaces[i].ip; + var respublickey = interfaces[i].publickey; + var bootstrapstackid = interfaces[i].stackid; + if(intconnection == 'no'){ + var nn = {} + nn.connection = interfaces[i].connection; + nn.AllowedIPs = interfaces[i].ip; + nn.publickey = interfaces[i].publickey; + nn.bootstrapstackid = interfaces[i].stackid; + console.log('no connection '+JSON.stringify(nn)); + } + } + }); + + log.stderr.on('data', function (data) { + var n = {} + n.data = data.toString() + console.log('error '+JSON.stringify(n)); + io.emit('message_err', n); + }); + + log.on('close', function (code) { + console.log('close '+JSON.stringify(code)); + var n = {} + n.data = code + io.emit('message_close', n); + }); + + +/* + try { + var mongourl = `mongodb://${mongoserver.mongo_user}:${mongoserver.mongo_pass}@${mongoserver.mongo_ip}:${mongoserver.mongo_port}`; + const OPTS = { + useNewUrlParser: true, + useUnifiedTopology: true + }; + MongoClient.connect(mongourl, OPTS, function(err, db) { + if(err){ + console.log(err); + } else { + var dbo = db.db(mongoserver.mongo_db); + var limit = 2 + dbo.collection(mongoserver.mongo_coll).find({"stackid":bootstrapstackid},{sort:{_id:-1}}).limit(limit).toArray(function(err, result) { + if(err){ + console.log(err); + } else { + console.log(JSON.stringify(result)); + //if("transferTx" in result[0]){ + if(result[0].log[0].peers[respublickey].transferTx && result.length == 2){ + if(result[0].log[0].peers[respublickey].transferTx == result[1].log[0].peers[respublickey].transferTx){ + console.log(JSON.stringify(result.length)); + console.log(JSON.stringify('-------- '+JSON.stringify(result[0].log[0].peers[respublickey].transferTx))); + console.log(JSON.stringify('-------- '+JSON.stringify(result[1].log[0].peers[respublickey].transferTx))); + } + } + db.close(); + } + }); + } + }); + } catch (err) { + console.error(err); + } + + + try { + var mongourl = `mongodb://${mongoserver.mongo_user}:${mongoserver.mongo_pass}@${mongoserver.mongo_ip}:${mongoserver.mongo_port}`; + const OPTS = { + useNewUrlParser: true, + useUnifiedTopology: true + }; + MongoClient.connect(mongourl, OPTS, function(err, db) { + if(err){ + console.log(err); + } else { + var dbo = db.db(mongoserver.mongo_db); + var limit = 2 + dbo.collection(mongoserver.mongo_coll).find({"stackid":res.bootstrapstackid},{sort:{_id:-1}}).limit(limit).toArray(function(err, result) { + if(err){ + console.log(err); + } else { + console.log(JSON.stringify(result)); + //if("transferTx" in result[0]){ + if(result[0].log[0].peers[res.publickey].transferTx && result.length == 2){ + console.log(JSON.stringify(result.length)); + console.log(JSON.stringify('-------- '+JSON.stringify(result[0].log[0].peers[res.publickey].transferTx))); + console.log(JSON.stringify('-------- '+JSON.stringify(result[1].log[0].peers[res.publickey].transferTx))); + } + db.close(); + } + }); + } + }); + } catch (err) { + console.error(err); + } +*/ +} +myserverStatus(); + +setInterval(function(){ + myserverStatus() +}, 30000) + +io.on('connection', function(socket) { + console.log('new connection'); + socket.on('start', (value) => { + console.log('start'+ value); + }); +}); server.listen(serverPort, function() { console.log('server up and running at %s port', serverPort); diff --git a/package.json b/package.json index df4d765..4c7a75f 100644 --- a/package.json +++ b/package.json @@ -16,6 +16,7 @@ "pm2": "^4.5.1", "read-last-lines": "^1.7.2", "simple-git": "^2.31.0", + "socket.io": "^4.1.0", "url-exist-sync": "^1.0.2", "utf-8-validate": "^5.0.3" }