forked from EFForg/https-everywhere
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
75 lines (72 loc) · 2.1 KB
/
index.js
File metadata and controls
75 lines (72 loc) · 2.1 KB
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
'use strict';
const _ = require('highland');
const fs = require('fs');
const https = require('https');
const JSONStream = require('JSONStream');
const base64 = require('base64-stream');
const xmlBuilder = require('xmlbuilder');
function hostToRegex(host) {
return host.replace(/\./g, '\\.');
}
_(push => {
https.get('https://chromium.googlesource.com/chromium/src/net/+/master/http/transport_security_state_static.json?format=TEXT', res => {
push(null, res);
push(null, _.nil);
});
})
.flatMap(_)
.pipe(_.pipeline(
base64.decode(),
_.split(),
_.filter(line => !/^\s*\/\//.test(line)),
JSONStream.parse('entries.*')
))
.reduce({
xml: xmlBuilder.create('rulesetlibrary'),
rulesets: new Map(),
greedyInclusions: '(?!)',
potentialExclusions: new Map()
}, (acc, { name, mode = '', include_subdomains = false }) => {
if (mode === 'force-https') {
const ruleset = acc.xml.ele('ruleset', { name });
acc.rulesets.set(name, ruleset);
ruleset.ele('target', {
host: name
});
if (include_subdomains) {
acc.greedyInclusions += `|${hostToRegex(name)}`;
ruleset.ele('target', {
host: `*.${name}`
});
}
ruleset.ele('rule', {
from: '^http:',
to: 'https:'
});
} else {
acc.potentialExclusions.set(name, include_subdomains);
}
return acc;
})
.map(acc => {
const regexp = new RegExp(`\.(${acc.greedyInclusions})$`);
for (const [ name, include_subdomains ] of acc.potentialExclusions) {
const match = name.match(regexp);
if (match) {
const ruleset = acc.rulesets.get(match[1]);
ruleset.ele('exclusion', {
pattern: `^http://${include_subdomains ? '(?:[\\w-]+\\.)*' : ''}${hostToRegex(name)}/`
});
ruleset.ele('test', {
url: `http://${name}/`
});
if (include_subdomains) {
ruleset.ele('test', {
url: `http://host-part.${name}/`
});
}
}
}
return acc.xml.end({ pretty: true });
})
.pipe(fs.createWriteStream(`${__dirname}/hsts.xml`));