2019-03-28 00:30:25 +00:00
|
|
|
package tuntap
|
2018-01-24 10:59:01 +00:00
|
|
|
|
2018-06-12 22:50:08 +00:00
|
|
|
import (
|
2019-07-19 21:21:30 +00:00
|
|
|
"errors"
|
2018-06-12 22:50:08 +00:00
|
|
|
"fmt"
|
|
|
|
"os/exec"
|
|
|
|
"strings"
|
2019-09-01 18:20:48 +00:00
|
|
|
"time"
|
2018-06-12 22:50:08 +00:00
|
|
|
|
2019-11-22 16:43:50 +00:00
|
|
|
wgtun "golang.zx2c4.com/wireguard/tun"
|
2018-06-12 22:50:08 +00:00
|
|
|
)
|
2018-01-24 10:59:01 +00:00
|
|
|
|
|
|
|
// This is to catch Windows platforms
|
|
|
|
|
2019-11-22 16:43:50 +00:00
|
|
|
// Configures the TUN adapter with the correct IPv6 address and MTU.
|
|
|
|
func (tun *TunAdapter) setup(ifname string, addr string, mtu int) error {
|
|
|
|
iface, err := wgtun.CreateTUN(ifname, mtu)
|
2018-01-25 03:14:26 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2018-01-24 10:59:01 +00:00
|
|
|
tun.iface = iface
|
2019-11-22 16:43:50 +00:00
|
|
|
if mtu, err := iface.MTU(); err == nil {
|
|
|
|
tun.mtu = getSupportedMTU(mtu)
|
|
|
|
} else {
|
|
|
|
tun.mtu = 0
|
2018-01-25 17:44:56 +00:00
|
|
|
}
|
2018-01-24 10:59:01 +00:00
|
|
|
return tun.setupAddress(addr)
|
|
|
|
}
|
|
|
|
|
2018-06-12 21:45:53 +00:00
|
|
|
// Sets the MTU of the TAP adapter.
|
2019-03-28 00:30:25 +00:00
|
|
|
func (tun *TunAdapter) setupMTU(mtu int) error {
|
2019-07-20 15:13:54 +00:00
|
|
|
if tun.iface == nil || tun.iface.Name() == "" {
|
|
|
|
return errors.New("Can't configure MTU as TAP adapter is not present")
|
|
|
|
}
|
2018-01-25 17:44:56 +00:00
|
|
|
// Set MTU
|
|
|
|
cmd := exec.Command("netsh", "interface", "ipv6", "set", "subinterface",
|
|
|
|
fmt.Sprintf("interface=%s", tun.iface.Name()),
|
|
|
|
fmt.Sprintf("mtu=%d", mtu),
|
|
|
|
"store=active")
|
2019-07-06 19:08:32 +00:00
|
|
|
tun.log.Debugln("netsh command:", strings.Join(cmd.Args, " "))
|
2018-01-25 17:44:56 +00:00
|
|
|
output, err := cmd.CombinedOutput()
|
|
|
|
if err != nil {
|
2019-07-06 19:08:32 +00:00
|
|
|
tun.log.Errorln("Windows netsh failed:", err)
|
2019-04-20 15:32:27 +00:00
|
|
|
tun.log.Traceln(string(output))
|
2018-01-25 17:44:56 +00:00
|
|
|
return err
|
|
|
|
}
|
2019-09-01 18:20:48 +00:00
|
|
|
time.Sleep(time.Second) // FIXME artifical delay to give netsh time to take effect
|
2018-01-25 17:44:56 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-06-12 21:45:53 +00:00
|
|
|
// Sets the IPv6 address of the TAP adapter.
|
2019-03-28 00:30:25 +00:00
|
|
|
func (tun *TunAdapter) setupAddress(addr string) error {
|
2019-07-20 15:13:54 +00:00
|
|
|
if tun.iface == nil || tun.iface.Name() == "" {
|
|
|
|
return errors.New("Can't configure IPv6 address as TAP adapter is not present")
|
|
|
|
}
|
2018-01-24 10:59:01 +00:00
|
|
|
// Set address
|
2018-01-25 03:14:26 +00:00
|
|
|
cmd := exec.Command("netsh", "interface", "ipv6", "add", "address",
|
2018-01-24 22:37:08 +00:00
|
|
|
fmt.Sprintf("interface=%s", tun.iface.Name()),
|
2018-01-25 03:14:26 +00:00
|
|
|
fmt.Sprintf("addr=%s", addr),
|
|
|
|
"store=active")
|
2019-07-06 19:08:32 +00:00
|
|
|
tun.log.Debugln("netsh command:", strings.Join(cmd.Args, " "))
|
2018-01-24 10:59:01 +00:00
|
|
|
output, err := cmd.CombinedOutput()
|
|
|
|
if err != nil {
|
2019-07-06 19:08:32 +00:00
|
|
|
tun.log.Errorln("Windows netsh failed:", err)
|
2019-04-20 15:32:27 +00:00
|
|
|
tun.log.Traceln(string(output))
|
2018-01-24 10:59:01 +00:00
|
|
|
return err
|
|
|
|
}
|
2019-09-01 18:20:48 +00:00
|
|
|
time.Sleep(time.Second) // FIXME artifical delay to give netsh time to take effect
|
2018-01-24 10:59:01 +00:00
|
|
|
return nil
|
|
|
|
}
|