feat: complate websocket plugin

This commit is contained in:
MiaoWoo 2019-10-30 20:45:59 +08:00
parent 48f7f0fdbb
commit eea2a225e8
7 changed files with 433 additions and 91 deletions

View File

@ -0,0 +1,72 @@
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<!-- UC应用模式 -->
<meta name="browsermode" content="application">
<!-- QQ应用模式 -->
<meta name="x5-page-mode" content="app">
<title>MiaoConsole 调试工具</title>
<script src="https://cdn.jsdelivr.net/npm/jquery@3.3.1/dist/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/avalon2@2.2.10/dist/avalon.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@3.4.1/dist/js/bootstrap.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/js-yaml@3.13.0/dist/js-yaml.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/messenger-hubspot@1.5.0/build/js/messenger.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/messenger-hubspot@1.5.0/build/js/messenger-theme-future.js"></script>
<script src="https://cdn.jsdelivr.net/npm/xterm@3.12.2/dist/xterm.js"></script>
<script src="https://cdn.jsdelivr.net/npm/xterm@3.12.2/dist/addons/fit/fit.js"></script>
<script src="https://cdn.jsdelivr.net/npm/xterm@3.12.2/dist/addons/attach/attach.js"></script>
<script src="https://cdn.jsdelivr.net/npm/xterm@3.12.2/dist/addons/fullscreen/fullscreen.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/xterm@3.12.2/dist/xterm.css" />
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/xterm@3.12.2/dist/addons/fullscreen/fullscreen.css">
<script src="https://cdn.jsdelivr.net/npm/monaco-editor@0.18.1/min/vs/loader.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/monaco-editor@0.18.1/min/vs/editor/editor.main.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.4.1/dist/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/github-markdown-css@3.0.1/github-markdown.min.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/messenger-hubspot@1.5.0/build/css/messenger.min.css">
<link rel="stylesheet"
href="https://cdn.jsdelivr.net/npm/messenger-hubspot@1.5.0/build/css/messenger-theme-future.min.css">
<script src="https://cdn.jsdelivr.net/npm/axios@0.19.0/dist/axios.min.js"></script>
<!-- <script src="https://raw.githubusercontent.com/imaya/zlib.js/develop/bin/gzip.min.js"></script>
<script src="https://raw.githubusercontent.com/imaya/zlib.js/develop/bin/gunzip.min.js"></script> -->
<script src="js/message.js"></script>
<script src="js/term.js"></script>
<script src="js/main.js"></script>
<script src="js/editor.js"></script>
</head>
<body noscroll="true">
<div class="container-fluid bs-docs-container" :controller="main">
<h3>欢迎使用 MiaoConsole 调试控制台</h3>
<div class="input-group">
<span class="input-group-addon">服务器地址</span>
<input id="address" class="form-control" :duplex="@server" :keydown="@connect" style="width: 100%"></input>
<a class="input-group-addon btn" :click="@connect">连接</a>
</div>
<div class="panel panel-success" style="margin-top: 10px; height: 332px;">
<div class="panel-heading">服务器日志 当前服务器类型: {{@type}}</div>
<div id="terminal" style="height: 300px; width:100%;"></div>
</div>
<div class="progress progress-striped active" :visible='@classes.total != @classes.loaded'>
<div class="progress-bar progress-bar-primary" role="progressbar" aria-valuenow="60" aria-valuemin="0"
aria-valuemax="100" :css="{width: (@classes.loaded/@classes.total*100) +'%', 'min-width': '30em'}">
<span>正在加载 {{@type}} 类型定义 {{@classes.loaded}}/{{@classes.total}}({{~~(@classes.loaded/@classes.total*100)|percent}}%) </span>
</div>
</div>
<div class="panel panel-info" :visible='@classes.total == @classes.loaded'>
<div class="panel-heading">代码编辑器</div>
<div id="editor" style="height: 450px; width:100%;"></div>
</div>
</div>
</body>
</html>

View File

