Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for unencrypted DNS #125

Merged
merged 4 commits into from
Jan 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ Options:
--port port address to bind proxy server [number] [default: 8000]
--dns-type [string] [choices: "https", "tls"] [default: "https"]
--dns-server [string] [default: "https://cloudflare-dns.com/dns-query"]
--dns-ip IP address for unencrypted DNS [string][default: "127.0.0.1"]
--dns-port Port for unencrypted DNS [number] [default: 53]
--silent, -s run in silent mode [boolean] [default: false]
--verbose, -v debug mode [string] [default: ""]
--system-proxy automatic set system-proxy [boolean] [default: true]
Expand Down
24 changes: 18 additions & 6 deletions bin/gt.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ const ora = require('ora');
const debug = require('debug');
const yargs = require('yargs');
const pkg = require('../package.json');
const {Proxy, config, getLogger} = require('../src/index.cjs');
const { Proxy, config, getLogger } = require('../src/index.cjs');

const logger = getLogger('cli');

const {argv} = yargs
const { argv } = yargs
.usage('Usage: green-tunnel [options]')
.usage('Usage: gt [options]')
.alias('help', 'h')
Expand All @@ -37,7 +37,7 @@ const {argv} = yargs

.option('dns-type', {
type: 'string',
choices: ['https', 'tls'],
choices: ['https', 'tls', 'unencrypted'],
default: config.dns.type,
})

Expand All @@ -46,6 +46,16 @@ const {argv} = yargs
default: config.dns.server,
})

.option('dns-ip', {
type: 'string',
default: config.dns.ip,
})

.option('dns-port', {
type: 'number',
default: config.dns.port,
})

.option('silent', {
alias: 's',
type: 'boolean',
Expand Down Expand Up @@ -113,7 +123,9 @@ async function main() {
httpsOnly: argv['https-only'],
dns: {
type: argv['dns-type'],
server: argv['dns-server']
server: argv['dns-server'],
ip: argv['dns-ip'],
port: argv['dns-port']
},
source: 'CLI',
});
Expand All @@ -138,12 +150,12 @@ async function main() {
process.on('unhandledRejection', errorTrap);
process.on('uncaughtException', errorTrap);

await proxy.start({setProxy: argv['system-proxy']});
await proxy.start({ setProxy: argv['system-proxy'] });

if (!argv['silent'] && !argv['verbose']) {
clear();
printBanner();
updateNotifier({pkg}).notify();
updateNotifier({ pkg }).notify();
printAlert(proxy);
showSpinner();
}
Expand Down
30 changes: 21 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"debug": "^4.1.1",
"dns-over-http": "^0.1.2",
"dns-over-tls": "0.0.6",
"dns-socket": "^4.2.2",
"esm": "^3.2.22",
"is-docker": "^2.0.0",
"lru-cache": "^5.1.1",
Expand Down
4 changes: 3 additions & 1 deletion src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ const config = {
httpsOnly: false,
clientHelloMTU: 100,
dns: {
type: 'https', // 'tls' or 'https'
type: 'https', // 'tls' or 'https' or 'unencrypted'
server: 'https://cloudflare-dns.com/dns-query',
ip: '127.0.0.1',
port: 53,
cacheSize: 1000,
}
};
Expand Down
2 changes: 1 addition & 1 deletion src/dns/base.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import LRU from 'lru-cache';
import {isIP} from 'validator';
import { isIP } from 'validator';
import getLogger from '../logger';
import config from '../config';

Expand Down
25 changes: 25 additions & 0 deletions src/dns/unencrypted.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import dnsSocket from 'dns-socket';
import BaseDNS from './base';

const socket = dnsSocket()

export default class DNSUnencrypted extends BaseDNS {
constructor(dnsIp, dnsPort) {
super();
this.dnsIp = dnsIp;
this.dnsPort = dnsPort;
}

_lookup(hostname) {
return new Promise((resolve, reject) => {
socket.query({
questions: [{
type: 'A',
name: hostname
}]
}, this.dnsPort, this.dnsIp, (err, res, query) => {
resolve(res.answers[0].data);
});
});
}
}
21 changes: 13 additions & 8 deletions src/proxy.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,38 @@
import net from 'net';
import {setProxy, unsetProxy} from './utils/system-proxy';
import { setProxy, unsetProxy } from './utils/system-proxy';
import handleRequest from './handlers/request';
import DNSOverTLS from './dns/tls';
import DNSOverHTTPS from './dns/https';
import DNSUnencrypted from './dns/unencrypted';
import config from './config';
import getLogger from './logger';
import {appInit} from './utils/analytics';
import { appInit } from './utils/analytics';

const logger = getLogger('proxy');

export default class Proxy {
constructor(customConfig) {
this.config = {...config, ...customConfig};
this.config = { ...config, ...customConfig };
this.server = undefined;
this.isSystemProxySet = false;
this.initDNS();
appInit(customConfig.source);
}

initDNS() {
this.dns = this.config.dns.type === 'https' ?
new DNSOverHTTPS(this.config.dns.server) :
new DNSOverTLS(this.config.dns.server);
if (this.config.dns.type === 'https') {
this.dns = new DNSOverHTTPS(this.config.dns.server);
} else if (this.config.dns.type === 'tls') {
this.dns = new DNSOverTLS(this.config.dns.server);
} else {
this.dns = new DNSUnencrypted(this.config.dns.ip, this.config.dns.port);
}
}

async start(options = {}) {
options.setProxy = options.setProxy === undefined ? false : options.setProxy;

this.server = net.createServer({pauseOnConnect: true}, clientSocket => {
this.server = net.createServer({ pauseOnConnect: true }, clientSocket => {
handleRequest(clientSocket, this).catch(err => {
logger.debug(String(err));
});
Expand All @@ -45,7 +50,7 @@ export default class Proxy {
this.server.listen(this.config.port, this.config.ip, () => resolve());
});

const {address, port} = this.server.address();
const { address, port } = this.server.address();
logger.debug(`server listen on ${address} port ${port}`);

if (options.setProxy) {
Expand Down