@@ -193,36 +193,36 @@ func (d *data) flip() {
193
193
}
194
194
}
195
195
196
+ func writeResponse (w http.ResponseWriter , code int , body string , format string , args ... any ) {
197
+ fmt .Printf (format , args ... )
198
+ w .WriteHeader (code )
199
+ if _ , err := io .WriteString (w , body ); err != nil {
200
+ fmt .Println ("WriteString failed:" , err )
201
+ }
202
+ }
203
+
196
204
func (p Puzzle ) handle (w http.ResponseWriter , req * http.Request , tmpl * template.Template ) {
197
205
d , err := p .dataFromReq (req )
198
206
fmt .Printf ("IP %s index %d\n " , d .IP , d .Index )
199
207
if err != nil {
200
- fmt .Println ("dataFromReq failed:" , err )
201
- w .WriteHeader (http .StatusInternalServerError )
202
- io .WriteString (w , errorPage )
208
+ writeResponse (w , http .StatusInternalServerError , errorPage , "dataFromReq failed: %v" , err )
203
209
return
204
210
}
205
211
206
212
if p .Client != nil {
207
213
resp , err := p .Client .Query (d .IP )
208
214
if err != nil {
209
215
if err == vpnapi .ErrRateLimited {
210
- fmt .Println ("rate limited:" , err )
211
- w .WriteHeader (http .StatusInternalServerError )
212
- io .WriteString (w , rateLimitPage )
216
+ writeResponse (w , http .StatusTooManyRequests , rateLimitPage , "rate limited: %v" , err )
213
217
return
214
218
}
215
219
// TODO: This fails closed. This may be too strict, especially if vpnapi.io is unreliable.
216
- fmt .Println ("Query failed:" , err )
217
- w .WriteHeader (http .StatusInternalServerError )
218
- io .WriteString (w , errorPage )
220
+ writeResponse (w , http .StatusInternalServerError , errorPage , "Query failed: %v" , err )
219
221
return
220
222
}
221
223
222
224
if resp .Security .VPN || resp .Security .Proxy || resp .Security .Tor || resp .Security .Relay {
223
- fmt .Printf ("%t %t %t %t\n " , resp .Security .VPN , resp .Security .Proxy , resp .Security .Tor , resp .Security .Relay )
224
- w .WriteHeader (http .StatusForbidden )
225
- io .WriteString (w , vpnPage )
225
+ writeResponse (w , http .StatusForbidden , vpnPage , "%t %t %t %t\n " , resp .Security .VPN , resp .Security .Proxy , resp .Security .Tor , resp .Security .Relay )
226
226
return
227
227
}
228
228
}
0 commit comments