Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support unsafe_routes on Windows #184

Merged
merged 3 commits into from
Feb 26, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 23 additions & 8 deletions tun_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@ import (
"fmt"
"net"
"os/exec"
"strconv"

"github.com/songgao/water"
)

type Tun struct {
Device string
Cidr *net.IPNet
MTU int
Device string
Cidr *net.IPNet
MTU int
UnsafeRoutes []route

*water.Interface
}
Expand All @@ -20,13 +22,12 @@ func newTun(deviceName string, cidr *net.IPNet, defaultMTU int, routes []route,
if len(routes) > 0 {
return nil, fmt.Errorf("Route MTU not supported in Windows")
}
if len(unsafeRoutes) > 0 {
return nil, fmt.Errorf("unsafeRoutes not supported in Windows")
}

// NOTE: You cannot set the deviceName under Windows, so you must check tun.Device after calling .Activate()
return &Tun{
Cidr: cidr,
MTU: defaultMTU,
Cidr: cidr,
MTU: defaultMTU,
UnsafeRoutes: unsafeRoutes,
}, nil
}

Expand Down Expand Up @@ -66,6 +67,20 @@ func (c *Tun) Activate() error {
return fmt.Errorf("failed to run 'netsh' to set MTU: %s", err)
}

iface, err := net.InterfaceByName(c.Device)
if err != nil {
return fmt.Errorf("failed to find interface named %s: %v", c.Device, err)
}

for _, r := range c.UnsafeRoutes {
err = exec.Command(
"C:\\Windows\\System32\\route.exe", "add", r.route.String(), r.via.String(), "IF", strconv.Itoa(iface.Index),
).Run()
if err != nil {
return fmt.Errorf("failed to add the unsafe_route %s: %v", r.route.String(), err)
}
}

return nil
}

Expand Down