-
Notifications
You must be signed in to change notification settings - Fork 11
/
index.js
183 lines (141 loc) · 4.18 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#!/usr/bin/env node
var mkdirp = require('mkdirp');
var path = require('path');
var _ = require('lodash');
var fs = require('fs');
var glob = require('glob');
var cheerio = require('cheerio');
var Hashids = require("hashids"),
hashids = new Hashids("what does that say?");
var DEFINITIONS_CAP = /([#.][\w-]+)/g;
var STRIP = {
defs : /\{[^\}]*?\}/g,
comments: /\/\*[\s\S]*?\*\//g,
scope: /\([\s\S]*?\)/g,
query: /\[[\s\S]*?\]/g
}
var SAFE_PREFIX = "obscure-";
var SEED = (new Date()).getTime();
var OUTPUT = './obscure-'+SEED+'/';
function getDefinitions(cssString) {
//prep csstring, STRIPping out all the defs
for(var k in STRIP) {
cssString = cssString.replace(STRIP[k],'');
}
var matchs = [];
var match = DEFINITIONS_CAP.exec(cssString);
while (match != null) {
//console.log(match);
matchs.push(match[1]);
match = DEFINITIONS_CAP.exec(cssString);
}
return _.uniq(matchs);
}
function globlist(s) {
var split = s.split(',')
var list = []
for (var i in split) {
var files = glob.sync(split[i]);
list.push(files)
}
return _.uniq(_.flatten(list));
}
function obscure(opts) {
if(opts.output === undefined) opts.output = OUTPUT
if(opts.seed === undefined) opts.seed = OUTPUT
mkdirp.sync(opts.output)
console.log();
console.log(" * Obfuscation term generation using seed value of: " + opts.seed);
console.log(" * Output directory set to: " + opts.output);
console.log();
var excluded = [];
if(opts.exclude) {
console.log("Building exclusion map...");
var excludes = opts.exclude ? globlist(opts.exclude) : [];
for(var e in excludes) {
var css = fs.readFileSync(excludes[e], 'utf8');
var defs = getDefinitions(css);
for(var i = 0; i<defs.length; i++) {
var selector = defs[i];
excluded.push(selector);
}
}
console.log(" - " + _.size(excluded) + " excluded definitions ( classes and ids ) found in: " + opts.exclude )
}
console.log("Building inclusion map...");
var includes = globlist(opts.include)
var csss = {};
var map = {};
for(var t in includes) {
var path = includes[t];
var css = fs.readFileSync(path, 'utf8');
var defs = getDefinitions(css);
csss[path] = css; //store to obfuscate later
for(var i = 0; i<defs.length; i++) {
var selector = defs[i];
if(_.indexOf(excluded,selector)==-1) {
var obfused = hashids.encode(i+opts.seed);
var obj = {
sym: selector[0],
origin: selector.substring(1),
obfused: SAFE_PREFIX + obfused
}
map[selector] = obj;
// map.push(obj);
}
}
}
console.log(" - " + _.size(map) + " definitions ( classes and ids ) found in: " + opts.include )
var doms = {};
if(opts.apply) {
console.log("Building apply map...");
// var applys = opts.apply.split(',');
var applys = opts.apply ? globlist(opts.apply) : [];
for(var e in applys) {
var path = applys[e];
console.log(path);
var html = fs.readFileSync(path, 'utf8');
var $ = cheerio.load(html,{decodeEntities: true});
doms[path] = $;
}
}
console.log("Obfuscating definitions...");
console.log("Obfuscating files...");
for(var i in map) {
var obj = map[i];
for(var t in csss) {
var css = csss[t];
var re = new RegExp("\\" + obj.sym + obj.origin + "(?=[#.,\\{\\[\\(\\s])","g")
csss[t] = css.replace(re,obj.sym+obj.obfused);
}
for(var d in doms) {
var $ = doms[d];
if(obj.sym == '#') {
//this one is easy
$('#' + obj.origin).attr('id',obj.obfused);
}else if(obj.sym == '.') {
$('.' + obj.origin).removeClass(obj.origin).addClass(obj.obfused);
}
}
}
console.log("Writing obfuscated files...");
for(var t in csss) {
safeWrite(opts.output + t, csss[t]);
}
for(var d in doms) {
safeWrite(opts.output + d, doms[d].html());
}
var allkeys = _.union(_.keys(csss),_.keys(doms));
console.log(" - " + (_.size(allkeys)) + " obfuscated files were successfully written: " + allkeys.join(',') );
console.log();
console.log(" * See generated map for reference: " + opts.output + "obscure.map ");
safeWrite(opts.output + 'obscure.map', JSON.stringify(map, null, 4) );
}
function safeWrite(fd, buffer) {
var dir = path.dirname(fd);
mkdirp(dir,(err)=>{
if(err) console.error(err);
else fs.writeFileSync(fd, buffer);
})
}
module.exports = obscure;