Skip to content

Commit

Permalink
Merge branch 'feature'
Browse files Browse the repository at this point in the history
  • Loading branch information
aesedepece committed Aug 31, 2016
2 parents 72139a5 + 3d0ce67 commit d23f086
Show file tree
Hide file tree
Showing 7 changed files with 146 additions and 48 deletions.
42 changes: 42 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Logs
logs
*.log
npm-debug.log*

# Runtime data
pids
*.pid
*.seed

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# nyc test coverage
.nyc_output

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules
jspm_packages

# Optional npm cache directory
.npm

# Optional REPL history
.node_repl_history

policies
*.pub.asc
.localstorage
rethinkdb_data
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "trailbot-watcher",
"version": "0.1.6",
"dependencies": {
"@horizon/client": "^1.1.1",
"@horizon/client": "^1.1.3",
"chokidar": "^1.5.1",
"coffee-script": "^1.10.0",
"colors": "^1.1.2",
Expand All @@ -18,6 +18,7 @@
"mkdirp": "^0.5.1",
"node-localstorage": "^1.3.0",
"npm": "^3.9.5",
"pgp-word-list-converter": "^1.0.0",
"progress": "^1.1.8",
"simple-git": "^1.37.0",
"sleep": "^3.0.1",
Expand All @@ -39,7 +40,7 @@
"start": "./scripts/start",
"export": "./scripts/export",
"service": "./scripts/service",
"setup": "./scripts/setup && ./scripts/export && ./scripts/service"
"setup": "./scripts/setup && ./scripts/service"
},
"bin": {
"trailbot-watcher": "./scripts/start"
Expand Down
13 changes: 8 additions & 5 deletions src/Crypto.iced
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,20 @@ class Crypto
constructor : (watcherArmored, clientArmored, cb) ->
esc = make_esc (err) -> console.error "[CRYPTO] #{err}"

mKey = {armored: clientArmored}

wKey = {armored: watcherArmored}
await kbpgp.KeyManager.import_from_armored_pgp mKey, esc defer @clientKey

await kbpgp.KeyManager.import_from_armored_pgp wKey, esc defer @watcherKey
if @watcherKey.is_pgp_locked()
await @watcherKey.unlock_pgp { passphrase: '' }, esc defer()

@ring = new kbpgp.keyring.KeyRing
for km in [@clientKey, @watcherKey]
@ring.add_key_manager km
@ring.add_key_manager @watcherKey

if clientArmored
mKey = {armored: clientArmored}
await kbpgp.KeyManager.import_from_armored_pgp mKey, esc defer @clientKey
@ring.add_key_manager @clientKey

cb this

Expand Down Expand Up @@ -47,4 +51,3 @@ class Crypto
cb extend data, JSON.parse literals[0].toString()

module.exports = Crypto

8 changes: 6 additions & 2 deletions src/Export.iced
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class Exporter
return

if process.argv[2]
console.log "indice argv ", process.argv[2]
await fs.writeFile process.argv[2], Config.watcher_pub_key, {encoding: 'utf8'}, defer err, res
if err
console.error 'Invalid output path: directory does not exist or writing to it is not allowed.'.red
Expand All @@ -36,7 +37,9 @@ class Exporter
]
.then ({output}) ->
if output is 'stdio'
console.log Config.watcher_pub_key
# console.log Config.watcher_pub_key
console.log "\nSentence:"
console.log "#{Config.sentence}\n".cyan.bold
else if output is 'filesystem'
inquirer.prompt [
name: 'path'
Expand All @@ -46,7 +49,8 @@ class Exporter
validate: (path) ->
new Promise (next) ->
console.log
await fs.writeFile path, Config.watcher_pub_key, {encoding: 'utf8'}, defer err, res
await fs.writeFile path, Config.sentence, {encoding: 'utf8'}, defer err, res
# await fs.writeFile path, Config.watcher_pub_key, {encoding: 'utf8'}, defer err, res
next err && 'Invalid output path: directory does not exist or writing permission is not granted.' || true
]
.then ({path}) ->
Expand Down
2 changes: 1 addition & 1 deletion src/Main.iced
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ app = class App extends EventEmitter
console.log '[WATCHER] Watcher fingerprint:', @watcherFP
console.log '[WATCHER] Client fingerprint', @clientFP

await new Vault this, Config.vault, @watcherFP, @clientFP, defer @vault
await new Vault this, Config.vault, @watcherFP, defer @vault
@emit 'vaultConnected'
console.log '[WATCHER] Connected to vault'

Expand Down
84 changes: 66 additions & 18 deletions src/Setup.iced
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
'use strict'

Config = require './Config'
Crypto = require './Crypto'
inquirer = require 'inquirer'
colors = require 'colors'
fs = require 'fs'
os = require 'os'
kbpgp = require 'kbpgp'
progress = require 'progress'
localStorage = new require 'node-localstorage'
.LocalStorage(Config.local_storage)
pgpWordList = require('pgp-word-list-converter')()
crypto = require 'crypto'
Vault = require './Vault'


class Configure

