2020-06-20 08:40:34 +00:00
|
|
|
|
/// <reference types="@ccms/nashorn" />
|
2020-01-16 09:27:24 +00:00
|
|
|
|
|
2020-09-22 10:43:02 +00:00
|
|
|
|
import { plugin as pluginApi, server, task, constants, command } from '@ccms/api'
|
2020-05-15 07:49:23 +00:00
|
|
|
|
import { plugin, interfaces, cmd, tab, enable, config, disable } from '@ccms/plugin'
|
2020-05-02 16:14:47 +00:00
|
|
|
|
import { inject, ContainerInstance, Container } from '@ccms/container'
|
|
|
|
|
import io, { Server as SocketIOServer, Socket as SocketIOSocket } from '@ccms/websocket'
|
2020-05-11 09:22:15 +00:00
|
|
|
|
import * as fs from '@ccms/common/dist/fs'
|
2020-05-13 10:42:41 +00:00
|
|
|
|
import * as reflect from '@ccms/common/dist/reflect'
|
2019-11-10 02:11:13 +00:00
|
|
|
|
|
2020-04-24 08:42:36 +00:00
|
|
|
|
const suffixMap = {
|
|
|
|
|
ts: 'typescript',
|
|
|
|
|
js: 'javascript',
|
|
|
|
|
yml: 'yaml'
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-15 06:29:13 +00:00
|
|
|
|
let help = [
|
|
|
|
|
'§6========= §6[§aMiaoConsole§6] 帮助 §aBy §bMiaoWoo §6=========',
|
|
|
|
|
'§6/mconsole §atoken [set] §e<Token> §6- §3生成/设置 登录Token',
|
|
|
|
|
'§6/mconsole §areload §6- §3重载插件',
|
2020-05-15 07:49:23 +00:00
|
|
|
|
]
|
2020-05-15 06:29:13 +00:00
|
|
|
|
|
|
|
|
|
@plugin({ name: 'MiaoConsole', prefix: 'Console', 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-09-22 10:43:02 +00:00
|
|
|
|
@inject(command.Command)
|
|
|
|
|
private command: command.Command
|
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-05-11 09:22:15 +00:00
|
|
|
|
@inject(pluginApi.PluginFolder)
|
2020-05-15 07:49:23 +00:00
|
|
|
|
private pluginFolder: string
|
2020-01-16 09:27:24 +00:00
|
|
|
|
|
2020-05-15 07:49:23 +00:00
|
|
|
|
private token: string
|
2020-05-26 07:55:40 +00:00
|
|
|
|
private instance: any
|
2020-05-15 07:49:23 +00:00
|
|
|
|
private socketIOServer: SocketIOServer
|
|
|
|
|
private rootLogger: any
|
|
|
|
|
private appender: any
|
|
|
|
|
private handler: any
|
2019-09-24 02:12:57 +00:00
|
|
|
|
|
2020-06-02 10:11:45 +00:00
|
|
|
|
private logCache: string[] = []
|
|
|
|
|
|
2020-05-15 06:29:13 +00:00
|
|
|
|
@config()
|
|
|
|
|
private secret = { token: undefined }
|
|
|
|
|
|
|
|
|
|
load() {
|
|
|
|
|
if (this.secret.token) {
|
2020-05-15 07:49:23 +00:00
|
|
|
|
this.token = this.secret.token
|
2020-05-26 07:55:40 +00:00
|
|
|
|
this.logger.console(`§c已从配置文件加载永久Token §4请注意服务器安全!`)
|
2020-05-15 06:29:13 +00:00
|
|
|
|
} else {
|
|
|
|
|
this.token = Java.type('java.util.UUID').randomUUID().toString()
|
|
|
|
|
this.logger.console(`§6已生成随机Token: §3${this.token} §c重启后或重新生成后失效!`)
|
|
|
|
|
}
|
2020-06-20 08:40:34 +00:00
|
|
|
|
process.on('message', (msg) => {
|
2020-06-02 10:11:45 +00:00
|
|
|
|
this.logCache.push(msg)
|
|
|
|
|
if (this.logCache.length > 30) {
|
|
|
|
|
this.logCache = this.logCache.slice(this.logCache.length - 30, this.logCache.length)
|
|
|
|
|
}
|
|
|
|
|
})
|
2020-05-15 06:29:13 +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-05-15 06:29:13 +00:00
|
|
|
|
let cmdKey = 'cmd' + (args[0] || 'help')
|
|
|
|
|
if (!this[cmdKey]) {
|
|
|
|
|
console.sender(sender, '§4未知的子命令: §c' + cmdKey)
|
|
|
|
|
console.sender(sender, `§6请执行 §b/${command} §ahelp §6查看帮助!`)
|
2020-05-15 07:49:23 +00:00
|
|
|
|
return
|
2020-04-07 05:39:35 +00:00
|
|
|
|
}
|
2020-05-15 06:29:13 +00:00
|
|
|
|
args.shift()
|
2020-05-15 07:49:23 +00:00
|
|
|
|
this[cmdKey](sender, ...args)
|
2020-05-15 06:29:13 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cmdhelp(sender: any) {
|
2020-05-15 07:49:23 +00:00
|
|
|
|
this.logger.sender(sender, help)
|
2020-05-15 06:29:13 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cmdreload(sender: any) {
|
|
|
|
|
// @ts-ignore
|
2020-05-15 07:49:23 +00:00
|
|
|
|
require.clear('websocket')
|
|
|
|
|
this.pluginManager.reload(this)
|
2020-05-15 06:29:13 +00:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cmdtoken(sender: any, sub: string, token: string) {
|
|
|
|
|
if (sub == "set") {
|
2020-05-15 07:49:23 +00:00
|
|
|
|
this.secret.token = this.token = token
|
2020-05-15 06:29:13 +00:00
|
|
|
|
this.logger.sender(sender, '§a已保存§6服务器登录Token:§3', this.token, '§4请勿分享给其他人 防止服务器被攻击!')
|
2020-05-15 07:49:23 +00:00
|
|
|
|
return
|
2020-05-15 06:29:13 +00:00
|
|
|
|
}
|
|
|
|
|
this.token = Java.type('java.util.UUID').randomUUID().toString()
|
|
|
|
|
this.logger.sender(sender, '§a已刷新§6服务器登录Token:§3', this.token, '§4请勿分享给其他人 防止服务器被攻击!')
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@tab()
|
|
|
|
|
tabmconsole(sender: any, command: string, args: string[]) {
|
|
|
|
|
if (args.length === 1) { return ["reload", "token"] }
|
|
|
|
|
if (args[0] == "token") { return ["set"] }
|
2020-01-17 09:47:38 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-07 05:39:35 +00:00
|
|
|
|
enable() {
|
2020-05-15 07:49:23 +00:00
|
|
|
|
let count = 0
|
2020-04-07 05:39:35 +00:00
|
|
|
|
let wait = this.task.create(() => {
|
2020-05-26 07:55:40 +00:00
|
|
|
|
this.instance = this.server.getNettyPipeline()
|
|
|
|
|
if (this.instance) {
|
|
|
|
|
wait?.cancel()
|
2020-04-07 05:39:35 +00:00
|
|
|
|
this.createSocketIOServer()
|
|
|
|
|
this.startSocketIOServer()
|
2020-06-02 10:11:45 +00:00
|
|
|
|
return
|
2020-01-16 09:27:24 +00:00
|
|
|
|
}
|
2020-06-02 10:11:45 +00:00
|
|
|
|
if (count++ > 5) {
|
2020-05-26 07:55:40 +00:00
|
|
|
|
wait?.cancel()
|
|
|
|
|
this.logger.console('§cNetty通道注入失败 §4所有功能将无法使用!')
|
|
|
|
|
return
|
|
|
|
|
}
|
2020-06-02 10:11:45 +00:00
|
|
|
|
}).later(20).timer(20).submit()
|
2020-05-15 07:49:23 +00:00
|
|
|
|
this.rootLogger = this.server.getRootLogger()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@enable({ servers: [constants.ServerType.Bukkit, constants.ServerType.Sponge] })
|
|
|
|
|
addLog4jForward() {
|
2020-05-13 10:42:41 +00:00
|
|
|
|
if (this.rootLogger) {
|
2020-05-15 07:49:23 +00:00
|
|
|
|
let AbstractAppender = Java.type('org.apache.logging.log4j.core.appender.AbstractAppender')
|
2020-06-02 10:11:45 +00:00
|
|
|
|
let Level = Java.type('org.apache.logging.log4j.Level')
|
2020-05-13 10:42:41 +00:00
|
|
|
|
let ProxyAppender = Java.extend(AbstractAppender, {
|
2020-06-02 10:11:45 +00:00
|
|
|
|
append: (logEvent) => {
|
|
|
|
|
if (logEvent.level.intLevel() <= Level.INFO.intLevel()) {
|
2020-06-20 08:40:34 +00:00
|
|
|
|
process.emit('message', logEvent.getMessage().getFormattedMessage())
|
2020-06-02 10:11:45 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-05-13 10:42:41 +00:00
|
|
|
|
})
|
2020-05-15 07:49:23 +00:00
|
|
|
|
this.appender = new ProxyAppender("ProxyLogger", null, null)
|
|
|
|
|
this.appender.start()
|
|
|
|
|
this.rootLogger.addAppender(this.appender)
|
|
|
|
|
this.rootLogger.setAdditive(true)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@enable({ servers: [constants.ServerType.Bungee] })
|
2020-05-26 07:55:40 +00:00
|
|
|
|
addLoggerForward() {
|
2020-05-15 07:49:23 +00:00
|
|
|
|
if (this.rootLogger) {
|
|
|
|
|
let AbstractHandler = Java.type('java.util.logging.Handler')
|
|
|
|
|
let ProxyHandler = Java.extend(AbstractHandler, {
|
2020-06-20 08:40:34 +00:00
|
|
|
|
publish: (record) => process.emit('message', record.getMessage()),
|
2020-05-15 07:49:23 +00:00
|
|
|
|
flush: () => { },
|
|
|
|
|
close: () => { }
|
|
|
|
|
})
|
|
|
|
|
this.handler = new ProxyHandler()
|
|
|
|
|
this.rootLogger.addHandler(this.handler)
|
2020-05-13 10:42:41 +00:00
|
|
|
|
}
|
2019-10-30 12:45:59 +00:00
|
|
|
|
}
|
2019-09-24 02:12:57 +00:00
|
|
|
|
|
2020-06-02 10:11:45 +00:00
|
|
|
|
private LOGBACK_APPENDER_NAME = "NashornProxyAppender"
|
|
|
|
|
|
2020-05-26 07:55:40 +00:00
|
|
|
|
@enable({ servers: [constants.ServerType.Spring] })
|
|
|
|
|
addLogbackForward() {
|
|
|
|
|
if (this.rootLogger) {
|
|
|
|
|
let AppenderBase = Java.type('ch.qos.logback.core.AppenderBase')
|
|
|
|
|
let ProxyAppender = Java.extend(AppenderBase, {
|
2020-06-20 08:40:34 +00:00
|
|
|
|
append: (logEvent) => process.emit('message', logEvent.getFormattedMessage())
|
2020-05-26 07:55:40 +00:00
|
|
|
|
})
|
|
|
|
|
this.appender = new ProxyAppender()
|
2020-06-02 10:11:45 +00:00
|
|
|
|
this.appender.start()
|
|
|
|
|
this.appender.setName(this.LOGBACK_APPENDER_NAME)
|
2020-05-26 07:55:40 +00:00
|
|
|
|
this.appender.setContext(this.rootLogger.getLoggerContext())
|
|
|
|
|
this.rootLogger.addAppender(this.appender)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-07 05:39:35 +00:00
|
|
|
|
disable() {
|
2020-05-14 11:05:07 +00:00
|
|
|
|
if (this.socketIOServer) {
|
|
|
|
|
this.socketIOServer.close()
|
2020-06-20 08:40:34 +00:00
|
|
|
|
process.removeAllListeners('message')
|
2020-05-14 11:05:07 +00:00
|
|
|
|
}
|
2020-04-07 05:39:35 +00:00
|
|
|
|
if (this.container.isBound(io.Instance)) {
|
|
|
|
|
this.container.unbind(io.Instance)
|
2019-10-30 12:45:59 +00:00
|
|
|
|
}
|
2020-05-15 07:49:23 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@disable({ servers: [constants.ServerType.Bukkit, constants.ServerType.Sponge] })
|
|
|
|
|
removeLog4jForward() {
|
|
|
|
|
try {
|
|
|
|
|
this.appender.stop()
|
|
|
|
|
this.rootLogger.removeAppender(this.appender)
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.ex(error)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@disable({ servers: [constants.ServerType.Bungee] })
|
2020-05-26 07:55:40 +00:00
|
|
|
|
removeLoggerForward() {
|
2020-05-13 10:42:41 +00:00
|
|
|
|
try {
|
2020-05-15 07:49:23 +00:00
|
|
|
|
this.rootLogger.removeHandler(this.handler)
|
2020-05-13 10:42:41 +00:00
|
|
|
|
} catch (error) {
|
2020-05-15 07:49:23 +00:00
|
|
|
|
console.ex(error)
|
2020-05-13 10:42:41 +00:00
|
|
|
|
}
|
2019-10-30 12:45:59 +00:00
|
|
|
|
}
|
2019-09-24 02:12:57 +00:00
|
|
|
|
|
2020-05-26 07:55:40 +00:00
|
|
|
|
@disable({ servers: [constants.ServerType.Spring] })
|
|
|
|
|
removeLogbackForward() {
|
|
|
|
|
try {
|
2020-06-02 10:11:45 +00:00
|
|
|
|
this.rootLogger.detachAppender(this.LOGBACK_APPENDER_NAME)
|
2020-05-26 07:55:40 +00:00
|
|
|
|
} catch (error) {
|
|
|
|
|
console.ex(error)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-07 05:39:35 +00:00
|
|
|
|
createSocketIOServer() {
|
2020-05-26 07:55:40 +00:00
|
|
|
|
this.socketIOServer = io(this.instance, {
|
2020-04-07 05:39:35 +00:00
|
|
|
|
path: '/ws',
|
2020-05-15 06:29:13 +00:00
|
|
|
|
root: fs.concat(root, 'wwwroot')
|
2020-05-15 07:49:23 +00:00
|
|
|
|
})
|
2020-04-07 05:39:35 +00:00
|
|
|
|
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')
|
2020-06-20 08:40:34 +00:00
|
|
|
|
process.on('message', (msg) => namespace.emit('log', msg))
|
2020-03-30 10:49:19 +00:00
|
|
|
|
namespace.on('connect', (client: SocketIOSocket) => {
|
2020-05-15 06:29:13 +00:00
|
|
|
|
if (!this.token) {
|
2020-05-15 07:49:23 +00:00
|
|
|
|
this.logger.console(`§6客户端 §b${client.id} §a请求连接 §4服务器尚未设置 Token 无法连接!`)
|
|
|
|
|
client.emit('unauthorized')
|
|
|
|
|
client.disconnect(true)
|
|
|
|
|
return
|
2020-05-15 06:29:13 +00:00
|
|
|
|
}
|
|
|
|
|
if (this.token != client.handshake.query.token) {
|
2020-06-02 10:11:45 +00:00
|
|
|
|
this.logger.console(`§6客户端 §b${client.id} §c无效请求 §4请提供正确Token后再次连接!`)
|
2020-05-15 07:49:23 +00:00
|
|
|
|
client.emit('unauthorized')
|
|
|
|
|
client.disconnect(true)
|
|
|
|
|
return
|
2020-05-14 11:05:07 +00:00
|
|
|
|
}
|
2020-06-02 10:11:45 +00:00
|
|
|
|
this.initWebSocketClient(client)
|
|
|
|
|
this.logCache.forEach(msg => client.emit('log', msg))
|
2020-05-15 06:29:13 +00:00
|
|
|
|
this.logger.console(`§6客户端 §b${client.id} §a新建连接 ${this.rootLogger ? '启动日志转发' : '§4转发日志启动失败'}...`)
|
2020-06-02 10:11:45 +00:00
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private initWebSocketClient(client: SocketIOSocket) {
|
|
|
|
|
client.on('type', (fn) => {
|
|
|
|
|
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执行成功!`)
|
|
|
|
|
})
|
2020-06-20 08:40:34 +00:00
|
|
|
|
client.on('tabComplate', (input, index, callback) => {
|
2020-09-22 10:43:02 +00:00
|
|
|
|
callback?.(this.command.tabComplete(this.server.getConsoleSender(), input, index))
|
2020-06-20 08:40:34 +00:00
|
|
|
|
})
|
2020-06-02 10:11:45 +00:00
|
|
|
|
client.on('exec', (code) => {
|
|
|
|
|
try {
|
|
|
|
|
client.emit('log', this.runCode(code, client.nsp, client))
|
|
|
|
|
} catch (ex) {
|
|
|
|
|
client.emit('log', `§4代码执行异常 错误: ${ex}\n${console.stack(ex).join('\n')}`)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
client.on('edit', (file: string, fn) => {
|
|
|
|
|
fn && fn(base.read(file), suffixMap[file.split('.', 2)[1]])
|
|
|
|
|
})
|
|
|
|
|
client.on('save', (name: string, content: string, fn) => {
|
|
|
|
|
this.logger.console(`§6客户端 §b${client.id} §6请求更新插件 §a${name} §6...`)
|
|
|
|
|
let file = fs.concat(root, this.pluginFolder, name + '.js')
|
|
|
|
|
if (!fs.exists(file)) { return fn('§6插件 §a' + name + ' §6尚未安装 §c请先创建空文件 或安装插件!') }
|
|
|
|
|
try {
|
|
|
|
|
base.save(file, content)
|
|
|
|
|
this.pluginManager.reload(name)
|
|
|
|
|
fn('§6插件 §a' + name + ' §6更新成功!')
|
|
|
|
|
} catch (error) {
|
2020-05-11 09:22:15 +00:00
|
|
|
|
this.logger.error(error)
|
2020-06-02 10:11:45 +00:00
|
|
|
|
fn('§6插件 §a' + name + ' §4更新异常 错误: ' + error)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
client.on('ls', (file: string, fn) => {
|
2020-06-20 08:40:34 +00:00
|
|
|
|
let dir = fs.file(file)
|
2020-06-02 10:11:45 +00:00
|
|
|
|
if (!dir.isDirectory()) {
|
|
|
|
|
return fn(undefined, `${file} 不是一个目录!`)
|
|
|
|
|
}
|
|
|
|
|
fn(fs.list(dir))
|
|
|
|
|
})
|
|
|
|
|
client.on('error', (error) => {
|
|
|
|
|
this.logger.console(`§6客户端 §b${client.id} §c触发异常: ${error}`)
|
|
|
|
|
this.logger.error(error)
|
|
|
|
|
})
|
|
|
|
|
client.on('disconnect', () => {
|
|
|
|
|
this.logger.console(`§6客户端 §b${client.id} §c断开连接...`)
|
2020-03-30 10:49:19 +00:00
|
|
|
|
})
|
2019-09-24 02:12:57 +00:00
|
|
|
|
}
|
2020-04-07 05:39:35 +00:00
|
|
|
|
|
2020-04-24 08:42:36 +00:00
|
|
|
|
private runCode(code: string, namespace: any, client: any) {
|
2020-06-02 10:11:45 +00:00
|
|
|
|
let paramNames = [
|
|
|
|
|
'namespace',
|
|
|
|
|
'client',
|
|
|
|
|
'reflect',
|
|
|
|
|
'container',
|
|
|
|
|
'pluginManager'
|
|
|
|
|
]
|
|
|
|
|
let params = [
|
|
|
|
|
namespace,
|
|
|
|
|
client,
|
|
|
|
|
reflect,
|
|
|
|
|
this.container,
|
|
|
|
|
this.pluginManager
|
|
|
|
|
]
|
|
|
|
|
let tfunc = new Function(
|
|
|
|
|
...paramNames,
|
|
|
|
|
`var api = require('@ccms/api');
|
|
|
|
|
if (this.serverType == "spring") {
|
|
|
|
|
var dbm = container.get(api.database.DataBaseManager)
|
|
|
|
|
var db = dbm.getMainDatabase()
|
2020-06-20 08:40:34 +00:00
|
|
|
|
var bf = base.getInstance().getAutowireCapableBeanFactory()
|
2020-06-02 10:11:45 +00:00
|
|
|
|
}
|
2020-06-20 08:40:34 +00:00
|
|
|
|
return '§a返回结果: §r'+ eval(${JSON.stringify(code)});`)
|
2020-06-02 10:11:45 +00:00
|
|
|
|
return this.task.callSyncMethod(() => tfunc.apply(this, params)) + ''
|
2020-04-07 05:39:35 +00:00
|
|
|
|
}
|
2020-02-27 10:15:33 +00:00
|
|
|
|
}
|