Repository navigation
Expand file tree
/
Copy pathserver.js
More file actions
151 lines (130 loc) · 5.01 KB
/
Copy pathserver.js
File metadata and controls
151 lines (130 loc) · 5.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
const express = require('express')
const app = express()
app.use(express.json());
var shell = require('shelljs');
const path = require('path');
app.use(express.static(path.join("/home/chacho/RouterConfigWeb/routerConfigClient", 'build')));
const ROUTER_PASSWORD = "password"
let XARXA_CONFIGURABLE = ""
let PREFIX_XARXA = ""
const jwt = require('jsonwebtoken')
const netmask2CIDR = (netmask) => {return (netmask.split('.').map(Number).map(part => (part >>> 0).toString(2)).join('')).split('1').length -1}
const verifyJWT = (req,res,next)=>{
const token = req.headers["x-access-token"]
if(!token){
res.status(401).send("You have no token")
}
else{
jwt.verify(token,"jwtSecret",(err,decoded)=>{
if (err){
res.status(401).json({auth:false, message:"Failed to authenticate"})
}
else{
req.id = decoded.id;
next();
}
})
}
}
//LOGIN ENDPOINT
app.post("/login",(req,res)=>{
const password = req.body.password
if(password===ROUTER_PASSWORD){
const id = 0
const token = jwt.sign({id},"jwtSecret",{
expiresIn: 3600
})
res.json({auth: true, token: token})
}
else{
res.status(403).send('Wrong Password');
}
})
//ENDPOINTS DE CONFIGURACIÓ
app.get("/dhcp",verifyJWT,(req,res)=>{
const { stdout, stderr, code } = shell.exec('sshpass -p xarxes ssh xarxes@8.0.2.1;cat ~/etc/dhcp/dhcpd.conf | grep subnet;cat ~/etc/dhcp/dhcpd.conf | grep range', { silent: true })
if(code===0){
const stdoutArray = stdout.split(" ")
const ipXarxa = stdoutArray[1]
XARXA_CONFIGURABLE = ipXarxa
const mask = stdoutArray[3]
PREFIX_XARXA = netmask2CIDR(mask)
const iniciRang = stdoutArray[5]
const finalRang = stdoutArray[6].replace(/(\r\n|\n|\r)/gm, "").replace(";","")
const obj = {
ip:ipXarxa,
mask:mask,
inici:iniciRang,
final:finalRang
}
res.send(obj)
}
else{
res.status(409).send("Not able to get the config data")
}
})
app.post("/dhcp",verifyJWT,(req,res)=>{ //POTSER S'HAURIEN D'ELIMINAR LES IP TABLES ?
const ip = req.body.ip
const mask = req.body.mask
const inici = req.body.inici
const final = req.body.final
const contingutFitxerConfig = "ddns-update-style none;\noption domain-name 'example.org';\noption domain-name-servers ns1.example.org, ns2.example.org;\ndefault-lease-time 600;\nmax-lease-time 7200;\nauthoritative;\nlog-facility local7;\nsubnet "+ip+" netmask "+mask+" {\n\trange "+inici+" "+final+";\n\toption routers 8.0.1.1;\n}"
if (shell.exec('sshpass -p xarxes ssh xarxes@8.0.2.1;echo "'+contingutFitxerConfig+'" > ~/etc/dhcp/dhcpd.conf',{silent:true}).code !== 0){
res.status(409).send("Not able to execute the configuration command")
}
else{
res.send("Config succesfull")
}
})
app.get("/ipTables",verifyJWT,(req,res)=>{
const { stdout, stderr, code } = shell.exec('sshpass -p xarxes ssh xarxes@8.0.2.1;sudo iptables -t nat -L -v -n | grep SNAT', { silent: true })
if(code===0){
let stdoutArray = stdout.split("\n")
stdoutArray.pop
res.send({entrades:stdoutArray})
}
else {
res.status(409).send("Not able to get the config data")
}
})
app.post("/ipTables",verifyJWT,(req,res)=>{
const origen = req.body.origen
const desti = req.body.desti
const interficie = req.body.interficie //destí no té /xx
if (shell.exec('sshpass -p xarxes ssh xarxes@8.0.2.1;sudo iptables -t nat -A POSTROUTING -s ' +origen+'-o '+interficie+' -j SNAT --to-source '+desti,{silent:true}).code !== 0){
res.status(409).send("Not able to execute the configuration command")
}
else{
res.send("Config succesfull")
}
})
app.delete("/ipTables",verifyJWT,(req,res)=>{
if (shell.exec('sshpass -p xarxes ssh xarxes@8.0.2.1;sudo iptables -t nat -F',{silent:true}).code !== 0){
res.status(409).send("Not able to execute the configuration command")
}
else{
res.send("Config succesfull")
}
})
app.get("/forwarding",verifyJWT,(req,res)=>{
const { stdout, stderr, code } = shell.exec('sshpass -p xarxes ssh xarxes@8.0.2.1;sysctl net.ipv4.ip_forward', { silent: true })
if(code===0){
const state = stdout.split(" = ")[1].replace(/(\r\n|\n|\r)/gm, "")
res.send({state:state})
}
else{
res.status(409).send("Not able to get the config data")
}
})
app.post("/forwarding",verifyJWT,(req,res)=>{
if (shell.exec('sshpass -p xarxes ssh xarxes@8.0.2.1;sudo sysctl net.ipv4.ip_forward='+req.body.state,{silent:true}).code !== 0){
res.status(409).send("Not able to execute the configuration command")
}
else{
res.send("Config succesfull")
}
})
app.get('*', (req,res) => {
res.sendFile(path.join("/home/chacho/RouterConfigWeb/routerConfigClient", 'build/index.html'));
});
app.listen(5000, ()=>{console.log("server started on port 5000")})