@ -0,0 +1,54 @@
let editor
let codeStorageKey = "MiaoScript:code";
let monaco_path = 'https://cdn.jsdelivr.net/npm/monaco-editor@0.18.1/min'
require.config({ paths: { 'vs': monaco_path + '/vs' } });
window.MonacoEnvironment = { getWorkerUrl: () => proxy };
let proxy = URL.createObjectURL(new Blob([`
self.MonacoEnvironment = {
baseUrl: '${monaco_path}/'
};
importScripts('${monaco_path}/vs/base/worker/workerMain.js');
`], { type: 'text/javascript' }));
require(["vs/editor/editor.main"], function() {
if (main.type !== 'unknow') {
let ts_d_src = `https://cdn.jsdelivr.net/gh/circlecloud/ms@master/packages/${main.type}/src/typings`
$.get(`${ts_d_src}/index.ts`, (res) => {
monaco.languages.typescript.javascriptDefaults.addExtraLib(res, 'file:///src/typings/index.ts')
let classes = res.split('\n').map(line => line.match(/.*\.\/(.*)".*/)).filter(line => line).map(dts => dts[1])
main.classes.total = classes.length
main.classes.loaded = 0
classes.forEach(fname => {
$.get(`${ts_d_src}/${fname}`, content => {
monaco.languages.typescript.javascriptDefaults.addExtraLib(content, `file:///src/typings/${fname}`)
main.classes.loaded++
})
})
})
}
editor = monaco.editor.create(document.getElementById('editor'), {
value: window.localStorage.getItem(codeStorageKey) || 'org.bukkit.Bukkit.server.version',
language: 'javascript',
automaticLayout: true,
scrollBeyondLastLine: false,
theme: 'vs-dark'
});
editor.addCommand(monaco.KeyMod.CtrlCmd | monaco.KeyCode.KEY_S, function() {
window.localStorage.setItem(codeStorageKey, editor.getValue())
showMessenger('代码保存成功!')
})
editor.addCommand(monaco.KeyMod.CtrlCmd | monaco.KeyCode.KEY_R, function() {
main.send('execCode', getSelectContent(editor) || editor.getValue())
})
editor.addCommand(monaco.KeyMod.CtrlCmd | monaco.KeyCode.KEY_E, function() {
main.send('execCommand', getSelectContent(editor))
})
editor.addCommand(monaco.KeyMod.CtrlCmd | monaco.KeyCode.KEY_Q, function() {
console.log('switch')
})
});
function getSelectContent(editor) {
let selInfo = editor.getSelection();
return editor.getModel().getLineContent(selInfo.startLineNumber).substr(selInfo.startColumn - 1, selInfo.endColumn - selInfo.startColumn);
}

View File

@ -0,0 +1,62 @@
var ws
var SPLIT_LINE = '\\M\\W\\S|T|S|S/L/T/'
let serverKey = 'MiaoScript:server'
var main = avalon.define({
$id: 'main',
server: window.localStorage.getItem(serverKey) || location.host,
type: 'unknow',
logs: '',
classes: {
total: 1,
loaded: 0,
},
precent: () => {
return classes.total
},
log: (info) => {
info.split("\n").forEach((line) => { term.writeln(mcColor2ANSI(line + '§r')) })
},
send: (type, content) => {
if (!ws || ws.readyState != 1) { term.writeln('Please Connect to Server first!'); return; }
ws.send(`${type}${SPLIT_LINE}${content}`);
},
connect: (event = { key: 'Enter' }) => {
if (event.key !== "Enter") {
return;
}
if (ws && ws.readyState == 1) {
ws.close()
}
window.localStorage.setItem(serverKey, main.server)
ws = new WebSocket(`${location.protocol == 'http:' ? 'ws' : 'wss'}://${main.server}/ws`)
ws.onmessage = (event) => {
const [type, obj] = event.data.split(SPLIT_LINE)
switch (type) {
case "log":
main.log(obj)
break;
case "type":
main.type = obj;
break;
}
}
ws.onopen = () => {
main.send("execDetect", "type");
}
ws.onclose = (ev) => {
main.log(`Remote Server Close Connection... ${ev.code}`)
if (ev.code == 1006) {
setTimeout(() => {
main.connect()
}, 1000)
}
}
},
init: () => {
if (main.server) {
main.connect()
}
}
});
main.init()

View File

@ -0,0 +1,10 @@
$._messengerDefaults = {
extraClasses: 'messenger-fixed messenger-theme-future messenger-on-top messenger-on-right'
};
var showMessenger = function(message, type) {
return Messenger().post({
message: message,
type: type || 'info',
showClo6seButton: true
});
};

View File

@ -0,0 +1,54 @@
Terminal.applyAddon(fit);
var term = new Terminal();
avalon.ready(() => {
term.open(document.getElementById('terminal'));
window.onresize = () => {
term.fit();
}
window.onresize()
})
term.on('data', (data) => {
if (data == '\r') {
term.writeln(data)
}
});
term.attachCustomKeyEventHandler(e => {
if (e.ctrlKey && e.key == 'c' && term.hasSelection()) {
showMessenger('内容已复制到粘贴板')
return false;
}
if (e.ctrlKey && e.key == 'v') {
return false;
}
return true;
})
var colorMap = []
colorMap['0'] = '38;5;0'
colorMap['1'] = '38;5;4'
colorMap['2'] = '38;5;2'
colorMap['3'] = '38;5;6'
colorMap['4'] = '38;5;1'
colorMap['5'] = '38;5;5'
colorMap['6'] = '38;5;3'
colorMap['7'] = '38;5;7'
colorMap['8'] = '38;5;8'
colorMap['9'] = '38;5;12'
colorMap['a'] = '38;5;10'
colorMap['b'] = '38;5;14'
colorMap['c'] = '38;5;9'
colorMap['d'] = '38;5;13'
colorMap['e'] = '38;5;11'
colorMap['f'] = '38;5;15'
colorMap['r'] = '0'
colorMap['l'] = '1'
colorMap['n'] = '4'
var regexMap = []
for (const c in colorMap) {
regexMap[colorMap[c]] = new RegExp(`§${c}`, "g")
}
function mcColor2ANSI(str) {
for (const regex in regexMap) {
str = str.replace(regexMap[regex], `\u001b[${regex}m`)
}
return str;
}

View File

View File

@ -1,107 +1,197 @@
import { plugin, interfaces } from '@ms/plugin'
import { DefaultContainer as container } from '@ms/container';
import * as ref from '@ms/common/dist/reflect'
import { plugin as pluginApi, server } from '@ms/api'
import { plugin, interfaces, cmd } from '@ms/plugin'
import { DefaultContainer as container, inject, postConstruct } from '@ms/container';
import * as reflect from '@ms/common/dist/reflect'
let clients: any[] = [];
let SPLIT_LINE = '\\M\\W\\S|T|S|S/L/T/'
const refList: Array<{ server: string, future: string }> = [
{ server: 'an', future: 'g' },
{ server: 'getServerConnection', future: 'f' },
{ server: 'func_147137_ag', future: 'field_151274_e' }
];
@plugin({ name: 'WebSocket', version: '1.0.0', author: 'MiaoWoo', source: __filename })
export class WebSocket extends interfaces.Plugin {
private channel: any;
private childHandler: any;
@inject(pluginApi.PluginManager)
private PluginManager: pluginApi.PluginManager;
@inject(server.ServerType)
private ServerType: string;
private pipeline: any;
load() {
this.logger.log('Test Plugin load from MiaoScript Plugin System...');
}
enable() {
this.logger.log('Test Plugin enable from MiaoScript Plugin System...');
@cmd()
ws(sender: any, command: string, args: string[]) {
switch (args[0]) {
case "reload":
this.PluginManager.reload(this);
break;
default:
}
}
disable() {
this.logger.log('Test Plugin disable from MiaoScript Plugin System...');
if (this.pipeline) {
this.pipeline.remove('miao_detect');
clients.forEach(c => c.close())
container.unbind('onmessage')
}
}
bukkitload() {
this.logger.log('Load When ServerType is Bukkit!')
}
bukkitenable() {
let Bukkit = Java.type('org.bukkit.Bukkit');
let promise = ref.on(Bukkit.getServer()).get('console').get('serverConnection').get('f').get().get(0);
this.channel = ref.on(promise).get('channel').get().pipeline().first();
this.childHandler = ref.on(this.channel).get('childHandler').get();
let ChannelHandler = Java.extend(Java.type('io.netty.channel.ChannelInitializer'), {
initChannel: function(channel: any) {
container.get<any>('handle')(channel);
}
})
//=======================
let ChannelInboundHandlerAdapter = Java.type('io.netty.channel.ChannelInboundHandlerAdapter');
let CharsetUtil = Java.type('io.netty.util.CharsetUtil')
let MiaoDetectHandler = Java.extend(ChannelInboundHandlerAdapter, {
channelRead: function(ctx: any, msg: any) {
msg.markReaderIndex();
console.log(msg.readChar());
msg.resetReaderIndex();
let message: string = msg.toString(CharsetUtil.UTF_8);
console.log(message);
let channel = ctx.channel();
let pipeline = channel.pipeline();
if (message.indexOf('HTTP/1.1') > 0) {
'timeout legacy_query splitter decoder prepender encoder packet_handler'.split(' ').forEach(f => channel.pipeline().remove(f))
let HttpServerCodec = Java.type('io.netty.handler.codec.http.HttpServerCodec');
let ChunkedWriteHandler = Java.type('io.netty.handler.stream.ChunkedWriteHandler');
let HttpObjectAggregator = Java.type('io.netty.handler.codec.http.HttpObjectAggregator');
let WebSocketServerProtocolHandler = Java.type('io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler');
let SimpleChannelInboundHandler = Java.type('io.netty.channel.SimpleChannelInboundHandler');
let TextWebSocketFrame = Java.type('io.netty.handler.codec.http.websocketx.TextWebSocketFrame');
let textWsHandler;
let TextWebSocketFrameHandler = Java.extend(SimpleChannelInboundHandler, {
userEventTriggered: (ctx: any, msg: any) => {
ctx.writeAndFlush(new TextWebSocketFrame(`Client ${ctx.channel} connect successful...`))
//@ts-ignore
Java.super(textWsHandler).userEventTriggered(ctx, msg);
},
channelRead0: (ctx: any, msg: any) => {
console.log(msg);
console.log(typeof msg);
console.log(msg.class);
ctx.writeAndFlush(msg.retain())
}
})
textWsHandler = new TextWebSocketFrameHandler()
pipeline.addLast('http', new HttpServerCodec());
pipeline.addLast('chunk', new ChunkedWriteHandler());
pipeline.addLast('httpobj', new HttpObjectAggregator(64 * 1024));
pipeline.addLast('websocket', new WebSocketServerProtocolHandler("/ws"));
pipeline.addLast('websocket_handler', textWsHandler);
}
pipeline.remove('miaowebsocket');
console.log(`Connect Complate. channel: ${channel}, pipeline: ${Java.from(channel.pipeline().names()).join(' ')}`)
msg.resetReaderIndex();
ctx.fireChannelRead(msg);
}
})
ref.on(this.channel).set('childHandler', new ChannelHandler());
container.bind('handle').toFunction(channel => {
ref.on(this.childHandler).call('initChannel', channel);
console.log(`channel: ${channel}, pipeline: ${Java.from(channel.pipeline().names()).join(' ')}`)
let pipeline = channel.pipeline();
pipeline.addFirst('miaowebsocket', new MiaoDetectHandler());
console.log(`channel: ${channel}, pipeline: ${Java.from(channel.pipeline().names()).join(' ')}`)
});
let consoleServer = reflect.on(Bukkit.getServer()).get('console').get();
this.injectMiaoDetect(this.reflectPromise(consoleServer))
}
bukkitdisable() {
ref.on(this.channel).set('childHandler', this.childHandler);
container.unbind('handle')
}
spongeload() {
this.logger.log('Load When ServerType is Sponge!')
}
spongeenable() {
this.logger.log('Enable When ServerType is Sponge!')
let Sponge = Java.type('org.spongepowered.api.Sponge');
let consoleServer = reflect.on(Sponge.getServer()).get();
this.injectMiaoDetect(this.reflectPromise(consoleServer))
}
spongedisable() {
this.logger.log('Disable When ServerType is Sponge!')
reflectPromise(consoleServer) {
for (const ref of refList) {
try { return reflect.on(consoleServer).call(ref.server).get(ref.future).get().get(0); } catch (error) { }
}
}
injectMiaoDetect(promise) {
if (!promise) { throw Error(`Can't found ServerConnection or ChannelFuture !`) };
this.pipeline = reflect.on(promise).get('channel').get().pipeline();
this.pipeline.addFirst('miao_detect', new MiaoDetectHandler());
container.bind('onmessage').toFunction(this.onmessage.bind(this))
}
onmessage(ctx: any, msg: any) {
let text: string = msg.text()
const [type, content] = text.split('\\M\\W\\S|T|S|S/L/T/')
try {
var result = this[type](ctx, content)
} catch (ex) {
var result = '§4代码执行异常\n' + console.stack(ex).join('\n')
}
result && this.sendResult(ctx, "log", result)
}
execCommand(ctx: any, cmd: string) {
org.bukkit.Bukkit.dispatchCommand(org.bukkit.Bukkit.getConsoleSender(), cmd)
return `§6命令: §b${cmd} §a执行成功!`
}
execCode(ctx: any, code: string) {
return eval(code) || '无返回结果'
}
execDetect(ctx: any, cmd: string) {
switch (cmd) {
case "type":
let version = this.ServerType == 'bukkit' ? org.bukkit.Bukkit.getServer().getVersion() : org.spongepowered.api.Sponge.getPlatform().getMinecraftVersion();
this.sendResult(ctx, "type", this.ServerType)
return `Currect Server Version is ${version}`;
}
}
sendResult(ctx: any, type: string, msg: string) {
ctx.writeAndFlush(new TextWebSocketFrame(`${type}${SPLIT_LINE}${msg}`))
}
}
const ChannelInboundHandlerAdapter = Java.type('io.netty.channel.ChannelInboundHandlerAdapter');
const CharsetUtil = Java.type('io.netty.util.CharsetUtil')
const TextWebSocketFrame = Java.type('io.netty.handler.codec.http.websocketx.TextWebSocketFrame')
const MiaoDetectHandler = Java.extend(ChannelInboundHandlerAdapter, {
channelRead: function(ctx: any, channel: any) {
channel.pipeline().addFirst('miaowebsocket', new WebSocketHandler())
ctx.fireChannelRead(channel);
}
})
const TypeParameterMatcher = Java.type('io.netty.util.internal.TypeParameterMatcher')
const DefaultHttpResponse = Java.type('io.netty.handler.codec.http.DefaultHttpResponse');
const DefaultFullHttpResponse = Java.type('io.netty.handler.codec.http.DefaultFullHttpResponse');
const HttpHeaders = Java.type('io.netty.handler.codec.http.HttpHeaders');
const HttpVersion = Java.type('io.netty.handler.codec.http.HttpVersion');
const HttpResponseStatus = Java.type('io.netty.handler.codec.http.HttpResponseStatus');
const LastHttpContent = Java.type('io.netty.handler.codec.http.LastHttpContent');
const HttpServerCodec = Java.type('io.netty.handler.codec.http.HttpServerCodec');
const ChunkedWriteHandler = Java.type('io.netty.handler.stream.ChunkedWriteHandler');
const HttpObjectAggregator = Java.type('io.netty.handler.codec.http.HttpObjectAggregator');
const WebSocketServerProtocolHandler = Java.type('io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler');
const SimpleChannelInboundHandler = Java.type('io.netty.channel.SimpleChannelInboundHandler');
const FullHttpRequestMatcher = TypeParameterMatcher.get(base.getClass('io.netty.handler.codec.http.FullHttpRequest'))
const File = Java.type('java.io.File');
const RandomAccessFile = Java.type('java.io.RandomAccessFile');
const DefaultFileRegion = Java.type('io.netty.channel.DefaultFileRegion');
const ChannelFutureListener = Java.type('io.netty.channel.ChannelFutureListener');
const HttpRequestHandler = Java.extend(SimpleChannelInboundHandler, {
acceptInboundMessage: (msg: any) => {
return FullHttpRequestMatcher.match(msg);
},
channelRead0: (ctx: any, request: any) => {
if ('/ws' == request.getUri()) {
ctx.fireChannelRead(request.retain())
} else {
if (HttpHeaders.is100ContinueExpected(request)) {
ctx.writeAndFlush(new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.CONTINUE));
}
let file = new File('/home/project/TSWorkSpace/ms/packages/plugins/public', request.getUri().split('?')[0])
if (!file.exists()) {
ctx.write(new DefaultHttpResponse(request.getProtocolVersion(), HttpResponseStatus.NOT_FOUND));
ctx.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT).addListener(ChannelFutureListener.CLOSE);
return;
}
let response = new DefaultHttpResponse(request.getProtocolVersion(), HttpResponseStatus.OK)
response.headers().set(HttpHeaders.Names.CONTENT_TYPE, "text/html; charset=UTF-8");
let raf = new RandomAccessFile(file, 'r');
let keepAlive = HttpHeaders.isKeepAlive(request);
if (keepAlive) {
response.headers().set(HttpHeaders.Names.CONTENT_LENGTH, file.length());
response.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.KEEP_ALIVE);
}
ctx.write(response);
ctx.write(new DefaultFileRegion(raf.getChannel(), 0, raf.length()));
let future = ctx.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT);
if (!keepAlive) {
future.addListener(ChannelFutureListener.CLOSE);
}
}
}
})
const TextWebSocketFrameMatcher = TypeParameterMatcher.get(base.getClass('io.netty.handler.codec.http.websocketx.TextWebSocketFrame'))
const TextWebSocketFrameHandler = Java.extend(SimpleChannelInboundHandler, {
userEventTriggered: (ctx: any, evt: any) => {
if (evt == 'HANDSHAKE_COMPLETE') {
clients.push(ctx.channel());
console.console(`§6[§cMS§6][§bWebSocket§6]§r new client §b${ctx.channel().id()} §aconnected...`)
}
},
acceptInboundMessage: (msg: any) => {
return TextWebSocketFrameMatcher.match(msg);
},
channelRead0: (ctx: any, msg: any) => {
container.get<any>('onmessage')(ctx, msg);
}
})
const WebSocketHandler = Java.extend(ChannelInboundHandlerAdapter, {
channelRead: function(ctx: any, msg: any) {
msg.markReaderIndex();
let message: string = msg.toString(CharsetUtil.UTF_8);
let channel = ctx.channel();
let pipeline = channel.pipeline();
if (message.indexOf('HTTP/1.1') > 0) {
try {
'protocol_lib_finish protocol_lib_decoder protocol_lib_encoder'.split(' ').forEach(f => channel.pipeline().remove(f))
} catch (error) {
}
'timeout legacy_query splitter decoder prepender encoder packet_handler'.split(' ').forEach(f => channel.pipeline().remove(f))
pipeline.addLast('http', new HttpServerCodec());
pipeline.addLast('chunk', new ChunkedWriteHandler());
pipeline.addLast('httpobj', new HttpObjectAggregator(64 * 1024));
pipeline.addLast('http_request', new HttpRequestHandler());
pipeline.addLast('websocket', new WebSocketServerProtocolHandler("/ws"));
pipeline.addLast('websocket_handler', new TextWebSocketFrameHandler());
}
pipeline.remove('miaowebsocket');
msg.resetReaderIndex();
ctx.fireChannelRead(msg);
}
})