Files: 1fa83afef777f28f13f52b22b0c8ce690c8eecb0 / server.js
4141 bytesRaw
1 | |
2 | |
3 | const fs = require('fs') |
4 | const path = require('path') |
5 | const ssbKeys = require('ssb-keys') |
6 | const minimist = require('minimist') |
7 | const notifier = require('node-notifier') |
8 | const SysTray = require('systray').default |
9 | let tray = {} |
10 | |
11 | function start(customConfig) { |
12 | |
13 | customConfig = customConfig || {} |
14 | let appname = customConfig.appname || false |
15 | let customPluginPaths = customConfig.plugins || false |
16 | let argv = process.argv.slice(2) |
17 | let i = argv.indexOf('--') |
18 | let conf = argv.slice(i + 1) |
19 | argv = ~i ? argv.slice(0, i) : argv |
20 | let ssb_appname = appname ? appname : process.env.ssb_appname |
21 | |
22 | const config = require('ssb-config/inject')(ssb_appname, minimist(conf)) |
23 | |
24 | const keys = ssbKeys.loadOrCreateSync(path.join(config.path, 'secret')) |
25 | if (keys.curve === 'k256') { |
26 | throw new Error('k256 curves are no longer supported,' + |
27 | 'please delete' + path.join(config.path, 'secret')) |
28 | } |
29 | |
30 | const manifestFile = path.join(config.path, 'manifest.json') |
31 | |
32 | const createSbot = require('scuttlebot') |
33 | // .use(require('scuttlebot/plugins/plugins')) |
34 | .use(require('scuttlebot/plugins/master')) |
35 | .use(require('scuttlebot/plugins/gossip')) |
36 | .use(require('scuttlebot/plugins/replicate')) |
37 | .use(require('scuttlebot/plugins/invite')) |
38 | .use(require('scuttlebot/plugins/local')) |
39 | .use(require('ssb-about')) |
40 | .use(require('ssb-backlinks')) |
41 | .use(require('ssb-blobs')) |
42 | .use(require('ssb-ebt')) |
43 | .use(require('ssb-chess-db')) |
44 | .use(require('ssb-friends')) |
45 | .use(require('ssb-meme')) |
46 | .use(require('ssb-names')) |
47 | .use(require('ssb-ooo')) |
48 | .use(require('ssb-private')) |
49 | .use(require('ssb-search')) |
50 | .use(require('ssb-query')) |
51 | .use(require('ssb-ws')) |
52 | |
53 | |
54 | // Custom plugins from json |
55 | let appManifestFile = path.resolve('scuttleshell.json') |
56 | if (fs.existsSync(appManifestFile)) { |
57 | let manifest = JSON.parse(fs.readFileSync(manifestPath)) |
58 | if (manifest.hasOwnProperty('plugins') && Array.isArray(manifest.plugins)) { |
59 | console.log("loading custom plugins: ", manifest.plugins.join(", ")) |
60 | manifest.plugins.forEach(plugin => createSbot.use(require(plugin))) |
61 | } |
62 | } |
63 | |
64 | if (Array.isArray(customPluginPaths)) { |
65 | console.log("loading custom plugins: ", customPluginPaths.join(", ")) |
66 | customPluginPaths.forEach(plugin => createSbot.use(require(plugin))) |
67 | } |
68 | |
69 | // start server |
70 | |
71 | config.keys = keys |
72 | const server = createSbot(config) |
73 | |
74 | // write RPC manifest to ~/.ssb/manifest.json |
75 | fs.writeFileSync(manifestFile, JSON.stringify(server.getManifest(), null, 2)) |
76 | |
77 | const icon = fs.readFileSync(path.join(__dirname, `icon.${process.platform === 'win32' ? 'ico' : 'png'}`)) |
78 | tray = new SysTray({ |
79 | menu: { |
80 | icon: icon.toString('base64'), |
81 | title: 'Scuttle-Shell', |
82 | tooltip: 'Secure Scuttlebutt', |
83 | items: [ |
84 | |
85 | { |
86 | title: 'Quit', |
87 | tooltip: 'Stop sbot and quit tray application', |
88 | checked: false, |
89 | enabled: true |
90 | } |
91 | ] |
92 | }, |
93 | debug: false, |
94 | copyDir: true, |
95 | }) |
96 | |
97 | tray.onClick(action => { |
98 | switch (action.seq_id) { |
99 | case 0: |
100 | console.log("### EXITING IN TWO SECONDS ###") |
101 | |
102 | notifier.notify({ |
103 | title: 'Secure Scuttlebutt', |
104 | message: `Secure Scuttlebutt will exit in two seconds...`, |
105 | icon: path.join(__dirname, "icon.png"), |
106 | wait: true, |
107 | id: 0, |
108 | }) |
109 | |
110 | tray.kill() |
111 | } |
112 | }) |
113 | |
114 | tray.onExit((code, signal) => { |
115 | setTimeout(() => |
116 | process.exit(0), 2000) |
117 | }) |
118 | |
119 | } |
120 | |
121 | function stop() { |
122 | tray.kill() |
123 | } |
124 | |
125 | const getConfig = () => { |
126 | try { |
127 | let secret = fs.readFileSync(pathToSecret, "utf8") |
128 | let keys = JSON.parse(secret.replace(/#[^\n]*/g, '')) |
129 | let manifest = JSON.parse(fs.readFileSync(path.join(config.path, 'manifest.json'))) |
130 | let remote = "ws://localhost:8989~shs:" + keys.id.substring(1, keys.id.indexOf('.')) |
131 | return { type: 'config', keys: keys, manifest: manifest, remote: remote, secret: secret } |
132 | } catch (n) { |
133 | return { type: 'error', msg: n.message } |
134 | } |
135 | } |
136 | |
137 | module.exports = { start, stop, getConfig } |
138 | |
139 | if (require.main === module) { |
140 | var errorLevel = start() |
141 | } |
Built with git-ssb-web