2019-11-10 02:11:13 +00:00
|
|
|
/// <reference types="@ms/types/dist/typings/bukkit" />
|
|
|
|
/// <reference types="@ms/types/dist/typings/sponge" />
|
2020-01-16 09:27:24 +00:00
|
|
|
/// <reference types="@ms/types/dist/typings/bungee" />
|
|
|
|
|
|
|
|
import { plugin as pluginApi, server, task } from '@ms/api'
|
2019-10-30 12:45:59 +00:00
|
|
|
import { plugin, interfaces, cmd } from '@ms/plugin'
|
2019-11-10 02:11:13 +00:00
|
|
|
import { DefaultContainer as container, inject, postConstruct } from '@ms/container'
|
2019-10-30 12:45:59 +00:00
|
|
|
import * as reflect from '@ms/common/dist/reflect'
|
|
|
|
|
2019-11-10 02:11:13 +00:00
|
|
|
let clients: any[] = []
|
2019-10-30 12:45:59 +00:00
|
|
|
let SPLIT_LINE = '\\M\\W\\S|T|S|S/L/T/'
|
|
|
|
const refList: Array<{ server: string, future: string }> = [
|
2020-01-16 09:27:24 +00:00
|
|
|
{ server: 'an', future: 'g' },//spigot 1.12.2
|
|
|
|
{ server: 'getServerConnection', future: 'f' },//spigot 1.14.4
|
|
|
|
{ server: 'func_147137_ag', future: 'field_151274_e' }//catserver 1.12.2
|
2019-11-10 02:11:13 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
const Callable = Java.type('java.util.concurrent.Callable')
|
2020-01-16 09:27:24 +00:00
|
|
|
const Runnable = Java.type('java.lang.Runnable')
|
2019-09-24 02:12:57 +00:00
|
|
|
|
2020-01-16 09:27:24 +00:00
|
|
|
@plugin({ name: 'MiaoConsole', version: '1.0.0', author: 'MiaoWoo', source: __filename })
|
|
|
|
export class MiaoConsole extends interfaces.Plugin {
|
2019-10-30 12:45:59 +00:00
|
|
|
@inject(pluginApi.PluginManager)
|
2019-11-10 02:11:13 +00:00
|
|
|
private PluginManager: pluginApi.PluginManager
|
2019-10-30 12:45:59 +00:00
|
|
|
@inject(server.ServerType)
|
2019-11-10 02:11:13 +00:00
|
|
|
private ServerType: string
|
2020-01-16 09:27:24 +00:00
|
|
|
@inject(server.Server)
|
|
|
|
private Server: server.Server
|
|
|
|
@inject(task.TaskManager)
|
|
|
|
private Task: task.TaskManager
|
2019-11-10 02:11:13 +00:00
|
|
|
@inject(pluginApi.PluginInstance)
|
|
|
|
private pluginInstance: any
|
2020-01-16 09:27:24 +00:00
|
|
|
|
2019-11-10 02:11:13 +00:00
|
|
|
private pipeline: any
|
2019-09-24 02:12:57 +00:00
|
|
|
|
2019-10-30 12:45:59 +00:00
|
|
|
@cmd()
|
2020-01-16 09:27:24 +00:00
|
|
|
mconsole(sender: any, command: string, args: string[]) {
|
2019-10-30 12:45:59 +00:00
|
|
|
switch (args[0]) {
|
|
|
|
case "reload":
|
2019-11-10 02:11:13 +00:00
|
|
|
this.PluginManager.reload(this)
|
|
|
|
break
|
2019-10-30 12:45:59 +00:00
|
|
|
default:
|
|
|
|
}
|
2019-09-24 02:12:57 +00:00
|
|
|
}
|
2019-10-30 12:45:59 +00:00
|
|
|
|
2019-09-24 02:12:57 +00:00
|
|
|
disable() {
|
2019-10-30 12:45:59 +00:00
|
|
|
if (this.pipeline) {
|
2020-01-16 09:27:24 +00:00
|
|
|
if (this.pipeline.names().contains('miao_detect')) {
|
|
|
|
this.pipeline.remove('miao_detect')
|
|
|
|
}
|
2019-10-30 12:45:59 +00:00
|
|
|
clients.forEach(c => c.close())
|
|
|
|
container.unbind('onmessage')
|
|
|
|
}
|
2019-09-24 02:12:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bukkitenable() {
|
2019-11-10 02:11:13 +00:00
|
|
|
let Bukkit = Java.type('org.bukkit.Bukkit')
|
|
|
|
let consoleServer = reflect.on(Bukkit.getServer()).get('console').get()
|
2020-01-16 09:27:24 +00:00
|
|
|
this.reflectChannel(this.reflectPromise(consoleServer))
|
|
|
|
this.injectMiaoDetect()
|
2019-10-30 12:45:59 +00:00
|
|
|
}
|
2019-09-24 02:12:57 +00:00
|
|
|
|
2019-10-30 12:45:59 +00:00
|
|
|
spongeenable() {
|
2019-11-10 02:11:13 +00:00
|
|
|
let Sponge = Java.type('org.spongepowered.api.Sponge')
|
|
|
|
let consoleServer = reflect.on(Sponge.getServer()).get()
|
2020-01-16 09:27:24 +00:00
|
|
|
this.reflectChannel(this.reflectPromise(consoleServer))
|
|
|
|
this.injectMiaoDetect()
|
|
|
|
}
|
|
|
|
|
|
|
|
bungeeenable() {
|
|
|
|
let wait = this.Task.create(() => {
|
|
|
|
try {
|
|
|
|
// @ts-ignore
|
|
|
|
this.pipeline = reflect.on(base.getInstance().getProxy()).get('listeners').get().toArray()[0].pipeline()
|
|
|
|
this.injectMiaoDetect()
|
|
|
|
wait.cancel();
|
|
|
|
} catch (ex) {
|
|
|
|
this.logger.warn('Wait BungeeCord start ready to get netty channel pipeline. Err: ' + ex)
|
|
|
|
}
|
|
|
|
}).later(300).timer(500).submit()
|
2019-10-30 12:45:59 +00:00
|
|
|
}
|
2019-09-24 02:12:57 +00:00
|
|
|
|
2019-10-30 12:45:59 +00:00
|
|
|
reflectPromise(consoleServer) {
|
|
|
|
for (const ref of refList) {
|
2019-11-10 02:11:13 +00:00
|
|
|
try { return reflect.on(consoleServer).call(ref.server).get(ref.future).get().get(0) } catch (error) { }
|
2019-10-30 12:45:59 +00:00
|
|
|
}
|
|
|
|
}
|
2019-09-24 02:12:57 +00:00
|
|
|
|
2020-01-16 09:27:24 +00:00
|
|
|
reflectChannel(promise) {
|
2019-11-10 02:11:13 +00:00
|
|
|
if (!promise) { throw Error(`Can't found ServerConnection or ChannelFuture !`) }
|
|
|
|
this.pipeline = reflect.on(promise).get('channel').get().pipeline()
|
2020-01-16 09:27:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
injectMiaoDetect() {
|
2019-11-10 02:11:13 +00:00
|
|
|
this.pipeline.addFirst('miao_detect', new MiaoDetectHandler())
|
2019-10-30 12:45:59 +00:00
|
|
|
container.bind('onmessage').toFunction(this.onmessage.bind(this))
|
2020-01-16 09:27:24 +00:00
|
|
|
this.logger.info('Netty Channel Pipeline Inject MiaoDetectHandler Successful!')
|
2019-09-24 02:12:57 +00:00
|
|
|
}
|
|
|
|
|
2019-10-30 12:45:59 +00:00
|
|
|
onmessage(ctx: any, msg: any) {
|
|
|
|
let text: string = msg.text()
|
2020-01-16 09:27:24 +00:00
|
|
|
const [type, content] = text.split(SPLIT_LINE)
|
2019-10-30 12:45:59 +00:00
|
|
|
try {
|
|
|
|
var result = this[type](ctx, content)
|
|
|
|
} catch (ex) {
|
|
|
|
var result = '§4代码执行异常\n' + console.stack(ex).join('\n')
|
|
|
|
}
|
|
|
|
result && this.sendResult(ctx, "log", result)
|
2019-09-24 02:12:57 +00:00
|
|
|
}
|
|
|
|
|
2019-10-30 12:45:59 +00:00
|
|
|
execCommand(ctx: any, cmd: string) {
|
2020-01-16 09:27:24 +00:00
|
|
|
setTimeout(() => this.Server.dispatchConsoleCommand(cmd), 0)
|
2019-10-30 12:45:59 +00:00
|
|
|
return `§6命令: §b${cmd} §a执行成功!`
|
2019-09-24 02:12:57 +00:00
|
|
|
}
|
2019-10-30 12:45:59 +00:00
|
|
|
|
|
|
|
execCode(ctx: any, code: string) {
|
2019-11-10 02:11:13 +00:00
|
|
|
switch (this.ServerType) {
|
|
|
|
case "bukkit":
|
|
|
|
return org.bukkit.Bukkit.getScheduler().callSyncMethod(this.pluginInstance, new Callable({ call: () => eval(code) })).get() || '无返回结果'
|
|
|
|
case "sponge":
|
2020-01-16 09:27:24 +00:00
|
|
|
return org.spongepowered.api.Sponge.getScheduler().createSyncExecutor(this.pluginInstance).schedule(new Runnable({ run: () => eval(code) }), 0, {})
|
|
|
|
case "bungee":
|
|
|
|
return eval(code)
|
2019-11-10 02:11:13 +00:00
|
|
|
}
|
2019-09-24 02:12:57 +00:00
|
|
|
}
|
2019-10-30 12:45:59 +00:00
|
|
|
|
|
|
|
execDetect(ctx: any, cmd: string) {
|
|
|
|
switch (cmd) {
|
|
|
|
case "type":
|
|
|
|
this.sendResult(ctx, "type", this.ServerType)
|
2020-01-16 09:27:24 +00:00
|
|
|
return `Currect Server Version is ${this.Server.getVersion()}`
|
2019-10-30 12:45:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sendResult(ctx: any, type: string, msg: string) {
|
|
|
|
ctx.writeAndFlush(new TextWebSocketFrame(`${type}${SPLIT_LINE}${msg}`))
|
2019-09-24 02:12:57 +00:00
|
|
|
}
|
|
|
|
}
|
2019-10-30 12:45:59 +00:00
|
|
|
|
2019-11-10 02:11:13 +00:00
|
|
|
const ChannelInboundHandlerAdapter = Java.type('io.netty.channel.ChannelInboundHandlerAdapter')
|
2019-10-30 12:45:59 +00:00
|
|
|
const CharsetUtil = Java.type('io.netty.util.CharsetUtil')
|
|
|
|
const TextWebSocketFrame = Java.type('io.netty.handler.codec.http.websocketx.TextWebSocketFrame')
|
|
|
|
const MiaoDetectHandler = Java.extend(ChannelInboundHandlerAdapter, {
|
2019-11-10 02:11:13 +00:00
|
|
|
channelRead: (ctx: any, channel: any) => {
|
2019-10-30 12:45:59 +00:00
|
|
|
channel.pipeline().addFirst('miaowebsocket', new WebSocketHandler())
|
2019-11-10 02:11:13 +00:00
|
|
|
ctx.fireChannelRead(channel)
|
2019-10-30 12:45:59 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
const TypeParameterMatcher = Java.type('io.netty.util.internal.TypeParameterMatcher')
|
2019-11-10 02:11:13 +00:00
|
|
|
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')
|
2019-10-30 12:45:59 +00:00
|
|
|
const FullHttpRequestMatcher = TypeParameterMatcher.get(base.getClass('io.netty.handler.codec.http.FullHttpRequest'))
|
2019-11-10 02:11:13 +00:00
|
|
|
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')
|
2019-10-30 12:45:59 +00:00
|
|
|
const HttpRequestHandler = Java.extend(SimpleChannelInboundHandler, {
|
|
|
|
acceptInboundMessage: (msg: any) => {
|
2019-11-10 02:11:13 +00:00
|
|
|
return FullHttpRequestMatcher.match(msg)
|
2019-10-30 12:45:59 +00:00
|
|
|
},
|
|
|
|
channelRead0: (ctx: any, request: any) => {
|
|
|
|
if ('/ws' == request.getUri()) {
|
|
|
|
ctx.fireChannelRead(request.retain())
|
|
|
|
} else {
|
|
|
|
if (HttpHeaders.is100ContinueExpected(request)) {
|
2019-11-10 02:11:13 +00:00
|
|
|
ctx.writeAndFlush(new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.CONTINUE))
|
2019-10-30 12:45:59 +00:00
|
|
|
}
|
2019-11-10 02:11:13 +00:00
|
|
|
let filename = request.getUri().split('?')[0].substr(1)
|
|
|
|
let file = new File('/home/project/TSWorkSpace/ms/packages/plugins/public', filename || 'index.html')
|
|
|
|
if (!file.exists() || !file.isFile()) {
|
|
|
|
ctx.write(new DefaultHttpResponse(request.getProtocolVersion(), HttpResponseStatus.NOT_FOUND))
|
|
|
|
ctx.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT).addListener(ChannelFutureListener.CLOSE)
|
|
|
|
return
|
2019-10-30 12:45:59 +00:00
|
|
|
}
|
|
|
|
let response = new DefaultHttpResponse(request.getProtocolVersion(), HttpResponseStatus.OK)
|
2019-11-10 02:11:13 +00:00
|
|
|
response.headers().set(HttpHeaders.Names.CONTENT_TYPE, "text/html charset=UTF-8")
|
|
|
|
let raf = new RandomAccessFile(file, 'r')
|
|
|
|
let keepAlive = HttpHeaders.isKeepAlive(request)
|
2019-10-30 12:45:59 +00:00
|
|
|
if (keepAlive) {
|
2019-11-10 02:11:13 +00:00
|
|
|
response.headers().set(HttpHeaders.Names.CONTENT_LENGTH, file.length())
|
|
|
|
response.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.KEEP_ALIVE)
|
2019-10-30 12:45:59 +00:00
|
|
|
}
|
2019-11-10 02:11:13 +00:00
|
|
|
ctx.write(response)
|
|
|
|
ctx.write(new DefaultFileRegion(raf.getChannel(), 0, raf.length()))
|
|
|
|
let future = ctx.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT)
|
2019-10-30 12:45:59 +00:00
|
|
|
if (!keepAlive) {
|
2019-11-10 02:11:13 +00:00
|
|
|
future.addListener(ChannelFutureListener.CLOSE)
|
2019-10-30 12:45:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
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') {
|
2019-11-10 02:11:13 +00:00
|
|
|
clients.push(ctx.channel())
|
2019-10-30 12:45:59 +00:00
|
|
|
console.console(`§6[§cMS§6][§bWebSocket§6]§r new client §b${ctx.channel().id()} §aconnected...`)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
acceptInboundMessage: (msg: any) => {
|
2019-11-10 02:11:13 +00:00
|
|
|
return TextWebSocketFrameMatcher.match(msg)
|
2019-10-30 12:45:59 +00:00
|
|
|
},
|
|
|
|
channelRead0: (ctx: any, msg: any) => {
|
2019-11-10 02:11:13 +00:00
|
|
|
container.get<any>('onmessage')(ctx, msg)
|
2019-10-30 12:45:59 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
const WebSocketHandler = Java.extend(ChannelInboundHandlerAdapter, {
|
|
|
|
channelRead: function(ctx: any, msg: any) {
|
2019-11-10 02:11:13 +00:00
|
|
|
msg.markReaderIndex()
|
|
|
|
let message: string = msg.toString(CharsetUtil.UTF_8)
|
|
|
|
let channel = ctx.channel()
|
|
|
|
let pipeline = channel.pipeline()
|
2019-10-30 12:45:59 +00:00
|
|
|
if (message.indexOf('HTTP/1.1') > 0) {
|
2020-01-16 09:27:24 +00:00
|
|
|
pipeline.names().forEach(f => {
|
2019-11-10 02:11:13 +00:00
|
|
|
if (f == 'miaowebsocket' || f.indexOf('DefaultChannelPipeline') > -1) { return }
|
|
|
|
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())
|
2019-10-30 12:45:59 +00:00
|
|
|
}
|
2019-11-10 02:11:13 +00:00
|
|
|
pipeline.remove('miaowebsocket')
|
|
|
|
msg.resetReaderIndex()
|
|
|
|
ctx.fireChannelRead(msg)
|
2019-10-30 12:45:59 +00:00
|
|
|
}
|
|
|
|
})
|