Skip to content

Commit

Permalink
更改服务端协议。添加日志功能
Browse files Browse the repository at this point in the history
  • Loading branch information
harry committed Aug 4, 2017
1 parent ebc74a8 commit 511b667
Show file tree
Hide file tree
Showing 60 changed files with 1,837 additions and 159 deletions.
2 changes: 1 addition & 1 deletion .idea/copyright/MIT.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 10 additions & 10 deletions .idea/modules/AndroidControl_main.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file removed docs/demo.gif
Binary file not shown.
100 changes: 100 additions & 0 deletions docs/protocol-design.md
@@ -0,0 +1,100 @@
| 版本 | 修订时间 | 修订内容 | 作者 |
| :---: | :---------: | :--: | :---: |
| 1.0.0 | 2017年07月14日 | 创建 | harry |
| | | | |
| | | | |

# 介绍

协议分为`文本协议``二进制协议`

文本协议:

* 属于二进制协议的一种,所有文本协议均可用二进制协议发送
* 文本协议只能用来传输文本信息

二进制协议:

* 可以传输任何信息,包括文本信息

# 文本协议 TextProtocol

文本协议格式:

`MessageHeader://Body`

MessageHeader:由普通字符串组成,标识该消息的类型

Body:由字符串组成,可以使普通字符串可也以是JSON数据,具体看消息类型

### HTPP接口

由于服务端是基于TCP的,所以接受HTTP协议传输

| URL | Return | Description |
| ------------------- | ----------------------------------- | ---------------- |
| devices | [{w:1920, h: 1080, sn:'xxxx'}, ...] | 返回已连接设备列表的JSON格式 |
| shot/{SerialNumber} | 手机截图的二进制内容 | 获取手机当前屏幕截图 |

### C->S

| Type | Data | Description |
| ---------- | ---------------------------------------- | -------------------------- |
| M_WAIT | JSON: {sn:SerialNumber[, key: [P2PKey]()]} | 请求连接手机,客户端已准备接受消息 |
| M_START | JSON: {type: xx, config:object} tpye:'cap'\|'event' config: [CapConfig]() | 通知服务端启动屏幕\|事件监听服务 |
| M_WAITTING | None | 等待服务端传来新的图像数据 |
| M_TOUCH | minitouch格式的输入数据 | 触摸事件 |
| M_KEYEVENT | 数值类型,关于[KeyEvent]() | 事件输入(目前为adb操作,速度较慢) |
| M_INPUT | 字符串 | 字符串输入(目前为adb操作,速度较慢切不支持中文) |
| M_PUSH | None | 保留 |
| M_SHOT | JSON: {sn:SerialNumber} | 请求目标设备的屏幕截图 |
| M_DEVICES | None | 请求已连接设备列表的JSON格式 |

### S->C

| Type | Data | Description |
| ---------------- | ---------------------------------------- | ------------------------------ |
| SM_OPENED | None | 接收到M_WAIT后,服务端与客户端连接建立成功后发送改消息 |
| SM_SERVICE_STATE | JSON: {type: xx, stat: "open"\|"close"} tpye:'cap'\|'event' | 服务的状态改变时,通知客户端 |
| SM_MESSAGE | text | 服务端主动给客户端发送的文本消息 |
| SM_DISCONNECT | None | 服务端主动关闭连接之前会发送 |
| SM_DEVICES | JSON:[{w:1920, h: 1080, sn:'xxxx'}, ...] | 返回已连接设备列表的JSON格式 |
| SM_SHOT | 详情请看BinaryProtocol | |
| SM_JPG | 详情请看BinaryProtocol | |



# 二进制协议 BinaryProtocol

二进制协议格式:

| MessageHeader | MessageBody |
| :-----------: | :---------: |
| short | * |
| 2byte | * |

### C->S

### S->C

* SM_SHOT `0x0010`

客户端发送M_SHOT后,服务端获取对应设备的屏幕截图,而后将截图返回给客户端

| Header | CType | Bytes | Description |
| -------------------- | ------ | ----- | ----------- |
| SM_SHOT | Int16 | 2 | 消息头 |
| SERIAL_NUMBER_LENGTH | UInt16 | 2 | 该图像所对应的设备序号 |
| DATA_LENGTH | UInt32 | 4 | 图像数据长度 |
| SERIAL_NUMBER_DATA | char[] | ... | 序列号 |
| DATA | char[] | ... | 图像数据 |

* SM_JPG `0x0011`

接受SM_SHOT消息,说明客户端已经打开了与设备的图像通路,所以该消息绝对只会和对应设备相关

| Header | CType | Bytes | Description |
| ----------- | ------ | ----- | ----------- |
| SM_JPG | Int16 | 2 | 消息头 |
| DATA_LENGTH | UInt32 | 4 | 图像数据长度 |
| DATA | char[] | ... | 图像数据 |
23 changes: 23 additions & 0 deletions docs/version-log.md
@@ -0,0 +1,23 @@
| VersionCode | VersionName | Time |
| :---------: | :---------: | :--------: |
| 0.1.0 | demo | 2017.07.06 |
| 1.0.0 | 正式版本 | 2017.08.11 |
| | | |



## 1.0.0

### 功能

* **增加**网页版入口,可直接通过浏览器访问服务器使用网页提供的简易功能
* **删除** 执行脚本的功能,因为该功能还不够稳定
* **删除**上传文件的功能,因为该功能还不够稳定

### 代码

* 重新设计了网络传输协议(仍然保留了旧协议的代码在代码中)

## 0.1.0

DEMO版本制作完成
26 changes: 0 additions & 26 deletions docs/接口说明-暂定.txt

