Skip to content

Commit 511b667

Browse files
author
harry
committed
更改服务端协议。添加日志功能
1 parent ebc74a8 commit 511b667

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+1837
-159
lines changed

.idea/copyright/MIT.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules/AndroidControl_main.iml

Lines changed: 10 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/demo.gif

-1.92 MB
Binary file not shown.

docs/protocol-design.md

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
| 版本 | 修订时间 | 修订内容 | 作者 |
2+
| :---: | :---------: | :--: | :---: |
3+
| 1.0.0 | 2017年07月14日 | 创建 | harry |
4+
| | | | |
5+
| | | | |
6+
7+
# 介绍
8+
9+
协议分为`文本协议``二进制协议`
10+
11+
文本协议:
12+
13+
* 属于二进制协议的一种,所有文本协议均可用二进制协议发送
14+
* 文本协议只能用来传输文本信息
15+
16+
二进制协议:
17+
18+
* 可以传输任何信息,包括文本信息
19+
20+
# 文本协议 TextProtocol
21+
22+
文本协议格式:
23+
24+
`MessageHeader://Body`
25+
26+
MessageHeader:由普通字符串组成,标识该消息的类型
27+
28+
Body:由字符串组成,可以使普通字符串可也以是JSON数据,具体看消息类型
29+
30+
### HTPP接口
31+
32+
由于服务端是基于TCP的,所以接受HTTP协议传输
33+
34+
| URL | Return | Description |
35+
| ------------------- | ----------------------------------- | ---------------- |
36+
| devices | [{w:1920, h: 1080, sn:'xxxx'}, ...] | 返回已连接设备列表的JSON格式 |
37+
| shot/{SerialNumber} | 手机截图的二进制内容 | 获取手机当前屏幕截图 |
38+
39+
### C->S
40+
41+
| Type | Data | Description |
42+
| ---------- | ---------------------------------------- | -------------------------- |
43+
| M_WAIT | JSON: {sn:SerialNumber[, key: [P2PKey]()]} | 请求连接手机,客户端已准备接受消息 |
44+
| M_START | JSON: {type: xx, config:object} tpye:'cap'\|'event' config: [CapConfig]() | 通知服务端启动屏幕\|事件监听服务 |
45+
| M_WAITTING | None | 等待服务端传来新的图像数据 |
46+
| M_TOUCH | minitouch格式的输入数据 | 触摸事件 |
47+
| M_KEYEVENT | 数值类型,关于[KeyEvent]() | 事件输入(目前为adb操作,速度较慢) |
48+
| M_INPUT | 字符串 | 字符串输入(目前为adb操作,速度较慢切不支持中文) |
49+
| M_PUSH | None | 保留 |
50+
| M_SHOT | JSON: {sn:SerialNumber} | 请求目标设备的屏幕截图 |
51+
| M_DEVICES | None | 请求已连接设备列表的JSON格式 |
52+
53+
### S->C
54+
55+
| Type | Data | Description |
56+
| ---------------- | ---------------------------------------- | ------------------------------ |
57+
| SM_OPENED | None | 接收到M_WAIT后,服务端与客户端连接建立成功后发送改消息 |
58+
| SM_SERVICE_STATE | JSON: {type: xx, stat: "open"\|"close"} tpye:'cap'\|'event' | 服务的状态改变时,通知客户端 |
59+
| SM_MESSAGE | text | 服务端主动给客户端发送的文本消息 |
60+
| SM_DISCONNECT | None | 服务端主动关闭连接之前会发送 |
61+
| SM_DEVICES | JSON:[{w:1920, h: 1080, sn:'xxxx'}, ...] | 返回已连接设备列表的JSON格式 |
62+
| SM_SHOT | 详情请看BinaryProtocol | |
63+
| SM_JPG | 详情请看BinaryProtocol | |
64+
65+
66+
67+
# 二进制协议 BinaryProtocol
68+
69+
二进制协议格式:
70+
71+
| MessageHeader | MessageBody |
72+
| :-----------: | :---------: |
73+
| short | * |
74+
| 2byte | * |
75+
76+
### C->S
77+
78+
### S->C
79+
80+
* SM_SHOT `0x0010`
81+
82+
客户端发送M_SHOT后,服务端获取对应设备的屏幕截图,而后将截图返回给客户端
83+
84+
| Header | CType | Bytes | Description |
85+
| -------------------- | ------ | ----- | ----------- |
86+
| SM_SHOT | Int16 | 2 | 消息头 |
87+
| SERIAL_NUMBER_LENGTH | UInt16 | 2 | 该图像所对应的设备序号 |
88+
| DATA_LENGTH | UInt32 | 4 | 图像数据长度 |
89+
| SERIAL_NUMBER_DATA | char[] | ... | 序列号 |
90+
| DATA | char[] | ... | 图像数据 |
91+
92+
* SM_JPG `0x0011`
93+
94+
接受SM_SHOT消息,说明客户端已经打开了与设备的图像通路,所以该消息绝对只会和对应设备相关
95+
96+
| Header | CType | Bytes | Description |
97+
| ----------- | ------ | ----- | ----------- |
98+
| SM_JPG | Int16 | 2 | 消息头 |
99+
| DATA_LENGTH | UInt32 | 4 | 图像数据长度 |
100+
| DATA | char[] | ... | 图像数据 |

