-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
executable file
·57 lines (48 loc) · 1.36 KB
/
app.js
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
'use strict'
if (process.env.UTP) {
require('multiplex-utp')
}
var crypto = require('crypto')
var path = require('path')
var externalIP = require('external-ip')()
var minimist = require('minimist')
var Keeper = require('bitkeeper-js')
var Server = require('./')
var conf = require('./conf/config.json')
var keeperConf = conf.keeper
var argv = minimist(process.argv.slice(2))
var DHT = Keeper.DHT
var port = argv.port || conf.port
getNodeId(init)
function getNodeId (cb) {
if (keeperConf.nodeId) cb(keeperConf.nodeId)
else {
externalIP(function (err, ip) {
if (err) cb()
else cb(crypto.createHash('sha256').update(ip + ':' + port).digest().slice(0, 20))
})
}
}
function init (nodeId) {
var dhtConf = {
bootstrap: keeperConf.bootstrap || false
}
if (nodeId) dhtConf.nodeId = nodeId
keeperConf.seedStored = false
keeperConf.dht = new DHT(dhtConf)
keeperConf.dht.listen(keeperConf.dhtPort)
keeperConf.storage = path.resolve(keeperConf.storage)
console.log('STORAGE: ' + keeperConf.storage)
var keeper = new Keeper(keeperConf)
keeper.on('ready', function () {
keeper.seedStored()
var server = Server.create({
keeper: keeper,
readonly: argv.readonly,
port: port
})
// whichever happens first
server.once('close', keeper.close.bind(keeper))
keeper.once('close', server.close.bind(server))
})
}