feat: support spring

Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
MiaoWoo 2020-05-26 15:55:40 +08:00
parent dd28d9fae5
commit 3a045bd335

View File

@ -37,7 +37,7 @@ export class MiaoConsole extends interfaces.Plugin {
private pluginFolder: string private pluginFolder: string
private token: string private token: string
private pipeline: any private instance: any
private socketIOServer: SocketIOServer private socketIOServer: SocketIOServer
private rootLogger: any private rootLogger: any
private appender: any private appender: any
@ -49,7 +49,7 @@ export class MiaoConsole extends interfaces.Plugin {
load() { load() {
if (this.secret.token) { if (this.secret.token) {
this.token = this.secret.token this.token = this.secret.token
this.logger.console(`§4已从配置文件加载永久Token 请注意服务器安全!`) this.logger.console(`§c已从配置文件加载永久Token §4请注意服务器安全!`)
} else { } else {
this.token = Java.type('java.util.UUID').randomUUID().toString() this.token = Java.type('java.util.UUID').randomUUID().toString()
this.logger.console(`§6已生成随机Token: §3${this.token} §c重启后或重新生成后失效!`) this.logger.console(`§6已生成随机Token: §3${this.token} §c重启后或重新生成后失效!`)
@ -98,14 +98,17 @@ export class MiaoConsole extends interfaces.Plugin {
enable() { enable() {
let count = 0 let count = 0
let wait = this.task.create(() => { let wait = this.task.create(() => {
this.pipeline = this.server.getNettyPipeline() this.instance = this.server.getNettyPipeline()
if (this.pipeline) { if (this.instance) {
wait.cancel() wait?.cancel()
this.createSocketIOServer() this.createSocketIOServer()
this.startSocketIOServer() this.startSocketIOServer()
} }
if (count > 30) { wait.cancel() } if (count++ > 30) {
count++ wait?.cancel()
this.logger.console('§cNetty通道注入失败 §4所有功能将无法使用')
return
}
}).later(20).timer(40).submit() }).later(20).timer(40).submit()
this.rootLogger = this.server.getRootLogger() this.rootLogger = this.server.getRootLogger()
} }
@ -125,7 +128,7 @@ export class MiaoConsole extends interfaces.Plugin {
} }
@enable({ servers: [constants.ServerType.Bungee] }) @enable({ servers: [constants.ServerType.Bungee] })
addJavaLoggerForward() { addLoggerForward() {
if (this.rootLogger) { if (this.rootLogger) {
let AbstractHandler = Java.type('java.util.logging.Handler') let AbstractHandler = Java.type('java.util.logging.Handler')
let ProxyHandler = Java.extend(AbstractHandler, { let ProxyHandler = Java.extend(AbstractHandler, {
@ -138,6 +141,20 @@ export class MiaoConsole extends interfaces.Plugin {
} }
} }
@enable({ servers: [constants.ServerType.Spring] })
addLogbackForward() {
if (this.rootLogger) {
let AppenderBase = Java.type('ch.qos.logback.core.AppenderBase')
let ProxyAppender = Java.extend(AppenderBase, {
append: (logEvent) => global.eventCenter.emit('log', logEvent.getFormattedMessage())
})
this.appender = new ProxyAppender()
this.appender.setName("NashornProxyAppender")
this.appender.setContext(this.rootLogger.getLoggerContext())
this.rootLogger.addAppender(this.appender)
}
}
disable() { disable() {
if (this.socketIOServer) { if (this.socketIOServer) {
this.socketIOServer.close() this.socketIOServer.close()
@ -159,7 +176,7 @@ export class MiaoConsole extends interfaces.Plugin {
} }
@disable({ servers: [constants.ServerType.Bungee] }) @disable({ servers: [constants.ServerType.Bungee] })
removeJavaLoggerForward() { removeLoggerForward() {
try { try {
this.rootLogger.removeHandler(this.handler) this.rootLogger.removeHandler(this.handler)
} catch (error) { } catch (error) {
@ -167,8 +184,17 @@ export class MiaoConsole extends interfaces.Plugin {
} }
} }
@disable({ servers: [constants.ServerType.Spring] })
removeLogbackForward() {
try {
this.rootLogger.detachAppender("NashornProxyAppender")
} catch (error) {
console.ex(error)
}
}
createSocketIOServer() { createSocketIOServer() {
this.socketIOServer = io(this.pipeline, { this.socketIOServer = io(this.instance, {
path: '/ws', path: '/ws',
root: fs.concat(root, 'wwwroot') root: fs.concat(root, 'wwwroot')
}) })
@ -224,6 +250,13 @@ export class MiaoConsole extends interfaces.Plugin {
fn('§6插件 §a' + name + ' §4更新异常 错误: ' + error) fn('§6插件 §a' + name + ' §4更新异常 错误: ' + error)
} }
}) })
client.on('ls', (file: string, fn) => {
let dir = fs.file(file);
if (!dir.isDirectory()) {
return fn(undefined, `${file} 不是一个目录!`)
}
fn(fs.list(dir))
})
client.on('error', (error) => { client.on('error', (error) => {
this.logger.console(`§6客户端 §b${client.id} §c触发异常: ${error}`) this.logger.console(`§6客户端 §b${client.id} §c触发异常: ${error}`)
this.logger.error(error) this.logger.error(error)
@ -232,7 +265,6 @@ export class MiaoConsole extends interfaces.Plugin {
this.logger.console(`§6客户端 §b${client.id} §c断开连接...`) this.logger.console(`§6客户端 §b${client.id} §c断开连接...`)
}) })
}) })
this.logger.info('Netty Channel Pipeline Inject MiaoDetectHandler Successful!')
} }
private runCode(code: string, namespace: any, client: any) { private runCode(code: string, namespace: any, client: any) {
@ -243,7 +275,7 @@ export class MiaoConsole extends interfaces.Plugin {
tempconcent += text + "\\n" tempconcent += text + "\\n"
} }
var result = eval(${JSON.stringify(code)}); var result = eval(${JSON.stringify(code)});
return tempconcent + result return tempconcent + '§a返回结果: §r'+ result
`) `)
return this.task.callSyncMethod(() => tfunc.apply(this, [namespace, client])) + '' return this.task.callSyncMethod(() => tfunc.apply(this, [namespace, client])) + ''
} }