docs/version-log.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
| VersionCode | VersionName | Time |
2+
| :---------: | :---------: | :--------: |
3+
| 0.1.0 | demo | 2017.07.06 |
4+
| 1.0.0 | 正式版本 | 2017.08.11 |
5+
| | | |
6+
7+
8+
9+
## 1.0.0
10+
11+
### 功能
12+
13+
* **增加**网页版入口,可直接通过浏览器访问服务器使用网页提供的简易功能
14+
* **删除** 执行脚本的功能,因为该功能还不够稳定
15+
* **删除**上传文件的功能,因为该功能还不够稳定
16+
17+
### 代码
18+
19+
* 重新设计了网络传输协议(仍然保留了旧协议的代码在代码中)
20+
21+
## 0.1.0
22+
23+
DEMO版本制作完成

docs/接口说明-暂定.txt

Lines changed: 0 additions & 26 deletions
This file was deleted.

docs/结构图.vsdx

-62.2 KB
Binary file not shown.

src/main/java/com/yeetor/Main.java

Lines changed: 38 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
/*
2+
*
23
* MIT License
34
*
4-
* Copyright (c) 2017 朱辉
5+
* Copyright (c) 2017 朱辉 https://blog.yeetor.com
56
*
67
* Permission is hereby granted, free of charge, to any person obtaining a copy
78
* of this software and associated documentation files (the "Software"), to deal
@@ -20,13 +21,18 @@
2021
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
2122
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2223
* SOFTWARE.
24+
*
2325
*/
2426

2527
package com.yeetor;
2628

29+
import com.neovisionaries.ws.client.WebSocketException;
2730
import com.yeetor.adb.AdbServer;
31+
import com.yeetor.androidcontrol.client.RemoteClient;
32+
import com.yeetor.androidcontrol.server.LocalServer;
33+
import com.yeetor.androidcontrol.server.RemoteServer;
34+
import com.yeetor.server.AndroidControlServer;
2835
import org.apache.log4j.Logger;
29-
import java.io.IOException;
3036
import java.security.InvalidParameterException;
3137

3238

