2018-01-24 10:59:01 +00:00
|
|
|
package yggdrasil
|
|
|
|
|
2018-03-01 11:49:49 +00:00
|
|
|
import water "github.com/neilalexander/water"
|
2018-01-24 10:59:01 +00:00
|
|
|
import "os/exec"
|
|
|
|
import "strings"
|
|
|
|
import "fmt"
|
|
|
|
|
|
|
|
// This is to catch Windows platforms
|
|
|
|
|
2018-02-11 21:45:44 +00:00
|
|
|
func (tun *tunDevice) setup(ifname string, iftapmode bool, addr string, mtu int) error {
|
|
|
|
if !iftapmode {
|
|
|
|
tun.core.log.Printf("TUN mode is not supported on this platform, defaulting to TAP")
|
|
|
|
}
|
2018-01-24 10:59:01 +00:00
|
|
|
config := water.Config{DeviceType: water.TAP}
|
|
|
|
config.PlatformSpecificParams.ComponentID = "tap0901"
|
|
|
|
config.PlatformSpecificParams.Network = "169.254.0.1/32"
|
|
|
|
iface, err := water.New(config)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2018-01-25 03:14:26 +00:00
|
|
|
// Disable/enable the interface to resets its configuration (invalidating iface)
|
|
|
|
cmd := exec.Command("netsh", "interface", "set", "interface", iface.Name(), "admin=DISABLED")
|
|
|
|
tun.core.log.Printf("netsh command: %v", strings.Join(cmd.Args, " "))
|
|
|
|
output, err := cmd.CombinedOutput()
|
|
|
|
if err != nil {
|
|
|
|
tun.core.log.Printf("Windows netsh failed: %v.", err)
|
|
|
|
tun.core.log.Println(string(output))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
cmd = exec.Command("netsh", "interface", "set", "interface", iface.Name(), "admin=ENABLED")
|
|
|
|
tun.core.log.Printf("netsh command: %v", strings.Join(cmd.Args, " "))
|
|
|
|
output, err = cmd.CombinedOutput()
|
|
|
|
if err != nil {
|
|
|
|
tun.core.log.Printf("Windows netsh failed: %v.", err)
|
|
|
|
tun.core.log.Println(string(output))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Get a new iface
|
|
|
|
iface, err = water.New(config)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2018-01-24 10:59:01 +00:00
|
|
|
tun.iface = iface
|
|
|
|
tun.mtu = mtu
|
2018-01-25 17:44:56 +00:00
|
|
|
err = tun.setupMTU(tun.mtu)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2018-01-24 10:59:01 +00:00
|
|
|
return tun.setupAddress(addr)
|
|
|
|
}
|
|
|
|
|
2018-01-25 17:44:56 +00:00
|
|
|
func (tun *tunDevice) setupMTU(mtu int) error {
|
|
|
|
// Set MTU
|
|
|
|
cmd := exec.Command("netsh", "interface", "ipv6", "set", "subinterface",
|
|
|
|
fmt.Sprintf("interface=%s", tun.iface.Name()),
|
|
|
|
fmt.Sprintf("mtu=%d", mtu),
|
|
|
|
"store=active")
|
|
|
|
tun.core.log.Printf("netsh command: %v", strings.Join(cmd.Args, " "))
|
|
|
|
output, err := cmd.CombinedOutput()
|
|
|
|
if err != nil {
|
|
|
|
tun.core.log.Printf("Windows netsh failed: %v.", err)
|
|
|
|
tun.core.log.Println(string(output))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-01-24 10:59:01 +00:00
|
|
|
func (tun *tunDevice) setupAddress(addr string) error {
|
|
|
|
// 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")
|
2018-01-24 10:59:01 +00:00
|
|
|
tun.core.log.Printf("netsh command: %v", strings.Join(cmd.Args, " "))
|
|
|
|
output, err := cmd.CombinedOutput()
|
|
|
|
if err != nil {
|
|
|
|
tun.core.log.Printf("Windows netsh failed: %v.", err)
|
|
|
|
tun.core.log.Println(string(output))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|