proxy-packer.js/test/pack.js

46 lines
1.3 KiB
JavaScript
Raw Permalink Normal View History

2017-02-16 06:04:56 +00:00
;(function () {
'use strict';
var fs = require('fs');
var infile = process.argv[2];
var outfile = process.argv[3];
2018-05-31 04:48:32 +00:00
var sni = require('sni');
2017-02-16 06:04:56 +00:00
if (!infile || !outfile) {
console.error("Usage:");
2018-05-31 04:48:32 +00:00
console.error("node test/pack.js test/input.json test/output.bin");
2017-02-16 06:04:56 +00:00
process.exit(1);
return;
}
2018-05-31 04:48:32 +00:00
var path = require('path');
2017-02-16 06:04:56 +00:00
var json = JSON.parse(fs.readFileSync(infile, 'utf8'));
2018-05-31 04:48:32 +00:00
var data = require('fs').readFileSync(path.resolve(path.dirname(infile), json.filepath), null);
var Packer = require('../index.js');
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
/*
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
2018-05-31 04:48:32 +00:00
json.address.name = servername || hostname;
var buf = Packer.pack(json.address, data);
2017-02-16 06:04:56 +00:00
fs.writeFileSync(outfile, buf, null);
console.log("wrote " + buf.byteLength + " bytes to '" + outfile + "' ('hexdump " + outfile + "' to inspect)");
}());