tls/status -- make stubs add warning and status randomly
This commit is contained in:
parent
ab11c912db
commit
38869b22a6
18
control.go
18
control.go
@ -6,6 +6,7 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"math/rand"
|
||||
"net"
|
||||
"net/http"
|
||||
"os"
|
||||
@ -1029,7 +1030,22 @@ func handleInstallConfigure(w http.ResponseWriter, r *http.Request) {
|
||||
// TLS
|
||||
// ---
|
||||
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 {
|
||||
httpError(w, http.StatusInternalServerError, "Failed to marshal json with TLS status: %s", err)
|
||||
return
|
||||
|
29
helpers.go
29
helpers.go
@ -6,6 +6,7 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"math/rand"
|
||||
"net"
|
||||
"net/http"
|
||||
"os"
|
||||
@ -235,6 +236,34 @@ func checkPacketPortAvailable(host string, port int) error {
|
||||
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
|
||||
// ---------------------
|
||||
|
Loading…
Reference in New Issue
Block a user