Repository navigation
Expand file tree
/
Copy pathConsole.java
More file actions
197 lines (163 loc) · 6.29 KB
/
Copy pathConsole.java
File metadata and controls
197 lines (163 loc) · 6.29 KB
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
/*
*
* MIT License
*
* 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
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* 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.console;
import com.yeetor.adb.AdbServer;
import com.yeetor.androidcontrol.server.BaseServer;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import org.apache.commons.lang3.RandomUtils;
import java.lang.reflect.Constructor;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
public class Console {
private static final String PROMPT = "> ";
private static final String BANNER = "help 帮助提示\r\nhello 人生格言\r\n";
private static Console instance;
private Map<String, Class> commands;
public static Console getInstance() {
if (instance == null) {
instance = new Console();
instance.commands = new HashMap<>();
instance.commands.put("help", HelpCommand.class);
instance.commands.put("hello", HelloCommand.class);
instance.commands.put("device", DeviceCommand.class);
}
return instance;
}
/**
* Console监听端口
* @param port
*/
public void listenOnTCP(int port) {
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch)
throws Exception {
ch.pipeline().addLast(new Adapter());
}
}).option(ChannelOption.SO_BACKLOG, 128)
.childOption(ChannelOption.SO_KEEPALIVE, true);
ChannelFuture f = b.bind(port);
f.channel().closeFuture().sync();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
workerGroup.shutdownGracefully();
bossGroup.shutdownGracefully();
}
}
class Adapter extends ChannelInboundHandlerAdapter {
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
ByteBuf buf = (ByteBuf) msg;
byte[] res = new byte[buf.readableBytes()];
buf.readBytes(res);
buf.release();
String str = new String(res).trim();
Class cls = commands.get(str);
if (cls != null) {
Constructor constructor = cls.getDeclaredConstructor(String.class);
constructor.setAccessible(true);
Command command = (Command) constructor.newInstance(str);
sendStringL(ctx, command.execute());
} else {
sendString(ctx, "command not found\n");
}
}
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
sendString(ctx, BANNER);
sendPrompt(ctx);
}
@Override
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
sendPrompt(ctx);
}
private void sendStringL(ChannelHandlerContext ctx, String text) {
if (!text.endsWith("\n")) {
text += "\n";
}
sendString(ctx, text);
}
private void sendString(ChannelHandlerContext ctx, String text) {
ByteBuf encoded = ctx.alloc().buffer(4 * text.length());
encoded.writeBytes(text.getBytes());
ctx.write(encoded);
ctx.flush();
}
private void sendPrompt(ChannelHandlerContext ctx) {
sendString(ctx, PROMPT);
}
}
public abstract static class Command {
private String command;
public Command(String command) {
this.command = command;
}
public abstract String execute();
}
public static class HelpCommand extends Command {
HelpCommand(String command) {
super(command);
}
@Override
public String execute() {
return "暂时没有帮助!";
}
}
public static class HelloCommand extends Command {
String[] strings = new String[] {
"Talking is cheap, show me the code!",
"支持作者<http://yeetor.com>"
};
HelloCommand(String command) {
super(command);
}
@Override
public String execute() {
return strings[RandomUtils.nextInt(0, strings.length)];
}
}
public static class DeviceCommand extends Command {
public DeviceCommand(String command) {
super(command);
}
@Override
public String execute() {
return BaseServer.getDevicesJSON();
}
}
}