Module: PrivacyHunter

Defined in:
lib/cocoapods-privacy/privacy/PrivacyHunter.rb

Overview

功能介绍:

1、检测本地隐私协议清单模版是否最新,如果不存在或不是最新,那么下载远端隐私协议模版
2、使用模版对相关文件夹进行检索
3、检索到的内容转换成隐私协议格式写入 隐私清单文件 PrivacyInfo.xcprivacy

Constant Summary collapse

KTypes =
"NSPrivacyAccessedAPITypes"
KType =
"NSPrivacyAccessedAPIType"
KReasons =
"NSPrivacyAccessedAPITypeReasons"
KAPI =
"NSPrivacyAccessedAPI"

Class Method Summary collapse

Class Method Details

.formatter_privacy_templateObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/cocoapods-privacy/privacy/PrivacyHunter.rb', line 17

def self.formatter_privacy_template()
  #模版数据源plist文件
  template_plist_file = fetch_template_plist_file()

  # 读取并解析 数据源 plist 文件
  json_str = `plutil -convert json -o - "#{template_plist_file}"`.chomp
  map = JSON.parse(json_str)
  type_datas = map[KTypes]

  apis = {}
  keyword_type_map = {} #{systemUptime:NSPrivacyAccessedAPICategorySystemBootTime,mach_absolute_time:NSPrivacyAccessedAPICategorySystemBootTime .....}
  type_datas.each do |value|
    type = value[KType]
    apis_inner = value[KAPI]
    apis_inner.each do |keyword, reason|
      keyword_type_map[keyword] = type
    end
    apis = apis.merge(apis_inner)
  end
  [apis,keyword_type_map]
end

.search_pricacy_apis(source_folders, exclude_folders = []) ⇒ Object



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
# File 'lib/cocoapods-privacy/privacy/PrivacyHunter.rb', line 39

def self.search_pricacy_apis(source_folders,exclude_folders=[])
  apis,keyword_type_map = formatter_privacy_template()

  # 优化写法,一次循环完成所有查询
  datas = []
  apis_found = search_files(source_folders, exclude_folders, apis)
  unless apis_found.empty?
    apis_found.each do |keyword,reason|
      reasons = reason.split(',')
      type = keyword_type_map[keyword]
      
      # 如果有数据 给data增加reasons
      datas.map! do |data|
        if data[KType] == type
          data[KReasons] += reasons
          data[KReasons] = data[KReasons].uniq
        end
        data
      end

      # 如果没数据,新建data
      unless datas.any? { |data| data[KType] == type }
        data = {}
        data[KType] = type
        data[KReasons] ||= []
        data[KReasons] += reasons
        data[KReasons] = data[KReasons].uniq
        datas.push(data)
      end
    end
  end

  # 打印出搜索结果
  puts datas

  # 转换成 JSON 字符串
  json_data = datas.to_json
end

.write_to_privacy(json_data, privacy_path) ⇒ Object



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
# File 'lib/cocoapods-privacy/privacy/PrivacyHunter.rb', line 79

def self.write_to_privacy(json_data,privacy_path)

  # 如果指定了--query 参数,那么不进行写入操作,仅用来查询
  return if Pod::Config.instance.is_query

  # 转换 JSON 为 plist 格式
  plist_data = `echo '#{json_data}' | plutil -convert xml1 - -o -`

  # 创建临时文件
  temp_plist = File.join(PrivacyUtils.cache_privacy_fold,"#{PrivacyUtils.to_md5(privacy_path)}.plist")
  File.write(temp_plist, plist_data)

  # 获取原先文件中的 NSPrivacyAccessedAPITypes 数据
  origin_privacy_data = `/usr/libexec/PlistBuddy -c 'Print :NSPrivacyAccessedAPITypes' '#{privacy_path}' 2>/dev/null`
  new_privacy_data = `/usr/libexec/PlistBuddy -c 'Print' '#{temp_plist}'`

  # 检查新数据和原先数据是否一致
  if origin_privacy_data.strip == new_privacy_data.strip
    puts "#{privacy_path} 数据一致,无需插入。"
  else
    unless origin_privacy_data.strip.empty?
      # 删除 :NSPrivacyAccessedAPITypes 键
      system("/usr/libexec/PlistBuddy -c 'Delete :NSPrivacyAccessedAPITypes' '#{privacy_path}'")
    end

    # 添加 :NSPrivacyAccessedAPITypes 键并设置为数组
    system("/usr/libexec/PlistBuddy -c 'Add :NSPrivacyAccessedAPITypes array' '#{privacy_path}'")

    # 合并 JSON 数据到隐私文件
    system("/usr/libexec/PlistBuddy -c 'Merge #{temp_plist} :NSPrivacyAccessedAPITypes' '#{privacy_path}'")

    puts "NSPrivacyAccessedAPITypes 数据已插入。"
  end

  # 删除临时文件
  File.delete(temp_plist)
end