Skip to content

Commit

Permalink
chore(api): update protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
heapwolf committed Jul 9, 2024
1 parent 353657e commit 218db64
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 19 deletions.
43 changes: 26 additions & 17 deletions api/latica/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ export class Peer {
this.encryption = new Encryption()

if (!config.peerId) throw new Error('constructor expected .peerId')
if (typeof config.peerId !== 'string' || !PEERID_REGEX.test(config.peerId)) throw new Error(`invalid .peerId (${config.peerId})`)
if (!Peer.isValidPeerId(config.peerId)) throw new Error(`invalid .peerId (${config.peerId})`)

//
// The purpose of this.config is to seperate transitioned state from initial state.
Expand Down Expand Up @@ -1008,7 +1008,7 @@ export class Peer {

// set the .packetId (any maybe the .previousId and .nextId)
for (let i = 0; i < packets.length; i++) {
packets[i].index = i
if (packets.length > 1) packets[i].index = i

if (i === 0) {
packets[0].packetId = await sha256(packets[0].message, { bytes: true })
Expand Down Expand Up @@ -1405,18 +1405,13 @@ export class Peer {
this.metrics.i[packet.type]++

this.lastUpdate = Date.now()
const { reflectionId, isReflection, isConnection, isHeartbeat } = packet.message
const { reflectionId, isReflection, isConnection, requesterPeerId } = packet.message

if (packet.message.requesterPeerId === this.peerId) return
if (!Peer.isValidPeerId(requesterPeerId)) return // required field
if (requesterPeerId === this.peerId) return // from self?

const { probeExternalPort, isProbe, pingId } = packet.message

if (isHeartbeat) {
// const peer = this.getPeer(packet.message.requesterPeerId)
// if (peer && natType) peer.natType = natType
return
}

// if (peer && reflectionId) peer.reflectionId = reflectionId
if (!port) port = packet.message.port
if (!address) address = packet.message.address
Expand All @@ -1425,14 +1420,13 @@ export class Peer {
cacheSize: this.cache.size,
uptime: this.uptime,
responderPeerId: this.peerId,
requesterPeerId: packet.message.requesterPeerId,
requesterPeerId,
port,
isProbe,
address
}

if (reflectionId) message.reflectionId = reflectionId
if (isHeartbeat) message.isHeartbeat = Date.now()
if (pingId) message.pingId = pingId

if (isReflection) {
Expand All @@ -1444,8 +1438,7 @@ export class Peer {
}

if (isConnection) {
const peerId = packet.message.requesterPeerId
this._onConnection(packet, peerId, port, address)
this._onConnection(packet, requesterPeerId, port, address)

message.isConnection = true
delete message.address
Expand Down Expand Up @@ -1481,12 +1474,15 @@ export class Peer {

const { reflectionId, pingId, isReflection, responderPeerId } = packet.message

if (!Peer.isValidPeerId(responderPeerId)) return // required field
if (responderPeerId === this.peerId) return // from self?

this._onDebug(`<- PONG (from=${address}:${port}, hash=${packet.message.cacheSummaryHash}, isConnection=${!!packet.message.isConnection})`)
const peer = this.getPeer(packet.message.responderPeerId)
const peer = this.getPeer(responderPeerId)

if (packet.message.isConnection) {
if (pingId) peer.pingId = pingId
this._onConnection(packet, packet.message.responderPeerId, port, address)
this._onConnection(packet, responderPeerId, port, address)
return
}

Expand Down Expand Up @@ -1594,6 +1590,8 @@ export class Peer {

if (packet.hops >= this.maxHops) return
if (!isNaN(ts) && ((ts + this.config.keepalive) < Date.now())) return
if (!Peer.isValidPeerId(packet.message.requesterPeerId)) return
if (!Peer.isValidPeerId(packet.message.responderPeerId)) return
if (packet.message.requesterPeerId === this.peerId) return // intro to myself?
if (packet.message.responderPeerId === this.peerId) return // intro from myself?

Expand Down Expand Up @@ -1813,7 +1811,8 @@ export class Peer {
this.metrics.i[packet.type]++

const pid = packet.packetId.toString('hex')
if (packet.message.requesterPeerId === this.peerId) return
if (!Peer.isValidPeerId(packet.message.requesterPeerId)) return // required field
if (packet.message.requesterPeerId === this.peerId) return // from self?
if (this.gate.has(pid)) return
if (packet.clusterId.length !== 32) return

Expand Down Expand Up @@ -2218,6 +2217,16 @@ export class Peer {
case PacketQuery.type: return this._onQuery(...args)
}
}

/**
* Test a peerID is valid
*
* @param {string} pid
* @returns boolean
*/
static isValidPeerId (pid) {
return typeof pid === 'string' && PEERID_REGEX.test(pid)
}
}

export default Peer
2 changes: 0 additions & 2 deletions api/latica/packets.js
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,6 @@ export class PacketPing extends Packet {
pingId: { type: 'string' },
natType: { type: 'number' },
uptime: { type: 'number' },
isHeartbeat: { type: 'number' },
cacheSize: { type: 'number' },
isConnection: { type: 'boolean' },
isReflection: { type: 'boolean' },
Expand All @@ -414,7 +413,6 @@ export class PacketPong extends Packet {
cacheSize: { type: 'number' },
natType: { type: 'number' },
isReflection: { type: 'boolean' },
isHeartbeat: { type: 'number' },
isConnection: { type: 'boolean' },
reflectionId: { type: 'string' },
pingId: { type: 'string' },
Expand Down

0 comments on commit 218db64

Please sign in to comment.