Skip to content

Commit

Permalink
no message
Browse files Browse the repository at this point in the history
  • Loading branch information
BoLaMN committed Jan 8, 2020
1 parent 625b83d commit eeef833
Show file tree
Hide file tree
Showing 12 changed files with 379 additions and 122 deletions.
6 changes: 3 additions & 3 deletions dist/dhcp/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
var args, server, toHexArray;

args = require('../args');
var path, server, toHexArray;

server = require('./server');

path = require('path');

toHexArray = function(str) {
return str.split('').map(function(d, i) {
return str.charCodeAt(i);
Expand Down
11 changes: 6 additions & 5 deletions dist/dhcp/options.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var Tools, attr, conf, i, opts;
var Tools, attr, conf, i, opts, v;

Tools = require('./tools');

Expand Down Expand Up @@ -507,10 +507,11 @@ conf = {};
attr = {};

for (i in opts) {
if (opts[i].config) {
conf[opts[i].config] = parseInt(i, 10);
} else if (opts[i].attr) {
conf[opts[i].config] = parseInt(i, 10);
v = opts[i];
if (v.config) {
conf[v.config] = parseInt(i, 10);
} else if (v.attr) {
conf[v.attr] = parseInt(i, 10);
}
}

Expand Down
11 changes: 6 additions & 5 deletions dist/dhcp/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ Server = (function(superClass) {
};

Server.prototype.sendAck = function(req) {
var ans;
var ans, options;
if (req.options[97] && req.options[55].indexOf(97) === -1) {
req.options[55].push(97);
}
Expand All @@ -313,6 +313,9 @@ Server = (function(superClass) {
}
});
}
options = this._getOptions({
53: DHCPACK
}, [1, 3, 51, 54, 6], req.options[55]);
ans = {
op: BOOTREPLY,
htype: 1,
Expand All @@ -327,10 +330,8 @@ Server = (function(superClass) {
giaddr: req.giaddr,
chaddr: req.chaddr,
sname: '',
file: '',
options: this._getOptions({
53: DHCPACK
}, [1, 3, 51, 54, 6], req.options[55])
file: req.file,
options: options
};
return this._send(this.config('broadcast'), ans, (function(_this) {
return function() {
Expand Down
8 changes: 6 additions & 2 deletions dist/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var args, ask, dhcpd, httpd, ip, pkg, port;
var args, ask, dhcpd, httpd, ip, pkg, port, tftp;

pkg = require('../package.json');

Expand All @@ -16,7 +16,11 @@ httpd = require('./http');

port = require('./get-port');

if (args.dhcponly) {
tftp = require('./tftp');

if (args.tftp) {
tftp(args);
} else if (args.dhcponly) {
dhcpd(ip, args.acsurl, args.acspass);
} else {
ask(ip).then(port).then(function(p) {
Expand Down
11 changes: 7 additions & 4 deletions dist/ips.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ var networkInterfaces;
networkInterfaces = require('os').networkInterfaces;

module.exports = function() {
var addr, address, details, family, i, internal, k, len, name, obj, ref, ref1, s, t, v;
var addr, address, base, details, family, i, internal, k, len, name, obj, ref, ref1, s, t, v;
addr = [];
obj = {};
ref = networkInterfaces();
Expand All @@ -16,11 +16,14 @@ module.exports = function() {
ref1 = details[i], family = ref1.family, internal = ref1.internal, address = ref1.address;
if (!internal) {
if (!address.startsWith('2001')) {
obj[name][family] = {
if ((base = obj[name])[family] == null) {
base[family] = [];
}
obj[name][family].push({
name: name,
address: address,
family: family
};
});
}
}
}
Expand All @@ -30,7 +33,7 @@ module.exports = function() {
if (v.IPv4 != null) {
for (s in v) {
t = v[s];
addr.push(t);
addr.push.apply(addr, t);
}
}
}
Expand Down
76 changes: 76 additions & 0 deletions dist/ntp/client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
'use strict';
var EventEmitter, NTP, Packet, createSocket,
extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
hasProp = {}.hasOwnProperty;

Packet = require('./packet');

createSocket = require('dgram').createSocket;

EventEmitter = require('events').EventEmitter;

NTP = (function(superClass) {
extend(NTP, superClass);

function NTP(options, callback) {
if (typeof options === 'function') {
callback = options;
options = {};
}
Object.assign(this, {
server: '127.0.0.1',
port: 123
}, options);
this.socket = new createSocket('udp4');
if (typeof callback === 'function') {
this.time(callback);
}
}

NTP.prototype.time = function(callback) {
var packet, port, ref, server, timeout;
ref = this, server = ref.server, port = ref.port, timeout = ref.timeout;
packet = NTP.createPacket();
this.socket.send(packet, 0, packet.length, port, server, (function(_this) {
return function(err) {
if (err) {
return callback(err);
}
return _this.socket.once('message', function(data) {
var message;
message = NTP.parse(data);
return callback(err, message);
});
};
})(this));
return this;
};

NTP.time = function(options, callback) {
return new NTP(options, callback);
};

NTP.createPacket = function() {
var packet;
packet = new Packet;
packet.mode = Packet.MODES.CLIENT;
return packet.toBuffer();
};

NTP.parse = function(buffer) {
var T1, T2, T3, T4, message;
message = Packet.parse(buffer);
T1 = message.originateTimestamp;
T2 = message.receiveTimestamp;
T3 = message.transmitTimestamp;
T4 = message.destinationTimestamp;
message.d = T4 - T1 - (T3 - T2);
message.t = (T2 - T1 + T3 - T4) / 2;
return message;
};

return NTP;

})(EventEmitter);

module.exports = NTP;
93 changes: 10 additions & 83 deletions dist/ntp/index.js
Original file line number Diff line number Diff line change
@@ -1,88 +1,15 @@
'use strict';
var EventEmitter, NTP, Packet, createSocket,
extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
hasProp = {}.hasOwnProperty;
var createServer, ntp;

Packet = require('./packet');
createServer = require('./server').createServer;

createSocket = require('dgram').createSocket;
ntp = createServer(function(message, response) {
console.log('server message:', message);
return response(message);
});

EventEmitter = require('events').EventEmitter;
ntp.listen(123, function(err) {
return console.log('ntp server is running at %s', ntp.address().port);
});

NTP = (function(superClass) {
extend(NTP, superClass);

function NTP(options, callback) {
if (typeof options === 'function') {
callback = options;
options = {};
}
Object.assign(this, {
server: 'pool.ntp.org',
port: 123
}, options);
this.socket = new createSocket('udp4');
if (typeof callback === 'function') {
this.time(callback);
}
}

NTP.prototype.time = function(callback) {
var packet, port, ref, server, timeout;
ref = this, server = ref.server, port = ref.port, timeout = ref.timeout;
packet = NTP.createPacket();
this.socket.send(packet, 0, packet.length, port, server, (function(_this) {
return function(err) {
if (err) {
return callback(err);
}
return _this.socket.once('message', function(data) {
var message;
this.socket.close();
message = NTP.parse(data);
return callback(err, message);
});
};
})(this));
return this;
};

NTP.time = function(options, callback) {
return new NTP(options, callback);
};

NTP.createPacket = function() {
var packet;
packet = new Packet;
packet.mode = Packet.MODES.CLIENT;
packet.originateTimestamp = Date.now();
return packet.toBuffer();
};

NTP.parse = function(buffer) {
var T1, T2, T3, T4, message;
message = Packet.parse(buffer);
message.destinationTimestamp = Date.now();
message.time = new Date(message.transmitTimestamp);
T1 = message.originateTimestamp;
T2 = message.receiveTimestamp;
T3 = message.transmitTimestamp;
T4 = message.destinationTimestamp;
message.d = T4 - T1 - (T3 - T2);
message.t = (T2 - T1 + T3 - T4) / 2;
return message;
};

return NTP;

})(EventEmitter);

exports.Client = NTP;

exports.Server = require('./server');

exports.createServer = function(options) {
return new exports.Server(options);
};

module.exports = NTP;
module.exports = ntp;
24 changes: 22 additions & 2 deletions dist/ntp/packet.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var Packet, SEVENTY_YEARS, assert, toMsecs, writeMsecs;
var Packet, SEVENTY_YEARS, after, assert, before, toMsecs, writeMsecs;

assert = require('assert');

Expand All @@ -23,7 +23,7 @@ toMsecs = function(buffer, offset) {

writeMsecs = function(buffer, offset, ts) {
var fraction, seconds;
seconds = Math.floor(ts / 1000) + SEVENTY_YEARS;
seconds = Math.floor(ts / 1000) + SEVENTY_YEARS - SEVENTY_YEARS;
fraction = Math.round(ts % 1000 / 1000 * Math.pow(2, 32));
buffer[offset + 0] = (seconds & 0xFF000000) >> 24;
buffer[offset + 1] = (seconds & 0x00FF0000) >> 16;
Expand All @@ -36,6 +36,26 @@ writeMsecs = function(buffer, offset, ts) {
return buffer;
};

before = function(val) {
var value;
value = parseInt(val.toString().split('.')[0], 10);
if (value) {
return value;
} else {
return 0;
}
};

after = function(val) {
var value;
value = parseInt(val.toString().split('.')[1], 10);
if (value) {
return value;
} else {
return 0;
}
};

Packet = (function() {
Packet.MODES = {
CLIENT: 3,
Expand Down
16 changes: 14 additions & 2 deletions dist/ntp/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ Packet = require('./packet');
NTPServer = (function(superClass) {
extend(NTPServer, superClass);

NTPServer.createServer = function(options) {
return new NTPServer(options);
};

function NTPServer(options, onRequest) {
NTPServer.__super__.constructor.call(this);
if (typeof options === 'function') {
Expand Down Expand Up @@ -39,19 +43,27 @@ NTPServer = (function(superClass) {
};

NTPServer.prototype.send = function(rinfo, message, callback) {
if (callback == null) {
callback = function() {};
}
if (message instanceof Packet) {
message.mode = Packet.MODES.SERVER;
message = message.toBuffer();
}
console.log('response', message, 0, message.length, rinfo.port, rinfo.address);
this.socket.send(message, 0, message.length, rinfo.port, rinfo.address);
this.socket.send(message, rinfo.port, rinfo.server, callback);
return this;
};

NTPServer.prototype.parse = function(message, rinfo) {
var packet;
packet = Packet.parse(message);
packet.receiveTimestamp = Date.now();
this.emit('request', packet, this.send.bind(this, rinfo));
this.send(rinfo, packet, function(err) {
if (err) {
return console.error(err);
}
});
return this;
};

Expand Down
Loading

0 comments on commit eeef833

Please sign in to comment.