From 38869b22a62be57dc807408e4cbef43fbbf80089 Mon Sep 17 00:00:00 2001 From: Eugene Bujak Date: Wed, 30 Jan 2019 15:01:00 +0300 Subject: [PATCH] tls/status -- make stubs add warning and status randomly --- control.go | 18 +++++++++++++++++- helpers.go | 29 +++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/control.go b/control.go index 7199ff11..c6e518ea 100644 --- a/control.go +++ b/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 diff --git a/helpers.go b/helpers.go index a0cf1fd7..14116ab6 100644 --- a/helpers.go +++ b/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<= 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 // ---------------------