Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

add security

  • Loading branch information...
commit 918ec9bc04d1668d8729282c800924e203f6e0d4 1 parent e80ceca
@dominictarr authored
Showing with 70 additions and 9 deletions.
  1. +7 −2 index.js
  2. +54 −0 keygen.js
  3. +9 −7 model.js
View
9 index.js
@@ -44,8 +44,8 @@ function Peers (opts, id) {
this.reconnect = true
merge(defaults, this.opts)
- this.id = id
this.model = Model(id || uuid.v4())
+ this.id = this.model.id
this.initial = []
}
@@ -60,7 +60,12 @@ Peers.prototype._connect = function () {
function connect () {
stream.pipe(mx).pipe(stream)
for(var k in self.plugins)
- self._apply(mx.createStream({type: k, from: self.model.id, to: target.id}), true)
+ self._apply(mx.createStream({
+ type: k,
+ from: self.model.id,
+ to: target.id
+ }, {allowHalfOpen: false})
+ , true)
}
var n = 0
View
54 keygen.js
@@ -0,0 +1,54 @@
+
+
+//ssh-keygen -q -f "$1" -b 768 -N ''
+//ssh-keygen -e -f "$1".pub -m PEM > "$1".pem
+
+var spawn = require('child_process').spawn
+var fs = require('fs')
+
+//GENERATE A PRIVATE KEY, AND PEM ENCODED PUBLIC KEY
+//add a function to load key-pairs
+
+
+module.exports = function (file, size, cb) {
+ var args = [].slice.call(arguments)
+ var cb = args.pop()
+ var file = args.shift() || '/tmp/'+ (''+Math.random()).substring(2)
+ var size = '' + (args.pop() || 786)
+
+ spawn('ssh-keygen', ['-q', '-f', file, '-b', size, '-N', ''])
+ .on('exit', function () {
+ spawn('ssh-keygen', ['-e', '-f', file + '.pub', '-m', 'PEM'])
+ .stdout.pipe(fs.createWriteStream(file + '.pem'))
+ .on('close', function () {
+
+ var n = 0, private, public, error
+ function done (err) {
+ if(error)
+ return
+ if(err)
+ return cb(error = err)
+ if(++n != 2)
+ return
+ cb(err, private, public)
+ }
+
+ fs.readFile(file, function (err, priv) {
+ private = priv
+ done(err)
+ })
+
+ fs.readFile(file + '.pem', function (err, pub) {
+ public = pub
+ done(err)
+ })
+ })
+ })
+}
+
+
+if(!module.parent) {
+ module.exports(function (err, private, public) {
+ console.log(''+private, ''+public)
+ })
+}
View
16 model.js
@@ -8,16 +8,18 @@
var Doc = require('crdt').Doc
-module.exports = function (id) {
+module.exports = function (security) {
- var model = new Doc(id)
-// model.sync = true
+ var model = new Doc(security)
+ var id = model.id
model.create = function (port, address) {
var me = model.add({
id: id,
port: port,
- address: address
+ address: address,
+ publicKey: security.publicKey,
+ type: 'peer'
})
return me
@@ -31,12 +33,12 @@ module.exports = function (id) {
return model.rows[k].toJSON()
}))
- var r= all[~~(Math.random()*all.length)]
-
+ var r = all[~~(Math.random()*all.length)]
+
var me = model.get(id).toJSON()
if(r.port == me.port && r.host == me.host && all.length > 1 && depth < 10)
- return model.choose(initial, (depth || 1) + 1)
+ return r = model.choose(initial, (depth || 1) + 1)
if(!r)
throw new Error('fail to select target')
Please sign in to comment.
Something went wrong with that request. Please try again.