proxy-packer.js/test/pack.js

56 lines
1.4 KiB
JavaScript
Raw Permalink Normal View History

2019-09-03 15:21:08 +00:00
(function() {
'use strict';
2017-02-16 06:04:56 +00:00
2019-09-03 15:21:08 +00:00
var fs = require('fs');
var infile = process.argv[2];
var outfile = process.argv[3];
var sni = require('sni');
2017-02-16 06:04:56 +00:00
2019-09-03 15:21:08 +00:00
if (!infile || !outfile) {
console.error('Usage:');
console.error('node test/pack.js test/input.json test/output.bin');
process.exit(1);
return;
}
2017-02-16 06:04:56 +00:00
2019-09-03 15:21:08 +00:00
var path = require('path');
var json = JSON.parse(fs.readFileSync(infile, 'utf8'));
var data = require('fs').readFileSync(
path.resolve(path.dirname(infile), json.filepath),
null
);
var Packer = require('../index.js');
2018-05-31 04:48:32 +00:00
2019-09-03 15:21:08 +00:00
var servername = sni(data);
var m = data.toString().match(/(?:^|[\r\n])Host: ([^\r\n]+)[\r\n]*/im);
var hostname = ((m && m[1].toLowerCase()) || '').split(':')[0];
2017-02-16 06:04:56 +00:00
2019-09-03 15:21:08 +00:00
/*
function pack() {
var version = json.version;
var address = json.address;
var header = address.family + ',' + address.address + ',' + address.port + ',' + data.byteLength
2018-05-31 04:48:32 +00:00
+ ',' + (address.service || '') + ',' + (address.serviceport || '') + ',' + (servername || hostname || '')
;
var buf = Buffer.concat([
Buffer.from([ 255 - version, header.length ])
, Buffer.from(header)
, data
]);
}
*/
2017-02-16 06:04:56 +00:00
2019-09-03 15:21:08 +00:00
json.address.name = servername || hostname;
var buf = Packer.pack(json.address, data);
fs.writeFileSync(outfile, buf, null);
console.log(
'wrote ' +
buf.byteLength +
" bytes to '" +
outfile +
"' ('hexdump " +
outfile +
"' to inspect)"
);
})();