mirror of https://codeberg.org/pzp/pzp-hub.git
add secret-stack peer
This commit is contained in:
parent
e36dd10e5b
commit
aa09550c75
|
@ -10,6 +10,7 @@ const fastifyView = require('@fastify/view')
|
||||||
const fastifyStatic = require('@fastify/static')
|
const fastifyStatic = require('@fastify/static')
|
||||||
const ejs = require('ejs')
|
const ejs = require('ejs')
|
||||||
const logger = require('./logger.cjs')
|
const logger = require('./logger.cjs')
|
||||||
|
const startPeer = require('./peer.cjs')
|
||||||
|
|
||||||
const staticsPath = path.join(__dirname, 'public')
|
const staticsPath = path.join(__dirname, 'public')
|
||||||
const viewsPath = path.join(__dirname, 'views')
|
const viewsPath = path.join(__dirname, 'views')
|
||||||
|
@ -29,9 +30,9 @@ app.get('/invite', (req, reply) => {
|
||||||
})
|
})
|
||||||
|
|
||||||
app.listen({ port: 3000 }, (err, address) => {
|
app.listen({ port: 3000 }, (err, address) => {
|
||||||
app.log.info(`server listening on ${address}`)
|
|
||||||
if (err) {
|
if (err) {
|
||||||
app.log.error(err)
|
app.log.error(err)
|
||||||
process.exit(1)
|
process.exit(1)
|
||||||
}
|
}
|
||||||
|
startPeer()
|
||||||
})
|
})
|
||||||
|
|
|
@ -0,0 +1,15 @@
|
||||||
|
const SecretStack = require('secret-stack')
|
||||||
|
const caps = require('ssb-caps')
|
||||||
|
|
||||||
|
module.exports = function startPeer() {
|
||||||
|
SecretStack({ appKey: caps.shs })
|
||||||
|
.use(require('ssb-conn'))
|
||||||
|
.use(require('./plugin-hub.cjs'))
|
||||||
|
.call(null, {
|
||||||
|
port: 8008,
|
||||||
|
host: '0.0.0.0',
|
||||||
|
conn: {
|
||||||
|
autostart: false,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
|
@ -0,0 +1,111 @@
|
||||||
|
const cat = require('pull-cat')
|
||||||
|
const Notify = require('pull-notify')
|
||||||
|
const pull = require('pull-stream')
|
||||||
|
const debug = require('debug')('ppppp:hub')
|
||||||
|
|
||||||
|
function ErrorDuplex(message) {
|
||||||
|
const err = new Error(message)
|
||||||
|
return {
|
||||||
|
source(_abort, cb) {
|
||||||
|
cb(err)
|
||||||
|
},
|
||||||
|
sink(read) {
|
||||||
|
read(err, () => {})
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
name: 'hub',
|
||||||
|
version: '1.0.0',
|
||||||
|
manifest: {
|
||||||
|
connect: 'duplex',
|
||||||
|
ping: 'sync',
|
||||||
|
attendants: 'source',
|
||||||
|
createToken: 'async',
|
||||||
|
},
|
||||||
|
permissions: {
|
||||||
|
anonymous: {
|
||||||
|
allow: ['connect', 'ping', 'attendants', 'createToken'],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
init(me) {
|
||||||
|
if (!me.conn || !me.conn.connect) {
|
||||||
|
throw new Error('tunnel plugin is missing the required ssb-conn plugin')
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ensure that incoming connections are only from members
|
||||||
|
me.auth.hook(function (fn, args) {
|
||||||
|
const [incomingId, cb] = args
|
||||||
|
|
||||||
|
// FIXME:
|
||||||
|
// if (members.has(incomingId)) {
|
||||||
|
// fn.apply(this, args);
|
||||||
|
// } else {
|
||||||
|
// debug('prevented stranger %s from connecting to us', incomingId);
|
||||||
|
// cb(new Error('client is a stranger'));
|
||||||
|
// }
|
||||||
|
})
|
||||||
|
|
||||||
|
const attendants = new Map()
|
||||||
|
const notifyAttendants = Notify()
|
||||||
|
|
||||||
|
pull(
|
||||||
|
me.conn.hub().listen(),
|
||||||
|
pull.filter(
|
||||||
|
({ type }) => type === 'connecting-failed' || type === 'disconnected'
|
||||||
|
),
|
||||||
|
pull.filter(({ key }) => !!key && attendants.has(key)),
|
||||||
|
pull.drain(({ key }) => {
|
||||||
|
debug('farewell %s', key)
|
||||||
|
attendants.delete(key)
|
||||||
|
notifyAttendants([...attendants.keys()])
|
||||||
|
})
|
||||||
|
)
|
||||||
|
|
||||||
|
setInterval(() => {
|
||||||
|
notifyAttendants([...attendants.keys()])
|
||||||
|
}, 10e3)
|
||||||
|
|
||||||
|
return {
|
||||||
|
attendants() {
|
||||||
|
const clientId = this.id
|
||||||
|
if (clientId && clientId !== me.id) {
|
||||||
|
debug('welcome %s', clientId)
|
||||||
|
if (!attendants.has(clientId)) {
|
||||||
|
attendants.set(clientId, me.peers[clientId][0])
|
||||||
|
notifyAttendants([...attendants.keys()])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const initial = pull.values([[...attendants.keys()]])
|
||||||
|
return cat([initial, notifyAttendants.listen()])
|
||||||
|
},
|
||||||
|
|
||||||
|
connect(opts) {
|
||||||
|
if (!opts) return ErrorDuplex('opts MUST be provided')
|
||||||
|
|
||||||
|
const target = opts.target
|
||||||
|
if (attendants.has(target)) {
|
||||||
|
debug(
|
||||||
|
'received tunnel request for target %s from %s',
|
||||||
|
target,
|
||||||
|
this.id
|
||||||
|
)
|
||||||
|
opts.origin = this.id
|
||||||
|
return attendants.get(target).tunnel.connect(opts, () => {})
|
||||||
|
} else {
|
||||||
|
return ErrorDuplex('Could not connect to: ' + target)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
ping() {
|
||||||
|
return Date.now()
|
||||||
|
},
|
||||||
|
|
||||||
|
createToken(cb) {
|
||||||
|
cb(new Error('not implemented'))
|
||||||
|
},
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
File diff suppressed because it is too large
Load Diff
|
@ -30,6 +30,12 @@
|
||||||
"ejs": "3.1.9",
|
"ejs": "3.1.9",
|
||||||
"fastify": "4.17.0",
|
"fastify": "4.17.0",
|
||||||
"pino": "8.14.1",
|
"pino": "8.14.1",
|
||||||
|
"pull-cat": "1.1.11",
|
||||||
|
"pull-notify": "0.1.2",
|
||||||
|
"pull-stream": "3.7.0",
|
||||||
|
"secret-stack": "6.4.1",
|
||||||
|
"ssb-caps": "1.1.0",
|
||||||
|
"ssb-conn": "6.0.4",
|
||||||
"unified": "10.1.2",
|
"unified": "10.1.2",
|
||||||
"remark-parse": "10.0.2",
|
"remark-parse": "10.0.2",
|
||||||
"remark-rehype": "10.1.0",
|
"remark-rehype": "10.1.0",
|
||||||
|
|
Loading…
Reference in New Issue