mirror of
https://github.com/juanfont/headscale.git
synced 2024-12-30 11:47:50 +00:00
34 lines
590 B
Go
34 lines
590 B
Go
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")
|
|
ZstdCompression = "zstd"
|
|
)
|
|
|
|
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
|
|
}
|