perf(web): improve logging
This commit is contained in:
parent
515beac231
commit
0c03d3a791
@ -41,6 +41,7 @@ func TokenGenerate(seed []byte) {
|
|||||||
value += int64(char) << (i * 8)
|
value += int64(char) << (i * 8)
|
||||||
}
|
}
|
||||||
TOKEN = fmt.Sprintf("%x", sha256.Sum256([]byte(strconv.FormatInt(value, 16))))
|
TOKEN = fmt.Sprintf("%x", sha256.Sum256([]byte(strconv.FormatInt(value, 16))))
|
||||||
|
defer log.Println("Token generated")
|
||||||
}
|
}
|
||||||
|
|
||||||
func Auth(handler http.HandlerFunc) http.HandlerFunc {
|
func Auth(handler http.HandlerFunc) http.HandlerFunc {
|
||||||
@ -51,6 +52,7 @@ func Auth(handler http.HandlerFunc) http.HandlerFunc {
|
|||||||
handler.ServeHTTP(w, r)
|
handler.ServeHTTP(w, r)
|
||||||
} else {
|
} else {
|
||||||
http.Redirect(w, r, "/auth", http.StatusSeeOther)
|
http.Redirect(w, r, "/auth", http.StatusSeeOther)
|
||||||
|
defer log.Println("Unauthorized request, redirecting to /auth")
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
token, err := r.Cookie("token")
|
token, err := r.Cookie("token")
|
||||||
@ -68,6 +70,7 @@ func HandlerAuth(w http.ResponseWriter, r *http.Request) {
|
|||||||
var hash [sha256.Size]byte
|
var hash [sha256.Size]byte
|
||||||
var passlen = sha256.Size
|
var passlen = sha256.Size
|
||||||
var err error
|
var err error
|
||||||
|
log.Println("Got authorization request")
|
||||||
passfile, err := os.Open("/etc/tanabata/.htpasswd")
|
passfile, err := os.Open("/etc/tanabata/.htpasswd")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Failed to open password file: %s\n", err)
|
log.Fatalf("Failed to open password file: %s\n", err)
|
||||||
@ -86,6 +89,7 @@ func HandlerAuth(w http.ResponseWriter, r *http.Request) {
|
|||||||
_, err = r.Body.Read(buffer)
|
_, err = r.Body.Read(buffer)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||||
|
defer log.Println("Bad authorization request")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
for i := 0; i < sha256.Size; i++ {
|
for i := 0; i < sha256.Size; i++ {
|
||||||
@ -97,7 +101,7 @@ func HandlerAuth(w http.ResponseWriter, r *http.Request) {
|
|||||||
hash = sha256.Sum256(buffer[:passlen])
|
hash = sha256.Sum256(buffer[:passlen])
|
||||||
TokenGenerate(buffer)
|
TokenGenerate(buffer)
|
||||||
if bytes.Equal(hash[:], passhash) {
|
if bytes.Equal(hash[:], passhash) {
|
||||||
TokenGenerate(buffer)
|
log.Println("Password valid")
|
||||||
response.Status = true
|
response.Status = true
|
||||||
response.Token = TOKEN
|
response.Token = TOKEN
|
||||||
http.SetCookie(w, &http.Cookie{
|
http.SetCookie(w, &http.Cookie{
|
||||||
@ -105,15 +109,17 @@ func HandlerAuth(w http.ResponseWriter, r *http.Request) {
|
|||||||
Value: TOKEN,
|
Value: TOKEN,
|
||||||
Expires: time.Now().Add(TOKEN_VALIDTIME * time.Second),
|
Expires: time.Now().Add(TOKEN_VALIDTIME * time.Second),
|
||||||
})
|
})
|
||||||
|
} else {
|
||||||
|
log.Println("Password invalid")
|
||||||
}
|
}
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
jsonData, err := json.Marshal(response)
|
jsonData, err := json.Marshal(response)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalln(err)
|
log.Fatalf("Failed to marshal json: %s\n", err)
|
||||||
}
|
}
|
||||||
_, err = w.Write(jsonData)
|
_, err = w.Write(jsonData)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalln(err)
|
log.Fatalf("Failed to send response: %s\n", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -121,24 +127,28 @@ func HandlerTDBMS(w http.ResponseWriter, r *http.Request) {
|
|||||||
var request JSON
|
var request JSON
|
||||||
var response []byte
|
var response []byte
|
||||||
var err error
|
var err error
|
||||||
|
log.Println("Got TDB request")
|
||||||
r.Body = http.MaxBytesReader(w, r.Body, 1048576)
|
r.Body = http.MaxBytesReader(w, r.Body, 1048576)
|
||||||
json_decoder := json.NewDecoder(r.Body)
|
json_decoder := json.NewDecoder(r.Body)
|
||||||
json_decoder.DisallowUnknownFields()
|
json_decoder.DisallowUnknownFields()
|
||||||
err = json_decoder.Decode(&request)
|
err = json_decoder.Decode(&request)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||||
|
defer log.Println("Bad TDB request")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
response = tdbms.Query(request.TRDB, request.TRC, request.TRB)
|
response = tdbms.Query(request.TRDB, request.TRC, request.TRB)
|
||||||
if response == nil {
|
if response == nil {
|
||||||
http.Error(w, "Failed to execute request", http.StatusInternalServerError)
|
http.Error(w, "Failed to execute request", http.StatusInternalServerError)
|
||||||
|
defer log.Println("Failed to execute request")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if request.TRDB == "$TFM" && (request.TRC == 0b10000 || request.TRC == 0b101000) {
|
if request.TRDB == "$TFM" && (request.TRC == 0b10000 || request.TRC == 0b101000) {
|
||||||
var json_response JSON
|
var json_response JSON
|
||||||
err = json.Unmarshal(response, &json_response)
|
err = json.Unmarshal(response, &json_response)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, "TDBMS error", http.StatusInternalServerError)
|
http.Error(w, "Bad TDBMS response", http.StatusInternalServerError)
|
||||||
|
defer log.Println("Bad TDBMS response")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
for index, element := range json_response.Data {
|
for index, element := range json_response.Data {
|
||||||
@ -151,12 +161,17 @@ func HandlerTDBMS(w http.ResponseWriter, r *http.Request) {
|
|||||||
json_response.Data[index]["path"] = path
|
json_response.Data[index]["path"] = path
|
||||||
}
|
}
|
||||||
response, err = json.Marshal(json_response)
|
response, err = json.Marshal(json_response)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Failed to marshal json: %s\n", err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||||
_, err = w.Write(response)
|
_, err = w.Write(response)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
defer log.Printf("Failed to send TDBMS response: %s\n", err)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
defer log.Println("TDBMS response sent")
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user