mirror of
https://github.com/tailscale/tailscale.git
synced 2026-09-18 09:08:16 -04:00
Move away from mutating global, exported variables in wireguard-go. Use device.Option's passed to device.NewDevice, instead. No functional changes, just API cleanup in preparation of future changes. Updates tailscale/corp#22467 Updates tailscale/corp#46396 Updates tailscale/corp#37878 Signed-off-by: Jordan Whited <jordan@tailscale.com>
41 lines
1.3 KiB
Go
41 lines
1.3 KiB
Go
// Copyright (c) Tailscale Inc & contributors
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
package wgcfg
|
|
|
|
import (
|
|
"fmt"
|
|
"net/netip"
|
|
|
|
"github.com/tailscale/wireguard-go/conn"
|
|
"github.com/tailscale/wireguard-go/device"
|
|
"github.com/tailscale/wireguard-go/tun"
|
|
"tailscale.com/types/logger"
|
|
)
|
|
|
|
// NewDevice returns a wireguard-go Device configured for Tailscale use.
|
|
func NewDevice(tunDev tun.Device, bind conn.Bind, logger *device.Logger) *device.Device {
|
|
return device.NewDevice(tunDev, bind, logger, getDeviceOptions()...)
|
|
}
|
|
|
|
// NewPeerLookupFunc returns a [device.PeerLookupFunc] that lazily
|
|
// creates peers using allowedIPs as the source of each peer's allowed
|
|
// IPs. The peer's endpoint is derived from its public key via bind.
|
|
func NewPeerLookupFunc(bind conn.Bind, logf logger.Logf, allowedIPs func(device.NoisePublicKey) ([]netip.Prefix, bool)) device.PeerLookupFunc {
|
|
return func(pubk device.NoisePublicKey) (_ *device.NewPeerConfig, ok bool) {
|
|
ips, ok := allowedIPs(pubk)
|
|
if !ok {
|
|
return nil, false
|
|
}
|
|
ep, err := bind.ParseEndpoint(fmt.Sprintf("%x", pubk[:]))
|
|
if err != nil {
|
|
logf("wgcfg: failed to parse endpoint for peer %x: %v", pubk[:8], err)
|
|
return nil, false
|
|
}
|
|
return &device.NewPeerConfig{
|
|
AllowedIPs: ips,
|
|
Endpoint: ep,
|
|
}, true
|
|
}
|
|
}
|