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
|
|
|
)
|
|
|
|
|
2023-03-17 10:37:27 +01:00
|
|
|
func (c *tunDevice) Create() error {
|
|
|
|
var err error
|
|
|
|
c.netInterface, err = c.createWithUserspace()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-06-04 19:41:01 +02:00
|
|
|
|
2023-03-17 10:37:27 +01:00
|
|
|
return c.assignAddr()
|
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
|
2023-03-17 10:37:27 +01:00
|
|
|
func (c *tunDevice) assignAddr() error {
|
|
|
|
cmd := exec.Command("ifconfig", c.name, "inet", c.address.IP.String(), c.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-03-17 10:37:27 +01:00
|
|
|
routeCmd := exec.Command("route", "add", "-net", c.address.Network.String(), "-interface", c.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
|
|
|
}
|