2018-05-02 15:45:45 -06:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2018-09-17 17:00:08 -06:00
|
|
|
"bufio"
|
2018-09-24 14:00:18 -06:00
|
|
|
"encoding/json"
|
2018-05-02 15:45:45 -06:00
|
|
|
"fmt"
|
2018-09-17 18:02:44 -06:00
|
|
|
"io"
|
2018-09-17 17:00:08 -06:00
|
|
|
"log"
|
2018-05-02 15:45:45 -06:00
|
|
|
"net/http"
|
2018-05-04 17:20:51 -06:00
|
|
|
"os"
|
2019-10-25 16:53:40 -06:00
|
|
|
"path"
|
2019-10-21 08:36:20 -06:00
|
|
|
"path/filepath"
|
2019-10-25 16:53:40 -06:00
|
|
|
"regexp"
|
2018-05-04 17:20:51 -06:00
|
|
|
"strconv"
|
2018-09-17 17:00:08 -06:00
|
|
|
"strings"
|
2018-05-02 15:45:45 -06:00
|
|
|
)
|
|
|
|
|
2019-02-22 17:43:04 -07:00
|
|
|
// https://github.com/omniti-labs/jsend
|
2018-09-24 14:00:18 -06:00
|
|
|
type JSend struct {
|
2019-02-25 09:07:53 -07:00
|
|
|
Status string `json:"status"`
|
2019-02-22 17:43:04 -07:00
|
|
|
Data struct {
|
|
|
|
Short string `json:"short"`
|
|
|
|
Description string `json:"description"`
|
2019-02-25 09:07:53 -07:00
|
|
|
} `json:"data"`
|
2018-09-24 14:00:18 -06:00
|
|
|
}
|
2019-02-21 16:00:06 -07:00
|
|
|
|
|
|
|
const (
|
2019-02-22 17:43:04 -07:00
|
|
|
JSendSuccess = "success"
|
2019-02-25 09:07:53 -07:00
|
|
|
JSendFail = "fail"
|
|
|
|
JSendError = "error"
|
2019-02-21 16:00:06 -07:00
|
|
|
)
|
2018-09-24 14:00:18 -06:00
|
|
|
|
2019-02-22 17:43:04 -07:00
|
|
|
func respond(w http.ResponseWriter, req *http.Request, status string, short string, format string, a ...interface{}) {
|
|
|
|
resp := JSend{}
|
|
|
|
resp.Status = status
|
|
|
|
resp.Data.Short = short
|
|
|
|
resp.Data.Description = fmt.Sprintf(format, a...)
|
2018-09-24 14:00:18 -06:00
|
|
|
|
|
|
|
respBytes, err := json.Marshal(resp)
|
2018-09-27 17:12:29 -06:00
|
|
|
if err != nil {
|
2018-09-24 14:00:18 -06:00
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2018-09-27 17:12:29 -06:00
|
|
|
|
2018-09-24 14:00:18 -06:00
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
w.WriteHeader(http.StatusOK) // RFC2616 makes it pretty clear that 4xx codes are for the user-agent
|
|
|
|
w.Write(respBytes)
|
|
|
|
}
|
|
|
|
|
2018-09-17 21:32:24 -06:00
|
|
|
// hasLine returns true if line appears in r.
|
|
|
|
// The entire line must match.
|
|
|
|
func hasLine(r io.Reader, line string) bool {
|
2018-09-17 18:02:44 -06:00
|
|
|
scanner := bufio.NewScanner(r)
|
|
|
|
for scanner.Scan() {
|
2018-09-17 21:32:24 -06:00
|
|
|
if scanner.Text() == line {
|
2018-09-17 18:02:44 -06:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2018-09-19 21:44:34 -06:00
|
|
|
func (ctx *Instance) registerHandler(w http.ResponseWriter, req *http.Request) {
|
2019-03-07 20:03:48 -07:00
|
|
|
teamName := req.FormValue("name")
|
|
|
|
teamId := req.FormValue("id")
|
2018-09-17 17:00:08 -06:00
|
|
|
|
2019-03-07 20:03:48 -07:00
|
|
|
if !ctx.ValidTeamId(teamId) {
|
2018-09-17 17:00:08 -06:00
|
|
|
respond(
|
2019-02-22 17:43:04 -07:00
|
|
|
w, req, JSendFail,
|
2018-09-17 17:00:08 -06:00
|
|
|
"Invalid Team ID",
|
|
|
|
"I don't have a record of that team ID. Maybe you used capital letters accidentally?",
|
|
|
|
)
|
2018-05-04 17:20:51 -06:00
|
|
|
return
|
|
|
|
}
|
2018-09-17 17:00:08 -06:00
|
|
|
|
2019-03-07 20:03:48 -07:00
|
|
|
f, err := os.OpenFile(ctx.StatePath("teams", teamId), os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0644)
|
|
|
|
if err != nil {
|
|
|
|
if os.IsExist(err) {
|
|
|
|
respond(
|
|
|
|
w, req, JSendFail,
|
|
|
|
"Already registered",
|
|
|
|
"This team ID has already been registered.",
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
log.Print(err)
|
|
|
|
respond(
|
|
|
|
w, req, JSendFail,
|
|
|
|
"Registration failed",
|
|
|
|
"Unable to register. Perhaps a teammate has already registered?",
|
|
|
|
)
|
|
|
|
}
|
2018-05-04 17:20:51 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
defer f.Close()
|
2019-03-07 20:03:48 -07:00
|
|
|
|
|
|
|
fmt.Fprintln(f, teamName)
|
2018-09-17 17:00:08 -06:00
|
|
|
respond(
|
2019-02-22 17:43:04 -07:00
|
|
|
w, req, JSendSuccess,
|
2018-09-17 17:00:08 -06:00
|
|
|
"Team registered",
|
2019-03-07 20:03:48 -07:00
|
|
|
"Your team has been named and you may begin using your team ID!",
|
2018-09-17 17:00:08 -06:00
|
|
|
)
|
2020-11-11 13:13:09 -07:00
|
|
|
ctx.LogEvent("register", req.FormValue("pid"), teamId, "", 0)
|
2018-05-04 17:20:51 -06:00
|
|
|
}
|
|
|
|
|
2018-09-19 21:44:34 -06:00
|
|
|
func (ctx *Instance) answerHandler(w http.ResponseWriter, req *http.Request) {
|
2019-03-07 20:03:48 -07:00
|
|
|
teamId := req.FormValue("id")
|
2018-09-17 17:40:05 -06:00
|
|
|
category := req.FormValue("cat")
|
|
|
|
pointstr := req.FormValue("points")
|
|
|
|
answer := req.FormValue("answer")
|
2018-05-04 17:20:51 -06:00
|
|
|
|
2019-11-13 13:47:56 -07:00
|
|
|
if !ctx.ValidTeamId(teamId) {
|
2019-03-07 20:03:48 -07:00
|
|
|
respond(
|
|
|
|
w, req, JSendFail,
|
|
|
|
"Invalid team ID",
|
|
|
|
"That team ID is not valid for this event.",
|
|
|
|
)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if ctx.TooFast(teamId) {
|
|
|
|
respond(
|
|
|
|
w, req, JSendFail,
|
|
|
|
"Submitting too quickly",
|
|
|
|
"Your team can only submit one answer every %v", ctx.AttemptInterval,
|
|
|
|
)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-05-04 17:20:51 -06:00
|
|
|
points, err := strconv.Atoi(pointstr)
|
|
|
|
if err != nil {
|
2018-09-17 17:00:08 -06:00
|
|
|
respond(
|
2019-02-22 17:43:04 -07:00
|
|
|
w, req, JSendFail,
|
2018-09-17 17:40:05 -06:00
|
|
|
"Cannot parse point value",
|
|
|
|
"This doesn't look like an integer: %s", pointstr,
|
2018-09-17 17:00:08 -06:00
|
|
|
)
|
2018-09-14 18:24:48 -06:00
|
|
|
return
|
2018-05-04 17:20:51 -06:00
|
|
|
}
|
|
|
|
|
2018-09-17 17:40:05 -06:00
|
|
|
haystack, err := ctx.OpenCategoryFile(category, "answers.txt")
|
2018-09-14 18:24:48 -06:00
|
|
|
if err != nil {
|
2018-09-17 17:00:08 -06:00
|
|
|
respond(
|
2019-02-22 17:43:04 -07:00
|
|
|
w, req, JSendFail,
|
2018-09-17 17:40:05 -06:00
|
|
|
"Cannot list answers",
|
|
|
|
"Unable to read the list of answers for this category.",
|
2018-09-17 17:00:08 -06:00
|
|
|
)
|
2018-09-14 18:24:48 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
defer haystack.Close()
|
2018-09-17 17:00:08 -06:00
|
|
|
|
2018-09-14 18:24:48 -06:00
|
|
|
// Look for the answer
|
|
|
|
needle := fmt.Sprintf("%d %s", points, answer)
|
2018-09-17 21:32:24 -06:00
|
|
|
if !hasLine(haystack, needle) {
|
2018-09-17 17:00:08 -06:00
|
|
|
respond(
|
2019-02-22 17:43:04 -07:00
|
|
|
w, req, JSendFail,
|
2018-09-17 17:00:08 -06:00
|
|
|
"Wrong answer",
|
2018-09-17 17:40:05 -06:00
|
|
|
"That is not the correct answer for %s %d.", category, points,
|
2018-09-17 17:00:08 -06:00
|
|
|
)
|
2020-11-11 13:13:09 -07:00
|
|
|
ctx.LogEvent("wrong", req.FormValue("pid"), teamId, category, points)
|
2018-09-14 18:24:48 -06:00
|
|
|
return
|
2018-05-04 17:20:51 -06:00
|
|
|
}
|
|
|
|
|
2019-03-07 20:03:48 -07:00
|
|
|
if err := ctx.AwardPoints(teamId, category, points); err != nil {
|
2018-09-17 17:00:08 -06:00
|
|
|
respond(
|
2019-02-22 17:43:04 -07:00
|
|
|
w, req, JSendError,
|
2018-09-20 14:44:56 -06:00
|
|
|
"Cannot award points",
|
|
|
|
"The answer is correct, but there was an error awarding points: %v", err.Error(),
|
2018-09-17 17:00:08 -06:00
|
|
|
)
|
2018-05-04 17:20:51 -06:00
|
|
|
return
|
|
|
|
}
|
2018-09-17 17:00:08 -06:00
|
|
|
respond(
|
2019-02-22 17:43:04 -07:00
|
|
|
w, req, JSendSuccess,
|
2018-09-17 17:00:08 -06:00
|
|
|
"Points awarded",
|
2019-03-07 20:03:48 -07:00
|
|
|
fmt.Sprintf("%d points for %s!", points, teamId),
|
2018-09-17 17:00:08 -06:00
|
|
|
)
|
2020-11-11 13:13:09 -07:00
|
|
|
ctx.LogEvent("correct", req.FormValue("pid"), teamId, category, points)
|
2018-09-17 17:00:08 -06:00
|
|
|
}
|
|
|
|
|
2018-09-19 21:44:34 -06:00
|
|
|
func (ctx *Instance) puzzlesHandler(w http.ResponseWriter, req *http.Request) {
|
2019-03-07 20:03:48 -07:00
|
|
|
teamId := req.FormValue("id")
|
|
|
|
if _, err := ctx.TeamName(teamId); err != nil {
|
|
|
|
http.Error(w, "Must provide team ID", http.StatusUnauthorized)
|
2019-02-22 19:09:38 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-09-17 17:00:08 -06:00
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
w.WriteHeader(http.StatusOK)
|
2018-09-19 21:44:34 -06:00
|
|
|
w.Write(ctx.jPuzzleList)
|
2018-05-08 12:45:50 -06:00
|
|
|
}
|
|
|
|
|
2018-09-19 21:44:34 -06:00
|
|
|
func (ctx *Instance) pointsHandler(w http.ResponseWriter, req *http.Request) {
|
2019-11-19 19:16:21 -07:00
|
|
|
teamId, ok := req.URL.Query()["id"]
|
|
|
|
pointsLog := ctx.jPointsLog
|
|
|
|
if ok && len(teamId[0]) > 0 {
|
|
|
|
pointsLog = ctx.generatePointsLog(teamId[0])
|
|
|
|
}
|
2018-09-17 17:00:08 -06:00
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
w.WriteHeader(http.StatusOK)
|
2019-11-19 19:16:21 -07:00
|
|
|
w.Write(pointsLog)
|
2018-09-17 17:00:08 -06:00
|
|
|
}
|
|
|
|
|
2018-09-19 21:44:34 -06:00
|
|
|
func (ctx *Instance) contentHandler(w http.ResponseWriter, req *http.Request) {
|
2018-09-19 17:56:26 -06:00
|
|
|
// Prevent directory traversal
|
|
|
|
if strings.Contains(req.URL.Path, "/.") {
|
|
|
|
http.Error(w, "Not Found", http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
2018-09-19 21:44:34 -06:00
|
|
|
|
2018-09-19 17:56:26 -06:00
|
|
|
// Be clever: use only the last three parts of the path. This may prove to be a bad idea.
|
|
|
|
parts := strings.Split(req.URL.Path, "/")
|
|
|
|
if len(parts) < 3 {
|
|
|
|
http.Error(w, "Not Found", http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
2018-09-19 21:44:34 -06:00
|
|
|
|
2018-09-19 17:56:26 -06:00
|
|
|
fileName := parts[len(parts)-1]
|
|
|
|
puzzleId := parts[len(parts)-2]
|
|
|
|
categoryName := parts[len(parts)-3]
|
2018-09-19 21:44:34 -06:00
|
|
|
|
2019-03-07 20:03:48 -07:00
|
|
|
mb, ok := ctx.categories[categoryName]
|
2018-09-19 17:56:26 -06:00
|
|
|
if !ok {
|
|
|
|
http.Error(w, "Not Found", http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
2018-09-19 21:44:34 -06:00
|
|
|
|
2018-09-19 17:56:26 -06:00
|
|
|
mbFilename := fmt.Sprintf("content/%s/%s", puzzleId, fileName)
|
|
|
|
mf, err := mb.Open(mbFilename)
|
|
|
|
if err != nil {
|
|
|
|
log.Print(err)
|
|
|
|
http.Error(w, "Not Found", http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer mf.Close()
|
2018-09-19 21:44:34 -06:00
|
|
|
|
2018-09-19 17:56:26 -06:00
|
|
|
http.ServeContent(w, req, fileName, mf.ModTime(), mf)
|
|
|
|
}
|
|
|
|
|
2018-09-19 21:44:34 -06:00
|
|
|
func (ctx *Instance) staticHandler(w http.ResponseWriter, req *http.Request) {
|
2018-09-27 17:12:29 -06:00
|
|
|
path := req.URL.Path
|
|
|
|
if strings.Contains(path, "..") {
|
|
|
|
http.Error(w, "Invalid URL path", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if path == "/" {
|
|
|
|
path = "/index.html"
|
|
|
|
}
|
|
|
|
|
2019-03-07 20:03:48 -07:00
|
|
|
f, err := os.Open(ctx.ThemePath(path))
|
2018-09-27 17:12:29 -06:00
|
|
|
if err != nil {
|
|
|
|
http.NotFound(w, req)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
d, err := f.Stat()
|
|
|
|
if err != nil {
|
|
|
|
http.NotFound(w, req)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
http.ServeContent(w, req, path, d.ModTime(), f)
|
2018-05-06 21:37:52 -06:00
|
|
|
}
|
2018-09-14 18:24:48 -06:00
|
|
|
|
2019-10-28 11:38:27 -06:00
|
|
|
func (ctx *Instance) manifestHandler(w http.ResponseWriter, req *http.Request) {
|
2019-11-13 13:47:56 -07:00
|
|
|
if !ctx.Runtime.export_manifest {
|
2019-10-28 11:38:27 -06:00
|
|
|
http.Error(w, "Endpoint disabled", http.StatusForbidden)
|
2019-10-21 08:36:20 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-10-28 11:38:27 -06:00
|
|
|
teamId := req.FormValue("id")
|
|
|
|
if _, err := ctx.TeamName(teamId); err != nil {
|
|
|
|
http.Error(w, "Must provide a valid team ID", http.StatusUnauthorized)
|
|
|
|
return
|
2019-10-21 08:36:20 -06:00
|
|
|
}
|
|
|
|
|
2019-11-13 13:47:56 -07:00
|
|
|
if req.Method == http.MethodHead {
|
2019-10-28 11:38:27 -06:00
|
|
|
w.WriteHeader(http.StatusOK)
|
2019-10-28 09:50:49 -06:00
|
|
|
return
|
|
|
|
}
|
2019-10-28 11:38:27 -06:00
|
|
|
|
2019-10-25 16:53:40 -06:00
|
|
|
manifest := make([]string, 0)
|
|
|
|
manifest = append(manifest, "puzzles.json")
|
|
|
|
manifest = append(manifest, "points.json")
|
|
|
|
|
|
|
|
// Pack up the theme files
|
|
|
|
theme_root_re := regexp.MustCompile(fmt.Sprintf("^%s/", ctx.ThemeDir))
|
2019-11-13 13:47:56 -07:00
|
|
|
filepath.Walk(ctx.ThemeDir, func(path string, info os.FileInfo, err error) error {
|
2019-10-25 16:53:40 -06:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-11-13 13:47:56 -07:00
|
|
|
if !info.IsDir() { // Only package up files
|
|
|
|
localized_path := theme_root_re.ReplaceAllLiteralString(path, "")
|
2019-10-25 16:53:40 -06:00
|
|
|
manifest = append(manifest, localized_path)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
// Package up files for currently-unlocked puzzles in categories
|
|
|
|
for category_name, category := range ctx.categories {
|
2019-11-13 13:47:56 -07:00
|
|
|
if _, ok := ctx.MaxPointsUnlocked[category_name]; ok { // Check that the category is actually unlocked. This should never fail, probably
|
2019-11-07 12:22:15 -07:00
|
|
|
for _, file := range category.zf.File {
|
|
|
|
parts := strings.Split(file.Name, "/")
|
|
|
|
|
2019-11-13 13:47:56 -07:00
|
|
|
if parts[0] == "content" { // Only pick up content files, not thing like map.txt
|
|
|
|
for _, puzzlemap := range category.puzzlemap { // Figure out which puzzles are currently unlocked
|
|
|
|
if puzzlemap.Path == parts[1] && puzzlemap.Points <= ctx.MaxPointsUnlocked[category_name] {
|
2019-11-07 12:22:15 -07:00
|
|
|
|
|
|
|
manifest = append(manifest, path.Join("content", category_name, path.Join(parts[1:]...)))
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-10-25 16:53:40 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
manifest_json, _ := json.Marshal(manifest)
|
|
|
|
w.Write(manifest_json)
|
|
|
|
}
|
|
|
|
|
2019-02-21 16:00:06 -07:00
|
|
|
type FurtiveResponseWriter struct {
|
2019-02-25 09:07:53 -07:00
|
|
|
w http.ResponseWriter
|
2019-02-21 16:00:06 -07:00
|
|
|
statusCode *int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w FurtiveResponseWriter) WriteHeader(statusCode int) {
|
|
|
|
*w.statusCode = statusCode
|
|
|
|
w.w.WriteHeader(statusCode)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w FurtiveResponseWriter) Write(buf []byte) (n int, err error) {
|
|
|
|
n, err = w.w.Write(buf)
|
|
|
|
return
|
2018-09-14 18:24:48 -06:00
|
|
|
}
|
2019-02-21 16:00:06 -07:00
|
|
|
|
|
|
|
func (w FurtiveResponseWriter) Header() http.Header {
|
|
|
|
return w.w.Header()
|
|
|
|
}
|
|
|
|
|
|
|
|
// This gives Instances the signature of http.Handler
|
|
|
|
func (ctx *Instance) ServeHTTP(wOrig http.ResponseWriter, r *http.Request) {
|
|
|
|
w := FurtiveResponseWriter{
|
2019-02-25 09:07:53 -07:00
|
|
|
w: wOrig,
|
2019-02-21 16:00:06 -07:00
|
|
|
statusCode: new(int),
|
|
|
|
}
|
2020-03-04 19:19:46 -07:00
|
|
|
|
2020-03-04 20:22:34 -07:00
|
|
|
clientIP := r.RemoteAddr
|
2020-03-04 19:19:46 -07:00
|
|
|
|
2020-11-11 13:13:09 -07:00
|
|
|
if ctx.UseXForwarded {
|
2020-03-04 20:22:34 -07:00
|
|
|
forwardedIP := r.Header.Get("X-Forwarded-For")
|
|
|
|
forwardedIP = strings.Split(forwardedIP, ", ")[0]
|
|
|
|
|
|
|
|
if forwardedIP != "" {
|
|
|
|
clientIP = forwardedIP
|
|
|
|
}
|
2020-03-04 19:19:46 -07:00
|
|
|
}
|
|
|
|
|
2019-02-22 19:09:38 -07:00
|
|
|
ctx.mux.ServeHTTP(w, r)
|
2019-02-21 16:00:06 -07:00
|
|
|
log.Printf(
|
|
|
|
"%s %s %s %d\n",
|
2020-03-04 19:19:46 -07:00
|
|
|
clientIP,
|
2019-02-21 16:00:06 -07:00
|
|
|
r.Method,
|
|
|
|
r.URL,
|
|
|
|
*w.statusCode,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *Instance) BindHandlers() {
|
|
|
|
ctx.mux.HandleFunc(ctx.Base+"/", ctx.staticHandler)
|
|
|
|
ctx.mux.HandleFunc(ctx.Base+"/register", ctx.registerHandler)
|
|
|
|
ctx.mux.HandleFunc(ctx.Base+"/answer", ctx.answerHandler)
|
|
|
|
ctx.mux.HandleFunc(ctx.Base+"/content/", ctx.contentHandler)
|
|
|
|
ctx.mux.HandleFunc(ctx.Base+"/puzzles.json", ctx.puzzlesHandler)
|
|
|
|
ctx.mux.HandleFunc(ctx.Base+"/points.json", ctx.pointsHandler)
|
2019-10-28 11:38:27 -06:00
|
|
|
ctx.mux.HandleFunc(ctx.Base+"/current_manifest.json", ctx.manifestHandler)
|
2019-02-21 16:00:06 -07:00
|
|
|
}
|