11import { IEvent } from "@/abstracts/IEvent" ;
2+ import type { Server } from "@/core/Server" ;
23import { Debug , ThrowError } from "@/decorators" ;
4+ import { config } from "@growserver/config" ;
5+ import { PacketTypes } from "@growserver/const" ;
6+ import type { Database } from "@growserver/db" ;
37import logger from "@growserver/logger" ;
8+ import { ExtendBuffer , parseAction } from "@growserver/utils" ;
49
510export default class EventRaw extends IEvent {
611 public name : string = "raw" ;
@@ -10,7 +15,53 @@ export default class EventRaw extends IEvent {
1015
1116 @Debug ( )
1217 @ThrowError ( "Failed to call Raw event" )
13- public async execute ( serverID : number , netID : number , channelID : number , data : Buffer ) {
18+ public async execute ( serverID : number , server : Server , database : Database , netID : number , channelID : number , data : Buffer ) {
1419 logger . info ( `[S-${ serverID } ] client sending data:\n${ data . toString ( "hex" ) . match ( / ../ g) ?. join ( " " ) } ` ) ;
20+
21+ const buf = new ExtendBuffer ( 0 ) ;
22+ buf . data = data ;
23+
24+ const type = buf . readU32 ( ) ;
25+
26+ switch ( type ) {
27+ case PacketTypes . TEXT : {
28+ const text : Record < string , string > = parseAction ( buf . data ) ;
29+
30+ const ltoken = text . ltoken ;
31+ const session = await this . validateToken ( ltoken , database ) ;
32+
33+ logger . info ( { session, ltoken} ) ;
34+
35+ break ;
36+ }
37+ }
38+ }
39+
40+ @Debug ( )
41+ @ThrowError ( "Failed to validating ltoken" )
42+ public async validateToken ( token : string , database : Database ) {
43+ const sessionData = await database . models . Session . findOne ( { token } )
44+ . populate ( {
45+ path : 'userId' ,
46+ model : 'User' ,
47+ populate : {
48+ path : 'playerId' ,
49+ model : 'Player'
50+ }
51+ } ) ;
52+
53+ if ( ! sessionData ) {
54+ return null ;
55+ }
56+
57+ // Check if session is expired
58+ if ( new Date ( sessionData . expiresAt ) < new Date ( ) ) {
59+ return null ;
60+ }
61+
62+ const user = sessionData . userId as Record < string , string > ;
63+ const player = user ?. playerId || null ;
64+
65+ return { session : sessionData , user, player } ;
1566 }
1667}
0 commit comments