-
Notifications
You must be signed in to change notification settings - Fork 2
/
ipp-server.js
79 lines (78 loc) · 3.42 KB
/
ipp-server.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
const Logger = require('winston'), logger = Logger.createLogger({
format: Logger.format.combine(
Logger.format.splat(),
Logger.format.simple(),
Logger.format.timestamp(),
Logger.format.printf(info => { return `[${info.timestamp}] [${info.level}] ${info.message}`; })
),
levels: Logger.config.syslog.levels,
transports: [
new Logger.transports.Console({ level: 'debug' }),
new Logger.transports.File({ filename: 'debug.log', level: 'debug' }),
new Logger.transports.File({ filename: 'info.log', level: 'info' }),
new Logger.transports.File({ filename: 'error.log', level: 'error' })
]
});
const Database = require('./Database'), db = new Database('data.sqlite3', logger);
const config = require('./config');
const gs = require('ghostscript4js');
const fs = require('fs');
const cwd = process.cwd();
const os = require('os');
const osType = os.type();
const exec = require('util').promisify(require('child_process').exec);
require('net').createServer(async s => {
const date = Date.now(), remoteAddr = s.remoteAddress === '::1' ? '127.0.0.1' : s.remoteAddress.substring(7);
let r;
if(!config.allowAll) {
if(remoteAddr === '127.0.0.1') {
r = { id: 1 };
} else {
r = await db.query('SELECT id, username, approved FROM user WHERE activeIp=(?);', remoteAddr);
if(!r) {
logger.info(`Dropped document from Not Registered IP ${remoteAddr}.`);
return s.end('Not Registered.');
}
if(!r.approved) {
logger.info(`Dropped document from Not Approvd Account ${r.id}(${remoteAddr}).`);
return s.end('Not Approved.');
}
}
} else {
r = { id: 0 };
}
let data = '';
logger.info(`Saving temporary data file to "${r.id}_${date}"...`);
s.pipe(fs.createWriteStream(`${cwd}/tmp/${r.id}_${date}`));
s.on('data', d => data += d);
s.on('end', () => {
if(!data.startsWith('%-12345X') && !data.startsWith('%!PS')) {
logger.error('This PJL not supported! Please use PS Printer Driver.');
return s.end('Not Supported.');
}
if(config.passthru) {
logger.info(`Saved tmp file from ${r.id}(${remoteAddr}) to "${cwd}/tmp/${r.id}_${date}". Printing...`);
let job = null;
switch(osType) {
case 'Windows_NT':
job = gs.execute(`-dPrinted -dBATCH -dNOPAUSE -dNOSAFER -dNumCopies=1 -sDEVICE=mswinpr2 -sOutputFile="%printer%${config.printerName}" "${cwd}/tmp/${r.id}_${date}"`); break;
case 'Darwin':
case 'Linux':
job = exec(`lpr -P "${config.printerName}" "${cwd}/tmp/${r.id}_${date}"`); break;
}
job.then(() => {
if(!config.allowAll) db.query('UPDATE user SET printCount=printCount+1 WHERE id=(?);', data.id);
}).catch(e => {
logger.error(`Error while print file "${cwd}/tmp/${r.id}_${date}": ${e.stack}`);
});
} else {
logger.info(`Saved tmp file from ${r.id}(${remoteAddr}) to "${cwd}/tmp/${r.id}_${date}". Saving to PDF File...`);
gs.execute(`-psconv -q -dNOPAUSE -sDEVICE=pdfwrite -o "${cwd}/data/${r.id}_${date}.pdf" -f "${cwd}/tmp/${r.id}_${date}"`).then(() => {
if(!config.allowAll) db.query('UPDATE user SET pdfCount=pdfCount+1 WHERE id=(?);', r.id);
logger.info(`Saved document from ${r.id}(${remoteAddr}) to "${cwd}/data/${r.id}_${date}.pdf". Removing tmp file...`);
fs.unlink(`${cwd}/tmp/${r.id}_${date}`, e => e ? logger.error(`Error while remove tmp file "${cwd}/tmp/${r.id}_${date}": ${e.stack}`) : logger.info(`Removed tmp file "${cwd}/tmp/${r.id}_${date}".`));
}).catch(e => logger.error(e.stack));
}
});
s.on('error', e => logger.error(e.stack));
}).listen(9100);