forked from zielmicha/fc00.org
-
Notifications
You must be signed in to change notification settings - Fork 1
/
sendEntireGraph2sql.js
92 lines (87 loc) · 3.18 KB
/
sendEntireGraph2sql.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
#!/usr/bin/env node
/* -*- Mode:js */
/* vim: set expandtab ts=4 sw=4: */
/*
* You may redistribute this program and/or modify it under the terms of
* the GNU General Public License as published by the Free Software Foundation,
* either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
const Fs = require('fs');
const Querystring = require('querystring');
const Http = require('http');
const Cjdnskeys = require('cjdnskeys');
const Split = require('split');
const MAIL = '[email protected]';
//(()=>{throw new Error("You need to set your email...")})();
const SERVER = '127.0.0.1';
//fc53:dcc5:e89d:9082:4097:6622:5e82:c654';
const parseNode = (x) => {
const nn = Cjdnskeys.parseNodeName(x[3]);
return { version: nn.v, ip: Cjdnskeys.publicToIp6(nn.key) };
};
const parseLink = (x) => {
const nodes = [ x[3], x[4] ];
nodes.sort();
return { a: Cjdnskeys.publicToIp6(nodes[0]), b: Cjdnskeys.publicToIp6(nodes[1]) };
};
const main = () => {
const dupeFilter = {};
const filterDups = (x) => {
const xstr = JSON.stringify(x);
if (xstr in dupeFilter) { return; }
dupeFilter[xstr] = 1;
return x;
}
const nodes = [];
const links = [];
process.stdin.pipe(Split()).on('data', (line) => {
if (!/^\["node"|\["link"/.test(line)) { return; }
const parsed = JSON.parse(line);
const isNode = (parsed[0] === 'node');
const obj = isNode ? parseNode(parsed) : parseLink(parsed);
if (!filterDups(obj)) { return; }
(isNode ? nodes : links).push(obj);
}).on('end', () => {
console.log('use cjdns;');
nodes.forEach((n) => {
console.log("INSERT INTO nodes (ip, name, version, first_seen, last_seen) VALUES ('" + n.ip + "', '-', " + n.version + ", -1, 0x7fffffff);");
});
links.forEach((l) => {
console.log("INSERT INTO edges (a, b, first_seen, last_seen, uploaded_by) VALUES ('" + l.a + "', '" + l.b + "', -1, 0x7fffffff, 'x');");
});
return;
//console.log(JSON.stringify({nodes:nodes, edges:links}, null, ' '));
//return;
const post = Querystring.stringify({
data: JSON.stringify({ nodes: nodes, edges: links }),
version: 2,
mail: MAIL });
var opts = {
host: SERVER,
port: '3000',
path: '/sendGraph',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': Buffer.byteLength(post)
}
};
const req = Http.request(opts, (res) => {
res.setEncoding('utf8');
res.on('data', (chunk) => {
console.log('Response: ' + chunk);
});
});
req.write(post);
req.end();
})
}
main();