server/backend/choices.go

52 lines
1.6 KiB
Go
Raw Normal View History

2018-12-02 22:18:32 +00:00
package main
import (
"encoding/base64"
"encoding/binary"
2018-12-02 22:18:32 +00:00
"encoding/json"
"io/ioutil"
2019-07-11 17:52:13 +00:00
"log"
"math/rand"
2018-12-02 22:18:32 +00:00
"os"
"srs.epita.fr/fic-server/libfic"
)
type wantChoices struct {
FlagId int `json:"id"`
2018-12-02 22:18:32 +00:00
}
2021-11-22 14:35:07 +00:00
func treatWantChoices(pathname string, team *fic.Team) {
// Generate a unique identifier to follow the request in logs
bid := make([]byte, 5)
binary.LittleEndian.PutUint32(bid, rand.Uint32())
id := "[" + base64.StdEncoding.EncodeToString(bid) + "]"
log.Println(id, "New wantChoices receive", pathname)
2018-12-02 22:18:32 +00:00
var ask wantChoices
if cnt_raw, err := ioutil.ReadFile(pathname); err != nil {
log.Printf("%s [ERR] %s\n", id, err)
2019-01-17 15:55:54 +00:00
} else if err = json.Unmarshal(cnt_raw, &ask); err != nil {
log.Printf("%s [ERR] %s\n", id, err)
2018-12-02 22:18:32 +00:00
} else if ask.FlagId == 0 {
log.Printf("%s [WRN] Invalid content in wantChoices file: %s\n", id, pathname)
2018-12-02 22:18:32 +00:00
os.Remove(pathname)
} else if flag, err := fic.GetFlagKey(ask.FlagId); err != nil {
log.Printf("%s [ERR] %s\n", id, err)
} else if !team.CanSeeFlag(flag) {
log.Printf("%s [!!!] The team asks to display choices whereas it doesn't have access to the flag\n", id)
} else if exercice, err := flag.GetExercice(); err != nil {
log.Printf("%s [ERR] Unable to retrieve the flag's underlying exercice: %s\n", id, err)
} else if !team.HasAccess(exercice) {
log.Printf("%s [!!!] The team asks to display choices whereas it doesn't have access to the exercice\n", id)
2019-01-17 15:55:54 +00:00
} else if err = team.DisplayChoices(flag); err != nil {
log.Printf("%s [ERR] %s\n", id, err)
2018-12-02 22:18:32 +00:00
} else {
2021-11-22 14:35:07 +00:00
genTeamQueue <- team
2019-01-17 15:55:54 +00:00
if err = os.Remove(pathname); err != nil {
log.Printf("%s [ERR] %s\n", id, err)
2018-12-02 22:18:32 +00:00
}
}
}