Skip to content

Commit 5a99e77

Browse files
committed
[[ ssb ]] launching multiple identity server work
- Caveat: shutting them down is buggy. dbConn tries to connect to pubs after the servers shutdown.
1 parent 91af82b commit 5a99e77

File tree

4 files changed

+157
-1
lines changed

4 files changed

+157
-1
lines changed

src/api/shutdown.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@ const lori = require("lori")
22

33
function shutdown(_useless, callback) {
44
lori.warn("Received shutdown method call")
5+
for (id in global._ssbServers) {
6+
lori.info(`Stopping SSB server for ${id}...`)
7+
const s = global._ssbServers[id]
8+
9+
s.close()
10+
11+
lori.info(`Stopped SSB server for ${id}.`)
12+
delete global._ssbServers[id]
13+
}
514
callback(null, true)
615
process.kill(process.pid, "SIGTERM")
716
}

src/common/identities.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,20 @@ function pathForIdentity(id) {
2222
return newPath
2323
}
2424

25+
function configurationForIdentity(id) {
26+
if (id[0] === "@") {
27+
id = id.slice(1)
28+
}
29+
const configPath = path.join(identitiesFolder, _.kebabCase(id.slice(0,10)), "config.toml")
30+
31+
if (!fs.existsSync(configPath)) {
32+
fs.writeFileSync(configFile, toml.stringify(minimalConfig))
33+
}
34+
35+
return toml.parse(fs.readFileSync(configPath))
36+
37+
}
38+
2539
function create() {
2640
const tempPath = path.join(identitiesFolder, "temp")
2741
const secretPath = path.join(tempPath, "secret")
@@ -71,5 +85,6 @@ module.exports = {
7185
create,
7286
remove,
7387
list,
74-
pathForIdentity
88+
pathForIdentity,
89+
configurationForIdentity
7590
}

src/sbot.js

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
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

src/server.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ const lori = require("lori")
66
const preferences = require("./common/preferences.js")
77
const startJsonRPCServer = require("./jsonrpc.js")
88
const startXMLRPCServer = require("./xmlrpc.js")
9+
const serverForIdentity = require("./sbot.js")
10+
const identities = require("./common/identities.js")
911

1012
const app = express()
1113
const port = preferences.get("server.port", 3000)
@@ -39,6 +41,20 @@ function startServer() {
3941
lori.debug("Patchfox server closed")
4042
})
4143
})
44+
45+
/* Launch all SSB identities */
46+
47+
const feeds = identities.list()
48+
49+
feeds.forEach(keys => {
50+
const c = identities.configurationForIdentity(keys.public)
51+
52+
if (c.autostart) {
53+
serverForIdentity(keys.public)
54+
} else {
55+
lori.warn(`@${keys.public} autostart is off.`)
56+
}
57+
})
4258
}
4359

4460
module.exports = startServer

0 commit comments

Comments
 (0)