This file was deleted.

Binary file removed docs/结构图.vsdx
Binary file not shown.
71 changes: 38 additions & 33 deletions src/main/java/com/yeetor/Main.java
@@ -1,7 +1,8 @@
/*
*
* MIT License
*
* Copyright (c) 2017 朱辉
* Copyright (c) 2017 朱辉 https://blog.yeetor.com
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand All @@ -20,13 +21,18 @@
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/

package com.yeetor;

import com.neovisionaries.ws.client.WebSocketException;
import com.yeetor.adb.AdbServer;
import com.yeetor.androidcontrol.client.RemoteClient;
import com.yeetor.androidcontrol.server.LocalServer;
import com.yeetor.androidcontrol.server.RemoteServer;
import com.yeetor.server.AndroidControlServer;
import org.apache.log4j.Logger;
import java.io.IOException;
import java.security.InvalidParameterException;


Expand All @@ -41,44 +47,43 @@ public class Main {
* [client ip port serialNumber]
* [client ip port]
*/
public static void main(String[] args) throws InterruptedException, IOException {

public static void main(String[] args) throws Exception {

// nc 127.0.0.1 4433 进入命令行交互界面
// Console.getInstance().listenOnTCP(4433);

// 监听USB的变化
AdbServer.server().listenUSB();

// 同步ADB的设备列表
AdbServer.server().listenADB();


AndroidControlServer server = new AndroidControlServer();
server.listen(6655);

while (true) {
System.out.print("> ");
System.in.read();
// System.console().read
try {
Config config = new Config(args);

if (config.isClient) {
new RemoteClient(config.ip, config.port, config.key, config.serialNumber);
} else {
if (config.isLocal) {
new LocalServer(config.port).start();
} else {
new RemoteServer(config.port).start();
}
}
} catch (InvalidParameterException ex) {
System.out.println("localserver <port>: 启动本地服务器(p2p)\n remoteserver <port> 启动服务器 \nremoteclient <ip> <port> <key> [serialNumber] 启动客户端");
System.exit(0);
} catch (WebSocketException |InterruptedException e) {
System.out.println("启动服务器失败: " + e.getMessage());
System.exit(0);
} catch (Exception e) {
e.printStackTrace();
System.exit(0);
}


// // parse命令行
// try {
// Config config = new Config(args);
//
// if (config.isClient) {
// new RemoteClient(config.ip, config.port, config.key, config.serialNumber);
// } else {
// if (config.isLocal) {
// new LocalServer(config.port).start();
// } else {
// new RemoteServer(config.port).start();
// }
// }
// } catch (InvalidParameterException ex) {
// System.out.println("localserver <port>: 启动本地服务器(p2p)\n remoteserver <port> 启动服务器 \nremoteclient <ip> <port> <key> [serialNumber] 启动客户端");
// System.exit(0);
// } catch (WebSocketException|InterruptedException e) {
// System.out.println("启动服务器失败: " + e.getMessage());
// System.exit(0);
// } catch (Exception e) {
// e.printStackTrace();
// System.exit(0);
// }
}

static class Config {
Expand Down
25 changes: 14 additions & 11 deletions src/main/java/com/yeetor/adb/AdbDevice.java
@@ -1,7 +1,8 @@
/*
*
* MIT License
*
* Copyright (c) 2017 朱辉
* Copyright (c) 2017 朱辉 https://blog.yeetor.com
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand All @@ -20,6 +21,7 @@
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/

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

/** PropertyCahe KEY for screenSize */
/** PropertyCahe KEY for screenSize 获取的是 widthxheight的字符串 */
public static final String SCREEN_SIZE = "SCREEN_SIZE";

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

// serialNumber
propertyCahe.put(SERIAL_NUMBER, iDevice.getSerialNumber());

// abi & sdk
String abi = iDevice.getProperty(Constant.PROP_ABI);
propertyCahe.put(Constant.PROP_ABI, abi);
String sdk = iDevice.getProperty(Constant.PROP_SDK);
propertyCahe.put(Constant.PROP_SDK, sdk);

// screenSize
String str = AdbServer.executeShellCommand(iDevice, "dumpsys window displays | sed -n '3p'");
// android 4.3 以下没有 displays
int sdkv = Integer.parseInt(sdk);
String shellCmd = sdkv > 16 ? "dumpsys window displays | sed -n '3p'" : "dumpsys window";
String str = AdbServer.executeShellCommand(iDevice, shellCmd);
if (str != null && !str.isEmpty()) {
Pattern pattern = Pattern.compile("init=(\\d+x\\d+)");
Matcher m = pattern.matcher(str);
if (m.find()) {
propertyCahe.put(SCREEN_SIZE, m.group(1));
}
}

// abi & sdk
String abi = iDevice.getProperty(Constant.PROP_ABI);
propertyCahe.put(Constant.PROP_ABI, abi);
String sdk = iDevice.getProperty(Constant.PROP_SDK);
propertyCahe.put(Constant.PROP_SDK, sdk);
}

/**
Expand All @@ -223,5 +227,4 @@ public String findPropertyCahe(String key) {
return propertyCahe.get(key);
}


}
4 changes: 3 additions & 1 deletion src/main/java/com/yeetor/adb/AdbForward.java
@@ -1,7 +1,8 @@
/*
*
* MIT License
*
* Copyright (c) 2017 朱辉
* Copyright (c) 2017 朱辉 https://blog.yeetor.com
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand All @@ -20,6 +21,7 @@
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/

package com.yeetor.adb;
Expand Down

0 comments on commit 511b667

Please sign in to comment.