-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.js
More file actions
126 lines (118 loc) · 3.43 KB
/
database.js
File metadata and controls
126 lines (118 loc) · 3.43 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
module.exports = function(dbName){
if(!dbName){
console.error('Filename not defined');
return false;
}
var p = require('path');
var fs = require('fs');
var db = require('dblite');
var dbFile = p.normalize(p.join(__dirname,dbName));
console.info('Set database to "%s"',dbFile);
module.exports = {
'init': function(){
fs.closeSync(fs.openSync(dbFile,'a'));
fs.chmodSync(dbFile,0666);
db=createTables(db(dbFile))
.on('info',function(data){console.info(data);})
.on('error',function(data){console.error(data.toString());})
.on('close',function(data){console.error('DB closed');});
module.exports = {
'save': function(packet,ip){return savePacket(db,packet,ip);},
'close': function(){return db.close();},
};
return module.exports;
},
};
return module.exports;
}
var createTable = new Object({
init: [
'PRAGMA foreign_keys=1',
'PRAGMA busy_timeout=45000',
],
packets: [
'CREATE TABLE IF NOT EXISTS `packets` ('
+ '`id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,'
+ '`ip` VARCHAR,'
+ '`version` INTEGER,'
+ '`count` INTEGER,'
+ '`sys_uptime` INTEGER,'
+ '`unix_secs` INTEGER,'
+ '`unix_nsecs` INTEGER,'
+ '`flow_sequence` INTEGER,'
+ '`engine_type` INTEGER,'
+ '`engine_id` INTEGER,'
+ '`sampling_interval` INTEGER'
+ ')',
'CREATE INDEX IF NOT EXISTS `ip` ON `packets` (`ip`)',
],
records: [
'CREATE TABLE IF NOT EXISTS `records` ('
+ '`id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,'
+ '`packet_id` INTEGER NOT NULL REFERENCES packets(`id`) ON UPDATE CASCADE ON DELETE CASCADE,'
+ '`srcaddr` BIGINT,'
+ '`dstaddr` BIGINT,'
+ '`nexthop` BIGINT,'
+ '`input` INTEGER,'
+ '`output` INTEGER,'
+ '`dPkts` INTEGER,'
+ '`dOctets` INTEGER,'
+ '`first` INTEGER,'
+ '`last` INTEGER,'
+ '`srcport` INTEGER,'
+ '`dstport` INTEGER,'
+ '`pad1` INTEGER,'
+ '`tcp_flags` INTEGER,'
+ '`prot` INTEGER,'
+ '`tos` INTEGER,'
+ '`src_as` INTEGER,'
+ '`dst_as` INTEGER,'
+ '`src_mask` INTEGER,'
+ '`dst_mask` INTEGER,'
+ '`pad2` INTEGER'
+ ')',
'CREATE INDEX IF NOT EXISTS `packet_id` ON `records` (`packet_id`)',
'CREATE INDEX IF NOT EXISTS `srcaddr` ON `records` (`srcaddr`)',
'CREATE INDEX IF NOT EXISTS `dstaddr` ON `records` (`dstaddr`)',
]
});
function createTables(db){
for(t in createTable)
for(var i=0;i<createTable[t].length;i++)
db.query(createTable[t][i]);
return db;
};
function savePacket(db,packet,ip){
packet.ip=ip?ip:null;
db.query('INSERT INTO `packets` (ip,version,count,sys_uptime,unix_secs,unix_nsecs,flow_sequence,engine_type,engine_id,sampling_interval)'
+' VALUES(:ip,:version,:count,:sys_uptime,:unix_secs,:unix_nsecs,:flow_sequence,:engine_type,:engine_id,:sampling_interval)',packet);
db.lastRowID('packets',function(packet_id){
for(var i=0;i<packet.count;i++){
packet.recs[i].packet_id=packet_id;
db.query('INSERT INTO `records` VALUES (null,'
+ ':packet_id,'
+ ':srcaddr,'
+ ':dstaddr,'
+ ':nexthop,'
+ ':input,'
+ ':output,'
+ ':dPkts,'
+ ':dOctets,'
+ ':first,'
+ ':last,'
+ ':srcport,'
+ ':dstport,'
+ '0,'
+ ':tcp_flags,'
+ ':prot,'
+ ':tos,'
+ ':src_as,'
+ ':dst_as,'
+ ':src_mask,'
+ ':dst_mask,'
+ '0)',
packet.recs[i]);
}
});
return db;
};