Commit 5fa82229 authored by Ahmad Nemati's avatar Ahmad Nemati

init

parent 86bb64cc
......@@ -3,6 +3,9 @@ const app = express()
const port = 3000
const shell = require('shelljs')
const cron = require('node-cron');
const cronSpeed = require('node-cron');
const shellExec = require('shell-exec')
var decimalPointRegex = /\-?\d+\.\d+/g;
app.get('/', (req, res) => {
......@@ -29,7 +32,7 @@ async function addNewIp(req, res) {
}
async function addBaship(ip) {
return await shell.exec(' ip addr add '+ip+' dev eth0').stderr;
return await shell.exec(' ip addr add ' + ip + ' dev eth0').stderr;
}
......@@ -38,7 +41,6 @@ app.listen(port, () => console.log(`Example app listening on port ${port}!`))
async function run() {
let a = await shell.exec('cd .. && cd mt && sudo ./server1.sh', {silent: true}).stdout;
// await shell.exec('iptables -A INPUT -s 95.156.255.148 -p ICMP --icmp-type 8 -j ACCEPT && iptables -A INPUT -p ICMP --icmp-type 8 -j DROP', {silent: true}).stdout;
console.log('Reset Proxy')
}
......@@ -53,3 +55,54 @@ cron.schedule('*/10 * * * * *', () => {
checkProcessWork()
});
async function calculateSpeed() {
let d = await shellExec('vnstat -tr')
d = d.stdout
d = d.match(decimalPointRegex)[0]
if (d.includes('kbit/s')) {
d[0] = 1
d[1] = 1
} else if (d.includes('Mbit/s')) {
} else if (d.includes('Gbit/s')) {
d[0] = d[0] * 1000
d[1] = d[1] * 1000
} else if (d.includes('bit/s')) {
d[0] = 1
d[1] = 1
}
await sendRate(d[0], d[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 * * * * *', () => {
return calculateSpeed()
});
......@@ -7,7 +7,9 @@
},
"dependencies": {
"express": "~4.16.0",
"shelljs": "^0.8.3",
"node-cron": "^2.0.3"
"node-cron": "^2.0.3",
"request": "^2.88.0",
"shell-exec": "^1.0.2",
"shelljs": "^0.8.3"
}
}
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