37 lines
1.0 KiB
Go
37 lines
1.0 KiB
Go
// Package util holds small shared helpers used across internal packages to
|
|
// avoid divergent same-named copies.
|
|
package util
|
|
|
|
import (
|
|
"crypto/rand"
|
|
"fmt"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
// FirstNonEmpty returns the first trimmed-non-empty value, or "" when none of
|
|
// the values are non-empty. Callers that need a specific fallback for the
|
|
// all-empty case should apply it explicitly at the call site.
|
|
func FirstNonEmpty(values ...string) string {
|
|
for _, v := range values {
|
|
if strings.TrimSpace(v) != "" {
|
|
return v
|
|
}
|
|
}
|
|
return ""
|
|
}
|
|
|
|
// RandomRequestID returns a random v4 UUID string, matching the format the
|
|
// Zhanlu plugin sends in the `request` header of every gateway call
|
|
// (crypto.randomUUID()). Extracted so the login and phone-code paths share
|
|
// one implementation.
|
|
func RandomRequestID() string {
|
|
b := make([]byte, 16)
|
|
if _, err := rand.Read(b); err != nil {
|
|
return fmt.Sprintf("%d", time.Now().UnixNano())
|
|
}
|
|
b[6] = (b[6] & 0x0f) | 0x40
|
|
b[8] = (b[8] & 0x3f) | 0x80
|
|
return fmt.Sprintf("%x-%x-%x-%x-%x", b[0:4], b[4:6], b[6:8], b[8:10], b[10:])
|
|
}
|