-
-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathserver.js
More file actions
143 lines (114 loc) · 3.52 KB
/
Copy pathserver.js
File metadata and controls
143 lines (114 loc) · 3.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// Code adapted from ssb-ahoy
// URL: https://github.com/ssbc/ssb-ahoy/
const secretStack = require("secret-stack")
const caps = require("ssb-caps")
const merge = require("deep-extend")
const { join } = require("path")
const {
preferencesFileExists,
getDefaultIdentity
} = require("../ui/core/kernel/prefs.js")
let config = require("ssb-config")
function buildConfig(identity, plugins, userConfig = {}) {
config.keys = identity.keys
config.remote = identity.remote
config = addSockets(config, plugins)
config = fixLocalhost(config)
config = forceLocalhost(config)
return config
}
function addSockets(config, plugins) {
if (process.platform === "win32") return config
const hasSocketPlugins = plugins.find(plugin => plugin.name === "unix-socket") && plugins.find(plugin => plugin.name === "no-auth")
if (!hasSocketPlugins) return config
const pubkey = config.keys.id.slice(1).replace(`.${config.keys.curve}`, "")
return merge(config, {
connections: {
incoming: { unix: [{ scope: "device", transform: "noauth", server: true }] },
},
remote: `unix:${join(config.path, "socket")}:~noauth:${pubkey}`, // overwrites
})
}
function fixLocalhost(config) {
if (process.platform !== "win32") return config
// host values default to :: which seems to break Windows 10
const unsafe = "::"
const safe = "127.0.0.1"
if (config.connections.incoming.net[0].host === unsafe) {
config.connections.incoming.net[0].host = safe
}
if (config.connections.incoming.ws[0].host === unsafe) {
config.connections.incoming.ws[0].host = safe
}
if (config.host === unsafe) {
config.host = safe
}
return config
}
function forceLocalhost(config) {
const safe = "127.0.0.1"
config.connections.incoming.net[0].host = safe
config.connections.incoming.ws[0].host = safe
config.host = safe
return config
}
function startSSB(plugins, config, cb) {
console.log("Starting Server")
const stack = secretStack({ caps: config.caps || caps })
plugins.forEach(plugin => {
console.log(`Loading plugin ${plugin.name}`)
stack.use(plugin)
})
const ssb = stack(config)
console.log("Knock! Knock! Is there a server there?")
const checkServer = () => {
let res = ssb.progress()
if (res.hasOwnProperty("indexes")) {
cb(null, ssb)
} else {
console.log("looping")
setTimeout(checkServer, 1000)
}
}
setTimeout(checkServer, 1000)
}
function startDefaultPatchfoxServer(identity, cb) {
const plugins = [
// Authentication often hooked for authentication.
require("ssb-master"),
// Methods often used during init().
require("ssb-db"),
require("ssb-ebt"),
require("ssb-friends"),
// Method `replicate()` often hooked for improvements.
require("ssb-replicate"),
require("ssb-replication-scheduler"),
// Required by ssb-about, ssb-tangle, etc.
require("ssb-backlinks"),
// Required by ssb-room
require("ssb-conn"),
require("ssb-about"),
require("ssb-blobs"),
require("ssb-invite-client"),
require("ssb-lan"),
require("ssb-logging"),
require("ssb-meme"),
require("ssb-no-auth"),
require("ssb-onion"),
require("ssb-ooo"),
require("ssb-plugins"),
require("ssb-private"),
require("ssb-private1"),
require("ssb-query"),
require("ssb-room/tunnel/client"),
require("ssb-search"),
require("ssb-unix-socket"),
require("ssb-ws"),
]
const config = buildConfig(identity, plugins)
startSSB(plugins, config, cb)
}
module.exports = {
startSSB,
startDefaultPatchfoxServer,
}