forked from arkime/arkime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimpleSource.js
141 lines (131 loc) · 4.38 KB
/
simpleSource.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
/******************************************************************************/
/* Middle class for simple sources
*
* Copyright 2012-2016 AOL Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this Software except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';
var util = require('util');
var wiseSource = require('./wiseSource.js');
var iptrie = require('iptrie');
function SimpleSource (api, section) {
SimpleSource.super_.call(this, api, section);
this.column = +api.getConfig(section, 'column', 0);
this.keyColumn = api.getConfig(section, 'keyColumn', 0);
this.typeSetting();
if (this.type === 'ip') {
this.cache = { items: new Map(), trie: new iptrie.IPTrie() };
} else {
this.cache = new Map();
}
}
util.inherits(SimpleSource, wiseSource);
module.exports = SimpleSource;
// ----------------------------------------------------------------------------
SimpleSource.prototype.dump = function (res) {
var cache = this.type === 'ip' ? this.cache.items : this.cache;
cache.forEach((value, key) => {
var str = `{key: "${key}", ops:\n` +
wiseSource.result2Str(wiseSource.combineResults([this.tagsResult, value])) + '},\n';
res.write(str);
});
res.end();
};
// ----------------------------------------------------------------------------
SimpleSource.prototype.sendResult = function (key, cb) {
var result = this.type === 'ip' ? this.cache.trie.find(key) : this.cache.get(key);
// Not found, or found but no extra values to add
if (!result) {
return cb(null, undefined);
}
if (result.num === 0) {
return cb(null, this.tagsResult);
}
// Found, so combine the two results (per item, and per source)
var newresult = { num: result.num + this.tagsResult.num, buffer: Buffer.concat([result.buffer, this.tagsResult.buffer]) };
return cb(null, newresult);
};
// ----------------------------------------------------------------------------
SimpleSource.prototype.initSimple = function () {
if (!this.type) {
console.log(this.section, '- ERROR not loading since no type specified in config file');
return false;
}
this.tagsSetting();
if (!this.formatSetting()) {
return false;
}
if (this.type === 'domain') {
this.getDomain = function (domain, cb) {
if (this.cache.get(domain)) {
return this.sendResult(domain, cb);
}
domain = domain.substring(domain.indexOf('.') + 1);
return this.sendResult(domain, cb);
};
} else {
this[this.api.funcName(this.type)] = this.sendResult;
}
this.api.addSource(this.section, this);
return true;
};
// ----------------------------------------------------------------------------
SimpleSource.prototype.getTypes = function () {
return [this.type];
};
// ----------------------------------------------------------------------------
SimpleSource.prototype.load = function () {
var setFunc;
var newCache;
var count = 0;
if (this.type === 'ip') {
newCache = { items: new Map(), trie: new iptrie.IPTrie() };
setFunc = (key, value) => {
key.split(',').forEach((cidr) => {
var parts = cidr.split('/');
try {
newCache.trie.add(parts[0], +parts[1] || (parts[0].includes(':') ? 128 : 32), value);
} catch (e) {
console.log('ERROR adding', this.section, cidr, e);
}
newCache.items.set(cidr, value);
count++;
});
};
} else {
newCache = new Map();
if (this.type === 'url') {
setFunc = (key, value) => {
if (key.lastIndexOf('http://', 0) === 0) {
key = key.substring(7);
}
newCache.set(key, value);
count++;
};
} else {
setFunc = function (key, value) {
newCache.set(key, value);
count++;
};
}
}
this.simpleSourceLoad(setFunc, (err) => {
if (err) {
console.log('ERROR loading', this.section, err);
return;
}
this.cache = newCache;
console.log(this.section, '- Done Loading', count, 'elements');
});
};