2021-06-06 00:40:44 +02:00
|
|
|
package iface
|
|
|
|
|
|
|
|
import (
|
2022-01-17 14:01:58 +01:00
|
|
|
"fmt"
|
2021-06-06 00:40:44 +02:00
|
|
|
log "github.com/sirupsen/logrus"
|
2022-01-17 14:01:58 +01:00
|
|
|
"golang.org/x/sys/windows"
|
|
|
|
"golang.zx2c4.com/wireguard/windows/driver"
|
2021-06-06 00:40:44 +02:00
|
|
|
"golang.zx2c4.com/wireguard/windows/tunnel/winipcfg"
|
2021-06-06 21:51:56 +02:00
|
|
|
"net"
|
2021-06-06 00:40:44 +02:00
|
|
|
)
|
|
|
|
|
2021-06-24 11:46:33 +02:00
|
|
|
// Create Creates a new Wireguard interface, sets a given IP and brings it up.
|
2022-01-17 14:01:58 +01:00
|
|
|
func (w *WGIface) Create() error {
|
2022-06-04 19:41:01 +02:00
|
|
|
w.mu.Lock()
|
|
|
|
defer w.mu.Unlock()
|
2021-06-06 00:40:44 +02:00
|
|
|
|
2022-01-17 14:01:58 +01:00
|
|
|
WintunStaticRequestedGUID, _ := windows.GenerateGUID()
|
|
|
|
adapter, err := driver.CreateAdapter(w.Name, "WireGuard", &WintunStaticRequestedGUID)
|
2021-06-06 00:40:44 +02:00
|
|
|
if err != nil {
|
2022-01-17 14:01:58 +01:00
|
|
|
err = fmt.Errorf("error creating adapter: %w", err)
|
2021-06-06 00:40:44 +02:00
|
|
|
return err
|
|
|
|
}
|
2022-01-17 14:01:58 +01:00
|
|
|
w.Interface = adapter
|
|
|
|
luid := adapter.LUID()
|
|
|
|
err = adapter.SetAdapterState(driver.AdapterStateUp)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
state, _ := luid.GUID()
|
|
|
|
log.Debugln("device guid: ", state.String())
|
|
|
|
return w.assignAddr(luid)
|
2021-06-06 00:40:44 +02:00
|
|
|
}
|
|
|
|
|
2022-01-17 14:01:58 +01:00
|
|
|
// assignAddr Adds IP address to the tunnel interface and network route based on the range provided
|
|
|
|
func (w *WGIface) assignAddr(luid winipcfg.LUID) error {
|
|
|
|
|
|
|
|
log.Debugf("adding address %s to interface: %s", w.Address.IP, w.Name)
|
|
|
|
err := luid.SetIPAddresses([]net.IPNet{{w.Address.IP, w.Address.Network.Mask}})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-07-19 15:02:11 +02:00
|
|
|
|
2022-01-17 14:01:58 +01:00
|
|
|
return nil
|
2021-07-19 15:02:11 +02:00
|
|
|
}
|
2022-06-04 19:41:01 +02:00
|
|
|
|
|
|
|
// UpdateAddr updates address of the interface
|
|
|
|
func (w *WGIface) UpdateAddr(newAddr string) error {
|
|
|
|
w.mu.Lock()
|
|
|
|
defer w.mu.Unlock()
|
|
|
|
|
|
|
|
luid := w.Interface.(*driver.Adapter).LUID()
|
|
|
|
addr, err := parseAddress(newAddr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Address = addr
|
|
|
|
return w.assignAddr(luid)
|
|
|
|
}
|
2022-07-02 12:02:17 +02:00
|
|
|
|
|
|
|
// WireguardModExists check if we can load wireguard mod (linux only)
|
|
|
|
func WireguardModExists() bool {
|
|
|
|
return false
|
|
|
|
}
|