update to ppppp-net

This commit is contained in:
Andre Staltz 2024-01-12 16:26:12 +02:00
parent d5b72c3abc
commit c499e0f945
No known key found for this signature in database
GPG Key ID: 9EDE23EA7E8A4890
3 changed files with 31 additions and 65 deletions

View File

@ -85,8 +85,7 @@ module.exports = class HubObserver {
const { name, admin } = this.#hubMetadata
if (name) metadata.name = name
if (admin) metadata.admin = admin
this.#peer.conn.db().update(this.#address, metadata)
this.#peer.conn.hub().update(this.#address, metadata)
this.#peer.net.updateInfo(this.#address, metadata)
}
debug('Announcing myself to hub %s', this.#hubPubkey)
@ -126,9 +125,8 @@ module.exports = class HubObserver {
// Update onlineCount metadata for this hub
const onlineCount = this.#attendants.size
this.#peer.conn.hub().update(this.#address, { onlineCount })
this.#peer.net.updateInfo(this.#address, { onlineCount })
// Update ssb-conn-staging
const hubName = this.#hubMetadata?.name
if (event.type === 'state') {
for (const pubkey of event.pubkeys) {
@ -139,8 +137,7 @@ module.exports = class HubObserver {
} else if (event.type === 'left') {
const address = this.#getAddress(event.pubkey)
debug('Will disconnect and unstage %s', address)
this.#peer.conn.unstage(address)
this.#peer.conn.disconnect(address)
this.#peer.net.disconnect(address)
}
}
@ -156,7 +153,7 @@ module.exports = class HubObserver {
/**
* Typically, this should stage the new attendant, but it's not up to us to
* decide that. We just notify other modules (like the ssb-conn scheduler) and
* decide that. We just notify other modules (like the net scheduler) and
* they listen to the notify stream and stage it if they want.
*
* @param {string} attendantPubkey
@ -212,16 +209,11 @@ module.exports = class HubObserver {
this.#attendantsDrain?.abort()
for (const pubkey of this.#attendants) {
const address = this.#getAddress(pubkey)
this.#peer.conn.unstage(address)
}
for (const [addr, data] of this.#peer.conn.staging().entries()) {
if (data.hub === this.#hubPubkey) {
this.#peer.conn.unstage(addr)
}
this.#peer.net.forget(address)
}
this.rpc.close(true, (/** @type {any} */ err) => {
if (err) debug('error when closing connection with room: %o', err)
})
this.#peer.conn.disconnect(this.#address, () => {})
this.#peer.net.disconnect(this.#address)
}
}

View File

@ -9,6 +9,18 @@ const { muxrpcMissing } = require('./utils')
* @typedef {Map<string, HubObserver>} Hubs
*/
/**
* @param {Array<[any, any]>} parsedAddress
* @returns {string | undefined}
*/
function extractSHSEPubkey(parsedAddress) {
for (const [transport, transform] of parsedAddress) {
if (transform.name === 'shse') {
return bs58.encode(transform.pubkey)
}
}
}
/**
* @param {Hubs} hubs
* @param {any} peer
@ -28,12 +40,11 @@ const makeTunnelPlugin = (hubs, peer) => (/** @type {any}} */ msConfig) => {
server(onConnect, startedCB) {
// Once a peer connects, detect hubs, and setup observers
pull(
peer.conn.hub().listen(),
peer.net.listen(),
pull.filter(({ type }) => type === 'connected'),
pull.drain(async ({ address, key, details }) => {
if (!key) return
// TODO: once ssb-conn is replaced, we won't need this anymore:
const pubkey = key.slice(1, -8) // not base64, it's @BASE58.ed25519
pull.drain(async ({ address, parsedAddress, details }) => {
const pubkey = extractSHSEPubkey(parsedAddress)
if (!pubkey) return
if (hubs.has(pubkey)) return
if (!details?.rpc) return
if (address.startsWith('tunnel:')) return
@ -56,12 +67,11 @@ const makeTunnelPlugin = (hubs, peer) => (/** @type {any}} */ msConfig) => {
// Once a hub disconnects, teardown
pull(
peer.conn.hub().listen(),
peer.net.listen(),
pull.filter(({ type }) => type === 'disconnected'),
pull.drain(({ key }) => {
if (!key) return
// TODO: once ssb-conn is replaced, we won't need this anymore:
const pubkey = key.slice(1, -8) // not base64, it's @BASE58.ed25519
pull.drain(({ parsedAddress }) => {
const pubkey = extractSHSEPubkey(parsedAddress)
if (!pubkey) return
if (!hubs.has(pubkey)) return
hubs.get(pubkey)?.close()
hubs.delete(pubkey)
@ -101,41 +111,6 @@ const makeTunnelPlugin = (hubs, peer) => (/** @type {any}} */ msConfig) => {
hubRPC = hubs.get(hub)?.rpc
}
// If no hub found, look up hub in connDB and connect to it
if (!hubRPC) {
for (const [msaddr] of peer.conn.db().entries()) {
const pubkey = msaddr.split('~shse:')[1]
if (pubkey === hub) {
// prettier-ignore
debug(`to connect to ${addrStr} we first have to connect to ${hub}`)
const [err, rpc] = await run(peer.conn.connect)(msaddr)
if (err) {
// prettier-ignore
cb(new Error(`cant connect to ${addrStr} because cant reach the hub ${hub} due to: ` + err.message ?? err))
return
}
hubRPC = rpc
}
}
}
// If no hub found, find tunnel addr in connDB and connect to its `hub`
if (!hubRPC) {
const addrStrPlusShse = `tunnel:${hub}:${target}~shse:${target}`
const peerData = peer.conn.db().get(addrStrPlusShse)
if (peerData?.hub === hub && peerData?.hubAddress) {
// prettier-ignore
debug(`to connect to ${addrStr} we first have to connect to ${hub}`)
const [err, rpc] = await run(peer.conn.connect)(peerData.hubAddress)
if (err) {
// prettier-ignore
cb(new Error(`cant connect to ${addrStr} because cant reach the hub ${hub} due to: ` + err.message ?? err))
return
}
hubRPC = rpc
}
}
// If still no hub is found, consider it unknown
if (!hubRPC) {
// prettier-ignore
@ -158,11 +133,10 @@ const makeTunnelPlugin = (hubs, peer) => (/** @type {any}} */ msConfig) => {
* @param {any} addr
*/
parse(addr) {
if (typeof addr === 'object') return addr
const [prefix, hub, target] = addr.split(':')
if (prefix !== 'tunnel') return
return { prefix, hub, target }
if (typeof addr === 'object' && addr.name === 'tunnel') return addr
const [name, hub, target] = addr.split(':')
if (name !== 'tunnel') return
return { name, hub, target }
},
stringify() {

View File

@ -7,7 +7,7 @@ const { ErrorDuplex } = require('./utils')
module.exports = {
name: 'hubClient',
// needs: ['conn'], // FIXME: uncomment once we re-write conn
needs: ['net'],
manifest: {
connect: 'duplex',
},