tailscale/derp/mesh_key.go
Mike O'Driscoll f075a65398
cmd/{derp,derpprobe},prober,derp: add mesh support to derpprobe
Add mesh key support to derpprobe for
probing derpers with verify set to true.

Move MeshKey checking to central point for code reuse.

Fix a bad error fmt msg.

Fixes tailscale/corp#27294
Fixes tailscale/corp#25756

Signed-off-by: Mike O'Driscoll <mikeo@tailscale.com>
2025-06-09 10:59:36 -04:00

34 lines
912 B
Go

// Copyright (c) Tailscale Inc & AUTHORS
// SPDX-License-Identifier: BSD-3-Clause
package derp
import (
"fmt"
"regexp"
"strings"
)
var (
// ValidMeshKey is a regular expression that matches a valid mesh key,
// which must be a 64-character hexadecimal string (lowercase only).
ValidMeshKey = regexp.MustCompile(`^[0-9a-f]{64}$`)
)
// CheckMeshKey checks if the provided key is a valid mesh key.
// It trims any leading or trailing whitespace and returns an error if the key
// does not match the expected format. If the key is empty or valid, it returns
// the trimmed key and a nil error. The key must be a 64-character
// hexadecimal string (lowercase only).
func CheckMeshKey(key string) (string, error) {
if key == "" {
return key, nil
}
key = strings.TrimSpace(key)
if !ValidMeshKey.MatchString(key) {
return "", fmt.Errorf("key must contain exactly 64 hex digits")
}
return key, nil
}