Module: Obscure

Defined in:
lib/codeobscure/obscure.rb

Constant Summary collapse

@@STRING_SYMBOL_FILE =
"func.list"
@@IGNORE_NAME =
"ignoresymbols"
@@PROJECT_TRAIT =

保留项目特征,避免过度混淆被识别

["Model", "View", "Field", "Label", "Item", "ViewModel", "Cell", "Button", "ViewController", "TableView", "CollectionView", "Control", "Entity", "Delegate"]

Class Method Summary collapse

Class Method Details

.ramdomString(var_type, replace_type = 1, reference_content = nil) ⇒ Object

type 1 :随机字符, 2 : 随机单词, 3 : 自定义单词替换 reference_content 原来准备替换的内容, 避免过度混淆



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
# File 'lib/codeobscure/obscure.rb', line 20

def self.ramdomString(var_type , replace_type = 1, reference_content = nil)  
  result = ""
  case replace_type 
  when 1
    result = `openssl rand -base64 64 | tr -cd 'a-zA-Z' |head -c 16`
  when 2 
    words = RandomWord.phrases.next.split /[ _]/  
    join_words = ""
    words.each_with_index do |word,index| 
      if index == 0  

        join_words += word
        if var_type == "c"
          join_words.capitalize!
        end
      else
        join_words += word.capitalize
      end
    end
    result = join_words
  when 3
    raise "c选项功能暂未实现,下一版本中加入!"
  end

  for trait in @@PROJECT_TRAIT do
    if reference_content 
      if reference_content.end_with? trait 
        result += trait
        break
      end
    end
  end

  result
end

.run(root_dir, type = 'r') ⇒ Object

有define重复的问题等待解决



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
# File 'lib/codeobscure/obscure.rb', line 82

def self.run(root_dir,type = 'r')

  replace_type = toTypeNumber type

  @@HEAD_FILE="#{root_dir}/codeObfuscation.h"  
  @@STRING_SYMBOL_FILE = "#{root_dir}/#{@@STRING_SYMBOL_FILE}"

  ignore_symbols = []
  ignore_path = "#{root_dir}/#{@@IGNORE_NAME}"
  if File.exist? ignore_path
    ignore_content = File.read ignore_path
    ignore_symbols = ignore_content.gsub(/\s/ , "").split ","
  end
  
  if File.exists? @@HEAD_FILE 
    `rm -f '#{@@HEAD_FILE}'` 
  end 

  date = `date`
  file = File.new @@HEAD_FILE , 'w'
  file.puts "#ifndef co_codeObfuscation_h" 
  file.puts "#define co_codeObfuscation_h" 
  file.puts "//confuse string at #{date.to_s}"

  symbol_file = File.open(@@STRING_SYMBOL_FILE).read
  symbol_file.each_line do |line|
    line_type = line.rstrip.split(":").first
    line_content = line.rstrip.split(":").last
    result = FiltSymbols.query(line_content) 
    if result.nil? || result.empty? 
      ramdom = ramdomString line_type , replace_type, line_content
      if line_type == "p"
        result = FiltSymbols.query("set#{line_content.upcase_first_letter}") 
        if result.nil? || result.empty? 
          if !ignore_symbols.include?(line_content)
            file.puts(toDefine "#{line_content}", "#{ramdom}")
            file.puts(toDefine "_#{line_content}", "_#{ramdom}")
            file.puts(toDefine "set#{line_content.upcase_first_letter}", "set#{ramdom.upcase_first_letter}")
          end
        end
      else 
          if !ignore_symbols.include?(line_content)
            file.puts(toDefine "#{line_content}", "#{ramdom}")
          end
      end
    end 
  end

  file.puts "#endif" 
  file.close

  if File.exist? @@STRING_SYMBOL_FILE
    `rm -f '#{@@STRING_SYMBOL_FILE}'`
  end
  @@HEAD_FILE
end

.toDefine(from, to) ⇒ Object



73
74
75
76
77
78
79
# File 'lib/codeobscure/obscure.rb', line 73

def self.toDefine(from, to) 
  <<~Define
      #ifndef #{from}
        #define #{from} #{to}
      #endif
  Define
end

.toTypeNumber(type) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/codeobscure/obscure.rb', line 56

def self.toTypeNumber(type)
  result = 1
  case type
  when "r"
    result = 1
  when "w" 
    result = 2
  when "c"
    result = 3
  else
    puts "若参数不符合规则,默认切换到[r]的规则形式进行替换!".colorize(:yellow)
    result = 1
  end

  result
end