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'
|
2020-03-30 10:49:19 +00:00
|
|
|
import { inject, Container } from '@ms/container'
|
2019-10-30 12:45:59 +00:00
|
|
|
import * as reflect from '@ms/common/dist/reflect'
|
2020-03-30 10:49:19 +00:00
|
|
|
import { Server as SocketIOServer, Socket as SocketIOSocket } from '@ms/websocket'
|
2019-10-30 12:45:59 +00:00
|
|
|
|
|
|
|
const refList: Array<{ server: string, future: string }> = [
|
2020-01-16 09:27:24 +00:00
|
|
|
{ server: 'an', future: 'g' },//spigot 1.12.2
|
2020-01-17 09:47:38 +00:00
|
|
|
{ server: 'getServerConnection', future: 'f' },//after spigot 1.14.4
|
2020-01-16 09:27:24 +00:00
|
|
|
{ server: 'func_147137_ag', future: 'field_151274_e' }//catserver 1.12.2
|
2019-11-10 02:11:13 +00:00
|
|
|
]
|
|
|
|
|
2020-02-27 10:15:33 +00:00
|
|
|
@plugin({ name: 'MiaoConsole', version: '1.0.0', author: 'MiaoWoo', servers: ['!nukkit'], source: __filename })
|
2020-01-16 09:27:24 +00:00
|
|
|
export class MiaoConsole extends interfaces.Plugin {
|
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
|
|
|
|
|
2020-03-30 10:49:19 +00:00
|
|
|
private pipeline: any;
|
|
|
|
private socketIOServer: SocketIOServer;
|
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[]) {
|
2020-01-17 09:47:38 +00:00
|
|
|
}
|
|
|
|
|
2019-09-24 02:12:57 +00:00
|
|
|
disable() {
|
2020-03-30 10:49:19 +00:00
|
|
|
this.socketIOServer?.close()
|
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
|
|
|
|
2020-03-30 10:49:19 +00:00
|
|
|
reflectPromise(consoleServer: any) {
|
2019-10-30 12:45:59 +00:00
|
|
|
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-03-30 10:49:19 +00:00
|
|
|
reflectChannel(promise: any) {
|
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() {
|
2020-03-30 10:49:19 +00:00
|
|
|
this.socketIOServer = new SocketIOServer(this.pipeline, {
|
|
|
|
path: '/ws'
|
|
|
|
});
|
|
|
|
let namespace = this.socketIOServer.of('/MiaoConsole')
|
|
|
|
namespace.on('connect', (client: SocketIOSocket) => {
|
|
|
|
global.setGlobal('client', client);
|
|
|
|
client.on('type', (fn) => {
|
|
|
|
this.logger.console(`§6客户端 §b${client.id} §a新建连接...`)
|
|
|
|
fn && fn(this.ServerType)
|
|
|
|
client.emit('log', `Currect Server Version is ${this.Server.getVersion()}`)
|
|
|
|
})
|
|
|
|
client.on('command', (cmd) => {
|
|
|
|
setTimeout(() => this.Server.dispatchConsoleCommand(cmd), 0)
|
|
|
|
client.emit('log', `§6命令: §b${cmd} §a执行成功!`)
|
|
|
|
})
|
|
|
|
client.on('exec', (code) => {
|
|
|
|
try {
|
|
|
|
client.emit('log', this.Task.callSyncMethod(() => eval(code)) + '')
|
|
|
|
} catch (ex) {
|
|
|
|
client.emit('log', '§4代码执行异常\n' + console.stack(ex).join('\n'))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
client.on('disconnect', () => {
|
|
|
|
this.logger.console(`§6客户端 §b${client.id} §c断开连接...`)
|
|
|
|
})
|
|
|
|
})
|
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
|
|
|
}
|
2020-02-27 10:15:33 +00:00
|
|
|
}
|