Skip to content

Instantly share code, notes, and snippets.

@talkahe
Last active December 19, 2017 23:20
Show Gist options
  • Save talkahe/f284419bd4b0496e5244d89e7a362d4e to your computer and use it in GitHub Desktop.
Save talkahe/f284419bd4b0496e5244d89e7a362d4e to your computer and use it in GitHub Desktop.
Bluetooth Server
var bluetooth_serial_port = require('bluetooth-serial-port');
var node_ssh = require('node-ssh');
var BLUESERVER = BLUESERVER || {
// Dependencies
bluetooth: new(bluetooth_serial_port).BluetoothSerialPortServer(),
ssh: new node_ssh(),
// Message codes
INITIAL_CONFIG: 0,
PIN_VERIFICATION: 1,
VERIFICATION_ACK: 2,
// Settings
server: {
address: '10.10.2.225',
user: 'jorge',
cwd: '/home/jorge',
},
guardPC: {
address: '10.10.2.225',
user: 'jorge',
cwd: '/home/jorge',
},
privateKeyPath: '/home/pi/.ssh/id_rsa',
webServerAddress: 'http://10.10.2.225/',
CHANNEL: 1,
UUID: '1101',
start: function(){
BLUESERVER.bluetooth.listen(function(clientAddress){
console.log("Client "+clientAddress+" connected!");
BLUESERVER.bluetooth.on('data', function (buffer) {
var msg = JSON.parse(buffer);
if(msg.code === BLUESERVER.INITIAL_CONFIG)
BLUESERVER.initialConfig(msg);
else if(msg.code === BLUESERVER.PIN_VERIFICATION)
BLUESERVER.pinVerification(msg);
});
},
function(error){
console.error("Something wrong happened!: "+error);
}, { uuid: BLUESERVER.UUID, channel: BLUESERVER.CHANNEL });
},
initialConfig: function(msg){
BLUESERVER.ssh.connect({
host: BLUESERVER.server.address,
username: BLUESERVER.server.user,
privateKey: BLUESERVER.privateKeyPath
}).then(function(){
BLUESERVER.ssh.execCommand("./signPin "+msg.pin, {cwd: BLUESERVER.server.cwd})
.then(function(result){
BLUESERVER.bluetooth.write(new Buffer(result.stdout),function(err, bytesWritten){
if(err)
console.log("Error -> Initial configuration: "+err);
});
});
});
},
pinVerification: function(msg){
BLUESERVER.ssh.connect({
host: BLUESERVER.server.address,
username: BLUESERVER.server.user,
privateKey: BLUESERVER.privateKeyPath
}).then(function(){
BLUESERVER.ssh.execCommand("./verification "+msg.pin+" "+msg.signedPin, {cwd: BLUESERVER.server.cwd})
.then(function(result){
BLUESERVER.bluetooth.write(new Buffer(BLUESERVER.VERIFICATION_ACK), function(err, bytesWritten){
if(err)
console.log("Error -> Verify PIN: "+err);
});
var url;
if(result.stdout === "OK")
url = BLUESERVER.webServerAddress+"index.php?pin="+msg.pin;
else
url = BLUESERVER.webServerAddress+"invalido.html";
BLUESERVER.showUserInfo(url);
});
});
},
showUserInfo: function(url) {
BLUESERVER.ssh.connect({
host: BLUESERVER.guardPC.address,
username: BLUESERVER.guardPC.user,
privateKey: BLUESERVER.privateKeyPath
}).then(function(){
BLUESERVER.ssh.execCommand("firefox --display=':1' "+url+" > /dev/null 2> /dev/null & pid=$!; sleep 10; kill $pid");
});
}
};
BLUESERVER.start();
{
"name": "bluetooth-server",
"version": "1.0.0",
"description": "",
"main": "index.js",
"dependencies": {
"bluetooth-serial-port": "^2.1.5",
"node-ssh": "^5.0.0"
},
"devDependencies": {},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment