2023-05-11 07:09:18 +00:00
|
|
|
package util
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"regexp"
|
|
|
|
|
|
|
|
"tailscale.com/types/key"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
NodePublicKeyRegex = regexp.MustCompile("nodekey:[a-fA-F0-9]+")
|
|
|
|
ErrCannotDecryptResponse = errors.New("cannot decrypt response")
|
2023-11-16 16:55:29 +00:00
|
|
|
ZstdCompression = "zstd"
|
2023-05-11 07:09:18 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func DecodeAndUnmarshalNaCl(
|
|
|
|
msg []byte,
|
|
|
|
output interface{},
|
|
|
|
pubKey *key.MachinePublic,
|
|
|
|
privKey *key.MachinePrivate,
|
|
|
|
) error {
|
|
|
|
decrypted, ok := privKey.OpenFrom(*pubKey, msg)
|
|
|
|
if !ok {
|
|
|
|
return ErrCannotDecryptResponse
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := json.Unmarshal(decrypted, output); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|