Skip to content

Commit 79ee19e

Browse files
author
harry
committed
添加jar包配置文件
1 parent 8563aab commit 79ee19e

File tree

5 files changed

+159
-90
lines changed

5 files changed

+159
-90
lines changed

docs/version-log.md

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,26 @@
44
| 1.0.0 | 正式版本 | 2017.08.11 |
55
| | | |
66

7-
8-
97
## 1.0.0
108

119
### 功能
1210

1311
* **增加**网页版入口,可直接通过浏览器访问服务器使用网页提供的简易功能
14-
* **删除** 执行脚本的功能,因为该功能还不够稳定
12+
13+
* **修改**废除旧版的协议相关代码,但是依然保留着。原因是旧版的协议非常不完善。并添加了新的协议。
14+
15+
* **优化**启动速度和屏幕截图预览的速度
16+
17+
* **删除**执行脚本的功能,因为该功能还不够稳定
1518
* **删除**上传文件的功能,因为该功能还不够稳定
19+
* **删除**localserver|remoteserver,它们的区别还不够明确,因为localserver同样支持外网访问。目前只保留了localserver,也是默认参数入口。
1620

1721
### 代码
1822

1923
* 重新设计了网络传输协议(仍然保留了旧协议的代码在代码中)
24+
* 废除gradle构建,改用intellij idea进行编译和调试
25+
* 微调了项目结构,由于保留了两版本的协议代码,代码结构依然冗余。在以后会做调整。
2026

2127
## 0.1.0
2228

23-
DEMO版本制作完成
29+
DEMO版本制作完成

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

Lines changed: 1 addition & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,10 @@
2626

2727
package com.yeetor;
2828

29-
import com.neovisionaries.ws.client.WebSocketException;
3029
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;
3430
import com.yeetor.server.AndroidControlServer;
31+
import com.yeetor.util.JarTool;
3532
import org.apache.log4j.Logger;
36-
import java.security.InvalidParameterException;
37-
3833

3934
/**
4035
* Created by harry on 2017/4/15.
@@ -61,74 +56,5 @@ public static void main(String[] args) throws Exception {
6156

6257
AndroidControlServer server = new AndroidControlServer();
6358
server.listen(6655);
64-
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);
86-
}
87-
}
88-
89-
static class Config {
90-
boolean isLocal = true;
91-
boolean isClient = false;
92-
String ip = "";
93-
int port = 6655;
94-
String serialNumber = "";
95-
String key = "";
96-
97-
Config(String[] args) throws InvalidParameterException {
98-
if (args.length > 0) {
99-
if ("localserver".equals(args[0])) {
100-
isLocal = true;
101-
isClient = false;
102-
} else if ("remoteserver".equals(args[0])) {
103-
isLocal = false;
104-
isClient = false;
105-
} else {
106-
isLocal = true;
107-
isClient = true;
108-
}
109-
}
110-
111-
if (isClient) {
112-
if (args.length == 4) {
113-
ip = args[1];
114-
port = Integer.parseInt(args[2]);
115-
key = args[3];
116-
} else if (args.length == 5) {
117-
ip = args[1];
118-
port = Integer.parseInt(args[2]);
119-
key = args[3];
120-
serialNumber = args[4];
121-
} else {
122-
throw new InvalidParameterException("参数不正确");
123-
}
124-
} else {
125-
if (args.length == 2) {
126-
port = Integer.parseInt(args[1]);
127-
} else {
128-
throw new InvalidParameterException("参数不正确");
129-
}
130-
}
131-
}
13259
}
133-
13460
}

src/main/java/com/yeetor/server/HttpServer.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ public HttpServer() {
5959

6060
public void onRequest(ChannelHandlerContext ctx, HttpRequest request, HttpResponse response) {
6161
String uri = request.uri();
62+
if (uri.indexOf("?") != -1) {
63+
uri = uri.substring(0, uri.indexOf("?"));
64+
}
65+
6266
logger.info("http:" + uri);
6367

6468
// 找到符合注解的方法
@@ -86,6 +90,9 @@ public void onRequest(ChannelHandlerContext ctx, HttpRequest request, HttpRespon
8690

8791
public void doFileRequest(ChannelHandlerContext ctx, HttpRequest request, HttpResponse response) {
8892
String location = request.uri().substring(1);
93+
if (location.indexOf("?") != -1) {
94+
location = location.substring(0, location.indexOf("?"));
95+
}
8996
if (location.equals("")) {
9097
location = INDEX_FILE;
9198
}

src/main/java/com/yeetor/util/Constant.java

Lines changed: 48 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,27 +40,64 @@ public class Constant {
4040

4141
public static final String PROP_ABI = "ro.product.cpu.abi";
4242
public static final String PROP_SDK = "ro.build.version.sdk";
43-
44-
public static File getResourceDir() {
43+
public static final String PROPERTIES_FILE = "yeetor.properties";
44+
45+
private static Properties properties = null;
46+
47+
/**
48+
* 这里初始化配置文件......yeetor.properties
49+
*
50+
* 优先级:同级目录 > 包内部默认文件
51+
*/
52+
static {
53+
properties = new Properties();
54+
if (!loadPropertiesWithFileName(JarTool.getJarDir() + File.separator + PROPERTIES_FILE)) {
55+
InputStream inputStream = ClassLoader.getSystemResourceAsStream(PROPERTIES_FILE);
56+
if (!loadPropertiesWithStream(inputStream)) {
57+
try {
58+
inputStream.close();
59+
} catch (IOException e) {
60+
e.printStackTrace();
61+
}
62+
}
63+
}
64+
}
4565

