-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
137 lines (117 loc) · 4.35 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
(async () => {
// Modules to control application life and create native browser window
const path = require('path')
const isDev = require('electron-is-dev');
const iconv = require('iconv-lite')
const { app, BrowserWindow } = require('electron')
const { ipcMain, ipcRenderer } = require('electron')
var usb = require('usb')
function createWindow() {
// Create the browser window.
const mainWindow = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
nodeIntegration: true,
preload: path.join(__dirname, 'preload.js')
}
})
if (isDev) {
mainWindow.webContents.openDevTools({ mode: 'undocked' })
}
// and load the index.html of the app.
mainWindow.loadFile('index.html')
// mainWindow.loadURL( format({
// pathname: join(__dirname, '/out/index.html'),
// protocol: 'file:',
// slashes: true
// }))
//`file://${__dirname}/out/index.html`
// Open the DevTools.
// mainWindow.webContents.openDevTools()
mainWindow.maximize()
}
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.whenReady().then(() => {
createWindow()
app.on('activate', function () {
// On macOS it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (BrowserWindow.getAllWindows().length === 0) createWindow()
})
})
ipcMain.on('device-info-send', (event, arg) => {
if (arg === 'usb') {
event.reply('device-info-reply', JSON.stringify(usb.getDeviceList()))
}
})
usb.on('attach', function (device) {
console.log(device)
});
const smartcard = require('smartcard');
const Devices = smartcard.Devices;
const Iso7816Application = smartcard.Iso7816Application;
const CommandApdu = smartcard.CommandApdu;
const devices = new Devices();
devices.on('device-activated', event => {
const currentDevices = event.devices;
let device = event.device;
for (let prop in currentDevices) {
}
device.on('card-inserted', event => {
let card = event.card;
console.log(`Card '${card.getAtr()}' inserted into '${event.device}'`);
card.on('command-issued', event => {
});
card.on('response-received', event => {
});
const application = new Iso7816Application(card);
application.issueCommand(new CommandApdu({ bytes: [0x00, 0xA4, 0x04, 0x00, 0x10, 0xD1, 0x58, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x00] }))
.then((response) => {
// console.log(`${response}`);
console.log(response.toString())
}).catch((error) => {
console.error(error);
});
application
.issueCommand(Buffer.from([0x00, 0xca, 0x11, 0x00, 0x02, 0x00, 0x00]))
.then((response) => {
console.log(response.toString())
const s1 = response.toString().substr(0, 23)
const s2 = response.toString().substr(24, 23)
const s3 = response.toString().substring(64, response.toString().length)
const s32 = Buffer(s3.substring(0, s3.length - 3).toString(), 'hex').toString();
const data = {
cardNumber: Buffer(s1, 'hex').toString(),
name: iconv.decode(Buffer.from(s2, 'hex'), 'big5').trim().replace(/(^ *)|( *$)/g, ""),
idNumber: s32.substr(0, 9),
birthday: s32.substr(10, 7),
sex: s32.substr(17, 1),
publish: s32.substr(18)
}
ipcMain.on('nhi-card-send', (event, arg) => {
console.log(arg)
console.log(data)
if (arg === 'ok') {
event.reply('nhi-card-reply', JSON.stringify(data))
}
})
}).catch((error) => {
console.error(error);
});
});
device.on('card-removed', event => {
console.log(`Card removed from '${event.name}' `);
});
});
devices.on('device-deactivated', event => {
console.log(`Device '${event.device}' deactivated, devices: [${event.devices}]`);
});
const glob = require("glob-promise")
const templateFiles = await glob('./ui/**/*.html')
ipcMain.on('template-send', (event, arg) => {
event.reply('template-reply', JSON.stringify(templateFiles))
})
})()