Add support for Unix socket with noauth

Problem: We're doing tons of unnecessary cryptography by encrypting the
connection between the "client" and "server", which are often running in
the same process.

Solution: Instead of connecting to the SSB service over TCP and
encrypting the stream, just connect over a socket (supported on Windows,
macOS, and Linux) and don't bother encrypting anything. This is what
Patchwork and Patchbay do already, and since our secret is at
`~/.ssb/secret` then we should be comfortable with `~/.ssb/socket` being
a trusted file where access implies authentication.

Local tests suggest that when sodium-native is available, this commit
reduces the time to render the 'Popular (Day)' page by 17%, but when we
have to fall back to JavaScript cryptography the same page now takes 30%
less time to render. My intuition is that this improvement is more
dramatic on mobile, but requires further testing before we can pat
ourselves on the back too much. :)
This commit is contained in:
Christian Bundy 2020-02-21 09:04:41 -08:00
parent bbbf3f8808
commit ed5e6e5f61
2 changed files with 15 additions and 2 deletions

View File

@ -28,6 +28,7 @@
"multiserver",
"muxrpc",
"nanohtml",
"noauth",
"nosniff",
"paramap",
"patchbay",

View File

@ -10,6 +10,16 @@ const ssbConfig = require("ssb-config");
const flotilla = require("@fraction/flotilla");
const ssbTangle = require("ssb-tangle");
const debug = require("debug")("oasis");
const path = require("path");
const socketPath = path.join(ssbConfig.path, "socket");
const publicInteger = ssbConfig.keys.public.replace(".ed25519", "");
const remote = `unix:${socketPath}~noauth:${publicInteger}`;
// This is unnecessary when https://github.com/ssbc/ssb-config/pull/72 is merged
ssbConfig.connections.incoming.unix = [
{ scope: "device", transform: "noauth" }
];
const server = flotilla(ssbConfig);
@ -22,7 +32,7 @@ const log = (...args) => {
const rawConnect = () =>
new Promise((resolve, reject) => {
ssbClient()
ssbClient(null, { remote })
.then(api => {
if (api.tangle === undefined) {
// HACK: SSB-Tangle isn't available in Patchwork, but we want that
@ -64,7 +74,9 @@ const createConnection = config => {
resolve(ssb);
})
.catch(e => {
log(e);
if (e.message !== "could not connect to sbot") {
log(e);
}
connectOrRetry();
});
};