Commit cca6d3f2 authored by nanahira's avatar nanahira

better replace ment

parent 962a5264
...@@ -12,7 +12,7 @@ ...@@ -12,7 +12,7 @@
"source-map-support": "^0.5.19" "source-map-support": "^0.5.19"
}, },
"devDependencies": { "devDependencies": {
"@types/lodash": "^4.14.175", "@types/lodash": "^4.14.177",
"@types/node": "^15.0.1", "@types/node": "^15.0.1",
"koishi": "^4.0.0-beta.2", "koishi": "^4.0.0-beta.2",
"lodash": "^4.17.21", "lodash": "^4.17.21",
...@@ -23,6 +23,9 @@ ...@@ -23,6 +23,9 @@
"typescript": "^4.2.4", "typescript": "^4.2.4",
"webpack": "^5.36.2", "webpack": "^5.36.2",
"webpack-cli": "^4.6.0" "webpack-cli": "^4.6.0"
},
"peerDependencies": {
"koishi": "^4.0.0-beta.2"
} }
}, },
"node_modules/@discoveryjs/json-ext": { "node_modules/@discoveryjs/json-ext": {
...@@ -248,9 +251,9 @@ ...@@ -248,9 +251,9 @@
} }
}, },
"node_modules/@types/lodash": { "node_modules/@types/lodash": {
"version": "4.14.175", "version": "4.14.177",
"resolved": "https://registry.npmjs.org/@types/lodash/-/lodash-4.14.175.tgz", "resolved": "https://registry.npmjs.org/@types/lodash/-/lodash-4.14.177.tgz",
"integrity": "sha512-XmdEOrKQ8a1Y/yxQFOMbC47G/V2VDO1GvMRnl4O75M4GW/abC5tnfzadQYkqEveqRM1dEJGFFegfPNA2vvx2iw==", "integrity": "sha512-0fDwydE2clKe9MNfvXHBHF9WEahRuj+msTuQqOmAApNORFvhMYZKNGGJdCzuhheVjMps/ti0Ak/iJPACMaevvw==",
"dev": true "dev": true
}, },
"node_modules/@types/mime": { "node_modules/@types/mime": {
...@@ -3248,9 +3251,9 @@ ...@@ -3248,9 +3251,9 @@
} }
}, },
"@types/lodash": { "@types/lodash": {
"version": "4.14.175", "version": "4.14.177",
"resolved": "https://registry.npmjs.org/@types/lodash/-/lodash-4.14.175.tgz", "resolved": "https://registry.npmjs.org/@types/lodash/-/lodash-4.14.177.tgz",
"integrity": "sha512-XmdEOrKQ8a1Y/yxQFOMbC47G/V2VDO1GvMRnl4O75M4GW/abC5tnfzadQYkqEveqRM1dEJGFFegfPNA2vvx2iw==", "integrity": "sha512-0fDwydE2clKe9MNfvXHBHF9WEahRuj+msTuQqOmAApNORFvhMYZKNGGJdCzuhheVjMps/ti0Ak/iJPACMaevvw==",
"dev": true "dev": true
}, },
"@types/mime": { "@types/mime": {
......
...@@ -10,7 +10,7 @@ ...@@ -10,7 +10,7 @@
"koishi": "^4.0.0-beta.2" "koishi": "^4.0.0-beta.2"
}, },
"devDependencies": { "devDependencies": {
"@types/lodash": "^4.14.175", "@types/lodash": "^4.14.177",
"@types/node": "^15.0.1", "@types/node": "^15.0.1",
"koishi": "^4.0.0-beta.2", "koishi": "^4.0.0-beta.2",
"lodash": "^4.17.21", "lodash": "^4.17.21",
......
...@@ -2,103 +2,127 @@ import "source-map-support/register"; ...@@ -2,103 +2,127 @@ import "source-map-support/register";
import { Logger, Context, Schema, segment } from "koishi"; import { Logger, Context, Schema, segment } from "koishi";
import { Attacker } from "./attacker"; import { Attacker } from "./attacker";
import moment from "moment"; import moment from "moment";
import _ from "lodash";
export interface HisoutensokuJammerConfig { export interface HisoutensokuJammerConfig {
attackTimeout?: number; attackTimeout?: number;
addressWhitelist?: string[]; addressWhitelist?: string[];
} }
const chineseCharacterList = [ const chineseCharacterWordList = [
{ character: '', value: 0 }, { character: "", value: 0 },
{ character: '', value: 1 }, { character: "", value: 1 },
{ character: '', value: 2 }, { character: "", value: 2 },
{ character: '', value: 3 }, { character: "", value: 3 },
{ character: '', value: 4 }, { character: "", value: 4 },
{ character: '', value: 5 }, { character: "", value: 5 },
{ character: '', value: 6 }, { character: "", value: 6 },
{ character: '', value: 7 }, { character: "", value: 7 },
{ character: '', value: 8 }, { character: "", value: 8 },
{ character: '', value: 9 }, { character: "", value: 9 },
{ character: '', value: '.' }, { character: "", value: "." },
{ character: '', value: '.' }, { character: "", value: "." },
{ character: '', value: 0 }, { character: "", value: 0 },
{ character: '', value: 1 }, { character: "", value: 1 },
{ character: '', value: 2 }, { character: "", value: 2 },
{ character: '', value: 3 }, { character: "", value: 3 },
{ character: '', value: 4 }, { character: "", value: 4 },
{ character: '', value: 5 }, { character: "", value: 5 },
{ character: '', value: 6 }, { character: "", value: 6 },
{ character: '', value: 7 }, { character: "", value: 7 },
{ character: '', value: 8 }, { character: "", value: 8 },
{ character: '', value: 9 }, { character: "", value: 9 },
{ character: '', value: 11 }, { character: "zero", value: 0 },
{ character: '', value: 12 }, { character: "one", value: 1 },
{ character: '', value: 13 }, { character: "two", value: 2 },
{ character: '', value: 14 }, { character: "three", value: 3 },
{ character: '', value: 15 }, { character: "four", value: 4 },
{ character: '', value: 16 }, { character: "five", value: 5 },
{ character: '', value: 17 }, { character: "six", value: 6 },
{ character: '', value: 18 }, { character: "seven", value: 7 },
{ character: '', value: 19 }, { character: "eight", value: 8 },
{ character: '', value: 20 }, { character: "nine", value: 9 },
{ character: 'ling', value: 0 }, { character: "", value: 11 },
{ character: 'yi', value: 1 }, { character: "", value: 12 },
{ character: 'er', value: 2 }, { character: "", value: 13 },
{ character: 'san', value: 3 }, { character: "", value: 14 },
{ character: 'si', value: 4 }, { character: "", value: 15 },
{ character: 'wu', value: 5 }, { character: "", value: 16 },
{ character: 'liu', value: 6 }, { character: "", value: 17 },
{ character: 'qi', value: 7 }, { character: "", value: 18 },
{ character: 'ba', value: 8 }, { character: "", value: 19 },
{ character: 'jiu', value: 9 }, { character: "", value: 20 },
{ character: 'shi', value: 10 }, { character: "ling", value: 0 },
{ character: '', value: 0 }, { character: "yi", value: 1 },
{ character: '', value: 0 }, { character: "er", value: 2 },
{ character: '', value: 0 }, { character: "san", value: 3 },
{ character: '', value: 1 }, { character: "si", value: 4 },
{ character: '', value: 1 }, { character: "wu", value: 5 },
{ character: '', value: 1 }, { character: "liu", value: 6 },
{ character: '', value: 1 }, { character: "qi", value: 7 },
{ character: '', value: 1 }, { character: "ba", value: 8 },
{ character: '', value: 2 }, { character: "jiu", value: 9 },
{ character: '', value: 2 }, { character: "shi", value: 10 },
{ character: '', value: 2 }, { character: "", value: 0 },
{ character: '', value: 3 }, { character: "", value: 0 },
{ character: '', value: 3 }, { character: "", value: 0 },
{ character: '', value: 3 }, { character: "", value: 1 },
{ character: '', value: 4 }, { character: "", value: 1 },
{ character: '', value: 4 }, { character: "", value: 1 },
{ character: '', value: 5 }, { character: "", value: 1 },
{ character: '', value: 5 }, { character: "", value: 1 },
{ character: '', value: 5 }, { character: "", value: 2 },
{ character: '', value: 5 }, { character: "", value: 2 },
{ character: '', value: 6 }, { character: "", value: 2 },
{ character: '', value: 6 }, { character: "", value: 3 },
{ character: '', value: 6 }, { character: "", value: 3 },
{ character: '', value: 7 }, { character: "", value: 3 },
{ character: '', value: 7 }, { character: "", value: 4 },
{ character: '', value: 7 }, { character: "", value: 4 },
{ character: '', value: 7 }, { character: "", value: 5 },
{ character: '', value: 7 }, { character: "", value: 5 },
{ character: '', value: 8 }, { character: "", value: 5 },
{ character: '', value: 8 }, { character: "", value: 5 },
{ character: '', value: 8 }, { character: "", value: 6 },
{ character: '', value: 8 }, { character: "", value: 6 },
{ character: '', value: 8 }, { character: "", value: 6 },
{ character: '', value: 8 }, { character: "", value: 7 },
{ character: '', value: 8 }, { character: "", value: 7 },
{ character: '', value: 8 }, { character: "", value: 7 },
{ character: '', value: 9 }, { character: "", value: 7 },
{ character: '', value: 9 }, { character: "", value: 7 },
{ character: '', value: 9 }, { character: "", value: 8 },
{ character: '', value: '' }, { character: "", value: 8 },
{ character: '', value: '' }, { character: "", value: 8 },
{ character: '', value: '' }, { character: "", value: 8 },
{ character: '', value: '' }, { character: "", value: 8 },
{ character: '', value: '' }, { character: "", value: 8 },
{ character: '', value: '' }, { character: "", value: 8 },
{ character: '', value: '' }, { character: "", value: 8 },
].map(list => ({ characterRegExp: new RegExp(list.character, 'g'), value: list.value.toString() })); { character: "", value: 9 },
{ character: "", value: 9 },
{ character: "", value: 9 },
{ character: "", value: 9 },
{ character: "", value: "" },
{ character: "", value: "" },
{ character: "", value: "" },
{ character: "", value: "" },
{ character: "", value: "" },
{ character: "", value: "" },
{ character: "", value: "" },
].map((l) => ({ character: l.character, value: l.value.toString() }));
const chineseCharacterWordGroup: Record<
string,
{ character: string; value: string }[]
> = _.groupBy(chineseCharacterWordList, "value");
const chineseCharacterList = Object.entries(chineseCharacterWordGroup).map(
([value, patternObjects]) => ({
value,
characterRegExp: patternObjects
.map((patternObject) => patternObject.character)
.join("|"),
})
);
class HisoutensokuJammer { class HisoutensokuJammer {
log: Logger; log: Logger;
...@@ -118,14 +142,25 @@ class HisoutensokuJammer { ...@@ -118,14 +142,25 @@ class HisoutensokuJammer {
combineMessage(message: string) { combineMessage(message: string) {
const segmentChain = segment.parse(message); const segmentChain = segment.parse(message);
const textSegments = segmentChain.filter(segment => segment.type === 'text'); const textSegments = segmentChain.filter(
return textSegments.map(segment => segment.data.content).join('').trim(); (segment) => segment.type === "text"
);
return textSegments
.map((segment) => segment.data.content)
.join("")
.trim();
} }
async handleMessage(message: string, sender: string) { async handleMessage(message: string, sender: string) {
let realMessage = this.combineMessage(message).split('\n').join(' '); let realMessage = this.combineMessage(message)
for (let chineseCharacter of chineseCharacterList) { .split("\n")
realMessage = realMessage.replace(chineseCharacter.characterRegExp, chineseCharacter.value) .join(" ")
.toLowerCase();
for (const chineseCharacter of chineseCharacterList) {
realMessage = realMessage.replace(
chineseCharacter.characterRegExp,
chineseCharacter.value
);
} }
const lastMessage = this.lastMessageMap.get(sender); const lastMessage = this.lastMessageMap.get(sender);
this.lastMessageMap.set(sender, realMessage); this.lastMessageMap.set(sender, realMessage);
...@@ -145,9 +180,12 @@ class HisoutensokuJammer { ...@@ -145,9 +180,12 @@ class HisoutensokuJammer {
const patternMatch = pattern.match( const patternMatch = pattern.match(
/([1-2]? *\d? *\d *)(([^\d][1-2]?\d{1,2}){3}).+?([1-6] *\d *\d *\d *\d)/ /([1-2]? *\d? *\d *)(([^\d][1-2]?\d{1,2}){3}).+?([1-6] *\d *\d *\d *\d)/
); );
const firstDigit = patternMatch[1].replace(/ +/g, ''); const firstDigit = patternMatch[1].replace(/ +/g, "");
const address = `${firstDigit}.${patternMatch[2].slice(1).split(/[^\d]/).join(".")}`; const address = `${firstDigit}.${patternMatch[2]
const port = parseInt(patternMatch[4].replace(/ +/g, '')); .slice(1)
.split(/[^\d]/)
.join(".")}`;
const port = parseInt(patternMatch[4].replace(/ +/g, ""));
return this.startAttack(address, port); return this.startAttack(address, port);
}); });
const results: boolean[] = await Promise.all(attackPromises); const results: boolean[] = await Promise.all(attackPromises);
...@@ -181,13 +219,10 @@ class HisoutensokuJammer { ...@@ -181,13 +219,10 @@ class HisoutensokuJammer {
export const name = "hisoutensoku-jammer"; export const name = "hisoutensoku-jammer";
export const schema: Schema<HisoutensokuJammerConfig> = Schema.object({ export const schema: Schema<HisoutensokuJammerConfig> = Schema.object({
attackTimeout: Schema.number('干扰时间。').default(10000), attackTimeout: Schema.number("干扰时间。").default(10000),
addressWhitelist: Schema.array(Schema.string(), 'IP 白名单').default([]) addressWhitelist: Schema.array(Schema.string(), "IP 白名单").default([]),
}) });
export function apply( export function apply(ctx: Context, config: HisoutensokuJammerConfig) {
ctx: Context,
config: HisoutensokuJammerConfig
) {
const hisoutensokuJammer = new HisoutensokuJammer(ctx, config); const hisoutensokuJammer = new HisoutensokuJammer(ctx, config);
hisoutensokuJammer.apply(); hisoutensokuJammer.apply();
} }
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