Browse Source

add venus-stats

master
zeus 4 years ago
parent
commit
569699d3fa
  1. 1
      .gitignore
  2. 4
      files/mongo/.env
  3. 4
      files/mongo/docker-compose.yml
  4. 27
      files/mongo/venus-stats.js
  5. 52
      files/mongo/wg-json
  6. 53
      llo/connect-new.js
  7. 127
      start-venus-stats

1
.gitignore

@ -3,6 +3,7 @@ node_modules
hybrid/connect/ hybrid/connect/
hybrid/keys.json hybrid/keys.json
venuslog/logs/ venuslog/logs/
hybrid/venus-stats/
venuslog/ venuslog/
venuslog/* venuslog/*
venuslog venuslog

4
files/mongo/.env

@ -2,3 +2,7 @@
MONGO_INITDB_ROOT_USERNAME=swarmlab MONGO_INITDB_ROOT_USERNAME=swarmlab
MONGO_INITDB_ROOT_PASSWORD=swarmlab MONGO_INITDB_ROOT_PASSWORD=swarmlab
MONGO_INITDB_DATABASE=venus MONGO_INITDB_DATABASE=venus
MONGO_VENUS_IP="127.0.0.1"
MONGO_VENUS_PORT=30001
MONGO_VENUS_COLLECTION=logs

4
files/mongo/docker-compose.yml

@ -10,13 +10,13 @@ services:
expose: expose:
- 27017 - 27017
ports: ports:
- 30001:27017 - ${MONGO_VENUS_PORT}:27017
networks: networks:
- netswlabmongovenusLocal - netswlabmongovenusLocal
volumes: volumes:
- swlabmongovenusData:/data/db - swlabmongovenusData:/data/db
- swlabmongovenusLog:/var/log/mongodb - swlabmongovenusLog:/var/log/mongodb
- ./mongod.conf:/etc/mongod.conf - ${MONGO_VENUS_CONF}:/etc/mongod.conf
env_file: env_file:
- .env - .env
environment: environment:

27
files/mongo/venus-stats.js

@ -22,8 +22,12 @@ function getArgs () {
} }
const args = getArgs(); const args = getArgs();
const { spawn, exec, execSync } = require('child_process')
//console.log(args) //console.log(args)
// #!/bin/bash
//logs=$("/usr/local/bin/wg-json")
//echo $logs
//node ./venus-stats.js --insert=yes --port=30001 --ip='127.0.0.1' --user=swarmlab --password=swarmlab --db=venus --coll=logs --json='{ "name" : "Hello", "log" : "World" }' //node ./venus-stats.js --insert=yes --port=30001 --ip='127.0.0.1' --user=swarmlab --password=swarmlab --db=venus --coll=logs --json='{ "name" : "Hello", "log" : "World" }'
if (args["insert"] == 'yes'){ if (args["insert"] == 'yes'){
@ -34,7 +38,26 @@ if (args["insert"] == 'yes'){
var collName = args["coll"] var collName = args["coll"]
var dbNamePort = args["port"] var dbNamePort = args["port"]
var dbNameIP = args["ip"] var dbNameIP = args["ip"]
var dbjson = JSON.parse(args["json"]) var dbdate = args["json"]
var showexec ='/usr/local/bin/wg-json';
try {
const stdout = execSync(showexec, {
env: {
NODE_ENV: "production",
},
});
var datajson = stdout.toString()
console.error('exec ' + JSON.stringify(datajson));
} catch (error) {
var datajson = error.toString()
console.error('error ' + JSON.stringify(datajson));
}
var RESlog = new Object();
RESlog.date = dbdate
RESlog.log = datajson
var MongoClient = require('mongodb').MongoClient; var MongoClient = require('mongodb').MongoClient;
var mongourl = `mongodb://${dbuser}:${dbpassword}@${dbNameIP}:${dbNamePort}`; var mongourl = `mongodb://${dbuser}:${dbpassword}@${dbNameIP}:${dbNamePort}`;
@ -58,7 +81,7 @@ if (args["insert"] == 'yes'){
}); });
} }
}); });
dbo.collection(collName).insertOne(dbjson, function(err, res) { dbo.collection(collName).insertOne(RESlog, function(err, res) {
if(err){ if(err){
console.log(err); console.log(err);
} else { } else {

52
files/mongo/wg-json

@ -0,0 +1,52 @@
#!/bin/bash
# SPDX-License-Identifier: GPL-2.0
#
# Copyright (C) 2015-2020 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
exec < <(exec wg show all dump)
printf '{'
while read -r -d $'\t' device; do
if [[ $device != "$last_device" ]]; then
[[ -z $last_device ]] && printf '\n' || printf '%s,\n' "$end"
last_device="$device"
read -r private_key public_key listen_port fwmark
printf '\t"%s": {' "$device"
delim=$'\n'
[[ $private_key == "(none)" ]] || { printf '%s\t\t"privateKey": "%s"' "$delim" "$private_key"; delim=$',\n'; }
[[ $public_key == "(none)" ]] || { printf '%s\t\t"publicKey": "%s"' "$delim" "$public_key"; delim=$',\n'; }
[[ $listen_port == "0" ]] || { printf '%s\t\t"listenPort": %u' "$delim" $(( $listen_port )); delim=$',\n'; }
[[ $fwmark == "off" ]] || { printf '%s\t\t"fwmark": %u' "$delim" $(( $fwmark )); delim=$',\n'; }
printf '%s\t\t"peers": {' "$delim"; end=$'\n\t\t}\n\t}'
delim=$'\n'
else
read -r public_key preshared_key endpoint allowed_ips latest_handshake transfer_rx transfer_tx persistent_keepalive
printf '%s\t\t\t"%s": {' "$delim" "$public_key"
delim=$'\n'
[[ $preshared_key == "(none)" ]] || { printf '%s\t\t\t\t"presharedKey": "%s"' "$delim" "$preshared_key"; delim=$',\n'; }
[[ $endpoint == "(none)" ]] || { printf '%s\t\t\t\t"endpoint": "%s"' "$delim" "$endpoint"; delim=$',\n'; }
[[ $latest_handshake == "0" ]] || { printf '%s\t\t\t\t"latestHandshake": %u' "$delim" $(( $latest_handshake )); delim=$',\n'; }
[[ $transfer_rx == "0" ]] || { printf '%s\t\t\t\t"transferRx": %u' "$delim" $(( $transfer_rx )); delim=$',\n'; }
[[ $transfer_tx == "0" ]] || { printf '%s\t\t\t\t"transferTx": %u' "$delim" $(( $transfer_tx )); delim=$',\n'; }
[[ $persistent_keepalive == "off" ]] || { printf '%s\t\t\t\t"persistentKeepalive": %u' "$delim" $(( $persistent_keepalive )); delim=$',\n'; }
printf '%s\t\t\t\t"allowedIps": [' "$delim"
delim=$'\n'
if [[ $allowed_ips != "(none)" ]]; then
old_ifs="$IFS"
IFS=,
for ip in $allowed_ips; do
printf '%s\t\t\t\t\t"%s"' "$delim" "$ip"
delim=$',\n'
done
IFS="$old_ifs"
delim=$'\n'
fi
printf '%s\t\t\t\t]' "$delim"
printf '\n\t\t\t}'
delim=$',\n'
fi
done
printf '%s\n' "$end"
printf '}\n'

53
llo/connect-new.js

@ -103,8 +103,24 @@ async function wg_save_clientconf(res) {
res.allowedips res.allowedips
res.interfaceip res.interfaceip
res.endpointfpath res.endpointfpath
*/
const { mkdir } = require('fs').promises;
(async function main() {
try {
const parent = '.';
const dirnames = ['pdfs', 'fonts', 'err/or', 'users'];
await Promise.all(
dirnames.map(dirname => mkdir(`${parent}/${dirname}`).catch(console.error))
);
// All dirs are created here or errors reported.
} catch (err) {
console.error(err);
}
})();
*/
const apptest = require('express')(); const apptest = require('express')();
const servertest = apptest.listen(0, () => { const servertest = apptest.listen(0, () => {
@ -120,6 +136,16 @@ const servertest = apptest.listen(0, () => {
RES.data = err RES.data = err
return RES return RES
}else{ }else{
fs.mkdir(`./hybrid/connect/${res.bootstrapstackid}/scripts/1`, { recursive: true }, (err) => {
if (err){
return err
}
})
fs.mkdir(`./hybrid/connect/${res.bootstrapstackid}/scripts/5`, { recursive: true }, (err) => {
if (err){
return err
}
})
var endpointfpath = `${res.serverip}:${res.endpoint}` var endpointfpath = `${res.serverip}:${res.endpoint}`
var peerconfig =` var peerconfig =`
@ -234,8 +260,12 @@ async function wg_save_connect_linux(res) {
res.allowedips res.allowedips
res.interfaceip res.interfaceip
res.endpointfpath res.endpointfpath
mongoconfig
{ "mongo_user": "swarmlab", "mongo_pass": "ukuGJTBJSfoDI", "mongo_db": "venus", "mongo_ip": "127.0.0.1", "mongo_port": "56623", "mongo_coll": "logs" }
export NODE_PATH=$(npm root --quiet -g)
*/ */
try { try {
var mongoserver = JSON.parse(fs.readFileSync('./hybrid/venus-stats/config.json', 'utf8'))
const myExec = ` const myExec = `
docker exec -it swarmlabwg-${res.bootstrapstackid} /bin/bash -c "ip link set ${DEV_NAME} down; ip link del ${DEV_NAME}" docker exec -it swarmlabwg-${res.bootstrapstackid} /bin/bash -c "ip link set ${DEV_NAME} down; ip link del ${DEV_NAME}"
sleep 1 sleep 1
@ -250,6 +280,17 @@ docker run -d \
--cap-add=SYS_MODULE \ --cap-add=SYS_MODULE \
-e PUID=1000 \ -e PUID=1000 \
-e PGID=1000 \ -e PGID=1000 \
-e NODE_PATH=/usr/local/lib/node_modules \
-e stackid=${res.bootstrapstackid} \
-e stackname=swarmlabwg-${res.bootstrapstackid} \
-e stackinterface=${res.privatekey} \
-e stackpeer=${res.publickey} \
-e mongo_user=${mongoserver.mongo_user} \
-e mongo_pass=${mongoserver.mongo_pass} \
-e mongo_db=${mongoserver.mongo_db} \
-e mongo_ip=${mongoserver.mongo_ip} \
-e mongo_port=${mongoserver.mongo_port} \
-e mongo_coll=${mongoserver.mongo_coll} \
-e TZ=Europe/Athens \ -e TZ=Europe/Athens \
-v ${mypath}/hybrid/connect/${res.bootstrapstackid}:/settingsclient \ -v ${mypath}/hybrid/connect/${res.bootstrapstackid}:/settingsclient \
-v /lib/modules:/lib/modules \ -v /lib/modules:/lib/modules \
@ -262,6 +303,16 @@ docker run -d \
fs.writeFileSync(`./hybrid/connect/${res.bootstrapstackid}/swarmlabwg`, myExec); fs.writeFileSync(`./hybrid/connect/${res.bootstrapstackid}/swarmlabwg`, myExec);
fs.writeFileSync(`./hybrid/connect/${res.bootstrapstackid}/swarmlabstatus`, 'wgstatusclient=off'); fs.writeFileSync(`./hybrid/connect/${res.bootstrapstackid}/swarmlabstatus`, 'wgstatusclient=off');
const myExecLog = `
#!/bin/bash
date=$(date '+%Y-%m-%d-%H:%M')
node /settingsclient/venus-stats.js --insert=yes --port=${mongoserver.mongo_port} --ip='127.0.0.1' --user=swarmlab --password=${mongoserver.mongo_pass} --db=${mongoserver.mongo_db} --coll=${mongoserver.mongo_coll} --json=$date
`
fs.writeFileSync(`./hybrid/connect/${res.bootstrapstackid}/scripts/1/logs`, myExecLog);
fs.chmodSync(`./hybrid/connect/${res.bootstrapstackid}/scripts/1/logs`, 0o755);
const myExec1 = ` const myExec1 = `
docker exec -it swarmlabwg-${res.bootstrapstackid} /bin/bash -c "ip link set ${DEV_NAME} down; ip link del ${DEV_NAME}" docker exec -it swarmlabwg-${res.bootstrapstackid} /bin/bash -c "ip link set ${DEV_NAME} down; ip link del ${DEV_NAME}"
sleep 1 sleep 1

127
start-venus-stats

@ -0,0 +1,127 @@
#!/bin/bash
SOURCE="${BASH_SOURCE[0]}"
while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink
TARGET="$(readlink "$SOURCE")"
if [[ $TARGET == /* ]]; then
#echo "SOURCE '$SOURCE' is an absolute symlink to '$TARGET'"
SOURCE="$TARGET"
else
DIR="$( dirname "$SOURCE" )"
#echo "SOURCE '$SOURCE' is a relative symlink to '$TARGET' (relative to '$DIR')"
SOURCE="$DIR/$TARGET" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located
fi
done
SRPATH="$( dirname "$SOURCE" )"
SFPATH="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
if [ "$SFPATH" != "$SRPATH" ]; then
RDIR=$SRPATH # relativ path directory
fi
cwdir=$PWD
wdir=$SFPATH
if [ "$cwdir" != "$wdir" ]; then
cd $wdir
fi
#
#files/mongo/
# docker-compose.yml
# .env
# logs
# mongod.conf
# package.json
# venus-stats.js
# wg-json
#
function EPHEMERAL_PORT() {
LOW_BOUND=49152
RANGE=16384
while true; do
CANDIDATE=$[$LOW_BOUND + ($RANDOM % $RANGE)]
(echo "" >/dev/tcp/127.0.0.1/${CANDIDATE}) >/dev/null 2>&1
if [ $? -ne 0 ]; then
echo $CANDIDATE
break
fi
done
}
if [ ! -d $wdir/hybrid/venus-stats ]; then
mkdir -p $wdir/hybrid/venus-stats
fi
cp -raf $wdir/files/mongo/wg-json $wdir/hybrid/venus-stats
cp -raf $wdir/files/mongo/mongod.conf $wdir/hybrid/venus-stats
cp -raf $wdir/files/mongo/docker-compose.yml $wdir/hybrid/venus-stats
cp -raf $wdir/files/mongo/files/mongo/venus-stats.js $wdir/hybrid/venus-stats
swuser=swarmlab
swpass=$(tr -dc A-Za-z0-9 </dev/urandom | head -c 13 ; echo '')
db=venus
conf=$wdir/hybrid/venus-stats/mongod.conf
port=$(EPHEMERAL_PORT)
coll=logs
ip='127.0.0.1'
cat << FOE > $wdir/hybrid/venus-stats/.env
#!/bin/bash
MONGO_INITDB_ROOT_USERNAME=$swuser
MONGO_INITDB_ROOT_PASSWORD=$swpass
MONGO_INITDB_DATABASE=$db
MONGO_VENUS_CONF=$conf
MONGO_VENUS_IP=$ip
MONGO_VENUS_PORT=$port
MONGO_VENUS_COLLECTION=$coll
FOE
cat << FOE > $wdir/hybrid/venus-stats/start
#!/bin/bash
cd $wdir/hybrid/venus-stats;
docker-compose down &&
docker-compose rm &&
docker-compose pull &&
docker-compose build --no-cache &&
docker-compose up -d --force-recreate
cd $wdir
FOE
chmod +x $wdir/hybrid/venus-stats/start
cat << FOE > $wdir/hybrid/venus-stats/stop
#!/bin/bash
cd $wdir/hybrid/venus-stats;
docker-compose down &&
docker-compose rm
cd $wdir
FOE
chmod +x $wdir/hybrid/venus-stats/stop
setconfig=$(jq \
--arg key0 'mongo_user' --arg mongo_user $swuser \
--arg key1 'mongo_pass' --arg mongo_pass $swpass \
--arg key2 'mongo_db' --arg mongo_db $db \
--arg key3 'mongo_ip' --arg mongo_ip $ip \
--arg key4 'mongo_port' --arg mongo_port $port \
--arg key5 'mongo_coll' --arg mongo_coll $coll \
'. | .[$key0]=$mongo_user | .[$key1]=$mongo_pass | .[$key2]=$mongo_db | .[$key3]=$mongo_ip | .[$key4]=$mongo_port | .[$key5]=$mongo_coll' <<<'{}'
)
echo $setconfig > $wdir/hybrid/venus-stats/config.json
Loading…
Cancel
Save