Commit b248c4a8 authored by nanahira's avatar nanahira

add message log

parent d7cad63e
import 'source-map-support/register';
import { Context, Logger, Next, Session, Cache, segment } from 'koishi';
import {
HisoutensokuJammerPluginConfig,
HisoutensokuJammerPluginConfigLike,
} from './config';
import { Logger, Next, Session, Cache, segment } from 'koishi';
import { HisoutensokuJammerPluginConfig } from './config';
import {
DefinePlugin,
InjectConfig,
UseMiddleware,
InjectLogger,
Inject,
UseEvent,
OnApply,
BasePlugin,
} from 'koishi-thirdeye';
import { Attacker } from './attacker';
import moment from 'moment';
import { chineseCharacterList } from './chinese-replace';
import { createWorker, Worker } from 'tesseract.js';
import { Worker } from 'tesseract.js';
import _ from 'lodash';
export * from './config';
declare module 'koishi' {
// eslint-disable-next-line @typescript-eslint/no-namespace
namespace Cache {
interface Tables {
lastMessages: string;
......@@ -38,12 +37,10 @@ const PROTOCOL_BASE64 = 'base64://';
name: 'hisoutensoku-jammer',
schema: HisoutensokuJammerPluginConfig,
})
export default class HisoutensokuJammerPlugin implements OnApply {
constructor(
private ctx: Context,
config: HisoutensokuJammerPluginConfigLike,
) {}
export default class HisoutensokuJammerPlugin
extends BasePlugin<HisoutensokuJammerPluginConfig>
implements OnApply
{
@InjectLogger()
private log: Logger;
......@@ -54,9 +51,6 @@ export default class HisoutensokuJammerPlugin implements OnApply {
this.cache.table('lastMessages', { maxAge: 600000 });
}
@InjectConfig()
private config: HisoutensokuJammerPluginConfig;
ocrWorker: Worker;
@UseEvent('ready')
......@@ -138,12 +132,15 @@ export default class HisoutensokuJammerPlugin implements OnApply {
);
}
this.log.info(`Parsing message from ${sender}: ${receivedMessage}`);
let messageMatch = receivedMessage.match(matcherGlobal);
if (useCache) {
const lastMessage = await this.cache.get('lastMessages', sender);
const currentMessage = receivedMessage;
if (lastMessage) {
receivedMessage = `${lastMessage} ${receivedMessage}`;
this.log.info(`Merged message from ${sender}: ${receivedMessage}`);
if (!messageMatch) {
messageMatch = receivedMessage.match(matcherGlobal);
}
......@@ -159,6 +156,9 @@ export default class HisoutensokuJammerPlugin implements OnApply {
const results: { address: string; port: number }[] = [];
for (const pattern of messageMatch) {
const patternMatch = pattern.match(matcherSingle);
if ((patternMatch?.length || 0) <= 6) {
continue;
}
const firstDigit = patternMatch[2].replace(/[^\d]+/g, '');
const address = `${firstDigit}.${patternMatch[3]
.split(/[^\d]+/)
......@@ -179,7 +179,17 @@ export default class HisoutensokuJammerPlugin implements OnApply {
}
}
}
return results;
const filteredResults = _.uniqWith(
results,
(a, b) => a.address === b.address && a.port === b.port,
);
this.log.info(
`Got addresses: ${filteredResults
.map((e) => `${e.address}:${e.port}`)
.join(', ')}.`,
);
return filteredResults;
}
private async handleMessage(
......@@ -188,7 +198,7 @@ export default class HisoutensokuJammerPlugin implements OnApply {
useCache = true,
) {
const targets = await this.getTargetsFromMessage(message, sender, useCache);
if (!targets) return;
if (!targets?.length) return;
const results: boolean[] = await Promise.all(
targets.map((target) => {
return this.startAttack(target.address, target.port);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment