2021-05-01 12:45:37 +02:00
|
|
|
package iface
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os/exec"
|
2023-02-13 18:34:56 +01:00
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
2021-05-01 12:45:37 +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()
|
|
|
|
|
|
|
|
return w.createWithUserspace()
|
2021-06-24 11:46:33 +02:00
|
|
|
}
|
2021-05-01 12:45:37 +02:00
|
|
|
|
|
|
|
// assignAddr Adds IP address to the tunnel interface and network route based on the range provided
|
2022-01-17 14:01:58 +01:00
|
|
|
func (w *WGIface) assignAddr() error {
|
2023-02-13 18:34:56 +01:00
|
|
|
cmd := exec.Command("ifconfig", w.name, "inet", w.address.IP.String(), w.address.IP.String())
|
2021-05-01 12:45:37 +02:00
|
|
|
if out, err := cmd.CombinedOutput(); err != nil {
|
2023-02-13 18:34:56 +01:00
|
|
|
log.Infof(`adding addreess command "%v" failed with output %s and error: `, cmd.String(), out)
|
2021-05-01 12:45:37 +02:00
|
|
|
return err
|
|
|
|
}
|
2021-08-29 17:48:31 +02:00
|
|
|
|
2023-02-13 18:34:56 +01:00
|
|
|
routeCmd := exec.Command("route", "add", "-net", w.address.Network.String(), "-interface", w.name)
|
2022-01-17 14:01:58 +01:00
|
|
|
if out, err := routeCmd.CombinedOutput(); err != nil {
|
2023-02-13 18:34:56 +01:00
|
|
|
log.Printf(`adding route command "%v" failed with output %s and error: `, routeCmd.String(), out)
|
2021-08-29 17:48:31 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2021-07-19 15:02:11 +02:00
|
|
|
}
|