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

validate inputs #121

Merged
merged 1 commit into from
Apr 14, 2022
Merged
Show file tree
Hide file tree
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
94 changes: 94 additions & 0 deletions cmd/gvproxy/flags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package main

import (
"flag"
"net"

"github.com/containers/gvisor-tap-vsock/pkg/types"
"github.com/pkg/errors"
)

const (
gatewayMacAddress = "5a:94:ef:e4:0c:dd"
vmMacAddress = "5a:94:ef:e4:0c:ee"
)

type proxyFlags struct {
debug bool
mtu int
subnet string
gatewayIP string
hostIP string
vmIP string
gatewayMacAddress string
vmMacAddress string
}

func parseProxyFlags() (*types.Configuration, error) {
f := &proxyFlags{}

flag.BoolVar(&f.debug, "debug", false, "Print debug info")
flag.IntVar(&f.mtu, "mtu", 1500, "Set the MTU")

flag.StringVar(&f.subnet, "subnet", "192.168.127.0/24", "Set the subnet")
flag.StringVar(&f.gatewayIP, "gateway-ip", "192.168.127.1", "Set the IP for the gateway")
flag.StringVar(&f.hostIP, "host-ip", "192.168.127.254", "Set the IP for accessing the host from the WSL 2 VM")
flag.StringVar(&f.vmIP, "vm-ip", "192.168.127.2", "Set the IP for the WSL 2 VM")

flag.Parse()

if net.ParseIP(f.gatewayIP) == nil {
return nil, errors.New("invalid gateway-ip")
}
if net.ParseIP(f.hostIP) == nil {
return nil, errors.New("invalid host-ip")
}
if net.ParseIP(f.vmIP) == nil {
return nil, errors.New("invalid vm-ip")
}
if _, _, err := net.ParseCIDR(f.subnet); err != nil {
return nil, errors.Wrap(err, "invalid subnet")
}

f.gatewayMacAddress = gatewayMacAddress
f.vmMacAddress = vmMacAddress

return configuration(f), nil
}

func configuration(c *proxyFlags) *types.Configuration {
return &types.Configuration{
Debug: c.debug,
CaptureFile: "",
MTU: c.mtu,
Subnet: c.subnet,
GatewayIP: c.gatewayIP,
GatewayMacAddress: gatewayMacAddress,
DHCPStaticLeases: map[string]string{
c.vmIP: c.vmMacAddress,
},
DNS: []types.Zone{
{
Name: "internal.",
Records: []types.Record{
{
Name: "gateway",
IP: net.ParseIP(c.gatewayIP),
},
{
Name: "host",
IP: net.ParseIP(c.hostIP),
},
},
},
},
DNSSearchDomains: nil,
Forwards: map[string]string{},
NAT: map[string]string{
c.hostIP: "127.0.0.1",
},
GatewayVirtualIPs: []string{c.hostIP},
VpnKitUUIDMacAddresses: map[string]string{},
Protocol: types.HyperKitProtocol,
}
}
76 changes: 11 additions & 65 deletions cmd/gvproxy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ package main

