|
| 1 | +/** |
| 2 | + * Code reworked from Perihelion server by Nico. |
| 3 | + * |
| 4 | + * Original source: https://github.com/nsantini/perihelion |
| 5 | + */ |
| 6 | + |
| 7 | +const SecretStack = require("secret-stack") |
| 8 | +const Config = require("ssb-config/inject") |
| 9 | +const caps = require("ssb-caps") |
| 10 | +const lori = require("lori") |
| 11 | +const path = require("path") |
| 12 | +const fs = require("fs-extra") |
| 13 | +const {pathForIdentity, configurationForIdentity} = require("./common/identities.js") |
| 14 | + |
| 15 | +let servers = 0 |
| 16 | + |
| 17 | +function serverForIdentity(feedid) { |
| 18 | + if (!Array.isArray(global._ssbServers)) { |
| 19 | + global._ssbServers = [] |
| 20 | + } |
| 21 | + |
| 22 | + if (global._ssbServers[feedid]) { |
| 23 | + return global._ssbServers[feedid] |
| 24 | + } |
| 25 | + |
| 26 | + const folder = pathForIdentity(feedid) |
| 27 | + const secret = path.join(folder, "secret") |
| 28 | + |
| 29 | + if (!fs.existsSync(secret)) { |
| 30 | + lori.error(`Secret not found for ${feedid}.`) |
| 31 | + return false |
| 32 | + } |
| 33 | + |
| 34 | + |
| 35 | + process.on("uncaughtException", function (err) { |
| 36 | + console.log(err) |
| 37 | + lori.error(err) |
| 38 | + }) |
| 39 | + |
| 40 | + const port = 26831 + servers |
| 41 | + servers = servers + 1 |
| 42 | + |
| 43 | + const ssb = SecretStack({ caps }) |
| 44 | + // Core |
| 45 | + .use(require("ssb-master")) |
| 46 | + .use(require("ssb-db2")) |
| 47 | + .use(require("ssb-db2/compat")) |
| 48 | + .use(require("ssb-db2/about-self")) // include index |
| 49 | + // Replication |
| 50 | + .use(require("ssb-ebt")) // needs: db2/compat |
| 51 | + .use(require("ssb-friends")) // needs: db2 |
| 52 | + .use(require("ssb-replicate")) |
| 53 | + .use(require("ssb-replication-scheduler")) // needs: friends, ebt |
| 54 | + // Connections |
| 55 | + .use(require("ssb-conn")) |
| 56 | + .use(require("ssb-lan")) |
| 57 | + .use(require("ssb-room-client")) // needs: conn |
| 58 | + .use(require("ssb-http-auth-client")) // needs: conn |
| 59 | + .use(require("ssb-http-invite-client")) |
| 60 | + .use(require("ssb-invite-client")) // needs: db2, conn |
| 61 | + // Queries |
| 62 | + .use(require("ssb-threads")) // needs: db, db2, friends |
| 63 | + // .use(require('ssb-search2')) // needs: db2 |
| 64 | + .use(require("ssb-blobs")) |
| 65 | + |
| 66 | + const ssbConfig = Config("patchfox", { |
| 67 | + path: folder, |
| 68 | + db2: { |
| 69 | + automigrate: true, |
| 70 | + dangerouslyKillFlumeWhenMigrated: true, |
| 71 | + }, |
| 72 | + replicate: { |
| 73 | + legacy: true, |
| 74 | + fallback: true, |
| 75 | + }, |
| 76 | + connections: { |
| 77 | + incoming: { |
| 78 | + net: [{ scope: "private", transform: "shs", port: port }], |
| 79 | + channel: [{ scope: "device", transform: "noauth" }], |
| 80 | + bluetooth: [{ scope: "public", transform: "shs" }], |
| 81 | + tunnel: [{ scope: "public", transform: "shs" }], |
| 82 | + }, |
| 83 | + outgoing: { |
| 84 | + net: [{ transform: "shs" }], |
| 85 | + ws: [{ transform: "shs" }], |
| 86 | + bluetooth: [{ scope: "public", transform: "shs" }], |
| 87 | + tunnel: [{ transform: "shs" }], |
| 88 | + }, |
| 89 | + }, |
| 90 | + }) |
| 91 | + |
| 92 | + global._ssbServers[feedid] = ssb(ssbConfig); |
| 93 | + |
| 94 | + // Connect to pubs |
| 95 | + const connectToPubs = () => { |
| 96 | + global._ssbServers[feedid].conn.dbPeers().forEach(([addr, data]) => { |
| 97 | + if (data.type === "pub" && data.autoconnect) { |
| 98 | + lori.debug(`Connecting to Pub ${data.name}: ${data.key}`) |
| 99 | + global._ssbServers[feedid].conn.connect(addr, (err, connData) => { |
| 100 | + if (err) console.error(err); |
| 101 | + else { |
| 102 | + console.log("Connected", connData.id); |
| 103 | + } |
| 104 | + }) |
| 105 | + } |
| 106 | + }) |
| 107 | + } |
| 108 | + |
| 109 | + setTimeout(connectToPubs, 5000) |
| 110 | + |
| 111 | + lori.info(`SSB server for ${feedid} on port ${port}`) |
| 112 | + |
| 113 | + return global._ssbServers[feedid] |
| 114 | +} |
| 115 | + |
| 116 | +module.exports = serverForIdentity |
0 commit comments