@@ -75,7 +75,7 @@ class GameServerQuery {
75
75
}
76
76
77
77
// If the server replied with a challenge, grab challenge number and send request again
78
- if ( resultBuffer . compare ( Buffer . from ( [ 0xFF , 0xFF , 0xFF , 0xFF , 0x41 ] ) , 0 , 5 , 0 , 5 ) === 0 ) {
78
+ if ( this . _isChallengeResponse ( resultBuffer ) ) {
79
79
resultBuffer = resultBuffer . slice ( 5 ) ;
80
80
const challenge = resultBuffer ;
81
81
try {
@@ -93,26 +93,40 @@ class GameServerQuery {
93
93
}
94
94
95
95
public async player ( ) : Promise < PlayerResponse > {
96
- let challengeResultBuffer : Buffer ;
97
- try {
98
- challengeResultBuffer = await this . _promiseSocket . send ( this . _buildPacket ( Buffer . from ( [ 0x55 ] ) ) , this . _host , this . _port ) ;
99
- } catch ( err : any ) {
100
- this . _promiseSocket . closeSocket ( ) ;
101
- throw new Error ( err ) ;
102
- }
96
+ let resultBuffer : Buffer ;
97
+ let gotPlayerResponse = false ;
98
+ let challengeTries = 0 ;
103
99
104
- const challenge = challengeResultBuffer . slice ( 5 ) ;
100
+ do {
101
+ let challengeResultBuffer : Buffer ;
102
+ try {
103
+ challengeResultBuffer = await this . _promiseSocket . send ( this . _buildPacket ( Buffer . from ( [ 0x55 ] ) ) , this . _host , this . _port ) ;
104
+ } catch ( err : any ) {
105
+ this . _promiseSocket . closeSocket ( ) ;
106
+ throw new Error ( err ) ;
107
+ }
105
108
106
- let resultBuffer : Buffer ;
107
- try {
108
- resultBuffer = await this . _promiseSocket . send ( this . _buildPacket ( Buffer . from ( [ 0x55 ] ) , challenge ) , this . _host , this . _port ) ;
109
- } catch ( err : any ) {
110
- this . _promiseSocket . closeSocket ( ) ;
111
- throw new Error ( err ) ;
112
- }
109
+ const challenge = challengeResultBuffer . slice ( 5 ) ;
110
+ try {
111
+ resultBuffer = await this . _promiseSocket . send ( this . _buildPacket ( Buffer . from ( [ 0x55 ] ) , challenge ) , this . _host , this . _port ) ;
112
+ } catch ( err : any ) {
113
+ this . _promiseSocket . closeSocket ( ) ;
114
+ throw new Error ( err ) ;
115
+ }
116
+
117
+ if ( ! this . _isChallengeResponse ( resultBuffer ) ) {
118
+ gotPlayerResponse = true ;
119
+ }
120
+
121
+ challengeTries ++ ;
122
+ } while ( ! gotPlayerResponse && challengeTries < 5 ) ;
113
123
114
124
this . _promiseSocket . closeSocket ( ) ;
115
125
126
+ if ( this . _isChallengeResponse ( resultBuffer ) ) {
127
+ throw new Error ( 'Server kept sending challenge responses.' ) ;
128
+ }
129
+
116
130
const parsedPlayerBuffer = this . _parsePlayerBuffer ( resultBuffer ) ;
117
131
return parsedPlayerBuffer ;
118
132
}
@@ -177,6 +191,10 @@ class GameServerQuery {
177
191
return packet ;
178
192
}
179
193
194
+ private _isChallengeResponse ( buffer : Buffer ) {
195
+ return buffer . compare ( Buffer . from ( [ 0xFF , 0xFF , 0xFF , 0xFF , 0x41 ] ) , 0 , 5 , 0 , 5 ) === 0 ;
196
+ }
197
+
180
198
private _parseInfoBuffer ( buffer : Buffer ) : InfoResponse {
181
199
const infoResponse : Partial < InfoResponse > = { } ;
182
200
buffer = buffer . slice ( 5 ) ;
0 commit comments