mirror of
https://github.com/tailscale/tailscale.git
synced 2025-02-18 02:48:40 +00:00
util/rands: add package with HexString func
We use it a number of places in different repos. Might as well make one. Another use is coming. Updates #cleanup Change-Id: Ib7ce38de0db35af998171edee81ca875102349a4 Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
This commit is contained in:
parent
f824274093
commit
7175f06e62
@ -343,6 +343,7 @@ tailscale.com/cmd/tailscaled dependencies: (generated by github.com/tailscale/de
|
|||||||
tailscale.com/util/osshare from tailscale.com/ipn/ipnlocal+
|
tailscale.com/util/osshare from tailscale.com/ipn/ipnlocal+
|
||||||
W tailscale.com/util/pidowner from tailscale.com/ipn/ipnauth
|
W tailscale.com/util/pidowner from tailscale.com/ipn/ipnauth
|
||||||
tailscale.com/util/racebuild from tailscale.com/logpolicy
|
tailscale.com/util/racebuild from tailscale.com/logpolicy
|
||||||
|
tailscale.com/util/rands from tailscale.com/ipn/localapi
|
||||||
tailscale.com/util/ringbuffer from tailscale.com/wgengine/magicsock
|
tailscale.com/util/ringbuffer from tailscale.com/wgengine/magicsock
|
||||||
tailscale.com/util/set from tailscale.com/health+
|
tailscale.com/util/set from tailscale.com/health+
|
||||||
tailscale.com/util/singleflight from tailscale.com/control/controlclient+
|
tailscale.com/util/singleflight from tailscale.com/control/controlclient+
|
||||||
|
@ -7,8 +7,6 @@ package localapi
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
"crypto/rand"
|
|
||||||
"encoding/hex"
|
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
@ -49,6 +47,7 @@ import (
|
|||||||
"tailscale.com/util/httpm"
|
"tailscale.com/util/httpm"
|
||||||
"tailscale.com/util/mak"
|
"tailscale.com/util/mak"
|
||||||
"tailscale.com/util/osdiag"
|
"tailscale.com/util/osdiag"
|
||||||
|
"tailscale.com/util/rands"
|
||||||
"tailscale.com/version"
|
"tailscale.com/version"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -118,12 +117,6 @@ var handler = map[string]localAPIHandler{
|
|||||||
"query-feature": (*Handler).serveQueryFeature,
|
"query-feature": (*Handler).serveQueryFeature,
|
||||||
}
|
}
|
||||||
|
|
||||||
func randHex(n int) string {
|
|
||||||
b := make([]byte, n)
|
|
||||||
rand.Read(b)
|
|
||||||
return hex.EncodeToString(b)
|
|
||||||
}
|
|
||||||
|
|
||||||
var (
|
var (
|
||||||
// The clientmetrics package is stateful, but we want to expose a simple
|
// The clientmetrics package is stateful, but we want to expose a simple
|
||||||
// imperative API to local clients, so we need to keep track of
|
// imperative API to local clients, so we need to keep track of
|
||||||
@ -318,7 +311,7 @@ func (h *Handler) serveBugReport(w http.ResponseWriter, r *http.Request) {
|
|||||||
defer h.b.TryFlushLogs() // kick off upload after bugreport's done logging
|
defer h.b.TryFlushLogs() // kick off upload after bugreport's done logging
|
||||||
|
|
||||||
logMarker := func() string {
|
logMarker := func() string {
|
||||||
return fmt.Sprintf("BUG-%v-%v-%v", h.backendLogID, h.clock.Now().UTC().Format("20060102150405Z"), randHex(8))
|
return fmt.Sprintf("BUG-%v-%v-%v", h.backendLogID, h.clock.Now().UTC().Format("20060102150405Z"), rands.HexString(16))
|
||||||
}
|
}
|
||||||
if envknob.NoLogsNoSupport() {
|
if envknob.NoLogsNoSupport() {
|
||||||
logMarker = func() string { return "BUG-NO-LOGS-NO-SUPPORT-this-node-has-had-its-logging-disabled" }
|
logMarker = func() string { return "BUG-NO-LOGS-NO-SUPPORT-this-node-has-had-its-logging-disabled" }
|
||||||
|
@ -7,7 +7,6 @@ package testcontrol
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
crand "crypto/rand"
|
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
@ -34,6 +33,7 @@ import (
|
|||||||
"tailscale.com/types/key"
|
"tailscale.com/types/key"
|
||||||
"tailscale.com/types/logger"
|
"tailscale.com/types/logger"
|
||||||
"tailscale.com/types/ptr"
|
"tailscale.com/types/ptr"
|
||||||
|
"tailscale.com/util/rands"
|
||||||
)
|
)
|
||||||
|
|
||||||
const msgLimit = 1 << 20 // encrypted message length limit
|
const msgLimit = 1 << 20 // encrypted message length limit
|
||||||
@ -569,9 +569,7 @@ func (s *Server) serveRegister(w http.ResponseWriter, r *http.Request, mkey key.
|
|||||||
|
|
||||||
authURL := ""
|
authURL := ""
|
||||||
if requireAuth {
|
if requireAuth {
|
||||||
randHex := make([]byte, 10)
|
authPath := fmt.Sprintf("/auth/%s", rands.HexString(20))
|
||||||
crand.Read(randHex)
|
|
||||||
authPath := fmt.Sprintf("/auth/%x", randHex)
|
|
||||||
s.addAuthPath(authPath, nk)
|
s.addAuthPath(authPath, nk)
|
||||||
authURL = s.BaseURL() + authPath
|
authURL = s.BaseURL() + authPath
|
||||||
}
|
}
|
||||||
|
25
util/rands/rands.go
Normal file
25
util/rands/rands.go
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
// Copyright (c) Tailscale Inc & AUTHORS
|
||||||
|
// SPDX-License-Identifier: BSD-3-Clause
|
||||||
|
|
||||||
|
// Package rands contains utility functions for randomness.
|
||||||
|
package rands
|
||||||
|
|
||||||
|
import (
|
||||||
|
crand "crypto/rand"
|
||||||
|
"encoding/hex"
|
||||||
|
)
|
||||||
|
|
||||||
|
// HexString returns a string of n cryptographically random lowercase
|
||||||
|
// hex characters.
|
||||||
|
//
|
||||||
|
// That is, HexString(3) returns something like "0fc", containing 12
|
||||||
|
// bits of randomness.
|
||||||
|
func HexString(n int) string {
|
||||||
|
nb := n / 2
|
||||||
|
if n%2 == 1 {
|
||||||
|
nb++
|
||||||
|
}
|
||||||
|
b := make([]byte, nb)
|
||||||
|
crand.Read(b)
|
||||||
|
return hex.EncodeToString(b)[:n]
|
||||||
|
}
|
15
util/rands/rands_test.go
Normal file
15
util/rands/rands_test.go
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
// Copyright (c) Tailscale Inc & AUTHORS
|
||||||
|
// SPDX-License-Identifier: BSD-3-Clause
|
||||||
|
|
||||||
|
package rands
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
func TestHexString(t *testing.T) {
|
||||||
|
for i := 0; i <= 8; i++ {
|
||||||
|
s := HexString(i)
|
||||||
|
if len(s) != i {
|
||||||
|
t.Errorf("HexString(%v) = %q; want len %v, not %v", i, s, i, len(s))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user