2017-12-13 15:23:50 +00:00
|
|
|
"use strict";
|
|
|
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
|
|
const ipaddr = require('ipaddr.js');
|
|
|
|
|
class IPAddress {
|
|
|
|
|
constructor(buf, pos) {
|
|
|
|
|
this.ip = null;
|
|
|
|
|
this.toString = () => {
|
2017-12-13 19:55:08 +00:00
|
|
|
try {
|
|
|
|
|
return this.ip.toString();
|
|
|
|
|
}
|
|
|
|
|
catch (ignore) {
|
|
|
|
|
return '';
|
2017-12-13 15:23:50 +00:00
|
|
|
}
|
2017-12-13 19:55:08 +00:00
|
|
|
};
|
|
|
|
|
try {
|
|
|
|
|
if (buf !== undefined && buf instanceof Buffer) {
|
|
|
|
|
if (pos !== undefined) {
|
|
|
|
|
const bytes = buf.slice(pos, 4);
|
|
|
|
|
this.ip = ipaddr.fromByteArray(bytes);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
if (ipaddr.isValid(buf)) {
|
|
|
|
|
this.ip = ipaddr.parse(buf);
|
|
|
|
|
}
|
2018-10-12 14:34:43 +01:00
|
|
|
else {
|
|
|
|
|
throw new Error('Invalid IP address');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
if (ipaddr.isValid(buf)) {
|
|
|
|
|
this.ip = ipaddr.parse(buf);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
throw new Error('Invalid IP address');
|
2017-12-13 15:23:50 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-12-13 19:55:08 +00:00
|
|
|
catch (ignore) {
|
|
|
|
|
this.ip = ipaddr.parse('0.0.0.0');
|
|
|
|
|
}
|
2017-12-13 15:23:50 +00:00
|
|
|
}
|
|
|
|
|
static zero() {
|
|
|
|
|
return new IPAddress('0.0.0.0');
|
|
|
|
|
}
|
|
|
|
|
writeToBuffer(buf, pos) {
|
|
|
|
|
const bytes = this.ip.toByteArray();
|
|
|
|
|
buf.writeUInt8(bytes[0], pos++);
|
|
|
|
|
buf.writeUInt8(bytes[1], pos++);
|
|
|
|
|
buf.writeUInt8(bytes[2], pos++);
|
|
|
|
|
buf.writeUInt8(bytes[3], pos);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
exports.IPAddress = IPAddress;
|
|
|
|
|
//# sourceMappingURL=IPAddress.js.map
|