Files: b05c12ba850a6a9d4f5af6d9f32136f3baf5d75f / bin.js
3431 bytesRaw
1 | var fs = require('fs') |
2 | var path = require('path') |
3 | var ssbKeys = require('ssb-keys') |
4 | var stringify = require('pull-stringify') |
5 | var open = require('opn') |
6 | var home = require('os-homedir')() |
7 | var nonPrivate = require('non-private-ip') |
8 | var muxrpcli = require('muxrpcli') |
9 | |
10 | var SEC = 1e3 |
11 | var MIN = 60*SEC |
12 | |
13 | var config = { |
14 | name: 'ssb', |
15 | host: nonPrivate.v4 || '', |
16 | timeout: 0, |
17 | local: 'true', |
18 | port: 8008, |
19 | path: path.join(home, '.ssb'), |
20 | ws: { port: 8989 }, |
21 | caps: { |
22 | shs: '1KHLiKZvAvjbY1ziZEHMXawbCEIM6qwjCDm3VYRan/s=', |
23 | sign: null |
24 | }, |
25 | friends: { |
26 | dunbar: 150, |
27 | hops: 3 |
28 | }, |
29 | gossip: { |
30 | connections: 3 |
31 | }, |
32 | timers: { |
33 | connection: 0, |
34 | reconnect: 5*SEC, |
35 | ping: 5*MIN, |
36 | handshake: 5*SEC |
37 | }, |
38 | master: [], |
39 | party: true |
40 | } |
41 | |
42 | config.keys = ssbKeys.loadOrCreateSync(path.join(config.path, 'secret')) |
43 | |
44 | var coraClient = fs.readFileSync(path.join('./build/index.html')) |
45 | |
46 | var manifestFile = path.join(config.path, 'manifest.json') |
47 | |
48 | var argv = process.argv.slice(2) |
49 | var i = argv.indexOf('--') |
50 | var conf = argv.slice(i+1) |
51 | argv = ~i ? argv.slice(0, i) : argv |
52 | |
53 | if (argv[0] == 'server') { |
54 | |
55 | var createSbot = require('scuttlebot') |
56 | .use(require('scuttlebot/plugins/master')) |
57 | .use(require('scuttlebot/plugins/gossip')) |
58 | .use(require('scuttlebot/plugins/replicate')) |
59 | .use(require('ssb-friends')) |
60 | .use(require('ssb-blobs')) |
61 | .use(require('ssb-query')) |
62 | .use(require('ssb-links')) |
63 | .use(require('ssb-ebt')) |
64 | .use(require('scuttlebot/plugins/invite')) |
65 | .use(require('scuttlebot/plugins/local')) |
66 | .use(require('decent-ssb/plugins/ws')) |
67 | .use({ |
68 | name: 'serve', |
69 | version: '1.0.0', |
70 | init: function (sbot) { |
71 | sbot.ws.use(function (req, res, next) { |
72 | var send = {} |
73 | send = config |
74 | delete send.keys // very important to keep this, as it removes the server keys from the config before broadcast |
75 | send.address = sbot.ws.getAddress() |
76 | if(req.url == '/') |
77 | res.end(coraClient) |
78 | if(req.url == '/get-config') |
79 | res.end(JSON.stringify(send)) |
80 | else next() |
81 | }) |
82 | } |
83 | }) |
84 | |
85 | open('http://localhost:' + config.ws.port, {wait: false}) |
86 | |
87 | var server = createSbot(config) |
88 | |
89 | fs.writeFileSync(manifestFile, JSON.stringify(server.getManifest(), null, 2)) |
90 | } else { |
91 | |
92 | var manifest |
93 | try { |
94 | manifest = JSON.parse(fs.readFileSync(manifestFile)) |
95 | } catch (err) { |
96 | throw explain(err, |
97 | 'no manifest file' |
98 | + '- should be generated first time server is run' |
99 | ) |
100 | } |
101 | |
102 | // connect |
103 | require('ssb-client')(config.keys, { |
104 | manifest: manifest, |
105 | port: config.port, |
106 | host: config.host||'localhost', |
107 | caps: config.caps, |
108 | key: config.key || config.keys.id |
109 | }, function (err, rpc) { |
110 | if(err) { |
111 | if (/could not connect/.test(err.message)) { |
112 | console.log('Error: Could not connect to the scuttlebot server.') |
113 | console.log('Use the "server" command to start it.') |
114 | if(config.verbose) throw err |
115 | process.exit(1) |
116 | } |
117 | throw err |
118 | } |
119 | |
120 | // add some extra commands |
121 | manifest.version = 'async' |
122 | manifest.config = 'sync' |
123 | rpc.version = function (cb) { |
124 | console.log(require('./package.json').version) |
125 | cb() |
126 | } |
127 | rpc.config = function (cb) { |
128 | console.log(JSON.stringify(config, null, 2)) |
129 | cb() |
130 | } |
131 | |
132 | // run commandline flow |
133 | muxrpcli(argv, manifest, rpc, config.verbose) |
134 | }) |
135 | } |
136 | |
137 |
Built with git-ssb-web