129 lines
3.6 KiB
JavaScript
129 lines
3.6 KiB
JavaScript
![]() |
#!/usr/bin/env nodejs
|
||
|
|
||
|
var safechat = function(keyserver) {
|
||
|
var hkp = new openpgp.HKP(keyserver)
|
||
|
return {
|
||
|
client: {
|
||
|
user: null,
|
||
|
createUser: function(name, host, password, success, fail) {
|
||
|
openpgp.generateKey({
|
||
|
numBits: 4096,
|
||
|
userIds: [{name: name, email: name+'@'+host}],
|
||
|
passphrase: password
|
||
|
}).then(function(keyPair) {
|
||
|
user = {
|
||
|
name: name,
|
||
|
email: name+'@'+host,
|
||
|
numBits: 4096,
|
||
|
key: {
|
||
|
pub: keyPair.publicKeyArmored,
|
||
|
priv: keyPair.privateKeyArmored
|
||
|
}
|
||
|
}
|
||
|
hkp.upload(user.key.pub).then(function() {
|
||
|
success(user)
|
||
|
}).catch(function(e) {
|
||
|
fail('upload key failed', e)
|
||
|
})
|
||
|
}).catch(function(e) {
|
||
|
fail('generating key pairs failed', e)
|
||
|
})
|
||
|
},
|
||
|
setUser: function() {
|
||
|
|
||
|
}
|
||
|
},
|
||
|
server: {
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
try {
|
||
|
|
||
|
var package = require(__dirname+'/../package.json')
|
||
|
var config = require(package.path.config)
|
||
|
var io = require('socket.io-client')
|
||
|
var program = require('commander')
|
||
|
var openpgp = require('openpgp')
|
||
|
var fs = require('fs');
|
||
|
|
||
|
program
|
||
|
.version(package.version)
|
||
|
.description('command line client for SafeChat, see https://safechat.ch')
|
||
|
.option('-u, --url <url>', 'url to safechat server [http://localhost:8888]', 'http://localhost:8888')
|
||
|
.option('-n, --name <name>', 'username [test]', 'test')
|
||
|
.option('-H, --host <host>', 'user\'s SafeChat host, mail is <name>@<host> [safechat.ch]', 'safechat.ch')
|
||
|
.option('-K, --keyserver <host>', 'pgp key server [https://safechat.ch]', 'https://safechat.ch')
|
||
|
.option('-p, --password <pwd>', 'password [ert456]', 'ert456')
|
||
|
.option('-k, --key <file>', 'pgp key file [key.pgp]', 'key.pgp')
|
||
|
.parse(process.argv)
|
||
|
|
||
|
openpgp.initWorker()
|
||
|
openpgp.config.aead_protect = true
|
||
|
var client = safechat(program.keyserver).client;
|
||
|
|
||
|
fs.stat(program.key, function(err, stats) {
|
||
|
if (err) {
|
||
|
console.log('generate keys')
|
||
|
client.createUser(program.name, program.host, program.password,
|
||
|
function(user) {
|
||
|
fs.writeFileSync(program.key, JSON.stringify(user))
|
||
|
console.log('new user credentials created')
|
||
|
},
|
||
|
function(msg, e) {
|
||
|
console.log("**** ERRROR:", msg, e)
|
||
|
})
|
||
|
} else if (stats.isFile()) {
|
||
|
client.user = JSON.parse(fs.readFileSync(program.key))
|
||
|
console.log("user:", client.user.name)
|
||
|
} else {
|
||
|
console.log('**** ERROR: cannot read file', program.key)
|
||
|
}
|
||
|
})
|
||
|
|
||
|
/*
|
||
|
console.log('connecting to:', program.url)
|
||
|
|
||
|
var socket = io(program.url)
|
||
|
socket
|
||
|
.on('connect', function() {
|
||
|
console.log('connect')
|
||
|
socket.emit('login', {name: 'test', })
|
||
|
})
|
||
|
.on("login", function() {
|
||
|
console.log('loggedin')
|
||
|
})
|
||
|
.on("fail", function() {
|
||
|
console.log('fail')
|
||
|
})
|
||
|
.on("user", function() {
|
||
|
console.log('user')
|
||
|
})
|
||
|
.on("users", function() {
|
||
|
console.log('users')
|
||
|
})
|
||
|
.on("message", function() {
|
||
|
console.log('message')
|
||
|
})
|
||
|
.on("messages", function() {
|
||
|
console.log('messages')
|
||
|
})
|
||
|
.io
|
||
|
.on("connect", function() {
|
||
|
console.log('io.connect')
|
||
|
})
|
||
|
.on("reconnect", function() {
|
||
|
console.log('io.reconnect')
|
||
|
})
|
||
|
.on("disconnect", function() {
|
||
|
console.log('io.disconnect')
|
||
|
})
|
||
|
.on("error", function() {
|
||
|
console.log('io.disconnect')
|
||
|
})
|
||
|
*/
|
||
|
|
||
|
} catch (e) {
|
||
|
console.log('**** ERROR:', e.stack)
|
||
|
}
|