improve kick command

This commit is contained in:
memetrollsXD 2022-08-03 04:18:14 +02:00
parent a8cba90d7b
commit 10fe4948fe
No known key found for this signature in database
GPG Key ID: 105C2F3417AC32CD
3 changed files with 34 additions and 20 deletions

View File

@ -1,5 +1,3 @@
import { BlackLimitLevel, PlayerKickOutScNotify, PlayerKickOutScNotify_KickType } from "../data/proto/StarRail";
import SRServer from "../server/kcp/SRServer";
import Logger from "../util/Logger"; import Logger from "../util/Logger";
import Interface, { Command } from "./Interface"; import Interface, { Command } from "./Interface";
const c = new Logger("/kick", "blue"); const c = new Logger("/kick", "blue");
@ -10,17 +8,7 @@ export default async function handle(command: Command) {
return; return;
} }
Interface.target.send("PlayerKickOutScNotify", { Interface.target.kick();
kickType: PlayerKickOutScNotify_KickType.KICK_BLACK,
blackInfo: {
limitLevel: BlackLimitLevel.BLACK_LIMIT_LEVEL_ALL,
beginTime: Math.round(Date.now() / 1000),
endTime: Math.round(Date.now() / 1000),
banType: 2
}
} as PlayerKickOutScNotify);
// SRServer.getInstance().sessions.delete(`${Interface.target.ctx.address}:${Interface.target.ctx.port}`);
c.log(`Kicked ${Interface.target.account.name}`); c.log(`Kicked ${Interface.target.account.name}`);
} }

View File

@ -40,8 +40,7 @@ export default class SRServer {
switch (handshake.handshakeType) { switch (handshake.handshakeType) {
case HandshakeType.CONNECT: case HandshakeType.CONNECT:
c.log(`${client} connected`); c.log(`${client} connected`);
const rsp = new Handshake(HandshakeType.SEND_BACK_CONV).encode(); this.handshake(HandshakeType.SEND_BACK_CONV, rinfo);
this.udpSocket.send(rsp, 0, rsp.byteLength, rinfo.port, rinfo.address);
const kcpobj = new KCP(0x69, 0x420, { const kcpobj = new KCP(0x69, 0x420, {
address: rinfo.address, address: rinfo.address,
port: rinfo.port, port: rinfo.port,
@ -50,7 +49,7 @@ export default class SRServer {
kcpobj.nodelay(1, 5, 2, 0); kcpobj.nodelay(1, 5, 2, 0);
kcpobj.output((d, s, u) => this.output(d, s, u)); kcpobj.output((d, s, u) => this.output(d, s, u));
kcpobj.wndsize(256, 256); kcpobj.wndsize(256, 256);
this.sessions.set(client, new Session(kcpobj, rinfo)); this.sessions.set(client, new Session(kcpobj, rinfo, client));
break; break;
case HandshakeType.DISCONNECT: case HandshakeType.DISCONNECT:
c.log(`${client} disconnected`); c.log(`${client} disconnected`);
@ -72,6 +71,11 @@ export default class SRServer {
this.udpSocket.send(buf, 0, size, ctx.port, ctx.address); this.udpSocket.send(buf, 0, size, ctx.port, ctx.address);
} }
public handshake(hType: HandshakeType, rinfo: RemoteInfo) {
const rsp = new Handshake(hType).encode();
this.udpSocket.send(rsp, 0, rsp.byteLength, rinfo.port, rinfo.address);
}
private async onError(err: Error) { private async onError(err: Error) {
c.error(err); c.error(err);
} }

View File

@ -8,8 +8,10 @@ import Logger, { VerboseLevel } from '../../util/Logger';
import defaultHandler from '../packets/PacketHandler'; import defaultHandler from '../packets/PacketHandler';
import Account from '../../db/Account'; import Account from '../../db/Account';
import Player from '../../db/Player'; import Player from '../../db/Player';
import { PlayerSyncScNotify } from '../../data/proto/StarRail'; import { BlackLimitLevel, PlayerKickOutScNotify, PlayerKickOutScNotify_KickType, PlayerSyncScNotify } from '../../data/proto/StarRail';
import Avatar from '../../db/Avatar'; import Avatar from '../../db/Avatar';
import SRServer from './SRServer';
import { HandshakeType } from './Handshake';
function r(...args: string[]) { function r(...args: string[]) {
return fs.readFileSync(resolve(__dirname, ...args)); return fs.readFileSync(resolve(__dirname, ...args));
@ -20,18 +22,21 @@ export default class Session {
public c: Logger; public c: Logger;
public account!: Account; public account!: Account;
public player!: Player; public player!: Player;
public constructor(private readonly kcpobj: KCP.KCP, public readonly ctx: RemoteInfo) { public kicked = false;
this.kcpobj = kcpobj;
public constructor(private kcpobj: KCP.KCP, public readonly ctx: RemoteInfo, public id: string) {
this.ctx = ctx; this.ctx = ctx;
this.c = new Logger(`${this.ctx.address}:${this.ctx.port}`, 'yellow'); this.c = new Logger(`${this.ctx.address}:${this.ctx.port}`, 'yellow');
this.update(); this.update();
} }
public inputRaw(data: Buffer) { public inputRaw(data: Buffer) {
if (this.kicked) return;
this.kcpobj.input(data); this.kcpobj.input(data);
} }
public async update() { public async update() {
if (this.kicked) return;
if (!this.kcpobj) { if (!this.kcpobj) {
console.error("wtf kcpobj is undefined"); console.error("wtf kcpobj is undefined");
console.debug(this) console.debug(this)
@ -79,10 +84,26 @@ export default class Session {
}, },
basicInfo: this.player.db.basicInfo basicInfo: this.player.db.basicInfo
} as PlayerSyncScNotify); } as PlayerSyncScNotify);
this.player.save(); this.player.save();
} }
public kick(hard: boolean = true) {
SRServer.getInstance().sessions.delete(this.id);
this.kicked = true;
if (hard) this.send("PlayerKickOutScNotify", {
kickType: PlayerKickOutScNotify_KickType.KICK_BLACK,
blackInfo: {
limitLevel: BlackLimitLevel.BLACK_LIMIT_LEVEL_ALL,
beginTime: Math.round(Date.now() / 1000),
endTime: Math.round(Date.now() / 1000),
banType: 2
}
} as PlayerKickOutScNotify);
SRServer.getInstance().handshake(HandshakeType.DISCONNECT, this.ctx);
}
public send(name: PacketName, body: {}) { public send(name: PacketName, body: {}) {
this.c.verbL(body); this.c.verbL(body);
const packet = Packet.encode(name, body); const packet = Packet.encode(name, body);
@ -93,6 +114,7 @@ export default class Session {
} }
public sendRaw(data: Buffer) { public sendRaw(data: Buffer) {
if (this.kicked) return;
this.kcpobj.send(data); this.kcpobj.send(data);
} }
} }