-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
156 lines (140 loc) · 4.67 KB
/
index.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
const fs = require('fs');
const os = require('os');
const ffi = require('ffi');
const ref = require('ref');
const path = require('path');
const Struct = require('ref-struct');
const ArrayType = require('ref-array');
const iconv = require('iconv-lite');
const hardware = {};
const find = '\u0000';
const re = new RegExp(find, 'g');
const stack = require('callsite');
function hazardous(location) {
const electronRegex = /[\\/]electron\.asar[\\/]/;
const asarRegex = /^(?:^\\\\\?\\)?(.*\.asar)[\\/](.*)/;
/* convert path when use electron asar unpack
*/
if (!path.isAbsolute(location)) {
return location;
}
if (electronRegex.test(location)) {
return location;
}
const matches = asarRegex.exec(location);
if (!matches || matches.length !== 3) {
return location;
}
/* Skip monkey patching when an electron method is in the callstack. */
const skip = stack().some(site => {
const siteFile = site.getFileName();
return /^ELECTRON_ASAR/.test(siteFile) || electronRegex.test(siteFile);
});
return skip ? location : location.replace(/\.asar([\\/])/, '.asar.unpacked$1');
}
const PersonInfoA = Struct({
name: ArrayType('char', 32),
sex: ArrayType('char', 4),
nation: ArrayType('char', 20),
birthday: ArrayType('char', 12),
address: ArrayType('char', 72),
cardId: ArrayType('char', 20),
police: ArrayType('char', 32),
validStart: ArrayType('char', 12),
validEnd: ArrayType('char', 12),
sexCode: ArrayType('char', 4),
nationCode: ArrayType('char', 4),
appendMsg: ArrayType('char', 72),
});
const libcvr = ffi.Library(hazardous(path.join(__dirname, './lib/cardapi3')), {
OpenCardReader: [ 'int', [ 'int', 'int', 'int' ]],
GetPersonMsgA: [ 'int', [ ref.refType(PersonInfoA), 'string' ]],
ResetCardReader: [ 'int', [ ]],
CloseCardReader: [ 'int', [ ]],
GetCardReaderStatus: [ 'int', [ 'int', 'int' ]],
GetErrorTextA: [ 'int', [ 'pointer', 'int' ]],
});
hardware.OpenCardReader = port => {
try {
const handle = libcvr.OpenCardReader(port, 2, 115200);
if (handle) {
return { error: -1 };
}
return { error: 0, data: { handle } };
} catch (e) {
return { error: -1 };
}
};
hardware.CloseCardReader = () => {
try {
const res = libcvr.CloseCardReader();
if (res === 0) {
return { error: 0 };
}
return { error: -1 };
} catch (e) {
return { error: -1 };
}
};
hardware.ResetCardReader = () => {
try {
const res = libcvr.ResetCardReader();
if (res === 0) {
return { error: 0 };
}
return { error: -1 };
} catch (e) {
return { error: -1 };
}
};
hardware.GetCardReaderStatus = port => {
try {
const res = libcvr.GetCardReaderStatus(port, 115200);
if (res === 0) {
return { error: 0 };
}
return { error: -1 };
} catch (e) {
return { error: -1 };
}
};
hardware.GetPersonMsg = () => {
try {
const personInfo = new PersonInfoA();
const folder = fs.mkdtempSync(`${os.tmpdir()}${path.sep}`);
const image = path.join(folder, 'image.bmp');
const res = libcvr.GetPersonMsgA(personInfo.ref(), image);
if (res === 0) {
return { error: 0, data: {
name: iconv.decode(Buffer.from(personInfo.name), 'gbk').replace(re, '').trim(),
sex: iconv.decode(Buffer.from(personInfo.sex), 'gbk').replace(re, '').trim(),
nation: iconv.decode(Buffer.from(personInfo.nation), 'gbk').replace(re, '').trim(),
birthday: iconv.decode(Buffer.from(personInfo.birthday), 'gbk').replace(re, '').trim(),
address: iconv.decode(Buffer.from(personInfo.address), 'gbk').replace(re, '').trim(),
cardId: iconv.decode(Buffer.from(personInfo.cardId), 'gbk').replace(re, '').trim(),
police: iconv.decode(Buffer.from(personInfo.police), 'gbk').replace(re, '').trim(),
validStart: iconv.decode(Buffer.from(personInfo.validStart), 'gbk').replace(re, '').trim(),
validEnd: iconv.decode(Buffer.from(personInfo.validEnd), 'gbk').replace(re, '').trim(),
sexCode: iconv.decode(Buffer.from(personInfo.sexCode), 'gbk').replace(re, '').trim(),
nationCode: iconv.decode(Buffer.from(personInfo.nationCode), 'gbk').replace(re, '').trim(),
appendMsg: iconv.decode(Buffer.from(personInfo.appendMsg), 'gbk').replace(re, '').trim(),
image,
} };
}
return { error: -1 };
} catch (e) {
return { error: -1 };
}
};
hardware.GetErrorText = () => {
try {
const len = ref.alloc(ref.types.byte);
const data = ref.alloc(ref.types.char);
libcvr.GetErrorTextA(data, len);
const outData = ref.reinterpret(data, len.deref());
return { error: 0, data: { errorText: iconv.convert(outData).toString() } };
} catch (e) {
return { error: -1 };
}
};
module.exports = hardware;