-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstats.js
More file actions
171 lines (134 loc) · 3.48 KB
/
stats.js
File metadata and controls
171 lines (134 loc) · 3.48 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
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
'use strict';
const Fs = require('fs');
const Punycode = require('punycode');
const Logger = {
log: console.error,
};
function fetchIgnoredHosts() {
const data = `
vk.com
navalny.zta.lk
youtube.com
www.youtube.com
youtu.be
ytimg.com
cloudfront.net
yt3.ggpht.com
yt4.ggpht.com
s.ytimg.com
i.ytimg.com
habr.ru
habrahabr.ru
article31.club
yandex.ru
mail.ru
r.mail.ru
img.imgsmail.ru
limg.imgsmail.ru
mail.google.com
e.mail.ru
sync.disk.yandex.net
storage.mds.yandex.net
akamaiedge.net
akamai.net
soupcdn.com
`;
return { content: data.trim().split(/\s*\r?\n\s*/g), ifOk: true };
}
function generatePacFromString(dumpCsv, typeToProxyString) {
Logger.log('Generate pac from script...');
const columnsSep = ';';
const valuesSep = /\s*\|\s*/g;
var ips = {};
var hosts = {
// Custom hosts
'archive.org': true,
'bitcoin.org': true,
// LinkedIn
'licdn.com': true,
'linkedin.com': true,
// Based on users complaints:
'koshara.net': true,
'koshara.co': true
};
Logger.log('Splitting input...');
var lines = dumpCsv.split('\n');
const remoteUpdated = lines[0].trim();
Logger.log('For each line..');
const ipv4v6Re = /^(?:(?:[0-9]{1,3}\.){3}[0-9]{1,3}|(?:[0-9a-f]{0,4}:){2,7}[0-9a-f]{0,4})$/i;
for( var ii = 1; ii < lines.length; ++ii ) {
var line = lines[ii].trim();
if (!line) {
continue;
}
var values = line.split( columnsSep );
var newIps = values.shift().split( valuesSep );
var newHosts = values.shift().split( valuesSep ).map( function(h) { return Punycode.toASCII( h.replace(/\.+$/g, '').replace(/^\*\./g, '').replace(/^www\./g, '') ); } );
newIps.forEach( function (ip) {
ip = ip.trim();
if (!ip) {
return;
}
ips[ip] = true;
});
newHosts.forEach( function (host) {
host = host.trim();
if (!host) {
return;
}
if (ipv4v6Re.test(host)) {
ips[host] = true;
}
else {
hosts[host] = true;
}
});
};
Logger.log('Done.');
var res = fetchIgnoredHosts();
if (res.content) {
for(var i in res.content) {
var host = res.content[i];
delete hosts[host];
}
}
Logger.log('Hosts ignored.');
ips = Object.keys(ips).sort();
hosts = Object.keys(hosts).sort( function(a, b) { return a.split('').reverse() < b.split('').reverse() ? -1 : a !== b ? 1 : 0 } );
// COMMON
const countSorter = (counts) => (a,b) => counts[b] - counts[a];
const logCounts = (counts) => {
console.log('================');
console.log('ALL:', Object.keys(counts).reduce((acc, key) => acc + counts[key], 0));
console.log('================');
Object.keys(counts)
.sort()
//.sort(countSorter(counts))
.forEach((ch) => console.log(ch, counts[ch]));
};
// HOSTS
const charToCount = hosts.reduce((charToCount, host) => {
const ch = host.charAt(0);
charToCount[ch] = (charToCount[ch] || 0) + 1;
return charToCount;
}, {});
logCounts(charToCount);
// IPS
const [fstToCount, fthToCount] = [{}, {}];
ips.reduce((_, ip) => {
const [fst, snd, trd, fth] = ip.split('.');
fstToCount[fst] = (fstToCount[fst] || 0) + 1;
fthToCount[fth] = (fthToCount[fth] || 0) + 1;
},
{}
);
logCounts(fstToCount);
logCounts(fthToCount);
};
module.exports = () => {
const dumpCsv = Fs.readFileSync('./dump.csv').toString();
return generatePacFromString(dumpCsv, { HTTPS: 'HTTPS your_proxy.here:8080;' });
};
if (require.main === module) {
module.exports();
}