2019-02-26 23:19:17 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2019-02-27 04:49:08 +00:00
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
2020-03-27 13:57:14 +00:00
|
|
|
|
2021-10-31 15:43:43 +00:00
|
|
|
"git.nemunai.re/srs/adlin/libadlin"
|
2019-02-26 23:19:17 +00:00
|
|
|
)
|
|
|
|
|
2019-02-27 04:49:08 +00:00
|
|
|
var PongSecret = "felixfixit"
|
|
|
|
|
2019-02-26 23:19:17 +00:00
|
|
|
func init() {
|
2019-03-26 12:15:36 +00:00
|
|
|
router.GET("/api/students/:sid/ping", apiHandler(studentHandler(lastPing)))
|
2021-03-07 11:39:38 +00:00
|
|
|
router.GET("/api/students/:sid/pong", apiHandler(studentHandler(func(student *adlin.Student, body []byte) (interface{}, error) {
|
2020-03-27 13:57:14 +00:00
|
|
|
return student.LastPongs()
|
2019-02-27 04:49:08 +00:00
|
|
|
})))
|
2019-02-26 23:19:17 +00:00
|
|
|
router.POST("/api/students/:sid/pong", apiHandler(studentHandler(stdPong), sslOnly))
|
|
|
|
}
|
|
|
|
|
2021-03-07 11:39:38 +00:00
|
|
|
func lastPing(student *adlin.Student, body []byte) (interface{}, error) {
|
2020-03-27 13:57:14 +00:00
|
|
|
if pongs, err := student.LastPongs(); err != nil {
|
2019-02-27 04:49:08 +00:00
|
|
|
return nil, err
|
|
|
|
} else if len(pongs) <= 0 {
|
|
|
|
return false, nil
|
|
|
|
} else {
|
2019-03-26 12:15:36 +00:00
|
|
|
return pongs[0], nil
|
2019-02-27 04:49:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-07 11:39:38 +00:00
|
|
|
func stdPong(student *adlin.Student, body []byte) (interface{}, error) {
|
2019-02-27 04:49:08 +00:00
|
|
|
var gt givenToken
|
|
|
|
if err := json.Unmarshal(body, >); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if gt.Token != PongSecret {
|
|
|
|
return nil, errors.New("This is not the expected token.")
|
|
|
|
}
|
|
|
|
|
2020-03-27 13:57:14 +00:00
|
|
|
return true, student.OnPong(gt.Challenge == 0)
|
2019-02-26 23:19:17 +00:00
|
|
|
}
|