-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathserver.js
executable file
·742 lines (600 loc) · 17.9 KB
/
server.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
#!/usr/bin/env node
// generate code in /tmp folder
var home_f = '/tmp/'
var args = process.argv.slice(2)
var port = args[0] || 8888
var yang_modules_dir = __dirname + "/public/yangs/"
var leveldb_dir = __dirname + "/db/"
var pyang_import_path = __dirname + "/ietf-yangs/"
var express = require('express')
var bodyParser = require('body-parser')
var fs = require('fs')
var nodemailer = require('nodemailer')
var validator = require('validator')
var exec = require('child_process').execFile
var compressor = require('node-minify')
var compress = require('compression')
var multer = require('multer')
var upload = multer({dest: './uploads/'})
var fse = require('fs-extra')
var yang_parser = require('yang-parser')
var YangFactory = require('./yang')
YangFactory.setup(leveldb_dir)
var logging = function (action, user) {
console.log(action, user, new Date())
}
var app = express()
app.use(compress())
app.use(bodyParser.urlencoded({
extended: false
}))
app.use(express.static(__dirname + '/public', {
maxAge: 25920000000
}))
console.log("minifying css/js files")
new compressor.minify({
type: 'clean-css',
fileIn: [
__dirname + '/css/bootstrap.css',
__dirname + '/css/bootstrap-theme.css',
__dirname + '/css/main.css'
],
fileOut: __dirname + '/public/main.css',
callback: function(error, min) {
if (error) throw error
console.log('css minified')
}
})
console.log("preparing config")
try {
fs.statSync('./config.js')
}
catch (e) {
console.log('config.js not found. creating it')
fs.createReadStream('config.js.example').pipe(fs.createWriteStream('config.js'))
}
var config = require('./config')
var transporter = nodemailer.createTransport(config.mail_options)
var tmpConfFileName = __dirname + '/public_config.js.tmp'
fs.writeFileSync(tmpConfFileName, 'var public_config = { "export_to_email": ' + config.export_to_email + ' }')
new compressor.minify({
type: 'uglifyjs',
fileIn: [
__dirname + '/js/jquery.js',
__dirname + '/js/sha256.js',
__dirname + '/js/ZeroClipboard.min.js',
__dirname + '/js/bootstrap.js',
__dirname + '/js/json2.js',
__dirname + '/js/jstorage.js',
__dirname + '/js/jquery.form.min.js',
__dirname + '/js/main.js',
__dirname + '/js/yang.js',
tmpConfFileName,
__dirname + '/js/editor.js'
],
fileOut: __dirname + '/public/main.js',
callback: function(error, _m) {
fs.unlinkSync(tmpConfFileName)
if (error) throw error
console.log('js minified')
}
})
var levelup = require('level')
/*
* unified function for writing json response
*
* @res - response object itself
* @error - error message
* @data - object or string with response data
*/
var response = function(res, error, data) {
var resp = {}
resp.error = typeof error === 'undefined' ? '' : error
resp.data = typeof data === 'undefined' ? '' : data
res.json(resp)
res.end()
}
/*
* function that we use to get 'uniq' user id
*/
function uniq_n(email, userpass_hash) {
return email + "_" + userpass_hash
}
/*
* validate username, password (SHA256 hash)
*
* @username - username of the user
* @userpass_hash - sha256 has of the user
*/
function user_is_valid(email, userpass_hash) {
if (!validator.isEmail(email) ||
!userpass_hash ||
userpass_hash.length !== 64 ||
new RegExp(/^[A-z0-9]{64}$/).test(userpass_hash) === false) {
return false
}
return true
}
/*
* check yang module name if matches identifier
* https://tools.ietf.org/html/rfc6020#page-163
*
* client side and pyang will handle yang validation in more detail
*
* @identifier - yang identifier - mostly yang module name
*/
function identifier_valid(identifier) {
if (!identifier)
return false
return new RegExp(/([A-z]|'_')+([A-z0-9]|\_|\-|\.)*/).test(identifier)
}
function get_dir_name(email, userpass_hash) {
var dir_name = uniq_n(email, userpass_hash)
if (dir_name.length > 254)
dir_name = dir_name.substr(0, 254)
dir_name = yang_modules_dir + dir_name
dir_name += "/"
return dir_name
}
/*
* save yang module content to database for user
*/
var rmdir = require('rimraf')
var AdmZip = require('adm-zip')
app.post('/register/:email/:userpass_hash', function(req, res) {
var userpass_hash = req.params.userpass_hash
var email = req.params.email
var Yang = YangFactory.createYang(email, userpass_hash)
Yang.register().then(function() {
response(res)
}, function(error) {
console.error(error)
response(res)
})
})
app.get("/backup/:email/:userpass_hash", function(req, res) {
var userpass_hash = req.params.userpass_hash
var email = req.params.email
var key = uniq_n(email, userpass_hash)
var db = levelup(leveldb_dir + uniq_n(email, userpass_hash), function(error, db) {
if (error) {
logging('unable to create backup', email)
return
}
var zip = new AdmZip()
db.createReadStream().on('data', function(entry) {
zip.addFile(entry.key, new Buffer(entry.value))
}).on('close', function() {
db.close()
var willSendThis = zip.toBuffer()
var filename = 'yang_creator_' + new Date().toISOString() + '.zip'
res.writeHead(200, {
'Content-Type': 'application/octet-stream',
'Content-Length': willSendThis.length,
'Content-Disposition': 'attachment; filename=' + filename
})
res.write(willSendThis)
res.end()
})
})
})
app.get("/yang/reset/:email/:userpass_hash", function(req, res) {
var userpass_hash = req.params.userpass_hash
var email = req.params.email
var Yang = YangFactory.createYang(email, userpass_hash)
logging('reseting users yang database', email)
Yang.reset_db().then(function() {
response(res)
}, function(error) {
console.error(error)
response(res)
})
})
app.put("/yang/:email/:userpass_hash/:yang_module_name", function(req, res) {
var userpass_hash = req.params.userpass_hash
var email = req.params.email
var yang_module_name = req.params.yang_module_name
var yang_module_content = req.body.yang_module_content
if (!user_is_valid(email, userpass_hash))
return response(res, "invalid username or password")
var Yang = YangFactory.createYang(email, userpass_hash)
Yang.update(yang_module_name, yang_module_content).then(function() {
return response(res)
}, function(error) {
return response(res, error.toString())
})
})
/*
* get all yang models for user
*/
app.get("/yang/:email/:userpass_hash", function(req, res) {
var userpass_hash = req.params.userpass_hash
var email = req.params.email
var Yang = YangFactory.createYang(email, userpass_hash)
Yang.all_names().then(function(names) {
response(res, null, names)
}, function(error) {
response(res, error, error)
})
})
/*
get yang module content from database for user
*/
app.get("/yang/:email/:userpass_hash/:yang_module_name", function(req, res) {
var userpass_hash = req.params.userpass_hash
var email = req.params.email
var yang_module_name = req.params.yang_module_name
if (!user_is_valid(email, userpass_hash))
return response(res, "invalid username or password")
var Yang = YangFactory.createYang(email, userpass_hash)
Yang.find(yang_module_name).then(function(yang_module) {
response(res, null, JSON.stringify(yang_module))
}, function(error) {
response(res, error.toString())
})
})
/*
* delete stored yang module from database for user by module name
*/
app.delete("/yang/:email/:userpass_hash/:yang_module_name", function(req, res) {
var userpass_hash = req.params.userpass_hash
var email = req.params.email
var yang_module_name = req.params.yang_module_name
if (!user_is_valid(email, userpass_hash))
return response(res, "invalid username or password")
var Yang = YangFactory.createYang(email, userpass_hash)
Yang.delete(yang_module_name).then(function() {
return response(res)
}, function(error) {
return response(error.toString())
})
})
/*
* create and validate yang module using pyang
*
* file name must match yang module name
* we create directory per user and save modules there
*
* @output: specifies if we should return generated yang module
*/
app.post("/yang_validate/:output?", function(req, res) {
var userpass_hash = req.body.userpass_hash
var email = req.body.email
var yang_module_name = req.body.yang_module_name
var yang_module_content = req.body.yang_module_content
var output = req.params.output
if (!user_is_valid(email, userpass_hash))
return response(res, "invalid username or password")
if (!identifier_valid(yang_module_name))
return response(res, "invalid YANG module name")
logging("validating yang module: " + yang_module_name, email)
/* max common dir length is 255 chars */
var dir_name = get_dir_name(email, userpass_hash)
var file_name = yang_module_name
write_file(dir_name, file_name, yang_module_content, function(error) {
if (error) {
console.error("error when writing yang files for user: %s", email)
console.error(error)
return response(res, error)
}
function validate() {
var error;
var yang = require("libyang")
yang.ly_verb(yang.LY_LLWRN);
var ctx = yang.ly_ctx_new(pyang_import_path);
var module = yang.lys_parse_mem(ctx, yang_module_content, yang.LYS_IN_YANG);
error = yang.ly_errmsg();
yang.ly_ctx_destroy(ctx)
// errmsg can have old logs, with (module == null) check success of parsing
if (module == null)
return error
else
return
}
error = validate()
var data = {
"error": error
}
output && (data.yang = yang_module_content)
response(res, (error && error.killed) ? error : '', data)
/* overwrite current yang file with validated */
write_file(dir_name, file_name, yang_module_content)
})
})
app.post("/email/:module_name", function(req, res) {
var userpass_hash = req.body.userpass_hash
var email = req.body.email
var yang_module_name = req.params.module_name
logging('sending yang module: ' + yang_module_name)
if (!user_is_valid(email, userpass_hash))
return response(res, "invalid username or password")
if (!identifier_valid(yang_module_name))
return response(res, "invalid YANG module name")
if (!config.export_to_email) {
var data = {
"error": "export to email is not enabled"
}
return response(res, '', data)
}
var dir_name = get_dir_name(email, userpass_hash)
var file_path = dir_name + yang_module_name
var yang_module_body = fs.readFileSync(file_path)
console.log(yang_module_body)
var mailOpts = {
from: config.mail_options.from,
to: email,
subject: yang_module_name,
text: yang_module_body
}
transporter.sendMail(mailOpts, function(error, info) {
var data = {
"error": error
}
if (!error) {
data.data = {
"info": "Yang module sent"
}
}
response(res, error, data)
})
})
/*
* download existing yang file from server
*/
app.get("/file/:email/:userpass_hash/:yang_module_name?", function(req, res) {
var email = req.params.email
var userpass_hash = req.params.userpass_hash
var yang_module_name = req.params.yang_module_name
if (!user_is_valid(email, userpass_hash))
return response(res, "invalid username or password")
if (!identifier_valid(yang_module_name))
return response(res, "invalid YANG module name")
var dir_name = get_dir_name(email, userpass_hash)
var file_path = dir_name + yang_module_name
fs.readFile(file_path, "binary", function(error, file) {
if (error) {
res.writeHead(error.code === 'ENOENT' ? 404 : 500, {
"Content-Type": "text/plain"
})
res.write(error + "\n")
res.end()
} else {
res.writeHead(200, {
'Content-Type': 'application/octet-stream'
})
res.write(file, "binary")
res.end()
}
})
})
/*
* generate source code
*/
function copy_yang_files(yang_dir, userpass_hash, email,key, callback) {
var buffer = []
var j=0
var db = levelup(leveldb_dir + uniq_n(email, userpass_hash), function(error, db) {
if (error) {
logging('unable create yin files from database', email)
return
}
db.createReadStream().on('data', function(entry) {
var path = yang_dir + entry.key
j++
buffer[j] = new Buffer(entry.value);
var k = j
fs.open(path, 'w', function(err, fd) {
if (err) {
throw 'error opening file: ' + err;
} else {
fs.write(fd, buffer[k], 0, buffer[k].length, null, function(err) {
if (err) throw 'error writing file: ' + err;
fs.close(fd, function() {})
});
}
});
}).on('close', function() {
db.close()
callback()
})
})
}
app.get("/file/yang2src/:email/:userpass_hash/:yang_module_name?", function(req, res) {
var email = req.params.email
var userpass_hash = req.params.userpass_hash
var yang_module_name = req.params.yang_module_name
var key = uniq_n(email, userpass_hash)
var tmp_dir = home_f + email + userpass_hash
var gen_name = 'source_code'
if (!fs.existsSync(tmp_dir)) {
fs.mkdirSync(tmp_dir)
} else {
fse.removeSync(tmp_dir)
fs.mkdirSync(tmp_dir)
}
//var yang_name = yang_module_name.split('.yang')[0].split('@')[0].replace(/-/g,'_')
var yang_name = yang_module_name
fs.mkdirSync(tmp_dir + '/' + gen_name)
tmp_dir = tmp_dir + '/' + gen_name
fs.mkdirSync(tmp_dir + '/ietf-yang')
if (!user_is_valid(email, userpass_hash))
return response(res, "invalid username or password")
if (!identifier_valid(yang_module_name))
return response(res, "invalid YANG module name")
var dir_name = get_dir_name(email, userpass_hash)
var file_path = dir_name + yang_module_name
var myCallback = function() {
fs.readFile(file_path, "binary", function(error, file) {
if (error) {
res.writeHead(error.code === 'ENOENT' ? 404 : 500, {
"Content-Type": "text/plain"
})
res.write(error + "\n")
res.end()
} else {
fs.writeFileSync(tmp_dir + '/ietf-yang/' + yang_module_name, file)
var yang2src = require('./yang2src/code_gen.js')
var myCallback_2 = function() {
var EasyZip = require('easy-zip').EasyZip;
var zip5 = new EasyZip();
zip5.zipFolder(tmp_dir, function(error, data){
if (error) {
res.writeHead(error.code === 'ENOENT' ? 404 : 500, {
"Content-Type": "text/plain"
})
res.write(error + "\n")
res.end()
} else {
zip5.writeToResponse(res, gen_name, function(error, data){
if (error) {
res.writeHead(error.code === 'ENOENT' ? 404 : 500, {
"Content-Type": "text/plain"
})
res.write(error + "\n")
res.end()
} else {
res.end()
}
});
}
})
}
try {
yang2src.generate(yang_module_name, "C", tmp_dir, myCallback_2)
} catch (e) {
// TODO, write a response
//return response(res, "invalid YIN model")
return
}
// delete generated files
if (fs.existsSync(tmp_dir)) {
fse.removeSync(tmp_dir)
}
}
})
}
copy_yang_files(tmp_dir + '/ietf-yang/', userpass_hash, email, key, myCallback)
})
app.get("/file/yang2src_all/:email/:userpass_hash/:yang_module_name?", function(req, res) {
var email = req.params.email
var userpass_hash = req.params.userpass_hash
var key = uniq_n(email, userpass_hash)
var tmp_dir = home_f + email + userpass_hash
var gen_name = 'source_code'
if (!fs.existsSync(tmp_dir)) {
fs.mkdirSync(tmp_dir)
} else {
fse.removeSync(tmp_dir)
fs.mkdirSync(tmp_dir)
}
fs.mkdirSync(tmp_dir + '/' + gen_name)
tmp_dir = tmp_dir + '/' + gen_name
fs.mkdirSync(tmp_dir + '/ietf-yang')
if (!user_is_valid(email, userpass_hash))
return response(res, "invalid username or password")
var dir_name = get_dir_name(email, userpass_hash)
var yang2src = require('./yang2src/code_gen.js')
var myCallback_2 = function() {
var EasyZip = require('easy-zip').EasyZip;
var zip5 = new EasyZip();
zip5.zipFolder(tmp_dir, function(error, data){
if (error) {
res.writeHead(error.code === 'ENOENT' ? 404 : 500, {
"Content-Type": "text/plain"
})
res.write(error + "\n")
res.end()
} else {
zip5.writeToResponse(res, gen_name, function(error, data){
if (error) {
res.writeHead(error.code === 'ENOENT' ? 404 : 500, {
"Content-Type": "text/plain"
})
res.write(error + "\n")
res.end()
} else {
res.end()
}
});
}
})
}
var myCallback = function() {
var yang = require("libyang")
var ctx = yang.ly_ctx_new(tmp_dir + "/ietf-yang");
try { var files = fs.readdirSync(tmp_dir + '/ietf-yang'); }
catch(e) { return; }
if (files.length > 0)
var totalCalls = files.length
for (var i = 0; i < files.length; i++) {
(function(i) {
var yang_file = tmp_dir + '/ietf-yang/' + files[i]
var module = yang.lys_parse_path(ctx, yang_file, yang.LYS_IN_YANG);
})(i);
}
yang.ly_ctx_destroy(ctx)
try {
yang2src.generate("/", "C", tmp_dir, myCallback_2)
} catch (e) {
// TODO, write a response
//return response(res, "invalid YIN model")
return
}
// delete generated files
if (fs.existsSync(tmp_dir)) {
fse.removeSync(tmp_dir)
}
}
copy_yang_files(tmp_dir + '/ietf-yang/', userpass_hash, email, key, myCallback)
})
/*
* upload yang file
*/
app.post("/file/", upload.single('import_file'), function(req, res) {
logging('uploading yang model')
var file = req.file
if (file.mimetype !== 'application/yang')
return response(res, 'invalid file')
fs.readFile(file.path, {
encoding: 'utf8'
}, function(error, data) {
if (error) {
console.error("error when uploading yang file: %s", file.path)
console.error(error)
return response(res, error)
}
try {
response(res, null, JSON.stringify(yang_parser.parse(data)))
} catch (e) {
return response(res, e)
}
})
})
/* create file with content, creates directory if doesn't exist
*
* because of the nodejs asnyc nature it's recommended to create and check
* errors instead of using 'fs.exists'
* we can change this when user validation is added
*/
function write_file(dir_name, file_name, file_content, callback) {
fs.mkdir(dir_name, function(error) {
/* check if already exists or some other kind of error */
if (error && error.code != 'EEXIST') {
callback && callback(error)
return
}
var file_path = dir_name + file_name
/* save yang file and validate it using pyang
* we set 'cwd' so that pyang can find multiple yang files easily
*/
fs.writeFile(file_path, file_content, function(error) {
if (error) {
callback && callback(error)
return
}
callback && callback()
})
})
}
app.listen(port)
console.log('server listening on port: ' + port)