├── .eslintrc ├── .gitignore ├── .travis.yml ├── History.md ├── LICENSE ├── README.md ├── index.js ├── package.json └── test └── index.test.js /.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "eslint-config-egg" 3 | } 4 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.seed 2 | *.log 3 | *.csv 4 | *.dat 5 | *.out 6 | *.pid 7 | *.gz 8 | 9 | pids 10 | logs 11 | results 12 | 13 | node_modules 14 | npm-debug.log 15 | coverage/ 16 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: false 2 | language: node_js 3 | node_js: 4 | - '6' 5 | - '4' 6 | script: 7 | - npm run ci 8 | after_script: 9 | - npm i codecov && codecov 10 | -------------------------------------------------------------------------------- /History.md: -------------------------------------------------------------------------------- 1 | 2 | 1.0.1 / 2016-10-01 3 | ================== 4 | 5 | * Release 1.0.1 6 | * chore:bump to beta1 7 | 8 | 1.0.0-beta.0 / 2016-09-30 9 | ================== 10 | 11 | * init commit 12 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 node-modules and other contributors 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # dns-hijacking 2 | 3 | Node.js DNS hijacking tool. 4 | 5 | [![NPM version][npm-image]][npm-url] 6 | [![build status][travis-image]][travis-url] 7 | [![Test coverage][codecov-image]][codecov-url] 8 | [![David deps][david-image]][david-url] 9 | [![Known Vulnerabilities][snyk-image]][snyk-url] 10 | [![npm download][download-image]][download-url] 11 | 12 | [npm-image]: https://img.shields.io/npm/v/dns-hijacking.svg?style=flat-square 13 | [npm-url]: https://npmjs.org/package/dns-hijacking 14 | [travis-image]: https://img.shields.io/travis/node-modules/dns-hijacking.svg?style=flat-square 15 | [travis-url]: https://travis-ci.org/node-modules/dns-hijacking 16 | [codecov-image]: https://codecov.io/github/node-modules/dns-hijacking/coverage.svg?branch=master 17 | [codecov-url]: https://codecov.io/github/node-modules/dns-hijacking?branch=master 18 | [david-image]: https://img.shields.io/david/node-modules/dns-hijacking.svg?style=flat-square 19 | [david-url]: https://david-dm.org/node-modules/dns-hijacking 20 | [snyk-image]: https://snyk.io/test/npm/dns-hijacking/badge.svg?style=flat-square 21 | [snyk-url]: https://snyk.io/test/npm/dns-hijacking 22 | [download-image]: https://img.shields.io/npm/dm/dns-hijacking.svg?style=flat-square 23 | [download-url]: https://npmjs.org/package/dns-hijacking 24 | 25 | ## Installation 26 | 27 | ```bash 28 | $ npm install dns-hijacking --save 29 | ``` 30 | 31 | ## Quick start 32 | 33 | ```js 34 | const DNSHijacking = require('dns-hijacking'); 35 | const hosts = { 'nodejs.org': '127.0.0.1' }; // also support IPV6. e.g: { 'nodejs.org': '2607:f8b0:4003:c18::65' } 36 | const dNSHijacking = new DNSHijacking(hosts); 37 | 38 | // doing logic under the hijacking 39 | dNSHijacking.setup(); 40 | 41 | // reset the hijacking 42 | dNSHijacking.reset(); 43 | 44 | ``` 45 | 46 | ## API 47 | 48 | DNSHijacking 49 | 50 | - constructor(hosts) *`hosts` this hijacking host config.* 51 | - config *`setter` custuom config the hijacking host.* 52 | - setup() *start hijacking.* 53 | - reset() *stop hijacking.* 54 | - resetConfig() *reset the hijacking host.* 55 | 56 | ## License 57 | [MIT](LICENSE) 58 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const util = require('util'); 4 | const net = require('net'); 5 | const cares = process.binding('cares_wrap'); 6 | 7 | const _getaddrinfo = cares.getaddrinfo; 8 | 9 | class DNSHijacking { 10 | constructor(hosts) { 11 | this._hosts = util._extend({}, hosts); 12 | } 13 | 14 | set config(hosts) { 15 | Object.assign(this._hosts, hosts); 16 | } 17 | 18 | setup() { 19 | const _hosts = this._hosts; 20 | cares.getaddrinfo = (req, hostname, family, hints) => { 21 | if (_hosts[hostname]) { 22 | return req.callback(null, _hosts[hostname], net.isIPv6(_hosts[ 23 | hostname]) ? 6 : 4); 24 | } 25 | return _getaddrinfo(req, hostname, family, hints); 26 | }; 27 | } 28 | 29 | reset() { 30 | cares.getaddrinfo = _getaddrinfo; 31 | } 32 | 33 | resetConfig() { 34 | Object.keys(this._hosts).map(key => delete this._hosts[key]); 35 | } 36 | } 37 | 38 | module.exports = DNSHijacking; 39 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "dns-hijacking", 3 | "version": "1.0.1", 4 | "description": "Node.js DNS hijacking tool.", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "npm run lint && mocha -t 15000 -r intelli-espower-loader test/*.test.js", 8 | "test-cov": "istanbul cover _mocha -- -t 15000 -r intelli-espower-loader test/*.test.js", 9 | "lint": "eslint test *.js", 10 | "ci": "npm run lint && npm run test-cov" 11 | }, 12 | "repository": { 13 | "type": "git", 14 | "url": "git+https://github.com/node-modules/dns-hijacking.git" 15 | }, 16 | "keywords": [ 17 | "dns", 18 | "hijacking", 19 | "node" 20 | ], 21 | "author": "ngot", 22 | "license": "MIT", 23 | "bugs": { 24 | "url": "https://github.com/node-modules/dns-hijacking/issues" 25 | }, 26 | "homepage": "https://github.com/node-modules/dns-hijacking#readme", 27 | "devDependencies": { 28 | "eslint": "3", 29 | "eslint-config-egg": "3", 30 | "intelli-espower-loader": "^1.0.1", 31 | "istanbul": "*", 32 | "mocha": "*", 33 | "power-assert": "^1.4.1" 34 | }, 35 | "files": [ 36 | "index.js" 37 | ], 38 | "engines": { 39 | "node": ">=4.0.0" 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /test/index.test.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const DNSHijacking = require('../'); 4 | const assert = require('power-assert'); 5 | const dns = require('dns'); 6 | 7 | describe('index.test.js', () => { 8 | const hosts = { 9 | 'cnpmjs.org': '127.0.0.1', 10 | }; 11 | const dNSHijacking = new DNSHijacking(hosts); 12 | const cnpmjs = 'cnpmjs.org'; 13 | 14 | afterEach(dNSHijacking.reset); 15 | 16 | it('should work', done => { 17 | dNSHijacking.setup(); 18 | dns.lookup(cnpmjs, (err, address, family) => { 19 | assert(err === null); 20 | assert(address === '127.0.0.1'); 21 | assert(family === 4); 22 | done(); 23 | }); 24 | }); 25 | 26 | it('should reset ok', done => { 27 | dNSHijacking.setup(); 28 | dNSHijacking.reset(); 29 | 30 | dns.lookup(cnpmjs, (err, address, family) => { 31 | assert(err === null); 32 | assert(address === '47.88.240.14'); 33 | assert(family === 4); 34 | done(); 35 | }); 36 | }); 37 | 38 | it('should set config ok', done => { 39 | dNSHijacking.setup(); 40 | dNSHijacking.config = { 41 | 'cnpmjs.org': '192.168.0.1', 42 | }; 43 | dns.lookup(cnpmjs, (err, address, family) => { 44 | assert(err === null); 45 | assert(address === '192.168.0.1'); 46 | assert(family === 4); 47 | done(); 48 | }); 49 | }); 50 | 51 | it('should reset config ok', done => { 52 | dNSHijacking.setup(); 53 | dNSHijacking.config = { 54 | 'cnpmjs.org': '192.168.0.1', 55 | }; 56 | dNSHijacking.resetConfig(); 57 | dns.lookup(cnpmjs, (err, address, family) => { 58 | assert(err === null); 59 | assert(address === '47.88.240.14'); 60 | assert(family === 4); 61 | done(); 62 | }); 63 | }); 64 | 65 | it('should support IPV6', done => { 66 | dNSHijacking.setup(); 67 | dNSHijacking.config = { 68 | 'cnpmjs.org': '2607:f8b0:4003:c18::65', 69 | }; 70 | dns.lookup(cnpmjs, (err, address, family) => { 71 | assert(err === null); 72 | assert(address === '2607:f8b0:4003:c18::65'); 73 | assert(family === 6); 74 | done(); 75 | }); 76 | }); 77 | }); 78 | --------------------------------------------------------------------------------