-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.js
135 lines (124 loc) · 3.62 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
'use strict';
const udp = require('dgram');
const util = require('util');
const Packet = require('./packet');
const EventEmitter = require('events');
const sleep = ms =>
new Promise(done => setTimeout(done, ms));
class NTPTimeoutError extends Error {
name = 'NTP Timeout Error'
}
/**
* [NTPClient description]
* @docs https://tools.ietf.org/html/rfc2030
*/
function NTP(options, callback) {
if (!(this instanceof NTP))
return new NTP(options, callback);
EventEmitter.call(this);
if (typeof options === 'function') {
callback = options;
options = {};
}
const { socketType } = Object.assign(this, {
server: 'pool.ntp.org',
port: 123,
socketType: 'udp4',
}, options);
this.socket = udp.createSocket(socketType);
if (typeof callback === 'function')
this.time(callback);
return this;
};
util.inherits(NTP, EventEmitter);
/**
* [time description]
* @param {Function} callback [description]
* @return {[type]} [description]
*/
NTP.time = function (options, callback) {
return new NTP(options, callback);
};
/**
* [time description]
* @param {Function} callback [description]
* @return {[type]} [description]
*/
NTP.prototype.time = function (callback) {
const { server, port, timeout = 400 } = this;
const packet = NTP.createPacket();
const t = sleep(timeout).then(() => {
throw new NTPTimeoutError();
});
const p = new Promise((resolve, reject) => {
this.socket.send(packet, 0, packet.length, port, server, err => {
if (err) return reject(err);
this.socket.once('message', data => {
const message = NTP.parse(data);
resolve(message);
});
});
});
return Promise
.race([p, t])
.then(res => {
this.socket.close();
callback && callback(null, res);
return res;
}, err => {
this.socket.close();
callback && callback(err);
throw err;
});
};
/**
* [createPacket description]
* @return {[type]} [description]
*/
NTP.createPacket = function () {
const packet = new Packet();
packet.mode = Packet.MODES.CLIENT;
packet.writeMsecs(packet.originateTimestamp, Date.now());
return packet.toBuffer();
};
/**
* [parse description]
* @param {Function} callback [description]
* @param {[type]} msg [description]
* @return {[type]} [description]
*/
NTP.parse = function (buffer) {
// const SEVENTY_YEARS = 2208988800;
// var secsSince1900 = buffer.readUIntBE(40, 4);
// var epoch = secsSince1900 - SEVENTY_YEARS;
// var date = new Date(0);
// date.setUTCSeconds(epoch);
// return date;
const message = Packet.parse(buffer);
// Timestamp Name ID When Generated
// ------------------------------------------------------------
// Originate Timestamp T1 time request sent by client
// Receive Timestamp T2 time request received by server
// Transmit Timestamp T3 time reply sent by server
// Destination Timestamp T4 time reply received by client
const T1 = message.toMsecs(message.originateTimestamp);
const T2 = message.toMsecs(message.receiveTimestamp);
const T3 = message.toMsecs(message.transmitTimestamp);
const T4 = message.toMsecs(Date.now());
// The roundtrip delay d and system clock offset t are defined as:
// -
// d = (T4 - T1) - (T3 - T2) t = ((T2 - T1) + (T3 - T4)) / 2
message.d = (T4 - T1) - (T3 - T2);
message.t = ((T2 - T1) + (T3 - T4)) / 2;
return message;
};
NTP.Client = NTP;
NTP.Server = require('./server');
/**
* [createServer description]
* @return {[type]} [description]
*/
NTP.createServer = function (options) {
return new NTP.Server(options);
};
module.exports = NTP;