mirror of
https://github.com/tailscale/tailscale.git
synced 2025-02-18 02:48:40 +00:00
wgengine/wglog: match upstream wireguard-go's code for wireguardGoString
It is a bit faster. But more importantly, it matches upstream byte-for-byte, which ensures there'll be no corner cases in which we disagree. name old time/op new time/op delta SetPeers-8 3.58µs ± 0% 3.16µs ± 2% -11.74% (p=0.016 n=4+5) name old alloc/op new alloc/op delta SetPeers-8 2.53kB ± 0% 2.53kB ± 0% ~ (all equal) name old allocs/op new allocs/op delta SetPeers-8 99.0 ± 0% 99.0 ± 0% ~ (all equal) Signed-off-by: Josh Bleecher Snyder <josh@tailscale.com>
This commit is contained in:
parent
adaecd83c8
commit
e92fd19484
@ -6,7 +6,6 @@
|
|||||||
package wglog
|
package wglog
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/base64"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
@ -121,17 +120,20 @@ func (x *Logger) SetPeers(peers []wgcfg.Peer) {
|
|||||||
|
|
||||||
// wireguardGoString prints p in the same format used by wireguard-go.
|
// wireguardGoString prints p in the same format used by wireguard-go.
|
||||||
func wireguardGoString(k wgkey.Key) string {
|
func wireguardGoString(k wgkey.Key) string {
|
||||||
const prefix = "peer("
|
src := k
|
||||||
b := make([]byte, len(prefix)+44)
|
b64 := func(input byte) byte {
|
||||||
copy(b, prefix)
|
return input + 'A' + byte(((25-int(input))>>8)&6) - byte(((51-int(input))>>8)&75) - byte(((61-int(input))>>8)&15) + byte(((62-int(input))>>8)&3)
|
||||||
r := b[len(prefix):]
|
}
|
||||||
base64.StdEncoding.Encode(r, k[:])
|
b := []byte("peer(____…____)")
|
||||||
r = r[4:]
|
const first = len("peer(")
|
||||||
copy(r, "…")
|
const second = len("peer(____…")
|
||||||
r = r[len("…"):]
|
b[first+0] = b64((src[0] >> 2) & 63)
|
||||||
copy(r, b[len(prefix)+39:len(prefix)+43])
|
b[first+1] = b64(((src[0] << 4) | (src[1] >> 4)) & 63)
|
||||||
r = r[4:]
|
b[first+2] = b64(((src[1] << 2) | (src[2] >> 6)) & 63)
|
||||||
r[0] = ')'
|
b[first+3] = b64(src[2] & 63)
|
||||||
r = r[1:]
|
b[second+0] = b64(src[29] & 63)
|
||||||
return string(b[:len(b)-len(r)])
|
b[second+1] = b64((src[30] >> 2) & 63)
|
||||||
|
b[second+2] = b64(((src[30] << 4) | (src[31] >> 4)) & 63)
|
||||||
|
b[second+3] = b64((src[31] << 2) & 63)
|
||||||
|
return string(b)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user