Class: JSObfu
Overview
The primary class, used to parse and obfuscate Javascript code.
Defined Under Namespace
Modules: Disable, Utils Classes: ECMANoWhitespaceVisitor, Hoister, Obfuscator, Scope
Instance Attribute Summary (collapse)
-
- (JSObfu::Scope) scope
readonly
The global scope.
Instance Method Summary (collapse)
-
- (Object) <<(str)
Add str to the un-obfuscated code.
-
- (RKelly::Nodes::SourceElementsNode) ast
The abstract syntax tree.
-
- (JSObfu) initialize(code)
constructor
Saves code for later obfuscation with #obfuscate.
-
- (self) obfuscate(opts = {})
Parse and obfuscate.
-
- (Object) parse
protected
Generate an Abstract Syntax Tree (#ast) for later obfuscation.
-
- (String) sym(sym)
Returns the obfuscated name for the variable or function sym.
-
- (String) to_s
The (possibly obfuscated) code.
Methods included from Disable
Constructor Details
- (JSObfu) initialize(code)
Saves code for later obfuscation with #obfuscate
22 23 24 25 |
# File 'lib/jsobfu.rb', line 22 def initialize(code) @code = code.to_s @scope = Scope.new end |
Instance Attribute Details
- (JSObfu::Scope) scope (readonly)
Returns the global scope
18 19 20 |
# File 'lib/jsobfu.rb', line 18 def scope @scope end |
Instance Method Details
- (Object) <<(str)
Add str to the un-obfuscated code. Calling this method after #obfuscate is undefined
29 30 31 |
# File 'lib/jsobfu.rb', line 29 def <<(str) @code << str end |
- (RKelly::Nodes::SourceElementsNode) ast
Returns the abstract syntax tree
39 40 41 |
# File 'lib/jsobfu.rb', line 39 def ast @ast || parse end |
- (self) obfuscate(opts = {})
Parse and obfuscate
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 |
# File 'lib/jsobfu.rb', line 53 def obfuscate(opts={}) return self if JSObfu.disabled? iterations = opts.fetch(:iterations, 1).to_i strip_whitespace = opts.fetch(:strip_whitespace, true) iterations.times do |i| obfuscator = JSObfu::Obfuscator.new(opts.merge(scope: @scope)) @code = obfuscator.accept(ast).to_s if strip_whitespace @code.gsub!(/(^\s+|\s+$)/, '') @code.delete!("\n") @code.delete!("\r") end new_renames = obfuscator.renames.dup if @renames # "patch up" the renames after each iteration @renames.each do |key, prev_rename| @renames[key] = new_renames[prev_rename] end else # on first iteration, take the renames as-is @renames = new_renames end unless i == iterations-1 @scope = Scope.new @ast = nil # force a re-parse end end self end |
- (Object) parse (protected)
Generate an Abstract Syntax Tree (#ast) for later obfuscation
102 103 104 |
# File 'lib/jsobfu.rb', line 102 def parse @ast = RKelly::Parser.new.parse(@code) end |
- (String) sym(sym)
Returns the obfuscated name for the variable or function sym
92 93 94 95 |
# File 'lib/jsobfu.rb', line 92 def sym(sym) return sym.to_s if @renames.nil? @renames[sym.to_s] end |
- (String) to_s
Returns the (possibly obfuscated) code
34 35 36 |
# File 'lib/jsobfu.rb', line 34 def to_s @code end |