tls/status -- make stubs add warning and status randomly

This commit is contained in:
Eugene Bujak 2019-01-30 15:01:00 +03:00 committed by Eugene Bujak
parent ab11c912db
commit 38869b22a6
2 changed files with 46 additions and 1 deletions

View File

@ -6,6 +6,7 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"math/rand"
"net" "net"
"net/http" "net/http"
"os" "os"
@ -1029,7 +1030,22 @@ func handleInstallConfigure(w http.ResponseWriter, r *http.Request) {
// TLS // TLS
// --- // ---
func handleTLSStatus(w http.ResponseWriter, r *http.Request) { func handleTLSStatus(w http.ResponseWriter, r *http.Request) {
err := json.NewEncoder(w).Encode(&config.TLS) data := struct {
tlsConfig `json:",inline"`
// only for API, no need to be stored in config
Status string `yaml:"-" json:"status,omitempty"`
Warning string `yaml:"-" json:"warning,omitempty"`
}{
tlsConfig: config.TLS,
}
if rand.Intn(2) == 0 {
data.Status = fmt.Sprintf("Random status #%s", RandStringBytesMaskImpr(6))
}
if rand.Intn(2) == 0 {
data.Warning = fmt.Sprintf("Random warning #%s", RandStringBytesMaskImpr(6))
}
err := json.NewEncoder(w).Encode(&data)
if err != nil { if err != nil {
httpError(w, http.StatusInternalServerError, "Failed to marshal json with TLS status: %s", err) httpError(w, http.StatusInternalServerError, "Failed to marshal json with TLS status: %s", err)
return return

View File

@ -6,6 +6,7 @@ import (
"fmt" "fmt"
"io" "io"
"io/ioutil" "io/ioutil"
"math/rand"
"net" "net"
"net/http" "net/http"
"os" "os"
@ -235,6 +236,34 @@ func checkPacketPortAvailable(host string, port int) error {
return err return err
} }
// ------------------------
// random string generation
// ------------------------
const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
const (
letterIdxBits = 6 // 6 bits to represent a letter index
letterIdxMask = 1<<letterIdxBits - 1 // All 1-bits, as many as letterIdxBits
letterIdxMax = 63 / letterIdxBits // # of letter indices fitting in 63 bits
)
func RandStringBytesMaskImpr(n int) string {
b := make([]byte, n)
// A rand.Int63() generates 63 random bits, enough for letterIdxMax letters!
for i, cache, remain := n-1, rand.Int63(), letterIdxMax; i >= 0; {
if remain == 0 {
cache, remain = rand.Int63(), letterIdxMax
}
if idx := int(cache & letterIdxMask); idx < len(letterBytes) {
b[i] = letterBytes[idx]
i--
}
cache >>= letterIdxBits
remain--
}
return string(b)
}
// --------------------- // ---------------------
// debug logging helpers // debug logging helpers
// --------------------- // ---------------------