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-04-07 05:39:35 +00:00
|
|
|
import { inject, ContainerInstance, Container } from '@ms/container'
|
|
|
|
import io, { Server as SocketIOServer, Socket as SocketIOSocket } from '@ms/websocket'
|
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 {
|
2020-04-07 05:39:35 +00:00
|
|
|
@inject(ContainerInstance)
|
|
|
|
private container: Container
|
2019-10-30 12:45:59 +00:00
|
|
|
@inject(server.ServerType)
|
2020-04-07 05:39:35 +00:00
|
|
|
private serverType: string
|
2020-01-16 09:27:24 +00:00
|
|
|
@inject(server.Server)
|
2020-04-07 05:39:35 +00:00
|
|
|
private server: server.Server
|
2020-01-16 09:27:24 +00:00
|
|
|
@inject(task.TaskManager)
|
2020-04-07 05:39:35 +00:00
|
|
|
private task: task.TaskManager
|
|
|
|
@inject(pluginApi.PluginManager)
|
|
|
|
private pluginManager: pluginApi.PluginManager
|
2020-01-16 09:27:24 +00:00
|
|
|
|
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-04-07 05:39:35 +00:00
|
|
|
if (args[0] == 'reload') {
|
|
|
|
// @ts-ignore
|
|
|
|
require.clear('websocket');
|
|
|
|
this.pluginManager.reload(this);
|
|
|
|
return
|
|
|
|
}
|
|
|
|
let tfunc = new Function('pipeline', args.join(' '))
|
|
|
|
tfunc.apply(this, [this.pipeline])
|
2020-01-17 09:47:38 +00:00
|
|
|
}
|
|
|
|
|
2020-04-07 05:39:35 +00:00
|
|
|
enable() {
|
|
|
|
let count = 0;
|
|
|
|
let wait = this.task.create(() => {
|
|
|
|
this.pipeline = this.server.getNettyPipeline()
|
|
|
|
if (this.pipeline) {
|
|
|
|
wait.cancel()
|
|
|
|
this.createSocketIOServer()
|
|
|
|
this.startSocketIOServer()
|
2020-01-16 09:27:24 +00:00
|
|
|
}
|
2020-04-07 05:39:35 +00:00
|
|
|
if (count > 30) { wait.cancel() }
|
|
|
|
count++
|
|
|
|
}).later(20).timer(40).submit()
|
2019-10-30 12:45:59 +00:00
|
|
|
}
|
2019-09-24 02:12:57 +00:00
|
|
|
|
2020-04-07 05:39:35 +00:00
|
|
|
disable() {
|
|
|
|
this.socketIOServer?.close()
|
|
|
|
if (this.container.isBound(io.Instance)) {
|
|
|
|
this.container.unbind(io.Instance)
|
2019-10-30 12:45:59 +00:00
|
|
|
}
|
|
|
|
}
|
2019-09-24 02:12:57 +00:00
|
|
|
|
2020-04-07 05:39:35 +00:00
|
|
|
createSocketIOServer() {
|
|
|
|
this.socketIOServer = io(this.pipeline, {
|
|
|
|
path: '/ws',
|
|
|
|
root: '/home/project/TSWorkSpace/ms/packages/plugins/public'
|
|
|
|
});
|
|
|
|
this.container.bind(io.Instance).toConstantValue(this.socketIOServer)
|
2020-01-16 09:27:24 +00:00
|
|
|
}
|
|
|
|
|
2020-04-07 05:39:35 +00:00
|
|
|
startSocketIOServer() {
|
2020-03-30 10:49:19 +00:00
|
|
|
let namespace = this.socketIOServer.of('/MiaoConsole')
|
|
|
|
namespace.on('connect', (client: SocketIOSocket) => {
|
2020-04-07 05:39:35 +00:00
|
|
|
this.logger.console(`§6客户端 §b${client.id} §a新建连接...`)
|
2020-03-30 10:49:19 +00:00
|
|
|
client.on('type', (fn) => {
|
2020-04-07 05:39:35 +00:00
|
|
|
fn && fn(this.serverType)
|
|
|
|
client.emit('log', `Currect Server Version is ${this.server.getVersion()}`)
|
2020-03-30 10:49:19 +00:00
|
|
|
})
|
|
|
|
client.on('command', (cmd) => {
|
2020-04-07 05:39:35 +00:00
|
|
|
setTimeout(() => this.server.dispatchConsoleCommand(cmd), 0)
|
2020-03-30 10:49:19 +00:00
|
|
|
client.emit('log', `§6命令: §b${cmd} §a执行成功!`)
|
|
|
|
})
|
|
|
|
client.on('exec', (code) => {
|
|
|
|
try {
|
2020-04-07 05:39:35 +00:00
|
|
|
client.emit('log', this.runCode(code, namespace, client))
|
2020-03-30 10:49:19 +00:00
|
|
|
} catch (ex) {
|
|
|
|
client.emit('log', '§4代码执行异常\n' + console.stack(ex).join('\n'))
|
|
|
|
}
|
|
|
|
})
|
2020-04-07 05:39:35 +00:00
|
|
|
client.on('edit', (file: string, fn) => {
|
|
|
|
fn && fn(base.read(file), file.split('.', 2)[1])
|
|
|
|
})
|
2020-03-30 10:49:19 +00:00
|
|
|
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-04-07 05:39:35 +00:00
|
|
|
|
|
|
|
private runCode(code: string, namespace, client) {
|
|
|
|
let tfunc = new Function('namespace', 'client', `
|
|
|
|
var reflect = require('@ms/common/dist/reflect');
|
|
|
|
var tempconcent = '';
|
|
|
|
function print(text) {
|
|
|
|
tempconcent += "\\n" + text
|
|
|
|
}
|
|
|
|
return eval(${JSON.stringify(code)}) + tempconcent
|
|
|
|
`)
|
|
|
|
console.log(tfunc)
|
|
|
|
return this.task.callSyncMethod(() => tfunc.apply(this, [namespace, client])) + ''
|
|
|
|
}
|
2020-02-27 10:15:33 +00:00
|
|
|
}
|