mirror of https://github.com/tailscale/tailscale/
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
175 lines
4.1 KiB
Go
175 lines
4.1 KiB
Go
4 years ago
|
// Copyright (c) 2020 Tailscale Inc & AUTHORS All rights reserved.
|
||
|
// Use of this source code is governed by a BSD-style
|
||
|
// license that can be found in the LICENSE file.
|
||
|
|
||
4 years ago
|
// Package deephash hashes a Go value recursively, in a predictable
|
||
|
// order, without looping.
|
||
|
package deephash
|
||
4 years ago
|
|
||
|
import (
|
||
4 years ago
|
"bufio"
|
||
4 years ago
|
"crypto/sha256"
|
||
|
"fmt"
|
||
|
"reflect"
|
||
4 years ago
|
|
||
|
"inet.af/netaddr"
|
||
|
"tailscale.com/tailcfg"
|
||
|
"tailscale.com/types/wgkey"
|
||
4 years ago
|
)
|
||
|
|
||
4 years ago
|
func Hash(v ...interface{}) string {
|
||
4 years ago
|
h := sha256.New()
|
||
4 years ago
|
// 64 matches the chunk size in crypto/sha256/sha256.go
|
||
|
b := bufio.NewWriterSize(h, 64)
|
||
|
Print(b, v)
|
||
|
b.Flush()
|
||
4 years ago
|
return fmt.Sprintf("%x", h.Sum(nil))
|
||
|
}
|
||
|
|
||
4 years ago
|
// UpdateHash sets last to the hash of v and reports whether its value changed.
|
||
|
func UpdateHash(last *string, v ...interface{}) (changed bool) {
|
||
|
sig := Hash(v)
|
||
|
if *last != sig {
|
||
|
*last = sig
|
||
|
return true
|
||
|
}
|
||
|
return false
|
||
|
}
|
||
|
|
||
4 years ago
|
func Print(w *bufio.Writer, v ...interface{}) {
|
||
4 years ago
|
print(w, reflect.ValueOf(v), make(map[uintptr]bool))
|
||
|
}
|
||
|
|
||
4 years ago
|
var (
|
||
|
netaddrIPType = reflect.TypeOf(netaddr.IP{})
|
||
|
netaddrIPPrefix = reflect.TypeOf(netaddr.IPPrefix{})
|
||
|
wgkeyKeyType = reflect.TypeOf(wgkey.Key{})
|
||
|
wgkeyPrivateType = reflect.TypeOf(wgkey.Private{})
|
||
|
tailcfgDiscoKeyType = reflect.TypeOf(tailcfg.DiscoKey{})
|
||
|
)
|
||
|
|
||
4 years ago
|
func print(w *bufio.Writer, v reflect.Value, visited map[uintptr]bool) {
|
||
4 years ago
|
if !v.IsValid() {
|
||
|
return
|
||
|
}
|
||
4 years ago
|
|
||
|
// Special case some common types.
|
||
|
if v.CanInterface() {
|
||
|
switch v.Type() {
|
||
|
case netaddrIPType:
|
||
|
var b []byte
|
||
|
var err error
|
||
|
if v.CanAddr() {
|
||
|
x := v.Addr().Interface().(*netaddr.IP)
|
||
|
b, err = x.MarshalText()
|
||
|
} else {
|
||
|
x := v.Interface().(netaddr.IP)
|
||
|
b, err = x.MarshalText()
|
||
|
}
|
||
|
if err == nil {
|
||
|
w.Write(b)
|
||
|
return
|
||
|
}
|
||
|
case netaddrIPPrefix:
|
||
|
var b []byte
|
||
|
var err error
|
||
|
if v.CanAddr() {
|
||
|
x := v.Addr().Interface().(*netaddr.IPPrefix)
|
||
|
b, err = x.MarshalText()
|
||
|
} else {
|
||
|
x := v.Interface().(netaddr.IPPrefix)
|
||
|
b, err = x.MarshalText()
|
||
|
}
|
||
|
if err == nil {
|
||
|
w.Write(b)
|
||
|
return
|
||
|
}
|
||
|
case wgkeyKeyType:
|
||
|
if v.CanAddr() {
|
||
|
x := v.Addr().Interface().(*wgkey.Key)
|
||
|
w.Write(x[:])
|
||
|
} else {
|
||
|
x := v.Interface().(wgkey.Key)
|
||
|
w.Write(x[:])
|
||
|
}
|
||
|
return
|
||
|
case wgkeyPrivateType:
|
||
|
if v.CanAddr() {
|
||
|
x := v.Addr().Interface().(*wgkey.Private)
|
||
|
w.Write(x[:])
|
||
|
} else {
|
||
|
x := v.Interface().(wgkey.Private)
|
||
|
w.Write(x[:])
|
||
|
}
|
||
|
return
|
||
|
case tailcfgDiscoKeyType:
|
||
|
if v.CanAddr() {
|
||
|
x := v.Addr().Interface().(*tailcfg.DiscoKey)
|
||
|
w.Write(x[:])
|
||
|
} else {
|
||
|
x := v.Interface().(tailcfg.DiscoKey)
|
||
|
w.Write(x[:])
|
||
|
}
|
||
|
return
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// Generic handling.
|
||
4 years ago
|
switch v.Kind() {
|
||
|
default:
|
||
|
panic(fmt.Sprintf("unhandled kind %v for type %v", v.Kind(), v.Type()))
|
||
|
case reflect.Ptr:
|
||
|
ptr := v.Pointer()
|
||
|
if visited[ptr] {
|
||
|
return
|
||
|
}
|
||
|
visited[ptr] = true
|
||
|
print(w, v.Elem(), visited)
|
||
|
return
|
||
|
case reflect.Struct:
|
||
4 years ago
|
w.WriteString("struct{\n")
|
||
4 years ago
|
for i, n := 0, v.NumField(); i < n; i++ {
|
||
4 years ago
|
fmt.Fprintf(w, " [%d]: ", i)
|
||
4 years ago
|
print(w, v.Field(i), visited)
|
||
4 years ago
|
w.WriteString("\n")
|
||
4 years ago
|
}
|
||
4 years ago
|
w.WriteString("}\n")
|
||
4 years ago
|
case reflect.Slice, reflect.Array:
|
||
|
if v.Type().Elem().Kind() == reflect.Uint8 && v.CanInterface() {
|
||
|
fmt.Fprintf(w, "%q", v.Interface())
|
||
|
return
|
||
|
}
|
||
|
fmt.Fprintf(w, "[%d]{\n", v.Len())
|
||
|
for i, ln := 0, v.Len(); i < ln; i++ {
|
||
|
fmt.Fprintf(w, " [%d]: ", i)
|
||
|
print(w, v.Index(i), visited)
|
||
4 years ago
|
w.WriteString("\n")
|
||
4 years ago
|
}
|
||
4 years ago
|
w.WriteString("}\n")
|
||
4 years ago
|
case reflect.Interface:
|
||
|
print(w, v.Elem(), visited)
|
||
|
case reflect.Map:
|
||
|
sm := newSortedMap(v)
|
||
|
fmt.Fprintf(w, "map[%d]{\n", len(sm.Key))
|
||
|
for i, k := range sm.Key {
|
||
|
print(w, k, visited)
|
||
4 years ago
|
w.WriteString(": ")
|
||
4 years ago
|
print(w, sm.Value[i], visited)
|
||
4 years ago
|
w.WriteString("\n")
|
||
4 years ago
|
}
|
||
4 years ago
|
w.WriteString("}\n")
|
||
4 years ago
|
case reflect.String:
|
||
4 years ago
|
w.WriteString(v.String())
|
||
4 years ago
|
case reflect.Bool:
|
||
|
fmt.Fprintf(w, "%v", v.Bool())
|
||
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
||
|
fmt.Fprintf(w, "%v", v.Int())
|
||
|
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
|
||
|
fmt.Fprintf(w, "%v", v.Uint())
|
||
|
case reflect.Float32, reflect.Float64:
|
||
|
fmt.Fprintf(w, "%v", v.Float())
|
||
|
case reflect.Complex64, reflect.Complex128:
|
||
|
fmt.Fprintf(w, "%v", v.Complex())
|
||
|
}
|
||
|
}
|