blob: 1aab1fe22f196b577c8a99f16363bfe64b5679ca [file] [log] [blame]
#!/usr/bin/env node
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file 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.
*/
var spawn = require('child_process').spawn
, sax = require('sax')
, saxpath = require('saxpath')
, xml2js = require('xml2js')
, url = process.argv[2];
if (process.argv.length !== 3) {
console.log('Usage: script/parse [URL of pcap]');
process.exit(1);
}
// reset all sax entities
for (var entity in sax.ENTITIES) {
sax.ENTITIES[entity] = '&' + entity + ';';
}
var PCAPToJSON = function (pcapUrl, cb) {
var saxParser = sax.createStream(true)
, streamer = new saxpath.SaXPath(saxParser, '/pdml/packet')
, parser = new xml2js.Parser()
, curl = spawn('curl', ['-s', pcapUrl])
, tshark = spawn('tshark', ['-i', '-', '-T', 'pdml']);
streamer.on('match', function (xml) {
parser.parseString(xml, function (err, result) {
if (err) {
console.log('problem with xml chunk:');
console.log(xml);
throw err;
} else {
cb(result);
}
});
});
curl.stdout.pipe(tshark.stdin);
tshark.stdout.pipe(saxParser);
};
PCAPToJSON(url, function (json) {
// uncomment to output formatted JSON
console.log(JSON.stringify(json, null, 2));
// uncomment to output condensed JSON
// console.log(JSON.stringify(json));
});