atcheck/lsscanner/new/main.js
2019-11-24 19:02:22 +01:00

366 lines
8.4 KiB
JavaScript
Executable File

//Get required packages
const electron = require('electron');
const path = require('path');
const url = require('url');
const fs = require('fs');
const asn1js = require('asn1js');
const util = require('util');
const notifier = require('node-notifier');
const smartcard = require('smartcard');
const {
app,
BrowserWindow,
Menu,
ipcMain
} = electron;
const Devices = smartcard.Devices;
const devices = new Devices();
const Iso7816Application = smartcard.Iso7816Application;
let mainWindow;
let AddWindow;
let AddClassesWindow;
let SelectClassesWindow;
var current_classes = -1;
//Listen for the app to be ready
app.on('ready', function () {
//Create new window
mainWindow = new BrowserWindow({
width: 500,
height: 600,
webPreferences: {
nodeIntegration: true
}});
//Load html into window
mainWindow.loadURL(url.format({
pathname: path.join(__dirname, "index.html"),
protocol: "file:",
slashes: true
}));
//Quit app when closed
mainWindow.on("closed", function () {
app.quit();
})
//Menu from template
const mainMenu = Menu.buildFromTemplate(mainMenuTemplate);
Menu.setApplicationMenu(mainMenu);
});
devices.on('device-activated', event => {
console.log('Smart card reader active');
const currentDevices = event.devices;
let device = event.device;
device.on('error', function (message) {
console.log(`Error '${message}' received`);
});
device.on('card-inserted', event => {
let card = event.card;
//console.log(`Card '${card.getAtr()}' inserted into '${event.device}'` + '\n');
const application = new Iso7816Application(card);
application.selectFile([0xD6, 0x16, 0x00, 0x00, 0x30, 0x01, 0x01])
.then(response => {
console.info(`Response: '${response}' '${response.meaning()}'`);
application.issueCommand([0x00, 0xA4, 0x02, 0x00, 0x02, 0x00, 0x02, 0x12])
.then(response => {
console.info(`Response: '${response}' '${response.meaning()}'`);
application.issueCommand([0x00, 0xB0, 0x00, 0x00, 0xF8])
.then(response => {
console.info(`Response: '${response}' '${response.meaning()}'`);
var response2 = `${response}`;
var start = response2.indexOf("2a8468016504010101") + 18;
response2 = response2.substring(start);
while (1) {
var type = response2.substring(0, 2);
if (type == "30") break;
if (parseInt(response2.substring(2, 4), 16) > 127) {
var count = parseInt(response2.substring(2, 4), 16) - 128;
var length = "";
var k = 1;
for (k = 1; k <= count; k++) {
length += response2.substring(2 + (k * 2), 4 + (k * 2));
}
var total_length = parseInt(length, 16) * 2;
response2 = response2.substr(4 + (k - 1) * 2, total_length);
} else {
var total_length = parseInt(response2.substring(2, 4), 16) * 2 + 4;
response2 = response2.substr(4, total_length);
}
}
//region How to work with ASN.1 strings
let bmp_string_encoded = new ArrayBuffer(response2.length / 2);
let bmp_string_view = new Uint8Array(bmp_string_encoded);
var kk = 0;
for (var i = 0; i < (response2.length / 2); i++) {
bmp_string_view[i] = "0x" + response2.substring(kk, kk + 2);
kk += 2;
}
let bmp_string_decoded = asn1js.fromBER(bmp_string_encoded);
if (bmp_string_decoded.offset === (-1)) return; // Error during decoding
let obj = bmp_string_decoded.result.valueBlock.value;
var names = obj[4].valueBlock.value[0].valueBlock.value;
var surnames = obj[3].valueBlock.value[0].valueBlock.value;
var index = obj[5].valueBlock.value;
var output = [names, surnames, index];
printStudent(output);
}).catch(error => {
//console.error('Error:', error, error.stack + '\n');
});
}).catch(error => {
//console.error('Error:', error, error.stack + '\n');
});
}).catch(error => {
//console.error('Error:', error, error.stack + '\n');
});
});
device.on('card-removed', event => {
console.log(`Card removed from '${event.name}' ` + '\n');
});
});
devices.on('device-deactivated', event => {
console.log(`Device '${event.device}' deactivated, devices: [${event.devices}]` + '\n');
});
//Add student manually
function createAddWindow() {
//Create new window
AddWindow = new BrowserWindow({
width: 300,
height: 400,
title: "Add student manually"
});
//Load html into window
AddWindow.loadURL(url.format({
pathname: path.join(__dirname, "addstudent.html"),
protocol: "file:",
slashes: true
}));
//Garbage
AddWindow.on("close", function () {
AddWindow = null;
});
}
function createAddClassesWindow() {
//Create new window
AddClassesWindow = new BrowserWindow({
width: 350,
height: 500,
title: "Add classes"
});
//Load html into window
AddClassesWindow.loadURL(url.format({
pathname: path.join(__dirname, "addclasses.html"),
protocol: "file:",
slashes: true
}));
//Garbage
AddClassesWindow.on("close", function () {
AddClassesWindow = null;
});
}
function createSelectClassesWindow() {
//Create new window
SelectClassesWindow = new BrowserWindow({
width: 500,
height: 475,
title: "Add classes"
});
//Load html into window
SelectClassesWindow.loadURL(url.format({
pathname: path.join(__dirname, "selectclasses.html"),
protocol: "file:",
slashes: true
}));
//Garbage
SelectClassesWindow.on("close", function () {
SelectClassesWindow = null;
});
}
function printStudent(item) {
var dateTime = require('node-datetime');
var dt = dateTime.create();
var formatted = dt.format('d-m-Y H:M:S');
item.push(formatted);
mainWindow.webContents.send("item:add", item);
notifier.notify({
title: "Student registered",
message: item[0] + " " + item[1],
icon: path.join(__dirname, 'ok.png')
});
/*
if (false) { //current_classes == -1
notifier.notify({
title: "No current classes selected",
message: "Adding student unsuccessful",
icon: path.join(__dirname, 'wrong.png')
});
} else {
if (true) {
var dateTime = require('node-datetime');
var dt = dateTime.create();
var formatted = dt.format('d-m-Y H:M:S');
item.push(formatted);
console.log(item);
mainWindow.webContents.send("item:add", item);
notifier.notify({
title: "Student registered",
message: item[0] + " " + item[1],
icon: path.join(__dirname, 'ok.png')
});
} else {
notifier.notify({
title: "Student already exists",
message: item[0] + " " + item[1],
icon: path.join(__dirname, 'wrong.png')
});
}
}*/
}
//Catch
ipcMain.on("item:add", function (e, item) {
printStudent(item);
AddWindow.close();
})
//Menu template
const mainMenuTemplate = [{
label: "Student",
submenu: [{
label: "Add student",
accelerator: process.platfrom == "darwin" ? "Command+E" : "Ctrl+E",
click() {
createAddWindow();
}
},
{
label: "Quit program",
accelerator: process.platfrom == "darwin" ? "Command+Q" : "Ctrl+Q",
click() {
app.quit();
}
}
]
}, {
label: "Classes",
submenu: [{
label: "Add classes",
click() {
createAddClassesWindow();
}
},
{
label: "Pick classes",
click() {
createSelectClassesWindow();
}
}
]
}];
//If mac psuh empty obj to Menu
if (process.platfrom == "darwin") {
mainMenuTemplate.unshift({});
}
//Add dev tools on DEV
if (process.env.NODE_ENV !== 'production') {
mainMenuTemplate.push({
label: "DEV",
submenu: [{
label: "Toggle dev tools",
accelerator: process.platfrom == "darwin" ? "Command+I" : "Ctrl+I",
click(item, focusedWindow) {
focusedWindow.toggleDevTools();
}
},
{
role: "reload"
}
]
});
}