Skip to content

Commit

Permalink
update to use new ssb-config
Browse files Browse the repository at this point in the history
  • Loading branch information
mixmix committed Jan 10, 2019
1 parent 53e1ba2 commit da3d0b3
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 18 deletions.
63 changes: 47 additions & 16 deletions bin.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ var toPull = require('stream-to-pull-stream')
var File = require('pull-file')
var explain = require('explain-error')
var ssbKeys = require('ssb-keys')
var stringify = require('pull-stringify')
var Config = require('ssb-config/inject')
var Client = require('ssb-client')
var createHash = require('multiblob/util').createHash
var minimist = require('minimist')
var muxrpcli = require('muxrpcli')
Expand All @@ -22,23 +23,28 @@ var i = argv.indexOf('--')
var conf = argv.slice(i+1)
argv = ~i ? argv.slice(0, i) : argv

var config = require('ssb-config/inject')(process.env.ssb_appname, minimist(conf))
var config = Config(process.env.ssb_appname, minimist(conf))

var keys = ssbKeys.loadOrCreateSync(path.join(config.path, 'secret'))
if(keys.curve === 'k256')
// TODO when ssb-config includes keys by default, rm this
if (!config.keys) {
config.keys = ssbKeys.loadOrCreateSync(path.join(config.path, 'secret'))
}
if (config.keys.curve === 'k256')
throw new Error('k256 curves are no longer supported,'+
'please delete' + path.join(config.path, 'secret'))

var manifestFile = path.join(config.path, 'manifest.json')

console.log('>>>>', JSON.stringify(config, null, 2))

if (argv[0] == 'server') {
console.log('WARNING-DEPRECATION: `sbot server` has been renamed to `ssb-server start`')
argv[0] = 'start'
}

if (argv[0] == 'start') {
console.log(packageJson.name, packageJson.version, config.path, 'logging.level:'+config.logging.level)
console.log('my key ID:', keys.public)
console.log('my key ID:', config.keys.public)

// special start command:
// import ssbServer and start the server
Expand Down Expand Up @@ -71,8 +77,6 @@ if (argv[0] == 'start') {
}

// start server

config.keys = keys
var server = createSsbServer(config)

// write RPC manifest to ~/.ssb/manifest.json
Expand All @@ -81,7 +85,6 @@ if (argv[0] == 'start') {
if(process.stdout.isTTY && (config.logging.level != 'info'))
ProgressBar(server.progress)
} else {

// normal command:
// create a client connection to the server

Expand All @@ -96,18 +99,20 @@ if (argv[0] == 'start') {
)
}

// connect
require('ssb-client')(keys, {
var opts = {
manifest: manifest,
port: config.port,
host: config.host||'localhost',
port: getPort(config),
host: getHost(config),
caps: config.caps,
key: config.key || keys.id
}, function (err, rpc) {
key: config.key || config.keys.id
}
console.log('OOOOOpts', opts)

// connect
Client(config.keys, opts, function (err, rpc) {
if(err) {
if (/could not connect/.test(err.message)) {
var serverAddr = (config.host || 'localhost') + ":" + config.port;
console.error('Error: Could not connect to ssb-server ' + serverAddr)
console.error('Error: Could not connect to ssb-server ' + opts.host + ':' + opts.port)
console.error('Use the "start" command to start it.')
if(config.verbose) throw err
process.exit(1)
Expand Down Expand Up @@ -167,3 +172,29 @@ if (argv[0] == 'start') {
})
}

function getConnection (config) {
return config.connections &&
config.connections.incoming &&
config.connections.incoming.net &&
config.connections.incoming.net.find(function (transport) {
return transport.scope === 'public' &&
transport.port
})
}

function getPort (config) {
return config.port // TEMP
var connection = getConnection(config)

if (connection && connection.port) return connection.port
return config.port
}

function getHost (config) {
return config.host || 'localhost' // TEMP
var connection = getConnection(config)

if (connection && connection.host) return connection.host
if (config.host) return config.host
return 'localhost'
}
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,11 @@
"pull-ping": "^2.0.2",
"pull-pushable": "^2.2.0",
"pull-stream": "^3.6.2",
"pull-stringify": "^2.0.0",
"rimraf": "^2.4.2",
"secret-stack": "^5.1.0",
"ssb-blobs": "^1.1.4",
"ssb-client": "^4.5.7",
"ssb-config": "^2.3.9",
"ssb-config": "^2.3.7",
"ssb-db": "^18.6.2",
"ssb-ebt": "^5.1.4",
"ssb-friends": "^3.1.3",
Expand Down

0 comments on commit da3d0b3

Please sign in to comment.