-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
115 lines (106 loc) · 3.03 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
const path = require('path');
const { exec, execSync } = require('child_process');
const debug = require('debug')('dkimpy');
const ms = require('ms');
const semver = require('semver');
const scripts = {
dkimVerify: path.join(__dirname, 'scripts', 'dkimverify.py'),
arcSign: path.join(__dirname, 'scripts', 'arcsign.py'),
arcVerify: path.join(__dirname, 'scripts', 'arcverify.py')
};
// ensure python installed
try {
execSync('which python3', {
stdio: 'ignore',
encoding: 'utf8',
timeout: ms('5s')
});
} catch (err) {
debug(err);
throw new Error(`Python v3.5+ is required`);
}
// ensure python v3.5+
const version = semver.coerce(
execSync('python3 --version', { encoding: 'utf8', timeout: ms('5s') })
.split(' ')[1]
.trim()
);
if (!semver.satisfies(version, '>= 3.5'))
throw new Error(
`Python v3.5+ is required, you currently have v${version} installed`
);
function dkimVerify(message, index = 0) {
return new Promise((resolve, reject) => {
const child = exec(`python3 ${scripts.dkimVerify} --index ${index}`, {
encoding: 'utf8',
timeout: ms('10s')
});
const stdout = [];
const stderr = [];
child.stderr.on('data', (data) => {
stderr.push(data);
});
child.stdout.on('data', (data) => {
stdout.push(data);
});
child.stdin.write(message);
child.stdin.end();
child.on('close', (code) => {
// exits with code 1 if failed
if (code === 1) return resolve(false);
if (stderr.length > 0) return reject(new Error(stderr.join('')));
const output = stdout.join('');
if (output && output === 'signature ok') return resolve(true);
reject(new Error(output));
});
});
}
// eslint-disable-next-line max-params
async function arcSign(message, selector, domain, privateKeyFile, srvId) {
return new Promise((resolve, reject) => {
const child = exec(
`python3 ${scripts.arcSign} ${selector} ${domain} ${privateKeyFile} ${srvId}`,
{
encoding: 'utf8',
timeout: ms('10s')
}
);
const stdout = [];
const stderr = [];
child.stderr.on('data', (data) => {
stderr.push(data);
});
child.stdout.on('data', (data) => {
stdout.push(data);
});
child.stdin.write(message);
child.stdin.end();
child.on('close', () => {
if (stderr.length > 0) return reject(new Error(stderr.join('')));
resolve(stdout.join(''));
});
});
}
async function arcVerify(message) {
return new Promise((resolve, reject) => {
const child = exec(`python3 ${scripts.arcVerify}`, {
encoding: 'utf8',
timeout: ms('10s')
});
const stdout = [];
const stderr = [];
child.stderr.on('data', (data) => {
stderr.push(data);
});
child.stdout.on('data', (data) => {
stdout.push(data);
});
child.stdin.write(message);
child.stdin.end();
child.on('close', () => {
if (stderr.length > 0) return reject(new Error(stderr.join('')));
resolve(stdout.join(''));
});
});
}
module.exports = { dkimVerify, arcSign, arcVerify };