Skip to content

Commit

Permalink
fix: revert new identify protocol versions
Browse files Browse the repository at this point in the history
  • Loading branch information
vasco-santos committed Jul 15, 2020
1 parent c2fa11a commit d67190b
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 114 deletions.
6 changes: 1 addition & 5 deletions src/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
exports.messages = {
NOT_STARTED_YET: 'The libp2p node is not started yet',
DHT_DISABLED: 'DHT is not available',
CONN_ENCRYPTION_REQUIRED: 'At least one connection encryption module is required',
ERR_INVALID_ENVELOPE: 'Invalid envelope received',
ERR_INVALID_PEER_RECORD: 'Invalid peer record received'
CONN_ENCRYPTION_REQUIRED: 'At least one connection encryption module is required'
}

exports.codes = {
Expand All @@ -22,8 +20,6 @@ exports.codes = {
ERR_DUPLICATE_TRANSPORT: 'ERR_DUPLICATE_TRANSPORT',
ERR_ENCRYPTION_FAILED: 'ERR_ENCRYPTION_FAILED',
ERR_HOP_REQUEST_FAILED: 'ERR_HOP_REQUEST_FAILED',
ERR_INVALID_ENVELOPE: 'ERR_INVALID_ENVELOPE',
ERR_INVALID_PEER_RECORD: 'ERR_INVALID_PEER_RECORD',
ERR_INVALID_KEY: 'ERR_INVALID_KEY',
ERR_INVALID_MESSAGE: 'ERR_INVALID_MESSAGE',
ERR_INVALID_PARAMETERS: 'ERR_INVALID_PARAMETERS',
Expand Down
8 changes: 2 additions & 6 deletions src/identify/consts.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,5 @@

module.exports.PROTOCOL_VERSION = 'ipfs/0.1.0'
module.exports.AGENT_VERSION = 'js-libp2p/0.1.0'
module.exports.MULTICODEC_IDENTIFY = '/p2p/id/1.1.0'
module.exports.MULTICODEC_IDENTIFY_PUSH = '/p2p/id/push/1.1.0'

// Legacy
module.exports.MULTICODEC_IDENTIFY_1_0_0 = '/ipfs/id/1.0.0'
module.exports.MULTICODEC_IDENTIFY_PUSH_1_0_0 = '/ipfs/id/push/1.0.0'
module.exports.MULTICODEC_IDENTIFY = '/ipfs/id/1.0.0'
module.exports.MULTICODEC_IDENTIFY_PUSH = '/ipfs/id/push/1.0.0'
111 changes: 37 additions & 74 deletions src/identify/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,12 @@ const PeerRecord = require('../record/peer-record')

const {
MULTICODEC_IDENTIFY,
MULTICODEC_IDENTIFY_1_0_0,
MULTICODEC_IDENTIFY_PUSH,
MULTICODEC_IDENTIFY_PUSH_1_0_0,
AGENT_VERSION,
PROTOCOL_VERSION
} = require('./consts')

const { messages, codes } = require('../errors')
const { codes } = require('../errors')

class IdentifyService {
/**
Expand Down Expand Up @@ -97,7 +95,7 @@ class IdentifyService {
push (connections) {
const pushes = connections.map(async connection => {
try {
const { stream } = await connection.newStream([MULTICODEC_IDENTIFY_PUSH, MULTICODEC_IDENTIFY_PUSH_1_0_0])
const { stream } = await connection.newStream(MULTICODEC_IDENTIFY_PUSH)
const signedPeerRecord = await this._getSelfPeerRecord()

await pipe(
Expand Down Expand Up @@ -145,7 +143,7 @@ class IdentifyService {
* @returns {Promise<void>}
*/
async identify (connection) {
const { protocol, stream } = await connection.newStream([MULTICODEC_IDENTIFY, MULTICODEC_IDENTIFY_1_0_0])
const { stream } = await connection.newStream(MULTICODEC_IDENTIFY)
const [data] = await pipe(
[],
stream,
Expand Down Expand Up @@ -183,40 +181,26 @@ class IdentifyService {
// Get the observedAddr if there is one
observedAddr = IdentifyService.getCleanMultiaddr(observedAddr)

// LEGACY: differentiate message with SignedPeerRecord
if (protocol === MULTICODEC_IDENTIFY_1_0_0) {
// Update peers data in PeerStore
this.peerStore.addressBook.set(id, listenAddrs.map((addr) => multiaddr(addr)))
this.peerStore.protoBook.set(id, protocols)
let addresses

// TODO: Track our observed address so that we can score it
log('received observed address of %s', observedAddr)

return
}

// Open envelope and verify if is authenticated
let envelope
try {
envelope = await Envelope.openAndCertify(signedPeerRecord, PeerRecord.DOMAIN)
const envelope = await Envelope.openAndCertify(signedPeerRecord, PeerRecord.DOMAIN)
const peerRecord = await PeerRecord.createFromProtobuf(envelope.payload)

addresses = peerRecord.multiaddrs
} catch (err) {
log('received invalid envelope, discard it')
throw errCode(new Error(messages.ERR_INVALID_ENVELOPE), codes.ERR_INVALID_ENVELOPE)
log('received invalid envelope, discard it and fallback to listenAddrs is available')
// Try Legacy
addresses = listenAddrs
}

// Decode peer record
let peerRecord
// Update peers data in PeerStore
try {
peerRecord = await PeerRecord.createFromProtobuf(envelope.payload)
this.peerStore.addressBook.set(id, addresses.map((addr) => multiaddr(addr)))
} catch (err) {
log('received invalid peer record, discard it')
throw errCode(new Error(messages.ERR_INVALID_PEER_RECORD), codes.ERR_INVALID_PEER_RECORD)
log.error('received invalid addrs', err)
}

// TODO: Store as certified record

// Update peers data in PeerStore
this.peerStore.addressBook.set(id, peerRecord.multiaddrs.map((addr) => multiaddr(addr)))
this.peerStore.protoBook.set(id, protocols)

// TODO: Track our observed address so that we can score it
Expand All @@ -235,10 +219,8 @@ class IdentifyService {
handleMessage ({ connection, stream, protocol }) {
switch (protocol) {
case MULTICODEC_IDENTIFY:
case MULTICODEC_IDENTIFY_1_0_0:
return this._handleIdentify({ connection, stream })
case MULTICODEC_IDENTIFY_PUSH:
case MULTICODEC_IDENTIFY_PUSH_1_0_0:
return this._handlePush({ connection, stream })
default:
log.error('cannot handle unknown protocol %s', protocol)
Expand Down Expand Up @@ -308,45 +290,23 @@ class IdentifyService {

const id = connection.remotePeer

// Legacy
if (!message.signedPeerRecord) {
try {
this.peerStore.addressBook.set(id, message.listenAddrs.map((addr) => multiaddr(addr)))
} catch (err) {
return log.error('received invalid listen addrs', err)
}

// Update the protocols
this.peerStore.protoBook.set(id, message.protocols)

return
}
let addresses

// Open envelope and verify if is authenticated
let envelope
try {
envelope = await Envelope.openAndCertify(message.signedPeerRecord, PeerRecord.DOMAIN)
} catch (err) {
log('received invalid envelope, discard it')
throw errCode(new Error(messages.ERR_INVALID_ENVELOPE), codes.ERR_INVALID_ENVELOPE)
}
const envelope = await Envelope.openAndCertify(message.signedPeerRecord, PeerRecord.DOMAIN)
const peerRecord = await PeerRecord.createFromProtobuf(envelope.payload)

// Decode peer record
let peerRecord
try {
peerRecord = await PeerRecord.createFromProtobuf(envelope.payload)
addresses = peerRecord.multiaddrs
} catch (err) {
log('received invalid peer record, discard it')
throw errCode(new Error(messages.ERR_INVALID_PEER_RECORD), codes.ERR_INVALID_PEER_RECORD)
log('received invalid envelope, discard it and fallback to listenAddrs is available')
// Try Legacy
addresses = message.listenAddrs
}

// Update peers data in PeerStore
try {
// TODO: Store as certified record

this.peerStore.addressBook.set(id, peerRecord.multiaddrs.map((addr) => multiaddr(addr)))
this.peerStore.addressBook.set(id, addresses.map((addr) => multiaddr(addr)))
} catch (err) {
return log.error('received invalid listen addrs', err)
log.error('received invalid addrs', err)
}

// Update the protocols
Expand All @@ -358,20 +318,25 @@ class IdentifyService {
* @return {Buffer}
*/
async _getSelfPeerRecord () {
// TODO: Verify if updated
// TODO: support invalidation when dynamic multiaddrs are supported
if (this._selfRecord) {
return this._selfRecord
}

const peerRecord = new PeerRecord({
peerId: this.peerId,
multiaddrs: this._libp2p.multiaddrs
})
const envelope = await Envelope.seal(peerRecord, this.peerId)
try {
const peerRecord = new PeerRecord({
peerId: this.peerId,
multiaddrs: this._libp2p.multiaddrs
})
const envelope = await Envelope.seal(peerRecord, this.peerId)

this._selfRecord = envelope.marshal()
this._selfRecord = envelope.marshal()

return this._selfRecord
return this._selfRecord
} catch (err) {
log.error('failed to get self peer record')
}
return null
}
}

Expand All @@ -382,8 +347,6 @@ module.exports.IdentifyService = IdentifyService
*/
module.exports.multicodecs = {
IDENTIFY: MULTICODEC_IDENTIFY,
IDENTIFY_1_0_0: MULTICODEC_IDENTIFY_1_0_0,
IDENTIFY_PUSH: MULTICODEC_IDENTIFY_PUSH,
IDENTIFY_PUSH_1_0_0: MULTICODEC_IDENTIFY_PUSH_1_0_0
IDENTIFY_PUSH: MULTICODEC_IDENTIFY_PUSH
}
module.exports.Message = Message
Loading

0 comments on commit d67190b

Please sign in to comment.