通过MonkeyDev加载网络或者自己的cy脚本

前言

一开始想到这个功能的时候是因为Hank老师在我的微信公众号(alonemonkey)里面问了一个问题:

image

越狱设备

在越狱设备上面,可以将自己的脚本放到/usr/lib/cycript0.9/下面,然后通过@import xxx来导入即可使用自定义脚本里面封装的一些函数,极为方便。比如其自带的/usr/lib/cycript0.9/com/saurik/substrate/MS.cy就可以通过@import com.saurik.substrate.MS来导入这个模块,同样也可以放入一些自己的脚本来导入。那么在非越狱环境怎么去解决使用自定义脚本的问题呢?

非越狱设备

其实非越狱设备实现这个很简单,只要让cycript能找到我们自己的脚本所在的位置就行了,修改libcycript.cy文件的require.resolve函数,使其从当前应用目录或者沙盒目录里面去查找脚本,然后程序从网络下载脚本保存到沙盒目录就可以了,修改部分如下:

1
2
3
4
5
6
7
8
9
10
11
12
require.resolve = function(name) {
......
let library = GetLibraryPath();
let path = ResolveFile(true, library + "/" + name + ".cy");
if (path != null)
return path;

let document = GetDocumentPath();
path = ResolveFile(true, document + "/cycript/" + name + ".cy");
if (path != null)
return path;
......

然后笔者在MonkeyDev的模块中增加了一个文件用于配置下载脚本的名字以及url:

key是脚本导入时候的名字,value是脚本所在的url,程序启动会自动下载脚本保存在Document的目录下面,然后在终端通过如下方式导入使用即可:

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
➜  cycript_0.9.594 ./cycript -r 192.168.2.248:6666
cy# @import md
{constants:{APPID:@"com.alonemonkey.TestCycript",APPPATH:@"/var/containers/Bundle/Application/FF940D03-A857-4BBC-A01A-23585A86F76E/TestCycript.app",APPHOME:@"/var/mobile/Containers/Data/Application/DADEBC7F-6472-40F3-8C1E-4383C4843405",APPDOC:@"/var/mobile/Containers/Data/Application/DADEBC7F-6472-40F3-8C1E-4383C4843405/Documents",APPLIBRARY:@"/var/mobile/Containers/Data/Application/DADEBC7F-6472-40F3-8C1E-4383C4843405/Library",APPCACHE:@"/var/mobile/Containers/Data/Application/DADEBC7F-6472-40F3-8C1E-4383C4843405/Library/Caches"},pviews:function (){return UIApp.keyWindow.recursiveDescription().toString()},pvcs:function (){return UIWindow.keyWindow().rootViewController._printHierarchy().toString()},rp:function (t){var e;e=""+t.toString();while(t.nextResponder){e+="\n"+t.nextResponder.toString();t=t.nextResponder}return e},pactions:function (r){var e,t,n,i;i="";n=r.allTargets.allObjects();for(e=0;e<n.length;e++){t=objc_msgSend(r,"actionsForTarget:forControlEvent:",n[e],0);i+=n[e]+" "+objc_msgSend(t,"componentsJoinedByString:",Instance.box(","))}return i}}
cy# APPID
@"com.alonemonkey.TestCycript"
cy# pviews()
`<UIWindow: 0x105313b60; frame = (0 0; 375 667); autoresize = W+H; gestureRecognizers = <NSArray: 0x1c425bae0>; layer = <UIWindowLayer: 0x1c40394e0>>
| <UIView: 0x1053205d0; frame = (0 0; 375 667); autoresize = W+H; layer = <CALayer: 0x1c403bd80>>
| | <UILabel: 0x10531e7d0; frame = (102.5 45; 170 40); text = 'AloneMonkey'; opaque = NO; autoresize = RM+BM; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x1c408c0d0>>
| | <UILabel: 0x1053207b0; frame = (97.5 110; 180 40); text = 'You are the best!!!'; opaque = NO; autoresize = RM+BM; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x1c408e150>>
| | <UITextView: 0x105836000; frame = (26 230; 343 427); text = '/opt/MonkeyDev/bin/md

- ...'; clipsToBounds = YES; autoresize = RM+BM; gestureRecognizers = <NSArray: 0x1c0058fc0>; layer = <CALayer: 0x1c002f1a0>; contentOffset: {0, 0}; contentSize: {343, 317}; adjustedContentInset: {0, 0, 0, 0}>
| | | <<_UITextContainerView: 0x105318060; frame = (0 0; 343 317); layer = <__UITextTiledLayer: 0x1c40c42f0>> minSize = {0, 0}, maxSize = {1.7976931348623157e+308, 1.7976931348623157e+308}, textContainer = <NSTextContainer: 0x1c0105fa0 size = (343.000000,inf); widthTracksTextView = YES; heightTracksTextView = NO>; exclusionPaths = 0x1c40025e0; lineBreakMode = 0>
| | | | <__UITileLayer: 0x1c0240780> (layer)
| | | | <__UITileLayer: 0x1c0240720> (layer)
| | | | <__UITileLayer: 0x1c0240a20> (layer)
| | | <UIImageView: 0x105322260; frame = (3 421.5; 337 2.5); alpha = 0; opaque = NO; autoresize = TM; userInteractionEnabled = NO; layer = <CALayer: 0x1c403c620>>
| | | <UIImageView: 0x105322490; frame = (337.5 380; 2.5 44); alpha = 0; opaque = NO; autoresize = LM; userInteractionEnabled = NO; layer = <CALayer: 0x1c403c6e0>>
| | <UIButton: 0x1053163b0; frame = (127.5 175; 120 30); opaque = NO; autoresize = RM+BM; layer = <CALayer: 0x1c4039aa0>>
| | | <UIButtonLabel: 0x10540fcf0; frame = (2 6; 116.5 18); text = 'ShowChangeLog'; opaque = NO; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x1c0087b70>>
| | <_UILayoutGuide: 0x105320fa0; frame = (0 0; 0 20); hidden = YES; layer = <CALayer: 0x1c403bfe0>>
| | <_UILayoutGuide: 0x1053213a0; frame = (0 667; 0 0); hidden = YES; layer = <CALayer: 0x1c403bee0>>`
cy# pvcs()
"<CustomViewController 0x1053133f0>, state: appeared, view: <UIView 0x1053205d0>"
cy# pactions(#0x1053163b0)
"<CustomViewController: 0x1053133f0> showChangeLog:"
cy# rp(#0x1053163b0)
`<UIButton: 0x1053163b0; frame = (127.5 175; 120 30); opaque = NO; autoresize = RM+BM; layer = <CALayer: 0x1c4039aa0>>
<UIView: 0x1053205d0; frame = (0 0; 375 667); autoresize = W+H; layer = <CALayer: 0x1c403bd80>>
<CustomViewController: 0x1053133f0>
<UIWindow: 0x105313b60; frame = (0 0; 375 667); autoresize = W+H; gestureRecognizers = <NSArray: 0x1c425bae0>; layer = <UIWindowLayer: 0x1c40394e0>>
<UIApplication: 0x105406120>
<AppDelegate: 0x1c002b1a0>`
cy# ?exit

当时在本地的脚本可以拷贝到Frameworks的目录下面:

image

然后通过@import new就可以导入使用了,是不是很方便!

总结

其实这个功能实现确实很简单,但是目前还没有把里面带的MS.cy弄进来,后面看看再弄吧。

AloneMonkey wechat
欢迎您扫一扫上面的微信公众号,订阅我的博客!