Merge pull request #68 from repos-holder/main
Fix build errors for Windows
This commit is contained in:
@@ -4,6 +4,7 @@ import "github.com/songgao/water"
|
||||
|
||||
type TUN struct {
|
||||
Iface *water.Interface
|
||||
MTU int
|
||||
Src string
|
||||
Dst string
|
||||
}
|
||||
|
@@ -4,6 +4,7 @@
|
||||
package tun
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
"os/exec"
|
||||
@@ -13,11 +14,19 @@ import (
|
||||
|
||||
// New creates and returns a new TUN interface for the application.
|
||||
func New(name string, opts ...Option) (*TUN, error) {
|
||||
result := TUN{}
|
||||
|
||||
// Apply options early to set struct values for interface creation.
|
||||
err := result.Apply(opts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// TUN on Windows requires address and network to be set on device creation stage
|
||||
// We also set network to 0.0.0.0/0 so we able to reach networks behind the node
|
||||
// https://github.com/songgao/water/blob/master/params_windows.go
|
||||
// https://gitlab.com/openconnect/openconnect/-/blob/master/tun-win32.c
|
||||
ip, _, err := net.ParseCIDR(address)
|
||||
ip, _, err := net.ParseCIDR(result.Src)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -37,30 +46,55 @@ func New(name string, opts ...Option) (*TUN, error) {
|
||||
},
|
||||
}
|
||||
|
||||
// Interface should be enabled before creation of water interface
|
||||
// Otherwise there will be an error "The system cannot find the file specified."
|
||||
netsh("interface", "set", "interface", "name=", name, "enable")
|
||||
|
||||
// Create Water Interface
|
||||
iface, err := water.New(cfg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Create TUN result struct
|
||||
result := TUN{
|
||||
Iface: iface,
|
||||
// Set TUN interface to newly created interface
|
||||
result.Iface = iface
|
||||
|
||||
// Apply options to setup TUN interface configuration
|
||||
// Setup interface address
|
||||
err = result.setupAddress(result.Src)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Setup interface mtu size
|
||||
err = result.setupMTU(result.MTU)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Apply options to set TUN config values
|
||||
err = result.Apply(opts...)
|
||||
return &result, err
|
||||
}
|
||||
|
||||
// setMTU sets the Maximum Tansmission Unit Size for a
|
||||
// Packet on the interface.
|
||||
// setMTU configures the interface's MTU.
|
||||
func (t *TUN) setMTU(mtu int) error {
|
||||
t.MTU = mtu
|
||||
return nil
|
||||
}
|
||||
|
||||
// setAddress configures the interface's address.
|
||||
func (t *TUN) setAddress(address string) error {
|
||||
t.Src = address
|
||||
return nil
|
||||
}
|
||||
|
||||
// setupMTU sets the Maximum Tansmission Unit Size for a
|
||||
// Packet on the interface.
|
||||
func (t *TUN) setupMTU(mtu int) error {
|
||||
return netsh("interface", "ipv4", "set", "subinterface", t.Iface.Name(), "mtu=", fmt.Sprintf("%d", mtu))
|
||||
}
|
||||
|
||||
// setAddress sets the interface's destination address and subnet.
|
||||
func (t *TUN) setAddress(address string) error {
|
||||
// setupAddress sets the interface's destination address and subnet.
|
||||
func (t *TUN) setupAddress(address string) error {
|
||||
return netsh("interface", "ip", "set", "address", "name=", t.Iface.Name(), "static", address)
|
||||
}
|
||||
|
||||
@@ -83,7 +117,7 @@ func (t *TUN) Down() error {
|
||||
|
||||
// Delete removes a TUN device from the host.
|
||||
func Delete(name string) error {
|
||||
return nil
|
||||
return netsh("interface", "set", "interface", "name=", name, "disable")
|
||||
}
|
||||
|
||||
func netsh(args ...string) (err error) {
|
||||
|
Reference in New Issue
Block a user