46-
Properties pro = new Properties();
47-
InputStream stream = null;
66+
static boolean loadPropertiesWithFileName(String fileName) {
67+
File file = new File(fileName);
68+
if (file.isHidden() || !file.exists()) {
69+
return false;
70+
}
71+
FileInputStream inputStream = null;
4872
try {
49-
stream = ClassLoader.getSystemResourceAsStream("yeetor.properties");
50-
pro.load(stream);
51-
} catch (IOException e) {
52-
73+
inputStream = new FileInputStream(file);
74+
loadPropertiesWithStream(inputStream);
75+
return true;
76+
} catch (FileNotFoundException e) {
5377
e.printStackTrace();
5478
} finally {
55-
if (stream != null) {
79+
if (inputStream != null) {
5680
try {
57-
stream.close();
81+
inputStream.close();
5882
} catch (IOException e) {
5983
e.printStackTrace();
6084
}
6185
}
6286
}
63-
File resources = new File(pro.getProperty("resource.root"));
87+
return false;
88+
}
89+
90+
static boolean loadPropertiesWithStream(InputStream inputStream) {
91+
try {
92+
properties.load(inputStream);
93+
} catch (IOException e) {
94+
return false;
95+
}
96+
return true;
97+
}
98+
99+
public static File getResourceDir() {
100+
File resources = new File(properties.getProperty("resource.root"));
64101
return resources;
65102
}
66103

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
*
3+
* MIT License
4+
*
5+
* Copyright (c) 2017 朱辉 https://blog.yeetor.com
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in all
15+
* copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23+
* SOFTWARE.
24+
*
25+
*/
26+
27+
package com.yeetor.util;
28+
29+
import java.io.File;
30+
31+
/**
32+
* Created by harry on 2017/8/8.
33+
*/
34+
public class JarTool {
35+
36+
/**
37+
* 获取jar绝对路径
38+
*
39+
* @return
40+
*/
41+
public static String getJarPath()
42+
{
43+
File file = getFile();
44+
if (file == null)
45+
return null;
46+
return file.getAbsolutePath();
47+
}
48+
49+
/**
50+
* 获取jar目录
51+
*
52+
* @return
53+
*/
54+
public static String getJarDir()
55+
{
56+
File file = getFile();
57+
if (file == null)
58+
return null;
59+
return getFile().getParent();
60+
}
61+
62+
/**
63+
* 获取jar包名
64+
*
65+
* @return
66+
*/
67+
public static String getJarName()
68+
{
69+
File file = getFile();
70+
if (file == null)
71+
return null;
72+
return getFile().getName();
73+
}
74+
75+
/**
76+
* 获取当前Jar文件
77+
*
78+
* @return
79+
*/
80+
private static File getFile()
81+
{
82+
String path = JarTool.class.getProtectionDomain().getCodeSource().getLocation().getFile();
83+
try
84+
{
85+
path = java.net.URLDecoder.decode(path, "UTF-8"); // 转换处理中文及空格
86+
}
87+
catch (java.io.UnsupportedEncodingException e)
88+
{
89+
return null;
90+
}
91+
return new File(path);
92+
}
93+
}

0 commit comments

Comments
 (0)