constructor : ->
@localStorage = new require 'node-localstorage'
.LocalStorage(Config.local_storage)
@done = false
process.on 'exit', =>
unless @done
Expand All @@ -25,36 +30,66 @@ class Configure
message: "Choose a name for this watcher"
type: 'input'
default: os.hostname()
,
name: 'clientKey'
message: "Type the route for the client's public key"
type: 'input'
default: './trailbot_client.pub.asc'
validate: (path) ->
new Promise (next) ->
fs.readFile path, {encode: 'utf8'}, (err, content) ->
next err or true
,
name: 'vault'
message: "Type the FQDN and port of the vault server you want to use"
message: "Type the domain and port of the vault server you want to use"
type: 'input'
default: 'vault.trailbot.io:8443'
]
.then (answers) =>
@alert "Ok, we are now generating a new PGP keypar for this watcher.", true
@alert "This may take up to a couple of minutes. Please wait while magic happens...\n "
@alert "This may take up to a couple of minutes. Please wait while the magic happens...\n "
@progress = new progress ' Generating... [:bar] :percent'.bold,
total: 330
complete: '='
incomplete: ' '
width: 50
await @keygen answers.hostname, defer watcher_priv_key, watcher_pub_key
await fs.readFile answers.clientKey, {encode: 'utf8'}, defer err, client_pub_key
localStorage.setItem 'watcher_priv_key', watcher_priv_key
localStorage.setItem 'watcher_pub_key', watcher_pub_key
localStorage.setItem 'client_pub_key', client_pub_key
localStorage.setItem 'vault', answers.vault
@localStorage.setItem 'watcher_priv_key', watcher_priv_key
@localStorage.setItem 'watcher_pub_key', watcher_pub_key
@localStorage.setItem 'vault', answers.vault

await new Crypto watcher_priv_key, null, defer cryptoBox
watcherFP = cryptoBox.watcherKey.get_pgp_fingerprint().toString('hex')

exchange =
channel: @generateChannel()
creator: watcherFP
watcher: watcher_pub_key
expires: @getExpirationDate()

@done = true
console.log '\n'

await new Vault this, answers.vault, watcherFP, defer vault
await vault.save 'exchange', exchange, defer {id}
process.exit 1 unless id
exchange.id = id

@alert "Now install Trailbot Client in your computer and start the setup wizard." , true
@alert "The following 8 words will be required by Trailbot Client:"
@alert "#{@channelToWords(exchange.channel)}".cyan.bold, true

@alert "Waiting for confirmation from Trailbot Client..." , true
vault.watch 'exchange', exchange.id, (change) =>
# if change is null the document was deleted
process.exit 0 unless change
if change?.client
@localStorage.setItem 'client_pub_key', change.client
vault.remove 'exchange', [change], (res) =>
console.log "file deleted"

# every 5 minutes generate new words
setInterval =>
exchange.channel = @generateChannel()
vault.replace 'exchange', exchange
@alert "Time to get confirmation from Trailbot Client expired", true
@alert "New words generated"
@alert "#{@channelToWords(exchange.channel)}".cyan.bold, true
, 300000




keygen : (identity, cb, pcb) =>
opts =
Expand All @@ -72,5 +107,18 @@ class Configure
b = breakBefore and "\n" or ""
console.log "#{b}! ".green + text.bold

generateChannel : () =>
word = Math.random().toString(36).substring(2)
crypto.createHash('md5').update(word).digest("hex").substr(0, 16)

getExpirationDate : () =>
now = new Date()
now.setMinutes(now.getMinutes() + 5)
now.toString()

channelToWords : (channel) =>
pgpWordList.toWords(channel).toString().replace(/,/g,' ')



new Configure()
40 changes: 20 additions & 20 deletions src/Vault.iced
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Config = require './Config'
Horizon = require '@horizon/client/dist/horizon'

class Vault
constructor : (app, host, watcherFP, clientFP, cb) ->
constructor : (app, host, watcherFP, cb) ->
@app = app
authType = @getToken()
secure = Config.secure
Expand All @@ -12,6 +12,7 @@ class Vault
@users = @hz 'users'
@settings = @hz 'settings'
@events = @hz 'events'
@exchange = @hz 'exchange'

@hz.onReady () =>
token = JSON.parse(@hz.utensils.tokenStorage._storage._storage.get('horizon-jwt')).horizon
Expand All @@ -21,15 +22,15 @@ class Vault
me.data =
key: watcherFP
@users.replace me
console.log 'Me:', me
@app.emit 'vaultLoggedIn', me
console.log 'Me:', me if @app.emit
@app.emit 'vaultLoggedIn', me if @app.emit
cb and cb this

@hz.onDisconnected (e) =>
unless @retried
@retried = true
@app.localStorage.removeItem 'horizon_jwt'
@constructor app, host, watcherFP, clientFP, cb
@constructor app, host, watcherFP, cb

getToken : () ->
jwt = @app.localStorage.getItem 'horizon_jwt'
Expand All @@ -39,27 +40,26 @@ class Vault
'anonymous'

save : (col, object, cb) ->
console.log "Saving into #{col}"
console.log 'SAVING', object
this[col]?.store object
cb and cb true
console.log "Saving into #{col}" if @app.emit
console.log 'SAVING', object if @app.emit
this[col]?.store(object).subscribe(cb)

replace : (col, object, cb) ->
console.log "Replacing into #{col}"
replace : (col, object) ->
console.log "Replacing into #{col}" if @app.emit
this[col]?.replace object
cb and cb true

get : (col, query, cb) ->
this[col]?.find(query).fetch().subscribe (items) ->
cb and cb items
this[col]?.find(query).fetch().defaultIfEmpty().subscribe(cb)

watch : (col, query, cb) ->
this[col]?.find(query).watch().subscribe (items) ->
cb and cb items
watch : (col, query, cb, err) ->
this[col]?.find(query).watch().subscribe(cb, err)

remove : (col, ids) ->
console.log "Removing from #{col}" if @app.emit
this[col].removeAll(ids)

getCollection : () ->
@exchange

remove : (col, ids, cb) ->
console.log "Removing from #{col}"
res = this[col].removeAll(ids)
cb and cb res

module.exports = Vault

0 comments on commit d23f086

Please sign in to comment.