2021-05-01 12:45:37 +02:00
|
|
|
package iface
|
|
|
|
|
|
|
|
import (
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"os/exec"
|
|
|
|
)
|
|
|
|
|
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 {
|
|
|
|
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 {
|
|
|
|
//mask,_ := w.Address.Network.Mask.Size()
|
|
|
|
//
|
|
|
|
//address := fmt.Sprintf("%s/%d",w.Address.IP.String() , mask)
|
2021-05-01 12:45:37 +02:00
|
|
|
|
2022-01-17 14:01:58 +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 {
|
2022-01-17 14:01:58 +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
|
|
|
|
2022-01-17 14:01:58 +01:00
|
|
|
routeCmd := exec.Command("route", "add", "-net", w.Address.Network.String(), "-interface", w.Name)
|
|
|
|
if out, err := routeCmd.CombinedOutput(); err != nil {
|
|
|
|
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
|
|
|
}
|