mirror of
https://github.com/tailscale/tailscale.git
synced 2024-11-26 03:25:35 +00:00
fe7c3e9c17
This is mostly code movement from the wireguard-go repo. Most of the new wgcfg package corresponds to the wireguard-go wgcfg package. wgengine/wgcfg/device{_test}.go was device/config{_test}.go. There were substantive but simple changes to device_test.go to remove internal package device references. The API of device.Config (now wgcfg.DeviceConfig) grew an error return; we previously logged the error and threw it away. Signed-off-by: Josh Bleecher Snyder <josh@tailscale.com>
62 lines
1.2 KiB
Go
62 lines
1.2 KiB
Go
// Copyright (c) 2021 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.
|
|
|
|
package wgcfg
|
|
|
|
import (
|
|
"io"
|
|
"sort"
|
|
|
|
"github.com/tailscale/wireguard-go/device"
|
|
"tailscale.com/types/logger"
|
|
)
|
|
|
|
func DeviceConfig(d *device.Device) (*Config, error) {
|
|
r, w := io.Pipe()
|
|
errc := make(chan error, 1)
|
|
go func() {
|
|
errc <- d.IpcGetOperation(w)
|
|
w.Close()
|
|
}()
|
|
cfg, err := FromUAPI(r)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if err := <-errc; err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
sort.Slice(cfg.Peers, func(i, j int) bool {
|
|
return cfg.Peers[i].PublicKey.LessThan(&cfg.Peers[j].PublicKey)
|
|
})
|
|
return cfg, nil
|
|
}
|
|
|
|
// ReconfigDevice replaces the existing device configuration with cfg.
|
|
func ReconfigDevice(d *device.Device, cfg *Config, logf logger.Logf) (err error) {
|
|
defer func() {
|
|
if err != nil {
|
|
logf("wgcfg.Reconfig failed: %v", err)
|
|
}
|
|
}()
|
|
|
|
prev, err := DeviceConfig(d)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
r, w := io.Pipe()
|
|
errc := make(chan error)
|
|
go func() {
|
|
errc <- d.IpcSetOperation(r)
|
|
}()
|
|
|
|
err = cfg.ToUAPI(w, prev)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
w.Close()
|
|
return <-errc
|
|
}
|