Commit aeac12d1 authored by Ahmad Nemati's avatar Ahmad Nemati

init

parent 79fcb898
let groups = 0
let generation = 1
const express = require('express')
const app = express()
const port = 3000
const shell = require('shelljs')
const cron = require('node-cron');
let Hetzner = require('./Hetzner')
const cronRemover = require('node-cron');
const cronRemover2 = require('node-cron');
let _ = require('lodash')
const request = require('request');
const fs = require('fs')
const cronSpeed = require('node-cron');
const shellExec = require('shell-exec')
var decimalPointRegex = /\-?\d+\.\d+/g;
let het
app.get('/', (req, res) => {
res.send('Hello World!')
run()
})
const cryptLib = require('@skavinvarnan/cryptlib');
const key = "6*sN_rZxHD4!X$=T";
app.get('/ip/:ip/', (req, res) => {
// console.log(req.params.acc)
return addNewIp(req, res)
})
app.get('/test', (req, res) => {
// console.log(req.params.acc)
res.sendfile('jetmtp.png')
})
app.listen(port, () => console.log(`Example app listening on port ${port}!`))
postRequest()
cronRemover.schedule(' */1 * * * *', () => {
init()
});
async function init() {
await addIp()
}
function postRequest() {
let data = {generation: generation, groups: groups}
data = JSON.stringify(data)
data = cryptLib.encryptPlainTextWithRandomIV(data, key)
var options = {
method: 'POST',
url: 'http://admin.fcfglobal.co:3000/addIp',
headers: {'content-type': 'application/x-www-form-urlencoded'},
body: 'data=' + Buffer.from(data).toString('base64')
};
return new Promise(function (resolve, reject) {
request(options, function (error, response, body) {
if (error)
console.log(error)
resolve(true)
});
});
}
function addIp() {
const request = require('request');
let url = 'http://admin.fcfglobal.co:3000/add/'
return new Promise(function (resolve, reject) {
request(url, {timeout: 15000}, function (error, res, body) {
resolve(body)
});
});
}
async function calculateSpeed() {
let d = await shellExec('vnstat -i ens3 -tr')
d = d.stdout
let arr = d.match(decimalPointRegex)
if (d.includes('kbit/s')) {
arr[0] = 1
arr[1] = 1
} else if (d.includes('Mbit/s')) {
} else if (d.includes('Gbit/s')) {
arr[0] = arr[0] * 1000
arr[1] = arr[1] * 1000
} else if (d.includes('bit/s')) {
arr[0] = 1
arr[1] = 1
}
await sendRate(arr[0], arr[1])
}
function sendRate(rx, tx) {
const request = require('request');
let url = 'http://admin.fcfglobal.co:3000/rate/' + rx + '/' + tx
return new Promise(function (resolve, reject) {
request(url, {timeout: 6000}, function (error, res, body) {
resolve(true)
});
});
}
cronSpeed.schedule(' */5 * * * * *', () => {
calculateSpeed()
});
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment