2021-06-06 00:40:44 +02:00
|
|
|
package iface
|
|
|
|
|
|
|
|
import (
|
2022-01-17 14:01:58 +01:00
|
|
|
"fmt"
|
2023-02-13 18:34:56 +01:00
|
|
|
"net"
|
|
|
|
|
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
|
|
|
)
|
|
|
|
|
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()
|
2023-02-13 18:34:56 +01:00
|
|
|
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
|
|
|
|
}
|
2023-02-13 18:34:56 +01:00
|
|
|
w.netInterface = adapter
|
2022-01-17 14:01:58 +01:00
|
|
|
err = adapter.SetAdapterState(driver.AdapterStateUp)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-02-13 18:34:56 +01:00
|
|
|
state, _ := adapter.LUID().GUID()
|
2022-01-17 14:01:58 +01:00
|
|
|
log.Debugln("device guid: ", state.String())
|
2023-02-13 18:34:56 +01:00
|
|
|
return w.assignAddr()
|
2021-06-06 00:40:44 +02:00
|
|
|
}
|
|
|
|
|
2023-02-13 18:34:56 +01:00
|
|
|
// GetInterfaceGUIDString returns an interface GUID string
|
|
|
|
func (w *WGIface) GetInterfaceGUIDString() (string, error) {
|
|
|
|
if w.netInterface == nil {
|
|
|
|
return "", fmt.Errorf("interface has not been initialized yet")
|
|
|
|
}
|
|
|
|
windowsDevice := w.netInterface.(*driver.Adapter)
|
|
|
|
luid := windowsDevice.LUID()
|
|
|
|
guid, err := luid.GUID()
|
2022-01-17 14:01:58 +01:00
|
|
|
if err != nil {
|
2023-02-13 18:34:56 +01:00
|
|
|
return "", err
|
2022-01-17 14:01:58 +01:00
|
|
|
}
|
2023-02-13 18:34:56 +01:00
|
|
|
return guid.String(), nil
|
2021-07-19 15:02:11 +02:00
|
|
|
}
|
2022-06-04 19:41:01 +02:00
|
|
|
|
2023-02-13 18:34:56 +01:00
|
|
|
// Close closes the tunnel interface
|
|
|
|
func (w *WGIface) Close() error {
|
2022-06-04 19:41:01 +02:00
|
|
|
w.mu.Lock()
|
|
|
|
defer w.mu.Unlock()
|
2023-02-13 18:34:56 +01:00
|
|
|
if w.netInterface == nil {
|
|
|
|
return nil
|
2022-06-04 19:41:01 +02:00
|
|
|
}
|
|
|
|
|
2023-02-13 18:34:56 +01:00
|
|
|
return w.netInterface.Close()
|
2022-06-04 19:41:01 +02:00
|
|
|
}
|
2022-07-02 12:02:17 +02:00
|
|
|
|
2023-02-13 18:34:56 +01:00
|
|
|
// assignAddr Adds IP address to the tunnel interface and network route based on the range provided
|
|
|
|
func (w *WGIface) assignAddr() error {
|
|
|
|
luid := w.netInterface.(*driver.Adapter).LUID()
|
|
|
|
|
|
|
|
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}})
|
2022-11-23 13:39:42 +01:00
|
|
|
if err != nil {
|
2023-02-13 18:34:56 +01:00
|
|
|
return err
|
2022-11-23 13:39:42 +01:00
|
|
|
}
|
|
|
|
|
2023-02-13 18:34:56 +01:00
|
|
|
return nil
|
2022-07-02 12:02:17 +02:00
|
|
|
}
|