37 lines
876 B
Go
37 lines
876 B
Go
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)
|
|
}
|
|
}
|