Commit 22cc6e7c authored by Ahmad Nemati's avatar Ahmad Nemati

init

parent c19ed73f
Pipeline #140 canceled with stages
......@@ -9,21 +9,19 @@ const shellExec = require('shell-exec')
var decimalPointRegex = /\-?\d+\.\d+/g;
let fs = require('fs')
let port = 5
let routeIp=5
let countReboot = 4
let tryFail = 0
run()
app.get('/', (req, res) => {
res.send('Hello World!')
run()
})
app.get('/ip/:ip/', (req, res) => {
// console.log(req.params.acc)
return addNewIp(req, res)
})
app.get('/rst2', (req, res) => {
......@@ -31,26 +29,15 @@ app.get('/rst2', (req, res) => {
})
app.get('/test', (req, res) => {
// console.log(req.params.acc)
res.sendfile('jetmtp.png')
})
async function addNewIp(req, res) {
res.send('Hello World!')
console.log(req.params.ip)
addBaship(req.params.ip)
}
async function addBaship(ip) {
return await shell.exec(' ip addr add ' + ip + ' dev eth0').stderr;
}
async function reboot() {
// return await shell.exec(' sudo reboot ').stderr;
return await shell.exec(' sudo reboot ').stderr;
}
......@@ -59,19 +46,34 @@ app.listen(3000, () => console.log(`Example app listening on port 3000!`))
async function run() {
let whiteIp = await getWhiteIp()
doIptable(whiteIp)
//doIptable(whiteIp)
let portRes = await getPort()
portRes=parseInt(portRes)
if (portRes.route ==='0')
{
await sleep(1500)
return run()
}
routeIp=portRes.route
portRes=parseInt(portRes.port)
console.log(portRes)
if (portRes === 0 || portRes === 443 || portRes ===5)
if (portRes === 0 || portRes === 443 || portRes ===5 )
port = randomInt(4000, 60000)
else
port = parseInt(portRes)
let a = await shell.exec('cd .. && cd mt && sudo ./server1.sh ' + port, {silent: true}).stdout;
let command='sysctl net.ipv4.ip_forward=1 && '
command=command+'iptables -t nat -A PREROUTING -p tcp --dport '+portRes+' -j DNAT --to-destination '+routeIp+':443 && '
command=command+'iptables -t nat -A POSTROUTING -j MASQUERADE'
let a = await shell.exec(command, {silent: true}).stdout;
console.log('Reset Proxy')
}
async function sleep(millis) {
return new Promise(resolve => setTimeout(resolve, millis));
}
async function doIptable(whiteIp) {
let command = ''
for (let i = 0; i < whiteIp.length; i++) {
......@@ -103,22 +105,8 @@ async function execLast() {
return true
}
async function checkProcessWork() {
let a = await shell.exec('nc -vz 127.0.0.1 ' + port).stderr;
if (a.includes('succeeded') || a.includes('Connected')) {
tryFail = 0
return true
}
tryFail++
if (tryFail >= countReboot)
reboot()
run()
}
function randomInt(low, high) {
return Math.floor(Math.random() * (high - low) + low)
}
......@@ -175,10 +163,10 @@ function sendRate(rx, tx) {
function getPort() {
const request = require('request');
let url = 'http://23.88.33.146:3000/getPort'
let url = 'http://23.88.33.146:3000/getPortAdv'
return new Promise(function (resolve, reject) {
request(url, {timeout: 15000}, function (error, res, body) {
resolve(body)
resolve(JSON.parse(body))
});
});
......
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