@@ -41,44 +47,43 @@ public class Main {
4147
* [client ip port serialNumber]
4248
* [client ip port]
4349
*/
44-
public static void main(String[] args) throws InterruptedException, IOException {
45-
50+
public static void main(String[] args) throws Exception {
51+
52+
// nc 127.0.0.1 4433 进入命令行交互界面
53+
// Console.getInstance().listenOnTCP(4433);
54+
4655
// 监听USB的变化
4756
AdbServer.server().listenUSB();
4857

4958
// 同步ADB的设备列表
5059
AdbServer.server().listenADB();
60+
61+
62+
AndroidControlServer server = new AndroidControlServer();
63+
server.listen(6655);
5164

52-
while (true) {
53-
System.out.print("> ");
54-
System.in.read();
55-
// System.console().read
65+
try {
66+
Config config = new Config(args);
67+
68+
if (config.isClient) {
69+
new RemoteClient(config.ip, config.port, config.key, config.serialNumber);
70+
} else {
71+
if (config.isLocal) {
72+
new LocalServer(config.port).start();
73+
} else {
74+
new RemoteServer(config.port).start();
75+
}
76+
}
77+
} catch (InvalidParameterException ex) {
78+
System.out.println("localserver <port>: 启动本地服务器(p2p)\n remoteserver <port> 启动服务器 \nremoteclient <ip> <port> <key> [serialNumber] 启动客户端");
79+
System.exit(0);
80+
} catch (WebSocketException |InterruptedException e) {
81+
System.out.println("启动服务器失败: " + e.getMessage());
82+
System.exit(0);
83+
} catch (Exception e) {
84+
e.printStackTrace();
85+
System.exit(0);
5686
}
57-
58-
59-
// // parse命令行
60-
// try {
61-
// Config config = new Config(args);
62-
//
63-
// if (config.isClient) {
64-
// new RemoteClient(config.ip, config.port, config.key, config.serialNumber);
65-
// } else {
66-
// if (config.isLocal) {
67-
// new LocalServer(config.port).start();
68-
// } else {
69-
// new RemoteServer(config.port).start();
70-
// }
71-
// }
72-
// } catch (InvalidParameterException ex) {
73-
// System.out.println("localserver <port>: 启动本地服务器(p2p)\n remoteserver <port> 启动服务器 \nremoteclient <ip> <port> <key> [serialNumber] 启动客户端");
74-
// System.exit(0);
75-
// } catch (WebSocketException|InterruptedException e) {
76-
// System.out.println("启动服务器失败: " + e.getMessage());
77-
// System.exit(0);
78-
// } catch (Exception e) {
79-
// e.printStackTrace();
80-
// System.exit(0);
81-
// }
8287
}
8388

8489
static class Config {

src/main/java/com/yeetor/adb/AdbDevice.java

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
/*
2+
*
23
* MIT License
34
*
4-
* Copyright (c) 2017 朱辉
5+
* Copyright (c) 2017 朱辉 https://blog.yeetor.com
56
*
67
* Permission is hereby granted, free of charge, to any person obtaining a copy
78
* of this software and associated documentation files (the "Software"), to deal
@@ -20,6 +21,7 @@
2021
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
2122
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2223
* SOFTWARE.
24+
*
2325
*/
2426

2527
package com.yeetor.adb;
@@ -57,7 +59,7 @@ public class AdbDevice {
5759
/** PropertyCahe KEY for serialNumber */
5860
public static final String SERIAL_NUMBER = "sn";
5961

60-
/** PropertyCahe KEY for screenSize */
62+
/** PropertyCahe KEY for screenSize 获取的是 widthxheight的字符串 */
6163
public static final String SCREEN_SIZE = "SCREEN_SIZE";
6264

6365
/** The claimed USB ADB interface. */
@@ -197,22 +199,24 @@ private void fillPropertyCahe() {
197199

198200
// serialNumber
199201
propertyCahe.put(SERIAL_NUMBER, iDevice.getSerialNumber());
202+
203+
// abi & sdk
204+
String abi = iDevice.getProperty(Constant.PROP_ABI);
205+
propertyCahe.put(Constant.PROP_ABI, abi);
206+
String sdk = iDevice.getProperty(Constant.PROP_SDK);
207+
propertyCahe.put(Constant.PROP_SDK, sdk);
200208

201-
// screenSize
202-
String str = AdbServer.executeShellCommand(iDevice, "dumpsys window displays | sed -n '3p'");
209+
// android 4.3 以下没有 displays
210+
int sdkv = Integer.parseInt(sdk);
211+
String shellCmd = sdkv > 16 ? "dumpsys window displays | sed -n '3p'" : "dumpsys window";
212+
String str = AdbServer.executeShellCommand(iDevice, shellCmd);
203213
if (str != null && !str.isEmpty()) {
204214
Pattern pattern = Pattern.compile("init=(\\d+x\\d+)");
205215
Matcher m = pattern.matcher(str);
206216
if (m.find()) {
207217
propertyCahe.put(SCREEN_SIZE, m.group(1));
208218
}
209219
}
210-
211-
// abi & sdk
212-
String abi = iDevice.getProperty(Constant.PROP_ABI);
213-
propertyCahe.put(Constant.PROP_ABI, abi);
214-
String sdk = iDevice.getProperty(Constant.PROP_SDK);
215-
propertyCahe.put(Constant.PROP_SDK, sdk);
216220
}
217221

218222
/**
@@ -223,5 +227,4 @@ public String findPropertyCahe(String key) {
223227
return propertyCahe.get(key);
224228
}
225229

226-
227230
}

src/main/java/com/yeetor/adb/AdbForward.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
/*
2+
*
23
* MIT License
34
*
4-
* Copyright (c) 2017 朱辉
5+
* Copyright (c) 2017 朱辉 https://blog.yeetor.com
56
*
67
* Permission is hereby granted, free of charge, to any person obtaining a copy
78
* of this software and associated documentation files (the "Software"), to deal
@@ -20,6 +21,7 @@
2021
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
2122
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2223
* SOFTWARE.
24+
*
2325
*/
2426

2527
package com.yeetor.adb;

0 commit comments

Comments
 (0)