Retrieve time through X-FIC-Time header instead of time.json
This commit is contained in:
parent
c33390fa80
commit
6034246015
|
@ -8,6 +8,7 @@ import (
|
|||
"log"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"srs.epita.fr/fic-server/libfic"
|
||||
|
||||
|
@ -60,6 +61,8 @@ func apiHandler(f DispatchFunction) func(http.ResponseWriter, *http.Request, htt
|
|||
resStatus = http.StatusNotFound
|
||||
}
|
||||
|
||||
w.Header().Set("X-FIC-Time", fmt.Sprintf("%f", float64(time.Now().UnixNano()/1000)/1000000))
|
||||
|
||||
if str, found := ret.(string); found {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(resStatus)
|
||||
|
|
|
@ -432,19 +432,31 @@ angular.module("FICApp")
|
|||
|
||||
.controller("SettingsController", function($scope, $rootScope, Settings, ROSettings, $location, $http) {
|
||||
$scope.config = Settings.get();
|
||||
$scope.config.$promise.then(function(response) {
|
||||
$rootScope.settings.start = new Date(response.start);
|
||||
$rootScope.settings.end = new Date(response.end);
|
||||
$rootScope.settings.generation = new Date(response.generation);
|
||||
})
|
||||
$scope.configro = ROSettings.get();
|
||||
$scope.duration = 240;
|
||||
|
||||
$scope.saveSettings = function(msg) {
|
||||
if (msg === undefined) { msg = 'New settings saved!'; }
|
||||
var nStart = this.config.start;
|
||||
var nEnd = this.config.end;
|
||||
var nGen = this.config.generation;
|
||||
this.config.$update(function() {
|
||||
$rootScope.newBox('success', msg);
|
||||
$rootScope.settings.start = new Date(nStart);
|
||||
$rootScope.settings.end = new Date(nEnd);
|
||||
$rootScope.settings.generation = new Date(nGen);
|
||||
}, function(response) {
|
||||
$rootScope.newBox('danger', 'An error occurs when saving settings:', response.data);
|
||||
});
|
||||
}
|
||||
$scope.regenerate = function() {
|
||||
this.config.generation = (new Date()).toISOString();
|
||||
$rootScope.settings.generation = new Date(this.config.generation);
|
||||
$scope.saveSettings("Regeneration in progress...");
|
||||
}
|
||||
$scope.launchChallenge = function() {
|
||||
|
@ -850,7 +862,7 @@ angular.module("FICApp")
|
|||
"id_assignee": $scope.whoami,
|
||||
"content": $scope.ndescription
|
||||
}
|
||||
}).then(function() {
|
||||
}).then(function(response) {
|
||||
$location.url("/claims/" + $scope.claim.id + "/");
|
||||
});
|
||||
}
|
||||
|
@ -1288,21 +1300,26 @@ angular.module("FICApp")
|
|||
presenceCal($scope, "#presenceCal", res);
|
||||
});
|
||||
})
|
||||
.controller("CountdownController", function($scope, $rootScope, $http, $timeout) {
|
||||
.controller("CountdownController", function(Settings, $scope, $rootScope, $http, $interval) {
|
||||
$scope.time = {};
|
||||
|
||||
function updTime() {
|
||||
$timeout.cancel($scope.cbm);
|
||||
$scope.cbm = $timeout(updTime, 1000);
|
||||
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 > 0 && time.st <= srv_cur) {
|
||||
var settings = $rootScope.settings;
|
||||
var srv_cur = new Date(Date.now() + (time.cu - time.he));
|
||||
|
||||
var remain = 0;
|
||||
if (settings.start > 0 && settings.start > srv_cur) {
|
||||
$scope.startIn = Math.floor((settings.start - srv_cur) / 1000);
|
||||
remain = settings.end - settings.start;
|
||||
} else if (settings.end > srv_cur) {
|
||||
$scope.startIn = 0;
|
||||
remain = time.st + time.du - srv_cur;
|
||||
} else if (time.st > 0) {
|
||||
$scope.startIn = Math.floor(time.st - srv_cur);
|
||||
remain = settings.end - srv_cur;
|
||||
}
|
||||
|
||||
remain = remain / 1000;
|
||||
|
||||
if (remain < 0) {
|
||||
remain = 0;
|
||||
$scope.time.end = true;
|
||||
|
@ -1314,22 +1331,35 @@ angular.module("FICApp")
|
|||
$scope.time.end = false;
|
||||
$scope.time.expired = false;
|
||||
}
|
||||
$scope.time.start = time.st * 1000;
|
||||
$scope.time.duration = time.du * 1000;
|
||||
|
||||
$scope.time.remaining = remain;
|
||||
$scope.time.hours = Math.floor(remain / 3600);
|
||||
$scope.time.minutes = Math.floor((remain % 3600) / 60);
|
||||
$scope.time.seconds = Math.floor(remain % 60);
|
||||
$rootScope.time = $scope.time;
|
||||
}
|
||||
};
|
||||
$interval(updTime, 1000);
|
||||
|
||||
$rootScope.updTime = function(response) {
|
||||
sessionStorage.userService = angular.toJson({
|
||||
"cu": Math.floor(response.headers("x-fic-time") * 1000),
|
||||
"he": (new Date()).getTime(),
|
||||
});
|
||||
updTime();
|
||||
}
|
||||
|
||||
$http.get("/time.json").then(function(response) {
|
||||
var time = response.data;
|
||||
time.he = (new Date()).getTime();
|
||||
sessionStorage.userService = angular.toJson(time);
|
||||
updTime();
|
||||
});
|
||||
function refresh() {
|
||||
$http.get("/api/settings.json").then(function(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;
|
||||
$rootScope.updTime(response);
|
||||
})
|
||||
}
|
||||
refresh();
|
||||
$interval(refresh, 10000);
|
||||
});
|
||||
|
||||
function solvedByLevelPie(location, data) {
|
||||
|
|
|
@ -1,25 +0,0 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"path"
|
||||
|
||||
"srs.epita.fr/fic-server/admin/api"
|
||||
"srs.epita.fr/fic-server/frontend/time"
|
||||
"srs.epita.fr/fic-server/settings"
|
||||
|
||||
"github.com/julienschmidt/httprouter"
|
||||
)
|
||||
|
||||
func init() {
|
||||
api.Router().GET("/time.json", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
||||
if config, err := settings.ReadSettings(path.Join(settings.SettingsDir, settings.SettingsFile)); err != nil {
|
||||
http.Error(w, fmt.Sprintf("{\"errmsg\":%q}", err), http.StatusInternalServerError)
|
||||
} else {
|
||||
time.ChallengeStart = config.Start
|
||||
time.ChallengeEnd = config.End
|
||||
time.TimeHandler{}.ServeHTTP(w, r)
|
||||
}
|
||||
})
|
||||
}
|
|
@ -160,18 +160,6 @@ server {
|
|||
proxy_redirect off;
|
||||
}
|
||||
|
||||
location = /time.json {
|
||||
proxy_pass http://localhost:8080/time.json;
|
||||
proxy_method GET;
|
||||
proxy_pass_request_body off;
|
||||
proxy_set_header Content-Length "";
|
||||
proxy_set_header X-Forwarded-For $remote_addr;
|
||||
proxy_set_header Host localhost;
|
||||
proxy_redirect off;
|
||||
proxy_cache STATIC;
|
||||
proxy_cache_valid 1s;
|
||||
}
|
||||
|
||||
location = /events.json {
|
||||
proxy_pass http://localhost:8081/api/events/;
|
||||
proxy_method GET;
|
||||
|
|
|
@ -131,6 +131,7 @@ server {
|
|||
location = /settings.json {
|
||||
root /srv/SETTINGS/;
|
||||
expires epoch;
|
||||
add_header X-FIC-time $msec;
|
||||
add_header Cache-Control no-cache;
|
||||
}
|
||||
|
||||
|
@ -161,15 +162,4 @@ server {
|
|||
proxy_set_header X-Forwarded-For $remote_addr;
|
||||
proxy_redirect off;
|
||||
}
|
||||
|
||||
location = /time.json {
|
||||
proxy_pass http://frontend:8080/time.json;
|
||||
proxy_method GET;
|
||||
proxy_pass_request_body off;
|
||||
proxy_set_header Content-Length "";
|
||||
proxy_set_header X-Forwarded-For $remote_addr;
|
||||
proxy_redirect off;
|
||||
proxy_cache STATIC;
|
||||
proxy_cache_valid 1s;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue