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
1 parent
b636f40
commit f900a59
Showing
5 changed files
with
227 additions
and
5 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
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
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,127 @@ | ||
'use strict'; | ||
|
||
const nanoresource = require('nanoresource/emitter'); | ||
const random = require('random-int'); | ||
const dns = require('dns-packet'); | ||
const UDPSocket = require('./udp-socket'); | ||
const { validateCertificate } = require('../certificate'); | ||
|
||
/** | ||
* @typedef {Object} UDPClientOptions | ||
* @property {Session} session Instance of dnscrypt session. | ||
*/ | ||
|
||
/** | ||
* DNSCrypt UDP client abstraction. | ||
*/ | ||
module.exports = class UDPClient extends nanoresource { | ||
/** | ||
* @class {UDPClient} | ||
* @param {UDPClientOptions} options | ||
*/ | ||
constructor(options) { | ||
super(); | ||
|
||
this.session = options.session; | ||
this.socket = new UDPSocket({ | ||
port: options.session.serverPort, | ||
address: options.session.serverAddress, | ||
}); | ||
} | ||
|
||
/** | ||
* @private | ||
* @param {Function} callback | ||
*/ | ||
_open(callback) { | ||
const packet = { | ||
id: random(1, 0xffff), | ||
type: 'query', | ||
questions: [ | ||
{ | ||
type: 'TXT', | ||
name: this.session.resolver.providerName, | ||
}, | ||
], | ||
}; | ||
|
||
this._certificate(packet, (err, certificate) => { | ||
if (err) { | ||
this.socket.close(() => callback(err)); | ||
return; | ||
} | ||
|
||
this.session.certificate = certificate; | ||
callback(null); | ||
}); | ||
} | ||
|
||
/** | ||
* @private | ||
* @param {Function} callback | ||
*/ | ||
_close(callback) { | ||
this.socket.close(() => callback()); | ||
} | ||
|
||
/** | ||
* Query certificate. | ||
* @param {Object} query DNS query. | ||
* @param {Function} callback | ||
* @private | ||
*/ | ||
_certificate(query, callback) { | ||
if (!this.active(callback)) { | ||
return; | ||
} | ||
|
||
const packet = dns.encode(query); | ||
const timer = setTimeout(() => { | ||
cleanup(this); | ||
this.inactive(callback, new Error('ETIMEDOUT')); | ||
}, this.session.queryTimeout); | ||
|
||
const onmessage = data => { | ||
let response; | ||
|
||
try { | ||
response = dns.decode(data); | ||
} catch (_) { | ||
return; | ||
} | ||
|
||
if (response.id !== query.id) { | ||
return; | ||
} | ||
|
||
let certificate; | ||
|
||
try { | ||
certificate = validateCertificate(response, this.session.resolver); | ||
} catch (error) { | ||
cleanup(this); | ||
this.inactive(callback, error); | ||
return; | ||
} | ||
|
||
cleanup(this); | ||
this.inactive(callback, null, certificate); | ||
}; | ||
|
||
this.socket.on('data', onmessage); | ||
this.socket.write(packet, err => { | ||
if (err) { | ||
cleanup(this); | ||
return this.inactive(callback, err); | ||
} | ||
}); | ||
|
||
/** | ||
* @param {UDPClient} client | ||
*/ | ||
function cleanup(client) { | ||
clearTimeout(timer); | ||
client.socket.off('data', onmessage); | ||
} | ||
} | ||
}; |
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
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,20 @@ | ||
'use strict'; | ||
|
||
const UDPClient = require('../src/transport/udp-client'); | ||
const { Session } = require('../src/session'); | ||
|
||
describe('udp client', () => { | ||
test('should work', done => { | ||
const session = new Session(4e3); | ||
const client = new UDPClient({ | ||
session, | ||
}); | ||
|
||
client.open(err => { | ||
expect(err).toBeFalsy(); | ||
expect(session.certificate).not.toBeNull(); | ||
|
||
client.close(done); | ||
}); | ||
}); | ||
}); |