11require ( "dotenv" ) . config ( ) ;
22import { Server , Socket } from "socket.io" ;
3- import { DataTypes , IAUthenticationData , isAuthedData } from "../model/eventData" ;
3+ import {
4+ DataTypes ,
5+ IAuthenticationData ,
6+ IAuxAuthenticationData ,
7+ isAuthedAuxData ,
8+ isAuthedData ,
9+ } from "../model/eventData" ;
410import { MatchController } from "../controller/MatchController" ;
511import logging from "../util/Logging" ;
612import { readFileSync } from "fs" ;
@@ -59,7 +65,10 @@ export class WebsocketIncoming {
5965
6066 ws . once ( "obs_logon" , async ( msg ) => {
6167 try {
62- const authenticationData : IAUthenticationData = JSON . parse ( msg . toString ( ) ) ;
68+ const authenticationData : IAuthenticationData = JSON . parse ( msg . toString ( ) ) ;
69+
70+ if ( WebsocketIncoming . authedClients . find ( ( client ) => client . ws . id === ws . id ) != undefined )
71+ return ;
6372
6473 // Check if the packet is valid
6574 if ( authenticationData . type !== DataTypes . AUTH ) {
@@ -139,6 +148,94 @@ export class WebsocketIncoming {
139148 Log . error ( e ) ;
140149 }
141150 } ) ;
151+
152+ ws . once ( "aux_logon" , async ( msg ) => {
153+ try {
154+ const authenticationData : IAuxAuthenticationData = JSON . parse ( msg . toString ( ) ) ;
155+
156+ if ( WebsocketIncoming . authedClients . find ( ( client ) => client . ws . id === ws . id ) != undefined )
157+ return ;
158+
159+ // Check if the packet is valid
160+ if ( authenticationData . type !== DataTypes . AUX_AUTH ) {
161+ ws . emit (
162+ "aux_logon_ack" ,
163+ JSON . stringify ( { type : DataTypes . AUTH , value : false , reason : `Invalid packet.` } ) ,
164+ ) ;
165+ ws . disconnect ( ) ;
166+ Log . info ( `Received BAD aux auth request, invalid packet.` ) ;
167+ return ;
168+ }
169+
170+ // Check if the client version is compatible with the server version
171+ if ( ! isCompatibleVersion ( authenticationData . clientVersion ) ) {
172+ ws . emit (
173+ "aux_logon_ack" ,
174+ JSON . stringify ( {
175+ type : DataTypes . AUTH ,
176+ value : false ,
177+ reason : `Client version ${ authenticationData . clientVersion } is not compatible with server version ${ module . exports . version } .` ,
178+ } ) ,
179+ ) ;
180+ ws . disconnect ( ) ;
181+ Log . info (
182+ `Received BAD aux auth request from ${ authenticationData . playerId } for match ${ authenticationData . matchId } , incompatible client version ${ authenticationData . clientVersion } .` ,
183+ ) ;
184+ return ;
185+ }
186+
187+ const groupCode = this . matchController . findMatch ( authenticationData . matchId ) ;
188+ // Check if the match exists
189+ if ( groupCode == null ) {
190+ ws . emit (
191+ "aux_logon_ack" ,
192+ JSON . stringify ( {
193+ type : DataTypes . AUTH ,
194+ value : false ,
195+ reason : `Game with Match ID ${ authenticationData . matchId } not found.` ,
196+ } ) ,
197+ ) ;
198+ ws . disconnect ( ) ;
199+ Log . info (
200+ `Received BAD aux auth request from ${ authenticationData . playerId } for match ${ authenticationData . matchId } , match not found.` ,
201+ ) ;
202+ return ;
203+ }
204+
205+ // All checks passed, send logon acknolwedgement
206+ ws . emit (
207+ "aux_logon_ack" ,
208+ JSON . stringify ( { type : DataTypes . AUX_AUTH , value : true , reason : groupCode } ) ,
209+ ) ;
210+ user . name = authenticationData . name ;
211+ user . groupCode = groupCode ;
212+ user . isAuxiliary = true ;
213+ user . playerId = authenticationData . playerId ;
214+ WebsocketIncoming . authedClients . push ( user ) ;
215+
216+ Log . info (
217+ `Received VALID aux auth request from ${ authenticationData . playerId } for Group Code ${ groupCode } ` ,
218+ ) ;
219+ this . onAuthSuccess ( user ) ;
220+ } catch ( e ) {
221+ Log . error ( `Error parsing incoming auth request: ${ e } ` ) ;
222+ Log . error ( e ) ;
223+ }
224+ } ) ;
225+
226+ ws . on ( "disconnect" , ( ) => {
227+ const index = WebsocketIncoming . authedClients . findIndex ( ( client ) => client . ws . id === ws . id ) ;
228+ if ( index != - 1 ) {
229+ const client = WebsocketIncoming . authedClients [ index ] ;
230+ if ( client . playerId !== "" ) {
231+ Log . info ( `Auxiliary player ${ client . playerId } disconnected.` ) ;
232+ this . matchController . setAuxDisconnected ( client . groupCode , client . playerId ) ;
233+ }
234+ if ( client . isAuxiliary ) {
235+ WebsocketIncoming . authedClients . splice ( index , 1 ) ;
236+ }
237+ }
238+ } ) ;
142239 } ) ;
143240
144241 serverInstance . listen ( 5100 ) ;
@@ -156,6 +253,20 @@ export class WebsocketIncoming {
156253 Log . error ( `Error parsing obs_data: ${ e } ` ) ;
157254 }
158255 } ) ;
256+
257+ user . ws . on ( "aux_data" , async ( msg : any ) => {
258+ try {
259+ const data = JSON . parse ( msg . toString ( ) ) ;
260+ if ( isAuthedAuxData ( data ) ) {
261+ await this . matchController . receiveMatchData ( data ) ;
262+ if ( data . type === DataTypes . AUX_SCOREBOARD && user . playerId === "" ) {
263+ user . playerId = data . playerId ;
264+ }
265+ }
266+ } catch ( e ) {
267+ Log . error ( `Error parsing aux_data: ${ e } ` ) ;
268+ }
269+ } ) ;
159270 }
160271
161272 private async isValidKey ( key : string ) : Promise < KeyValidity > {
@@ -184,6 +295,8 @@ export class WebsocketIncoming {
184295class ClientUser {
185296 name : string ;
186297 groupCode : string ;
298+ isAuxiliary : boolean = false ;
299+ playerId : string = "" ;
187300 ws : Socket ;
188301
189302 constructor ( name : string , groupCode : string , ws : Socket ) {
0 commit comments