-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 1066ce8
Showing
16 changed files
with
2,595 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
node_modules/ | ||
.nyc_output/ | ||
coverage/ | ||
temp/* | ||
!temp/.gitkeep |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
.nyc_output/ | ||
coverage/ | ||
temp/* | ||
!temp/.gitkeep |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
language: node_js | ||
|
||
node_js: | ||
- "node" | ||
|
||
notifications: | ||
email: | ||
on_success: never | ||
on_failure: change | ||
|
||
script: | ||
- npm test | ||
|
||
after_success: npm run coverage |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# Pac File Tester | ||
|
||
Tests a PAC file and returns its output. | ||
|
||
## Usage | ||
|
||
### As a CLI tool | ||
|
||
``` | ||
npm install pac-file-tester -g | ||
pac-file-tester -f http://your.site/proxy.pac -u http://www.google.com | ||
``` | ||
|
||
#### Options | ||
|
||
- -f --file, the url/path of the pac file to test. | ||
- -u --url, the url to test the pac file against. | ||
- -i --ip, the IP address to return from `myIpAddress()`, defaults to your primary IP. (Optional) | ||
- -c --compare, the url/path of another pac file to compare output & speed with. (Optional) | ||
|
||
### Pragmatically | ||
|
||
``` | ||
npm install --save pac-file-tester | ||
``` | ||
|
||
```js | ||
cost PACFileTester = require('pac-file-tester') | ||
|
||
PACFileTester.getResult('/path/to/proxy.pac', 'http://some.site.com', function(result){ | ||
console.log(result) | ||
}) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
#!/usr/bin/env node | ||
|
||
const Benchmark = require('benchmark') | ||
const fs = require('fs') | ||
const PACFileTester = require('../lib/pac-file-tester') | ||
const path = require('path') | ||
const program = require('commander') | ||
|
||
const pkg = require(path.join(__dirname, '..', 'package.json')) | ||
|
||
|
||
program | ||
.version(pkg.version) | ||
.option('-f --file <url>', 'PAC File to test') | ||
.option('-u --url <url>', 'URL to supply for testing') | ||
.option('-i --ip <ip>', 'The IP to supply to the PAC file (defaults to your own)') | ||
.option('-c --compare <url>', 'URL of another PAC file to compare to') | ||
.parse(process.argv) | ||
|
||
if(!program.file){ | ||
console.log('No PAC File supplied') | ||
return | ||
} | ||
|
||
if(!program.url){ | ||
console.log('No URL supplied') | ||
return | ||
} | ||
|
||
if(program.file.substr(0,4) === 'http'){ | ||
PACFileTester.downloadFile(program.file, 'proxy.pac', function(err){ | ||
runTest(path.join(__dirname, '..', 'temp', 'proxy.pac')) | ||
}) | ||
}else{ | ||
runTest(program.file) | ||
} | ||
|
||
if(program.ip){ | ||
PACFileTester.ip = program.ip | ||
} | ||
|
||
var runTest = function(file){ | ||
if(!program.compare){ | ||
PACFileTester.getResult(file, program.url, function(result){ | ||
console.log(result) | ||
}) | ||
}else{ | ||
if(program.compare.substr(0,4) === 'http'){ | ||
PACFileTester.downloadFile(program.compare, 'compare.pac', function(err){ | ||
runCompare(file, path.join(__dirname, '..', 'temp', 'compare.pac')) | ||
}) | ||
}else{ | ||
runCompare(file, program.compare) | ||
} | ||
} | ||
} | ||
|
||
var runCompare = function(file1, file2){ | ||
console.log('Comparing 2 Files') | ||
console.log('Getting Result') | ||
PACFileTester.getResult(file1, program.url, function(result){ | ||
var stats = fs.statSync(file1) | ||
|
||
console.log('File 1: ' + result + ' size: ' + (stats.size / 1000.00) + 'KB') | ||
PACFileTester.getResult(file2, program.url, function(result){ | ||
var stats = fs.statSync(file2) | ||
|
||
console.log('File 2: ' + result + ' size: ' + (stats.size / 1000.00) + 'KB') | ||
|
||
console.log('Benchmarking') | ||
|
||
var suite = new Benchmark.Suite | ||
|
||
var prepared1 = false | ||
var prepared2 = false | ||
|
||
suite.add('File 1', function(deffered){ | ||
if(prepared1){ | ||
PACFileTester.runPAC(program.url, function(){ | ||
deffered.resolve() | ||
}) | ||
}else{ | ||
PACFileTester.getResult(file1, program.url, function(result){ | ||
prepared1 = true | ||
deffered.resolve() | ||
}) | ||
} | ||
}, {defer: true}) | ||
.add('File 2', function(deffered){ | ||
if(prepared2){ | ||
PACFileTester.runPAC(program.url, function(){ | ||
deffered.resolve() | ||
}) | ||
}else{ | ||
PACFileTester.getResult(file2, program.url, function(result){ | ||
prepared2 = true | ||
deffered.resolve() | ||
}) | ||
} | ||
}, {defer: true}) | ||
.on('cycle', function(event) { | ||
console.log(String(event.target)) | ||
}) | ||
.on('complete', function() { | ||
console.log('Fastest is ' + this.filter('fastest').map('name')) | ||
}) | ||
.run({ 'async': true }); | ||
}) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
const dnsSync = require('dns-sync') | ||
const download = require('download-file') | ||
const fs = require('fs') | ||
const http = require('http') | ||
const ip = require('ip') | ||
const path = require('path') | ||
|
||
// Taken from the Mozilla PAC file parser (https://dxr.mozilla.org/mozilla/source/netwerk/base/src/nsProxyAutoConfig.js) | ||
global.shExpMatch = function(url, pattern) { | ||
pattern = pattern.replace(/\./g, '\\.'); | ||
pattern = pattern.replace(/\*/g, '.*'); | ||
pattern = pattern.replace(/\?/g, '.'); | ||
var newRe = new RegExp('^'+pattern+'$'); | ||
return newRe.test(url); | ||
} | ||
|
||
global.isInNet = function(addr, network, mask){ | ||
return ip.subnet(network, mask).contains(addr) | ||
} | ||
|
||
global.dnsCache = {} | ||
global.dnsResolve = function(host){ | ||
if(dnsCache[host]){ | ||
return dnsCache[host] | ||
}else{ | ||
var result = dnsSync.resolve(host) | ||
dnsCache[host] = result | ||
return result | ||
} | ||
} | ||
|
||
module.exports = { | ||
ip: ip.address(), | ||
|
||
prepareFile(filePath, cb){ | ||
fs.readFile(filePath, function(err, data){ | ||
var contents = data.toString() | ||
|
||
contents = contents.replace(/function FindProxyForURL/m, 'module.exports = function') | ||
|
||
var writeTo = path.join(__dirname, '..', 'temp', 'prepared.js') | ||
|
||
fs.writeFile(writeTo, contents, function(err){ | ||
delete require.cache[require.resolve(path.join(__dirname, '..', 'temp', 'prepared.js'))] | ||
cb() | ||
}) | ||
}) | ||
}, | ||
|
||
getResult(pacFile, url, cb){ | ||
// Provide the machines IP (or a stubbed one) | ||
global.myIpAddress = function(){ | ||
return module.exports.ip | ||
} | ||
|
||
this.prepareFile(pacFile, function(err){ | ||
module.exports.runPAC(url, cb) | ||
}) | ||
}, | ||
|
||
runPAC(url, cb){ | ||
var pac = require(path.join(__dirname, '..', 'temp', 'prepared.js')) | ||
|
||
cb(pac(url, module.exports.getHost(url))) | ||
}, | ||
|
||
getHost(url){ | ||
var regex = /^(?:https?:\/\/)?(?:[^@\n]+@)?(?:www\.)?([^:\/\n]+)/im | ||
|
||
var match = regex.exec(url) | ||
|
||
return match[1] | ||
}, | ||
|
||
downloadFile(url, name, cb){ | ||
download(url, { | ||
directory: path.join(__dirname, '..', 'temp'), | ||
filename: name | ||
}, function(err){ | ||
cb() | ||
}) | ||
} | ||
} |
Oops, something went wrong.