-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
314 lines (267 loc) · 9.7 KB
/
app.js
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var index = require('./routes/index');
var users = require('./routes/users');
var dns = require('./routes/dns');
var ip = require('./routes/ip');
//var mongoose = require('mongoose');
//mongoose.connect('mongodb://localhost:27017/mikrotik'); // connect to our database
var api = require('mikronode');
var Router = require('./models/router').Router;
var User = require('./models/user').User;
var RouterController = require("./controllers/RouterController");
var async = require("async");
var Q = require('q');
require('q-foreach')(Q);
var router = express.Router(); // get an instance of the express Router
var app = express();
var cors = require('cors');
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use(cors());
app.use('/', index);
app.use('/users', users);
app.use('/dns', dns);
app.use('/ip', ip);
app.use('/api', router);
// catch 404 and forward to error handler
app.use(function (req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
// error handlers
// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
app.use(function (err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: err
});
});
}
// production error handler
// no stacktraces leaked to user
app.use(function (err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: {}
});
});
router.route('/users')
.post(function (req, res) {
var user = new User(); // create a new instance of the Router model
user.name = req.body.name; // set the routers name (comes from the request)
user.username = req.body.username;
user.password = req.body.password;
user.save(function (err) {
if (err)
res.end(err);
else
res.json({message: 'User created!'});
});
})
.get(function (req, res) {
User.find(function (err, routers) {
if (err)
res.end(err);
else
res.json(routers);
});
})
.delete(function (req, res) { //kjo eshte delete all nuk duhet perdor
User.remove(function (err, users) {
if (err)
res.send(err);
else
res.json(users);
});
})
.put(function (req, res) {
routerId = req.body.routerId;
id = req.body.id;
User.findById(id, function (err, user) {
if (err)
res.end(err);
else {
if (req.body.username)
user.username = req.body.username;
if (req.body.password)
user.password = req.body.password;
if (req.body.name)
user.name = req.body.name;
//user.routers.push({name: "kot"})
Router.findById(routerId, function (err, router) {
if (err)
res.end(err);
else {
user.routers.push(router);
user.save(function (err) {
if (err)
res.end(err);
else
res.json({message: 'User updated!'});
});
}
});
}
});
})
router.route('/routers')
.post(function (req, res) {
RouterController.createRouter(req, res);
})
.get(function (req, res) {
RouterController.getRouters(function (err, router) {
if (err) {
res.end(err);
} else {
res.json(router);
}
});
})
router.route('/routers/:id')
.delete(function (req, res) {
RouterController.removeById(req, res);
})
.get(function (req, res) {
RouterController.getRouterById(req, res);
})
.put(function (req, res) { //kjo poshte eshte ip
RouterController.updateByIp(req.params.id, req.body.address, req.body.username, req.body.password, req.body.name, function (err, data) {
if (err) {
res.end(err);
} else {
res.json(data);
}
});
})
router.route('/dns')
.post(function (req, res) {
//console.log(req.body.ip)
connect(req.body.ip, function (err, conn) {
var chan = conn.openChannel();
var arrDns = ['/ip/dns/set'];
if (req.body.servers) {
arrDns.push('=servers=' + req.body.servers);
}
console.log(" arrray eshte " + arrDns);
chan.write(arrDns, function () {
chan.on('done', function (data) {
console.log(data);
chan.close();
conn.close();
res.json(data);
});
});
});
})
.get(function (req, res) {
printDns(req.query.ip, function (err, data) {
if (err) {
res.end(err);
} else {
res.json(data);
}
});
})
router.route('/commands')
.post(function (req, res) {
var templateObj = {
'dynamic-servers': "",
'servers': "",
'allow-remote-requests': 'true',
'max-udp-packet-size': 4096,
'query-server-timeout': '2s',
'query-total-timeout': '10s',
'cache-size': 2048,
'cache-max-ttl': '1w',
'cache-used': 10
};
var routers = JSON.parse(req.body.selectedRouters);
var selectedRouters = [];
//console.log(routers)
var templateArray = [];
async.each(routers, function (router, callback1) {
printDns(router.ip, function (err, data) {
if (err) {
res.end(""+err);
} else {
var parsed = api.parseItems(data);
var templateObj2 = Object.assign({}, templateObj); //Object.assign() mbaje mend
async.each(parsed, function (item, callback2) {
templateObj2.servers = item.servers;
templateObj2.ip = router.ip;
callback2();
}, function (err) {
templateArray.push(templateObj2);
selectedRouters.push(router);
callback1();
});
}
});
}, function (err) {
console.log(templateArray);
res.render('dns', {templateObjs: templateArray, routers: selectedRouters});
});
})
module.exports = app;
function connect(ip, cb) {
//console.log(ip)
RouterController.getRouterByIp(ip, function (err, router) {
if (err) {
cb(err);
} else {
console.log("login to router");
console.log(router);
var connection = new api(router[0].ip, router[0].username, router[0].password);
connection.connect(function (conn) {
cb(null, conn);
});
connection.on('error', function (err) {
console.error('Error: ', err);
cb(err);
});
}
});
}
function printDns(ip, cb) {
connect(ip, function (err, conn) {
if (err) {
cb(err);
} else {
var chan = conn.openChannel();
chan.write('/ip/dns/print', function () {
chan.on('done', function (data) {
//console.log(data)
cb(null, data);
chan.close();
conn.close();
});
chan.once('trap', function (trap, chan) {
cb(new Error('komanda nuk u ekzekutua sakte ne ip: ' + ip));
console.log('Command failed: ' + trap);
});
chan.once('error', function (err, chan) {
cb(err);
console.log('Oops: ' + err);
});
});
}
});
}