first test with cli client
This commit is contained in:
		
							
								
								
									
										128
									
								
								nodejs/client/safechat.js
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										128
									
								
								nodejs/client/safechat.js
									
									
									
									
									
										Executable file
									
								
							@@ -0,0 +1,128 @@
 | 
			
		||||
#!/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)
 | 
			
		||||
}
 | 
			
		||||
@@ -4,6 +4,7 @@
 | 
			
		||||
  "documentation": "https://dev.marc.waeckerlin.org/doc/safechat/",
 | 
			
		||||
  "private": true,
 | 
			
		||||
  "dependencies": {
 | 
			
		||||
    "commander": "^2.9.0",
 | 
			
		||||
    "ejs": "~2.5.2",
 | 
			
		||||
    "express": "2.5.8",
 | 
			
		||||
    "jquery": "~3.1.0",
 | 
			
		||||
@@ -17,8 +18,12 @@
 | 
			
		||||
  "main": "@PACKAGE_NAME@.js",
 | 
			
		||||
  "devDependencies": {},
 | 
			
		||||
  "scripts": {
 | 
			
		||||
    "start": "@PACKAGE_NAME@.js",
 | 
			
		||||
    "test": "echo \"Error: no test specified\" && exit 1"
 | 
			
		||||
  },
 | 
			
		||||
  "bin": {
 | 
			
		||||
    "safechat": "client/safechat.js"
 | 
			
		||||
  },
 | 
			
		||||
  "author": "@AUTHOR@",
 | 
			
		||||
  "license": "@LICENSE@",
 | 
			
		||||
  "path": {
 | 
			
		||||
 
 | 
			
		||||
@@ -21,6 +21,13 @@ module.exports = function(sql) {
 | 
			
		||||
      socket.broadcast.emit(signal, data);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    socket
 | 
			
		||||
      .on('login', function() {})
 | 
			
		||||
      .on('', function() {})
 | 
			
		||||
      .on('', function() {})
 | 
			
		||||
      .on('', function() {})
 | 
			
		||||
      .on('', function() {})
 | 
			
		||||
/*
 | 
			
		||||
    socket.on("message", function(msg) {
 | 
			
		||||
      console.log("-> signal: message");
 | 
			
		||||
      if (!msg || !msg.user || !msg.content) return emit("fail", "wrong message format");
 | 
			
		||||
@@ -111,7 +118,7 @@ module.exports = function(sql) {
 | 
			
		||||
        if (!err && res && res.length) emit('users', res);
 | 
			
		||||
      });
 | 
			
		||||
    });
 | 
			
		||||
    
 | 
			
		||||
*/    
 | 
			
		||||
  };
 | 
			
		||||
  
 | 
			
		||||
  return module;
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user