Comparing version 0.0.4 to 0.0.5
{ | ||
"name": "dnsproxy", | ||
"version": "0.0.4", | ||
"version": "0.0.5", | ||
"description": "A simple DNS proxy server for Node.js", | ||
@@ -10,3 +10,3 @@ "main": "proxy.js", | ||
"scripts": { | ||
"test": "sudo node test.js dnsproxy.json" | ||
"test": "sudo node test.js" | ||
}, | ||
@@ -13,0 +13,0 @@ "repository": { |
11
proxy.js
@@ -281,2 +281,4 @@ 'use strict'; | ||
this._bind = options.bind || false; | ||
this.port = parseInt(options.port) || DNS_SERVER_PORT; | ||
this.addresses = (options && options.addresses) || {}; | ||
@@ -289,5 +291,10 @@ this.cache = !! options.cache; | ||
Server.prototype.start = function( /*address, callback*/ ) { | ||
Server.prototype.start = function( /*callback*/ ) { | ||
responseBuffer = responseBuffer || new Buffer(DNS_BUFFER_SIZE); | ||
this.bind.apply(this, [DNS_SERVER_PORT].concat(toArray(arguments, 0))); | ||
var args = toArray(arguments); | ||
if (this._bind) { | ||
args.unshift(this._bind); | ||
} | ||
args.unshift(this.port); | ||
this.bind.apply(this, args); | ||
}; // }}} | ||
@@ -294,0 +301,0 @@ |
node-dnsproxy | ||
============== | ||
A simple DNS proxy server for Node.js | ||
A simple DNS proxy server for Node.js | ||
一个简单的 Node.js DNS 代理服务器 | ||
## Installation | ||
## 安装 | ||
``` shell | ||
npm install dnsproxy | ||
sudo dnsproxy dnsproxy.json | ||
npm install dnsproxy # install from npm | ||
sudo dnsproxy -a a.com:127.0.0.1 & # start dns proxy server | ||
dig @localhost a.com # test the server, return 127.0.0.1 | ||
``` | ||
## Usage | ||
## 使用 | ||
``` text | ||
Usage: dnsproxy [-?hv] [-b address] [-p port] [-c true|false] [-a domain1:ip1 [domain2:ip2 [...]]] [-f filename] | ||
Options: | ||
-?,-h : this help | ||
-v : show version and exit | ||
-b address : set bind address | ||
-p port : set bind port(default: 53) | ||
-c true|false : enable/disable cache | ||
-a domain:ip : add one or more DNS recode | ||
-f filename : load options from file | ||
``` | ||
## Pan-analytic | ||
## 泛解析 | ||
``` shell | ||
sudo dnsproxy -a *.a.com:127.0.0.1 *.b.a.com:127.0.0.2 & | ||
dig @localhost xx.a.com # return 127.0.0.1 | ||
dig @localhost xx.b.a.com # return 127.0.0.2 | ||
``` | ||
## Who am I | ||
## 我是谁 | ||
``` shell | ||
# start server on 192.168.1.1 | ||
sudo dnsproxy -a who.am.i:localhost who.you.are:proxyhost & | ||
# run on 192.168.1.2 | ||
dig @192.168.1.1 who.am.i # return 192.168.1.2 | ||
dig @192.168.1.1 who.you.are # return 192.168.1.1 | ||
# run on 192.168.1.3 | ||
dig @192.168.1.1 who.am.i # return 192.168.1.3 | ||
dig @192.168.1.1 who.you.are # return 192.168.1.1 | ||
``` |
116
test.js
var fs = require("fs"), | ||
dnsproxy = require(__dirname + "/proxy.js"); | ||
var args = process.argv, | ||
configFile, addresses; | ||
var args = process.argv.slice(2), | ||
index, count, argv, parts, config, key, domain, | ||
options; | ||
if(args.length < 3){ | ||
console.error(["Usage:", args[0], args[1], "[config file]"].join(" ")); | ||
process.exit(-1); | ||
options = { | ||
addresses: {} | ||
}; | ||
for (index = 0, count = args.length; index < count; index++) { | ||
argv = args[index]; | ||
switch (argv.toLowerCase()) { | ||
case "-b": // -b 192.168.1.1 | ||
if (argv = getNextArgv()) { | ||
options.bind = argv; | ||
} else { | ||
printUsage(); | ||
} | ||
break; | ||
case "-p": // -p 53 | ||
if (argv = getNextArgv()) { | ||
options.port = parseInt(argv); | ||
} else { | ||
printUsage(); | ||
} | ||
break; | ||
case "-c": // -c true|false | ||
argv = getNextArgv(); | ||
options.cache = argv === false || /^true|1$/i.test(argv) ? true : false; | ||
break; | ||
case "-a": // -a a.com:1.2.3.4 *.com:2.3.4.5 ... | ||
if (argv = getNextArgv()) { | ||
do { | ||
parts = argv.split(":"); | ||
if (parts.length === 2) { | ||
options.addresses[parts[0]] = parts[1]; | ||
} | ||
} while (argv = getNextArgv()); | ||
} else { | ||
printUsage(); | ||
} | ||
break; | ||
case "-f": // -f dnsproxy.json | ||
if (argv = getNextArgv()) { | ||
if (fs.existsSync(argv)) { | ||
config = JSON.parse(fs.readFileSync(argv, "utf8")); | ||
for (key in config) { | ||
if (config.hasOwnProperty(key)) { | ||
if (key === "addresses") { | ||
for (domain in config.addresses) { | ||
options.addresses[domain] = config.addresses[domain]; | ||
} | ||
} else { | ||
options[key] = config[key]; | ||
} | ||
} | ||
} | ||
} else { | ||
console.error("config file \"" + argv + "\" dose not exists."); | ||
printUsage(); | ||
} | ||
} else { | ||
printUsage(); | ||
} | ||
break; | ||
case "-v": | ||
console.log("dnsproxy version: " + JSON.parse(fs.readFileSync(__dirname + "/package.json", "utf8"))["version"]); | ||
process.exit(0); | ||
break; | ||
case "-?": | ||
case "-h": | ||
case "--help": | ||
default: | ||
printUsage(); | ||
} | ||
} | ||
configFile = args[2]; | ||
options = JSON.parse(fs.readFileSync(configFile)); | ||
function getNextArgv() { | ||
var i = index + 1, | ||
v; | ||
if (i < count) { | ||
v = args[i]; | ||
dnsproxy.createServer({ | ||
addresses: options.addresses || {}, | ||
cache: options.hasOwnProperty("cache") ? !!options.cache : true | ||
}).start(options.bind || "0.0.0.0"); | ||
if (v.indexOf("-") === 0) { | ||
return false; | ||
} | ||
index++; | ||
return v; | ||
} | ||
return false; | ||
} | ||
function printUsage() { | ||
console.error([ | ||
"Usage: dnsproxy [-?hv] [-b address] [-p port] [-c true|false] [-a domain1:ip1 [domain2:ip2 [...]]] [-f filename]", | ||
"", | ||
"Options:", | ||
" -?,-h : this help", | ||
" -v : show version and exit", | ||
" -b address : set bind address", | ||
" -p port : set bind port(default: 53)", | ||
" -c true|false : enable/disable cache", | ||
" -a domain:ip : add one or more DNS recode", | ||
" -f filename : load options from file" | ||
].join("\n")); | ||
process.exit(-1); | ||
} | ||
dnsproxy.createServer(options).start(); | ||
// vim600: sw=4 ts=4 fdm=marker syn=javascript |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
18437
442
54