This repository was archived by the owner on Mar 23, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathobsfucate.js
159 lines (124 loc) · 3.47 KB
/
obsfucate.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
const fs = require('fs')
const regex_block_comments = /([\-]{2,}[\[]{2}[\s\S]*?[\]]{2})/igm
const regex_block_comments2 = /\/\*[\s\S]*?\*\//igm
const regex_tabs = /([\t]{1,})/ig
const regex_newlines = /(\n)/gm
const regex_block_str_to_normal_str = /([\[]{2}[\s\S]*?[\]]{2})/igm
const STR_OPEN = ['"', "'", '`']
const STR_CLOSE = ['"', "'", '`']
const SCOPE_IN = ['do', 'then', 'function']
const SCOPE_OUT = ['end', 'elseif']
/*function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min
}
function randomWS(match, m) {
return ' '.repeat(getRandomInt(1, 10))
}*/
const regex_newline = /(\n)/g
const regex_newline2 = /(\r)/g
const regex_quote = /(")/g
function replace_block_str(match, m, offset, string) {
m = m.replace(regex_newline, '\\n')
m = m.replace(regex_newline2, '')
m = m.replace(regex_quote, '\\"')
return '"' + m.substring(2, m.length - 2) + '"'
}
function replace_locals(str) {
// TODO
/*var varName
var varId = 1
varName = 'v' + varId
varId++*/
return str
}
function decomment_line(str) {
if (str.substring(0, 2) === '--' || str.substring(0, 2) === '//')
return ' '
var si
var in_string = null
for (var i=0; i < str.length; i++) {
if (in_string !== null) {
if (str[i] === STR_CLOSE[in_string] && str[i - 1] !== '\\')
in_string = null
}
else if (str[i] === '-' && str[i + 1] === '-' || str[i] === '/' && str[i + 1] === '/') {
return str.substring(0, i) + ' '
}
else {
si = STR_OPEN.indexOf(str[i])
if (si >= 0)
in_string = si
}
}
return str
}
function obsfucate(str) {
str = str.replace(/(\r)/g, '')
str = str.replace(regex_block_comments, ' ')
str = str.replace(regex_block_comments2, ' ')
str = str.replace(regex_block_str_to_normal_str, replace_block_str)
const lines = str.split('\n')
for (var i=0; i < lines.length; i++) {
lines[i] = decomment_line(lines[i])
}
str = lines.join(' ')
str = str.replace(regex_tabs, ' ' /*randomWS*/)
str = str.replace(regex_newlines, ' ')
str = replace_locals(str)
return str
}
const regex_to_out = /(^[a-z0-9_-]*?)\//im
function toOut(file) {
return file.replace(regex_to_out, '$1_obsfucated/')
}
function obsfucateFile(file, skip) {
var str = fs.readFileSync(file, 'utf8')
if (skip)
console.log('copying ' + file)
else {
console.log('obfuscating ' + file)
str = obsfucate(str)
}
fs.writeFileSync(toOut(file), str)
}
function obfuscateDir(dir) {
var out_dir = toOut(dir)
if (!fs.existsSync(out_dir))
fs.mkdirSync(out_dir)
const readdir = fs.readdirSync(dir)
for (var i=0; i < readdir.length; i++) {
if (readdir[i].indexOf('.') >= 0) {
if (readdir[i].indexOf('.lua') >= 0)
obsfucateFile(dir + '/' + readdir[i], readdir[i] === 'init.lua' || readdir[i] === 'server.lua' || readdir[i].substr(0, 3) === 'sv_')
}
else {
out_dir = toOut(dir + '/' + readdir[i])
if (!fs.existsSync(out_dir))
fs.mkdirSync(out_dir)
obfuscateDir(dir + '/' + readdir[i])
}
}
}
function deleteFolderRecursive(path) {
if (fs.existsSync(path)) {
fs.readdirSync(path).forEach(function(file, index) {
var curPath = path + "/" + file
if (fs.lstatSync(curPath).isDirectory())
deleteFolderRecursive(curPath)
else
fs.unlinkSync(curPath)
})
fs.rmdirSync(path)
}
}
if (process.argv.length < 2)
console.log('No folders specified')
else
process.argv.forEach(function (val, index, array) {
if (index >= 2) {
const out_path = val + '_obsfucated'
deleteFolderRecursive(out_path)
fs.mkdirSync(out_path)
obfuscateDir(val)
}
})