2021-01-29 20:16:36 +00:00
|
|
|
// 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"
|
|
|
|
|
2022-12-09 23:12:20 +00:00
|
|
|
"github.com/tailscale/wireguard-go/conn"
|
|
|
|
"github.com/tailscale/wireguard-go/device"
|
|
|
|
"github.com/tailscale/wireguard-go/tun"
|
2021-01-29 20:16:36 +00:00
|
|
|
"tailscale.com/types/logger"
|
2021-10-29 20:26:45 +00:00
|
|
|
"tailscale.com/util/multierr"
|
2021-01-29 20:16:36 +00:00
|
|
|
)
|
|
|
|
|
2021-11-16 19:35:25 +00:00
|
|
|
// NewDevice returns a wireguard-go Device configured for Tailscale use.
|
|
|
|
func NewDevice(tunDev tun.Device, bind conn.Bind, logger *device.Logger) *device.Device {
|
|
|
|
ret := device.NewDevice(tunDev, bind, logger)
|
|
|
|
ret.DisableSomeRoamingForBrokenMobileSemantics()
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2021-01-29 20:16:36 +00:00
|
|
|
func DeviceConfig(d *device.Device) (*Config, error) {
|
|
|
|
r, w := io.Pipe()
|
|
|
|
errc := make(chan error, 1)
|
|
|
|
go func() {
|
|
|
|
errc <- d.IpcGetOperation(w)
|
|
|
|
w.Close()
|
|
|
|
}()
|
2021-10-29 20:26:45 +00:00
|
|
|
cfg, fromErr := FromUAPI(r)
|
|
|
|
r.Close()
|
|
|
|
getErr := <-errc
|
|
|
|
err := multierr.New(getErr, fromErr)
|
2021-04-30 21:15:03 +00:00
|
|
|
if err != nil {
|
2021-01-29 20:16:36 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
sort.Slice(cfg.Peers, func(i, j int) bool {
|
2021-10-28 00:42:33 +00:00
|
|
|
return cfg.Peers[i].PublicKey.Less(cfg.Peers[j].PublicKey)
|
2021-01-29 20:16:36 +00:00
|
|
|
})
|
|
|
|
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()
|
2021-04-29 22:48:00 +00:00
|
|
|
errc := make(chan error, 1)
|
2021-01-29 20:16:36 +00:00
|
|
|
go func() {
|
|
|
|
errc <- d.IpcSetOperation(r)
|
2021-10-29 20:26:45 +00:00
|
|
|
r.Close()
|
2021-01-29 20:16:36 +00:00
|
|
|
}()
|
|
|
|
|
2021-11-11 02:42:16 +00:00
|
|
|
toErr := cfg.ToUAPI(logf, w, prev)
|
2021-01-29 20:16:36 +00:00
|
|
|
w.Close()
|
2021-10-29 20:26:45 +00:00
|
|
|
setErr := <-errc
|
|
|
|
return multierr.New(setErr, toErr)
|
2021-01-29 20:16:36 +00:00
|
|
|
}
|