2020-09-15 07:48:54 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2020-09-15 08:29:41 +00:00
|
|
|
"fmt"
|
2020-09-15 07:48:54 +00:00
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
"text/template"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/xlzd/gotp"
|
|
|
|
)
|
|
|
|
|
2020-09-15 08:29:41 +00:00
|
|
|
var addr = "0.0.0.0:8080"
|
|
|
|
|
2020-09-15 07:48:54 +00:00
|
|
|
type secret struct {
|
|
|
|
S string `json:"secret"`
|
|
|
|
}
|
|
|
|
type code struct {
|
|
|
|
C string `json:"code"`
|
|
|
|
T int `json:"time"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func calculate(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var s secret
|
|
|
|
json.NewDecoder(r.Body).Decode(&s)
|
|
|
|
totp := gotp.NewDefaultTOTP(s.S).Now()
|
|
|
|
c := &code{
|
|
|
|
C: totp,
|
|
|
|
T: int(30 - time.Now().Unix()%30),
|
|
|
|
}
|
|
|
|
response, _ := json.Marshal(c)
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
w.Write(response)
|
|
|
|
}
|
|
|
|
|
|
|
|
func index(w http.ResponseWriter, r *http.Request) {
|
|
|
|
t, _ := template.ParseFiles("templates/index.html")
|
|
|
|
t.Execute(w, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
staticFolder := http.FileServer(http.Dir("./static"))
|
|
|
|
http.HandleFunc("/", index)
|
|
|
|
http.HandleFunc("/calculate", calculate)
|
|
|
|
http.Handle("/static/", http.StripPrefix(strings.TrimRight("/static/", "/"), staticFolder))
|
2020-09-15 08:29:41 +00:00
|
|
|
fmt.Println("Listening on ", addr)
|
|
|
|
http.ListenAndServe(addr, nil)
|
2020-09-15 07:48:54 +00:00
|
|
|
}
|