Browse Source

add version

v4
Mathias Buus 5 years ago
parent
commit
f9293beb69
  1. 1
      index.js
  2. 10
      lib/io.js
  3. 22
      lib/messages.js
  4. 2
      schema.proto

1
index.js

@ -113,6 +113,7 @@ class DHT extends EventEmitter {
if (value.to) {
const to = decodePeer(value.to)
if (!to || samePeer(to, peer)) return
message.version = IO.VERSION
message.id = this._io.id
message.value = Holepunch.encode({ from: peers.encode([peer]) })
this.emit('holepunch', peer, to)

10
lib/io.js

@ -4,6 +4,7 @@ const peers = require('ipv4-peers')
const sodium = require('sodium-universal')
const speedometer = require('speedometer')
const VERSION = 1
const QUERY = Symbol('QUERY')
const UPDATE = Symbol('UPDATE')
@ -76,6 +77,7 @@ class IO {
: this._free()
const punch = {
version: VERSION,
type: req.message.type,
rid,
command: '_holepunch',
@ -98,6 +100,8 @@ class IO {
const message = decodeMessage(buf)
if (!message) return
if (message.id && message.id.length !== 32) return
// Force eph if older version
if (message.id && !(message.version >= VERSION)) message.id = null
const peer = { port: rinfo.port, host: rinfo.address }
@ -239,6 +243,7 @@ class IO {
response (request, value, closerNodes, peer) {
const message = {
version: VERSION,
type: TYPE.RESPONSE,
rid: request.rid,
to: peers.encode([peer]),
@ -252,6 +257,7 @@ class IO {
error (request, error, closerNodes, peer, value) {
const message = {
version: VERSION,
type: TYPE.RESPONSE,
rid: request.rid,
to: peers.encode([peer]),
@ -267,6 +273,7 @@ class IO {
if (!callback) callback = noop
this._request({
version: VERSION,
type: TYPE.QUERY,
rid: 0,
to: encodeIP(peer),
@ -281,6 +288,7 @@ class IO {
if (!callback) callback = noop
this._requestImmediately({
version: VERSION,
type: TYPE.QUERY,
rid: 0,
to: encodeIP(peer),
@ -295,6 +303,7 @@ class IO {
if (!callback) callback = noop
this._request({
version: VERSION,
type: TYPE.UPDATE,
rid: 0,
to: encodeIP(peer),
@ -309,6 +318,7 @@ class IO {
IO.QUERY = QUERY
IO.UPDATE = UPDATE
IO.VERSION = VERSION
module.exports = IO

22
lib/messages.js

@ -109,6 +109,10 @@ function defineMessage () {
function encodingLength (obj) {
var length = 0
if (defined(obj.version)) {
var len = encodings.varint.encodingLength(obj.version)
length += 1 + len
}
if (!defined(obj.type)) throw new Error("type is required")
var len = encodings.enum.encodingLength(obj.type)
length += 1 + len
@ -154,6 +158,11 @@ function defineMessage () {
if (!offset) offset = 0
if (!buf) buf = Buffer.allocUnsafe(encodingLength(obj))
var oldOffset = offset
if (defined(obj.version)) {
buf[offset++] = 88
encodings.varint.encode(obj.version, buf, offset)
offset += encodings.varint.encode.bytes
}
if (!defined(obj.type)) throw new Error("type is required")
buf[offset++] = 8
encodings.enum.encode(obj.type, buf, offset)
@ -212,6 +221,7 @@ function defineMessage () {
if (!(end <= buf.length && offset <= buf.length)) throw new Error("Decoded message is not valid")
var oldOffset = offset
var obj = {
version: 0,
type: 1,
rid: 0,
to: null,
@ -223,11 +233,11 @@ function defineMessage () {
error: "",
value: null
}
var found0 = false
var found1 = false
var found2 = false
while (true) {
if (end <= offset) {
if (!found0 || !found1) throw new Error("Decoded message is not valid")
if (!found1 || !found2) throw new Error("Decoded message is not valid")
decode.bytes = offset - oldOffset
return obj
}
@ -235,15 +245,19 @@ function defineMessage () {
offset += varint.decode.bytes
var tag = prefix >> 3
switch (tag) {
case 11:
obj.version = encodings.varint.decode(buf, offset)
offset += encodings.varint.decode.bytes
break
case 1:
obj.type = encodings.enum.decode(buf, offset)
offset += encodings.enum.decode.bytes
found0 = true
found1 = true
break
case 2:
obj.rid = encodings.varint.decode(buf, offset)
offset += encodings.varint.decode.bytes
found1 = true
found2 = true
break
case 10:
obj.to = encodings.bytes.decode(buf, offset)

2
schema.proto

@ -10,6 +10,8 @@ enum TYPE {
}
message Message {
optional uint64 version = 11;
// request/response type + id
required TYPE type = 1;
required uint64 rid = 2;

Loading…
Cancel
Save