package results import ( "html/template" "net/http" log "github.com/sirupsen/logrus" "github.com/librespeed/speedtest/config" "github.com/librespeed/speedtest/database" "github.com/librespeed/speedtest/database/schema" ) type StatsData struct { NoPassword bool LoggedIn bool Data []schema.TelemetryData } func Stats(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "text/html; charset=utf-8") t, err := template.New("template").Parse(htmlTemplate) if err != nil { log.Errorf("Failed to parse template: %s", err) w.WriteHeader(http.StatusInternalServerError) return } conf := config.LoadedConfig() var data StatsData if conf.StatsPassword == "PASSWORD" { data.NoPassword = true } if !data.NoPassword { op := r.FormValue("op") c, _ := r.Cookie("logged") if c != nil && c.Value == "true" { if op == "logout" { cookie := &http.Cookie{ Name: "logged", Value: "false", } http.SetCookie(w, cookie) http.Redirect(w, r, "/stats", http.StatusTemporaryRedirect) } else { data.LoggedIn = true id := r.FormValue("id") switch id { case "L100": stats, err := database.DB.FetchLast100() if err != nil { log.Errorf("Error fetching data from database: %s", err) w.WriteHeader(http.StatusInternalServerError) return } data.Data = stats case "": default: stat, err := database.DB.FetchByUUID(id) if err != nil { log.Errorf("Error fetching data from database: %s", err) w.WriteHeader(http.StatusInternalServerError) return } data.Data = append(data.Data, *stat) } } } else { if op == "login" { password := r.FormValue("password") if password == conf.StatsPassword { cookie := &http.Cookie{ Name: "logged", Value: "true", } http.SetCookie(w, cookie) http.Redirect(w, r, "/stats", http.StatusTemporaryRedirect) } else { w.WriteHeader(http.StatusForbidden) } } } } if err := t.Execute(w, data); err != nil { log.Errorf("Error executing template: %s", err) w.WriteHeader(http.StatusInternalServerError) } } const htmlTemplate = ` LibreSpeed - Stats

LibreSpeed - Stats

{{ if .NoPassword }} Please set statistics_password in settings.toml to enable access. {{ else if .LoggedIn }}

Search test results

{{ range $i, $v := .Data }}
Test ID{{ $v.UUID }}
Date and time{{ $v.Timestamp }}
IP and ISP Info{{ $v.IPAddress }}
{{ $v.ISPInfo }}
User agent and locale{{ $v.UserAgent }}
{{ $v.Language }}
Download speed{{ $v.Download }}
Upload speed{{ $v.Upload }}
Ping{{ $v.Ping }}
Jitter{{ $v.Jitter }}
Log{{ $v.Log }}
Extra info{{ $v.Extra }}
{{ end }} {{ else }}

Login

{{ end }} `