forked from AloneMonkey/MonkeyDev
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmd-install
executable file
·377 lines (294 loc) · 9.66 KB
/
md-install
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
#!/bin/bash
#set -e表示一旦脚本中有命令的返回值为非0,则脚本立即退出,后续命令不再执行;
#set -o pipefail表示在管道连接的命令序列中,只要有任何一个命令返回非0值,则整个管道返回非0值,即使最后一个命令返回0.
export setCmd="set -eo pipefail"
$setCmd
#导出环境变量
export PATH=/opt/MonkeyDev/bin:/usr/local/bin:/usr/bin:/usr/sbin:/bin:/sbin:$PATH
#脚本名称和版本
export scriptName="${0##*/}"
export scriptVer="2.0"
#本地存储文件的目录
export MonkeyDevPath="/opt/MonkeyDev"
export backupFileExt=".MonkeyDev"
#获取用户名、用户组、用户目录、和profile文件
export userName="${SUDO_USER-$USER}"
export userGroup=`id -g $userName`
export userHome=`eval echo ~$userName`
#用户可能存在的profile文件
export bashProfileFiles=("$userHome/.zshrc" "$userHome/.bash_profile" "$userHome/.bashrc" "$userHome/.bash_login" "$userHome/.profile")
#获取临时文件名
export tempDirsFile="`mktemp -d -t $scriptName`/tempdirs"
touch "$tempDirsFile"
#把LANG变量从当前环境中删除
unset LANG
#出错退出
function cleanup()
{
local exitCode=$?
set +e
trap - $signals
removeTempData
exit $exitCode
}
function panic()
{
local exitCode=$1
set +e
shift
[[ "$@" == "" ]] || echo "$@" >&2
exit $exitCode
}
export signals="0 1 2 3 15"
#当shell接收到signals指定的信号时,执行cleanup命令
trap cleanup $signals
function removeTempData()
{
local tempDirs
if [[ -f "$tempDirsFile" ]]; then
tempDirs=(`cat "$tempDirsFile"`)
for td in "${tempDirs[@]}"; do
rm -rf "$td" || true
done
rm -rf "`dirname $tempDirsFile`" || true
fi
}
function getTempDir()
{
$setCmd
local tempDir
tempDir=`mktemp -d -t $scriptName` || \
panic $? "Failed to create temporary directory"
echo "$tempDir" >> "$tempDirsFile" || \
panic $? "Failed to echo into $tempDirsFile"
echo "$tempDir"
}
function copyFile()
{
cp -f "$1" "$2" || \
panic $? "Failed to copy file $1 to $2"
}
#备份原文件
function requireBackup()
{
[[ ! -f "$1" || -f "${1}${backupFileExt}" ]] || \
copyFile "$1" "${1}${backupFileExt}"
}
#获取SDK信息
function getSdkProperty()
{
$setCmd
local sdk="$1"
local propertyName="$2"
propertyValue=`xcodebuild -version -sdk $sdk $propertyName` || \
panic $? "Failed to get $sdk SDK property $propertyName"
[[ $propertyValue != "" ]] || \
panic 1 "Value of $sdk SDK property $propertyName cannot be empty"
# return #
echo "$propertyValue"
}
#下载文件
function downloadFile() # args: sourceUrl, targetPath
{
local sourceUrl="$1"
local targetPath="$2"
local curlPath
mkdir -p "${targetPath%/*}" || \
panic $? "Failed to make directory: ${targetPath%/*}"
curlPath=`which curl` || \
panic $? "Failed to get curl path"
"$curlPath" --output "$targetPath" "$sourceUrl" || \
panic $? "Failed to download $sourceUrl to $targetPath"
}
#解压文件
function extractTar() # args: tarPath, outputPath
{
local tarPath="$1"
local outputPath="$2"
tar -C "$outputPath" -zxf "$tarPath" || \
panic $? "Failed to extract $tarPath to $outputPath"
}
#下载github文件
function downloadGithubTarball() # args: url, outputDir, title
{
$setcmd
local url="$1"
local outputDir="$2"
local title="$3"
local tempDirForTar
local tempDirForFiles
local untardDir
local tarFile="file.tar.gz"
echo "Downloading $title from Github..."
tempDirForTar=`getTempDir`
tempDirForFiles=`getTempDir`
downloadFile "$url" "$tempDirForTar/$tarFile"
extractTar "$tempDirForTar/$tarFile" "$tempDirForFiles"
untardDir=`find "$tempDirForFiles/"* -type d -depth 0` || \
panic $? "Failed to get untar'ed directory name of $tempDirForTar/$tarFile"
mkdir -p "$outputDir" || \
panic $? "Failed to make directory: $outputDir"
cp -fR "$untardDir/"* "$outputDir/"
}
#修改文件权限
function changeMode()
{
local mode="$1"
local target="$2"
local recursive="$3"
local options
[[ $recursive != "true" ]] || \
options="-R"
if [[ -e "$target" ]]; then
chmod $options "$mode" "$target" || \
panic $? "Failed to change mode to $mode on $target"
fi
}
#获取用户profile文件
function determineUserBashProfileFile()
{
$setCmd
local f
local filePath
for f in "${bashProfileFiles[@]}"; do
if [[ -f "$f" ]]; then
filePath="$f"
echo "" >> "$f" || \
panic $? "Failed to echo into $f"
break
fi
done
if [[ $filePath == "" ]]; then
filePath="$bashProfileFiles"
touch "$filePath" || \
panic $? "Failed to touch $filePath"
chown "$userName:$userGroup" "$filePath" || \
panic $? "Failed to change owner-group of $filePath"
changeMode 0600 "$filePath"
fi
# return #
echo "$filePath"
}
#验证是否存在文件
function requireFile() # args: filePath [, touchFileIfNotFound]
{
local filePath="$1"
local touchFileIfNotFound="$2"
if [[ ! -f "$filePath" ]]; then
if [[ $touchFileIfNotFound == "true" ]]; then
touch "$filePath" || \
panic $? "Failed to touch $filePath"
else
panic 1 "File $filePath not found"
fi
fi
}
#增加内容到文件
function addToFileIfMissing() # args: filePath, pattern, value
{
local filePath="$1"
local pattern="$2"
local value="$3"
local doesContain
doesContain=`doesFileContain "$filePath" "$pattern"`
[[ $doesContain == "true" ]] || \
echo "$value" >> "$filePath" || \
panic $? "Failed to echo into $filePath"
}
#判断文件是否包含内容
function doesFileContain() # args: filePath, pattern
{
$setCmd
local filePath="$1"
local pattern="$2"
local perlValue
local funcReturn
perlValue=`perl -ne 'if (/'"$pattern"'/) { print "true"; exit; }' "$filePath"` || \
panic $? "Failed to perl"
if [[ $perlValue == "true" ]]; then
funcReturn="true"
else
funcReturn="false"
fi
# return #
echo $funcReturn
}
#从spec读取内容
function readXcodeSpecificationById(){ #args: filePath, id
local filePath="$1"
local id="$2"
content=`/usr/libexec/PlistBuddy -x -c Print "$filePath"` || \
panic $? "Failed to get $filePath content"
for (( i=0; i<=1; i++)); do
dict=`/usr/libexec/PlistBuddy -x -c "Print $i" "$filePath"`
if echo $dict | grep -qE "<string>$id</string>"; then
echo "$dict"
fi
done
}
#往spec文件写入内容
function writeDictToSpecification(){ #args: filePath, content
local filePath="$1"
local content="$2"
tempfile=`getTempDir`/dictfile
echo "$content" >> $tempfile
/usr/libexec/PlistBuddy -x -c 'add 0 dict' "$filePath" > /dev/null
/usr/libexec/PlistBuddy -x -c "merge $tempfile 0" "$filePath" > /dev/null
}
# start it
# 创建/opt/MonkeyDev
mkdir -p "$MonkeyDevPath" || \
panic $? "Failed to make directory: $MonkeyDevPath"
branch="master"
if [[ "$1" ]]; then
branch="$1"
fi
#下载一些基础文件和模板文件
downloadGithubTarball "https://codeload.github.com/AloneMonkey/MonkeyDev/tar.gz/$branch" "$MonkeyDevPath" "MonkeyDev base"
downloadGithubTarball "https://codeload.github.com/AloneMonkey/MonkeyDev-Xcode-Templates/tar.gz/$branch" "$MonkeyDevPath/templates" "Xcode templates"
#下载frida-ios-dump
echo "Downloading frida-ios-dump from Github..."
downloadFile "https://raw.githubusercontent.com/AloneMonkey/frida-ios-dump/3.x/dump.py" "$MonkeyDevPath/bin/dump.py"
downloadFile "https://raw.githubusercontent.com/AloneMonkey/frida-ios-dump/3.x/dump.js" "$MonkeyDevPath/bin/dump.js"
chmod +x "$MonkeyDevPath/bin/dump.py"
#创建符号链接
echo "Creating symlink to Xcode templates..."
#$userHome/Library/Developer/Xcode/Templates/MonkeyDev linkto $MonkeyDevPath/templates
userDevDir="$userHome/Library/Developer"
userTemplatesDir="$userDevDir/Xcode/Templates"
if [[ ! -d "$userTemplatesDir" ]]; then
mkdir -p "$userTemplatesDir" || \
panic $? "Failed to make directory: $userTemplatesDir"
chown -R "$userName:$userGroup" "$userDevDir" || \
panic $? "Failed to change ownership-group of $userDevDir"
fi
ln -fhs "$MonkeyDevPath/templates" "$userTemplatesDir/MonkeyDev"
#修改用户profile文件
echo "Modifying Bash personal initialization file..."
userBashProfileFile=`determineUserBashProfileFile`
addToFileIfMissing "$userBashProfileFile" "^(export)? *MonkeyDevPath=.*" "export MonkeyDevPath=$MonkeyDevPath"
addToFileIfMissing "$userBashProfileFile" "^(export)? *MonkeyDevDeviceIP=.*" "export MonkeyDevDeviceIP="
addToFileIfMissing "$userBashProfileFile" "^(export)? *PATH=.*(\\\$MonkeyDevPath\\/bin|${MonkeyDevPath//\//\\/}\\/bin).*" "export PATH=$MonkeyDevPath/bin:\$PATH"
#支持iphoneos command line tools
iosSdkPlatformPath=`getSdkProperty iphoneos PlatformPath`
macosSdkPlatformPath=`getSdkProperty macosx PlatformPath`
specificationFile=$(cd $iosSdkPlatformPath/../../.. && pwd)/PlugIns/IDEiOSSupportCore.ideplugin/Contents/Resources/Embedded-Device.xcspec
requireFile "$specificationFile" false
#backup
requireBackup "$specificationFile"
hasPackageTypeForCommandLineTool=`doesFileContain "$specificationFile" 'com.apple.package-type.mach-o-executable'`
hasProductTypeForCommandLineTool=`doesFileContain "$specificationFile" 'com.apple.product-type.tool'`
macosxSDKSpecificationsPath=$macosSdkPlatformPath/Developer/Library/Xcode/Specifications
packageTypesForMacOSXPath="$macosxSDKSpecificationsPath/MacOSX Package Types.xcspec"
productTypesForMacOSXPath="$macosxSDKSpecificationsPath/MacOSX Product Types.xcspec"
requireFile "$packageTypesForMacOSXPath" false
requireFile "$productTypesForMacOSXPath" false
if [[ $hasPackageTypeForCommandLineTool != "true" ]]; then
machoDict=`readXcodeSpecificationById "$packageTypesForMacOSXPath" "com.apple.package-type.mach-o-executable"`
writeDictToSpecification "$specificationFile" "$machoDict"
fi
if [[ $hasProductTypeForCommandLineTool != "true" ]]; then
toolDict=`readXcodeSpecificationById "$productTypesForMacOSXPath" "com.apple.product-type.tool"`
writeDictToSpecification "$specificationFile" "$toolDict"
fi
exit 0