token-validator: save ping state (0: ping ok, 1: arping only)

This commit is contained in:
nemunaire 2020-02-21 05:50:38 +01:00
parent 22a3d75645
commit c35a8f5e6b
3 changed files with 16 additions and 9 deletions

View File

@ -31,7 +31,7 @@ do
1)
PAT=baaaaaad;;
2)
PAT=baadfood;;
PAT=baadf00d;;
3)
PAT=baddcafe;;
4)
@ -41,8 +41,9 @@ do
esac
arping -I br-ext -c 1 -w 1 "${IP}" && {
ping -c 1 -w 1 -p "${PAT}" "${IP}" &
curl -k -X POST -d '{"token": "'${SECRET_KEY}'"}' "https://172.23.200.1/api/students/$sid/pong"
ping -c 1 -w 1 -p "${PAT}" "${IP}"
STATE=$?
curl -k -X POST -d '{"challenge": '${STATE}', "token": "'${SECRET_KEY}'"}' "https://172.23.200.1/api/students/$sid/pong"
}
fi
done

View File

@ -100,6 +100,7 @@ CREATE TABLE IF NOT EXISTS student_challenges(
CREATE TABLE IF NOT EXISTS student_pong(
id_student INTEGER NOT NULL,
time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
state BOOLEAN NOT NULL DEFAULT 0,
FOREIGN KEY(id_student) REFERENCES students(id_student)
) DEFAULT CHARACTER SET = utf8 COLLATE = utf8_bin;
`); err != nil {

View File

@ -26,18 +26,23 @@ func lastPing(student Student, body []byte) (interface{}, error) {
}
}
func (s Student) lastPongs() (pongs []time.Time, err error) {
if rows, errr := DBQuery("SELECT time FROM student_pong WHERE id_student = ? ORDER BY time DESC", s.Id); errr != nil {
type Pong struct {
Date time.Time
State bool
}
func (s Student) lastPongs() (pongs []Pong, err error) {
if rows, errr := DBQuery("SELECT time, state FROM student_pong WHERE id_student = ? ORDER BY time DESC", s.Id); errr != nil {
return nil, errr
} else {
defer rows.Close()
for rows.Next() {
var t time.Time
if err = rows.Scan(&t); err != nil {
var p Pong
if err = rows.Scan(&p.Date, &p.State); err != nil {
return
}
pongs = append(pongs, t)
pongs = append(pongs, p)
}
if err = rows.Err(); err != nil {
return
@ -58,7 +63,7 @@ func stdPong(student Student, body []byte) (interface{}, error) {
}
if res, err := DBExec("INSERT INTO student_pong (id_student, time) VALUES (?, ?)", student.Id, time.Now()); err != nil {
if res, err := DBExec("INSERT INTO student_pong (id_student, time, state) VALUES (?, ?, ?)", student.Id, time.Now(), gt.Challenge == 0); err != nil {
return false, err
} else if _, err := res.LastInsertId(); err != nil {
return false, err