Retrieve time through X-FIC-Time header instead of time.json
This commit is contained in:
parent
c33390fa80
commit
6034246015
11 changed files with 107 additions and 131 deletions
|
@ -12,7 +12,6 @@ import (
|
|||
"strings"
|
||||
"syscall"
|
||||
|
||||
fronttime "srs.epita.fr/fic-server/frontend/time"
|
||||
"srs.epita.fr/fic-server/settings"
|
||||
)
|
||||
|
||||
|
@ -51,7 +50,6 @@ func main() {
|
|||
http.Handle(fmt.Sprintf("%s/registration/", *prefix), http.StripPrefix(fmt.Sprintf("%s/registration/", *prefix), submissionChecker{"registration", RegistrationHandler}))
|
||||
http.Handle(fmt.Sprintf("%s/resolution/", *prefix), http.StripPrefix(fmt.Sprintf("%s/resolution/", *prefix), ResolutionHandler{}))
|
||||
http.Handle(fmt.Sprintf("%s/submission/", *prefix), http.StripPrefix(fmt.Sprintf("%s/submission/", *prefix), submissionTeamChecker{"submission", SubmissionHandler, *teamsDir}))
|
||||
http.Handle(fmt.Sprintf("%s/time.json", *prefix), http.StripPrefix(*prefix, fronttime.TimeHandler{}))
|
||||
|
||||
if *simulator != "" {
|
||||
if _, err := os.Stat(path.Join(*teamsDir, *simulator)); os.IsNotExist(err) {
|
||||
|
|
|
@ -5,13 +5,14 @@ import (
|
|||
"os"
|
||||
"time"
|
||||
|
||||
fronttime "srs.epita.fr/fic-server/frontend/time"
|
||||
"srs.epita.fr/fic-server/settings"
|
||||
)
|
||||
|
||||
var startedFile = "started"
|
||||
|
||||
var touchTimer *time.Timer = nil
|
||||
var challengeStart time.Time
|
||||
var challengeEnd time.Time
|
||||
|
||||
func touchStartedFile() {
|
||||
if fd, err := os.Create(startedFile); err == nil {
|
||||
|
@ -23,10 +24,21 @@ func touchStartedFile() {
|
|||
}
|
||||
|
||||
func reloadSettings(config settings.FICSettings) {
|
||||
if fronttime.ChallengeStart != config.Start || fronttime.ChallengeEnd != config.End {
|
||||
if challengeStart != config.Start || challengeEnd != config.End {
|
||||
if touchTimer != nil {
|
||||
touchTimer.Stop()
|
||||
}
|
||||
|
||||
if config.Start.Unix() == 0 {
|
||||
log.Println("WARNING: No challenge start defined!")
|
||||
|
||||
if _, err := os.Stat(startedFile); !os.IsNotExist(err) {
|
||||
os.Remove(startedFile)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
startSub := config.Start.Sub(time.Now())
|
||||
if startSub > 0 {
|
||||
log.Println("Challenge will starts at", config.Start, "in", startSub)
|
||||
|
@ -42,8 +54,8 @@ func reloadSettings(config settings.FICSettings) {
|
|||
}
|
||||
log.Println("Challenge ends on", config.End)
|
||||
|
||||
fronttime.ChallengeStart = config.Start
|
||||
fronttime.ChallengeEnd = config.End
|
||||
challengeStart = config.Start
|
||||
challengeEnd = config.End
|
||||
} else {
|
||||
log.Println("Configuration reloaded, but start/end times doesn't change.")
|
||||
}
|
||||
|
|
|
@ -19,22 +19,22 @@
|
|||
<div class="navbar navbar-expand-lg navbar-dark bg-dark text-light">
|
||||
<div class="container">
|
||||
<div class="col-md-auto">
|
||||
<a href="https://www.forum-fic.com/" ng-if="!(time.start || my.team_id)">
|
||||
<a href="https://www.forum-fic.com/" ng-if="!(time.remaining === undefined || my.team_id)">
|
||||
<img src="/img/fic.png" alt="Forum International de la Cybersécurité" class="center-block">
|
||||
</a>
|
||||
<a href="/" ng-if="(time.start || my.team_id)" ng-cloak>
|
||||
<a href="/" ng-if="(time.remaining === undefined || my.team_id)" ng-cloak>
|
||||
<img src="/img/fic.png" alt="Forum International de la Cybersécurité" class="center-block">
|
||||
</a>
|
||||
</div>
|
||||
<div id="clock" class="col-md-auto text-center" ng-hide="1">Chargement...</div>
|
||||
<div id="clock" class="col-md-auto text-center" ng-class="{expired: time.expired, end: time.end}" ng-if="time.start || my.team_id" ng-cloak>
|
||||
<div id="clock" class="col-md-auto text-center" ng-class="{expired: time.expired, end: time.end}" ng-if="time.remaining !== undefined && settings.end - settings.start > 0" ng-cloak>
|
||||
<span id="hours">{{ time.hours | time }}</span>
|
||||
<span class="point">:</span>
|
||||
<span id="min">{{ time.minutes | time }}</span>
|
||||
<span class="point">:</span>
|
||||
<span id="sec">{{ time.seconds | time }}</span>
|
||||
</div>
|
||||
<div id="clock" class="col-md-auto btn-group btn-group-lg" ng-if="!(time.start || my.team_id)" ng-cloak>
|
||||
<div id="clock" class="col-md-auto btn-group btn-group-lg" ng-if="time.remaining === undefined || settings.end - settings.start <= 0" ng-cloak>
|
||||
<a class="btn btn-light" href="/">
|
||||
<span class="glyphicon glyphicon-home"></span> Accueil
|
||||
</a>
|
||||
|
@ -83,7 +83,7 @@
|
|||
<span class="teamname">{{ my.name }}</span>
|
||||
</a>
|
||||
</span>
|
||||
<span class="navbar-text text-light" ng-show="!my.team_id && time.start" ng-cloak>
|
||||
<span class="navbar-text text-light" ng-show="!my.team_id && time.remaining === undefined" ng-cloak>
|
||||
<a ng-href="/register" class="badge badge-warning" role="button">
|
||||
Inscription
|
||||
</a>
|
||||
|
|
|
@ -45,17 +45,37 @@ angular.module("FICApp", ["ngRoute", "ngSanitize"])
|
|||
|
||||
$('[data-toggle="popover"]').popover();
|
||||
|
||||
$rootScope.recvTime = function(response) {
|
||||
sessionStorage.userService = angular.toJson({
|
||||
"cu": Math.floor(response.headers("x-fic-time") * 1000),
|
||||
"he": (new Date()).getTime(),
|
||||
});
|
||||
}
|
||||
|
||||
function updTime() {
|
||||
if (sessionStorage.userService) {
|
||||
if (sessionStorage.userService && $rootScope.settings) {
|
||||
var time = angular.fromJson(sessionStorage.userService);
|
||||
var srv_cur = (Date.now() + (time.cu * 1000 - time.he)) / 1000;
|
||||
var remain = time.du;
|
||||
if (time.st == Math.floor(srv_cur)) {
|
||||
var settings = $rootScope.settings;
|
||||
var srv_cur = new Date(Date.now() + (time.cu - time.he));
|
||||
|
||||
if (Math.floor(settings.start / 1000) == Math.floor(srv_cur / 1000)) {
|
||||
$rootScope.refresh(true);
|
||||
}
|
||||
if (time.st > 0 && time.st <= srv_cur) {
|
||||
remain = time.st + time.du - srv_cur;
|
||||
|
||||
var remain = 0;
|
||||
if (settings.start == 0) {
|
||||
$rootScope.time = {};
|
||||
return
|
||||
} else if (settings.start > srv_cur) {
|
||||
$rootScope.startIn = Math.floor((settings.start - srv_cur) / 1000);
|
||||
remain = settings.end - settings.start;
|
||||
} else if (settings.end > srv_cur) {
|
||||
$rootScope.startIn = 0;
|
||||
remain = settings.end - srv_cur;
|
||||
}
|
||||
|
||||
remain = remain / 1000;
|
||||
|
||||
if (remain < 0) {
|
||||
remain = 0;
|
||||
$rootScope.time.end = true;
|
||||
|
@ -67,8 +87,7 @@ angular.module("FICApp", ["ngRoute", "ngSanitize"])
|
|||
$rootScope.time.end = false;
|
||||
$rootScope.time.expired = false;
|
||||
}
|
||||
$rootScope.time.start = time.st * 1000;
|
||||
$rootScope.time.duration = time.du;
|
||||
|
||||
$rootScope.time.remaining = remain;
|
||||
$rootScope.time.hours = Math.floor(remain / 3600);
|
||||
$rootScope.time.minutes = Math.floor((remain % 3600) / 60);
|
||||
|
@ -95,13 +114,12 @@ angular.module("FICApp", ["ngRoute", "ngSanitize"])
|
|||
if (!justMy) {
|
||||
$timeout.cancel($scope.cbr);
|
||||
$scope.cbr = $timeout($rootScope.refresh, 42000);
|
||||
$http.get("/time.json").then(function(response) {
|
||||
var time = response.data;
|
||||
time.he = (new Date()).getTime();
|
||||
sessionStorage.userService = angular.toJson(time);
|
||||
});
|
||||
$http.get("/settings.json").then(function(response) {
|
||||
$scope.settings = response.data;
|
||||
$rootScope.recvTime(response);
|
||||
response.data.start = new Date(response.data.start);
|
||||
response.data.end = new Date(response.data.end);
|
||||
response.data.generation = new Date(response.data.generation);
|
||||
$rootScope.settings = response.data;
|
||||
});
|
||||
$http.get("/themes.json").then(function(response) {
|
||||
$scope.themes = response.data;
|
||||
|
|
|
@ -6,12 +6,10 @@ import (
|
|||
"path"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
fronttime "srs.epita.fr/fic-server/frontend/time"
|
||||
)
|
||||
|
||||
func SubmissionHandler(w http.ResponseWriter, r *http.Request, team string, sURL []string) {
|
||||
if time.Now().Sub(fronttime.ChallengeEnd) > 0 {
|
||||
if time.Now().Sub(challengeEnd) > 0 {
|
||||
http.Error(w, "{\"errmsg\":\"Vous ne pouvez plus soumettre, le challenge est terminé.\"}", http.StatusForbidden)
|
||||
return
|
||||
}
|
||||
|
|
|
@ -1,36 +0,0 @@
|
|||
package time
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
var ChallengeStart time.Time
|
||||
var ChallengeEnd time.Time = time.Unix(0, 0)
|
||||
|
||||
type TimeHandler struct {}
|
||||
|
||||
type timeObject struct {
|
||||
Started int64 `json:"st"`
|
||||
Time int64 `json:"cu"`
|
||||
Duration int `json:"du"`
|
||||
}
|
||||
|
||||
func (t TimeHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
if addr := r.Header.Get("X-Forwarded-For"); addr != "" {
|
||||
r.RemoteAddr = addr
|
||||
}
|
||||
log.Printf("%s \"%s %s\" [%s]\n", r.RemoteAddr, r.Method, r.URL.Path, r.UserAgent())
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
|
||||
if j, err := json.Marshal(timeObject{ChallengeStart.Unix(), time.Now().Unix(), int(ChallengeEnd.Sub(ChallengeStart).Seconds())}); err != nil {
|
||||
http.Error(w, fmt.Sprintf("{\"errmsg\":%q}", err), http.StatusInternalServerError)
|
||||
} else {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write(j)
|
||||
}
|
||||
}
|
Reference in a new issue