import (
"context"
"flag"
"fmt"
"net"
"os"
"os/signal"
"syscall"
Expand All @@ -19,34 +17,23 @@ import (
)

var (
debug bool
mtu int

exitCode int

subnet string
gatewayIP string
hostIP string
vmIP string
)

const (
gatewayMacAddress = "5a:94:ef:e4:0c:dd"
vmMacAddress = "5a:94:ef:e4:0c:ee"
)

func main() {
log.SetOutput(os.Stderr)

flag.BoolVar(&debug, "debug", false, "Print debug info")
flag.IntVar(&mtu, "mtu", 1500, "Set the MTU")
config, err := parseProxyFlags()
if err != nil {
log.Error(err)
exitCode = 1
return
}

flag.StringVar(&subnet, "subnet", "192.168.127.0/24", "Set the subnet")
flag.StringVar(&gatewayIP, "gateway-ip", "192.168.127.1", "Set the IP for the gateway")
flag.StringVar(&hostIP, "host-ip", "192.168.127.254", "Set the IP for accessing the host from the WSL 2 VM")
flag.StringVar(&vmIP, "vm-ip", "192.168.127.2", "Set the IP for the WSL 2 VM")
if config.Debug {
log.SetLevel(log.DebugLevel)
}

flag.Parse()
ctx, cancel := context.WithCancel(context.Background())
// Make this the last defer statement in the stack
defer os.Exit(exitCode)
Expand All @@ -56,49 +43,8 @@ func main() {
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM, syscall.SIGINT)

if debug {
log.SetLevel(log.DebugLevel)
}

config := types.Configuration{
Debug: debug,
CaptureFile: "",
MTU: mtu,
Subnet: subnet,
GatewayIP: gatewayIP,
GatewayMacAddress: gatewayMacAddress,
DHCPStaticLeases: map[string]string{
vmIP: vmMacAddress,
},
DNS: []types.Zone{
{
Name: "internal.",
Records: []types.Record{
{
Name: "gateway",
IP: net.ParseIP(gatewayIP),
},
{
Name: "host",
IP: net.ParseIP(hostIP),
},
},
},
},
DNSSearchDomains: nil,
Forwards: map[string]string{},
NAT: map[string]string{
hostIP: "127.0.0.1",
},
GatewayVirtualIPs: []string{hostIP},
VpnKitUUIDMacAddresses: map[string]string{
"c3d68012-0208-11ea-9fd7-f2189899ab08": vmMacAddress,
},
Protocol: types.HyperKitProtocol,
}

groupErrs.Go(func() error {
return run(ctx, groupErrs, &config)
return run(ctx, groupErrs, config)
})

// Wait for something to happen
Expand All @@ -125,7 +71,7 @@ func run(ctx context.Context, g *errgroup.Group, configuration *types.Configurat
return err
}

lnDns, err := vn.Listen("tcp", fmt.Sprintf("%s:53", gatewayIP))
lnDns, err := vn.Listen("tcp", fmt.Sprintf("%s:53", configuration.GatewayIP))
if err != nil {
return err
}
Expand Down
76 changes: 76 additions & 0 deletions cmd/vm/flags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package main

import (
"flag"
"fmt"
"net"
"os"

"github.com/pkg/errors"
"github.com/vishvananda/netlink"
)

const (
vmMacAddress = "5a:94:ef:e4:0c:ee"
)

type VMFlags struct {
Endpoint string
Iface string
Debug bool
MTU int

Subnet string
GatewayIP string
HostIP string
VMIP string
ProxyMTU int
MAC string
}

func parseVMFlags() (*VMFlags, error) {
f := &VMFlags{}

flag.StringVar(&f.Endpoint, "path", "gvproxy.exe", "path to gvproxy.exe")
flag.StringVar(&f.Iface, "iface", "tap0", "tap interface name")
flag.BoolVar(&f.Debug, "debug", false, "debug")
flag.IntVar(&f.MTU, "mtu", 4000, "mtu")

flag.StringVar(&f.Subnet, "subnet", "192.168.127.0/24", "Set the subnet")
flag.StringVar(&f.GatewayIP, "gateway-ip", "192.168.127.1", "Set the IP for the gateway")
flag.StringVar(&f.HostIP, "host-ip", "192.168.127.254", "Set the IP for accessing the host from the WSL 2 VM")
flag.StringVar(&f.VMIP, "vm-ip", "192.168.127.2", "Set the IP for the WSL 2 VM")
flag.IntVar(&f.ProxyMTU, "proxy-mtu", 1500, "Set the MTU for the proxy")

flag.Parse()

if _, err := os.Stat(f.Endpoint); err != nil {
return nil, errors.Wrapf(err, "error verifying path %s", f.Endpoint)
}
if net.ParseIP(f.GatewayIP) == nil {
return nil, errors.New("invalid gateway-ip")
}
if net.ParseIP(f.HostIP) == nil {
return nil, errors.New("invalid host-ip")
}
if net.ParseIP(f.VMIP) == nil {
return nil, errors.New("invalid vm-ip")
}
if _, _, err := net.ParseCIDR(f.Subnet); err != nil {
return nil, errors.Wrap(err, "invalid subnet")
}

links, err := netlink.LinkList()
if err != nil {
return nil, err
}
for _, link := range links {
if f.Iface == link.Attrs().Name {
return nil, errors.New(fmt.Sprintf("interface %s already exists", link.Attrs().Name))
}
}

f.MAC = vmMacAddress

return f, nil
}
Loading