Restricted VM networking

This commit is contained in:
AlexSSD7 2023-08-26 16:43:04 +01:00
commit 34e66cb01c
5 changed files with 31 additions and 27 deletions

View file

@ -4,6 +4,7 @@ import (
"context"
"log/slog"
"os"
"strings"
"github.com/AlexSSD7/linsk/vm"
"github.com/spf13/cobra"
@ -22,6 +23,22 @@ var shellCmd = &cobra.Command{
passthroughArg = args[0]
}
var forwardPortsConfig []vm.PortForwardingConfig
for i, fp := range strings.Split(forwardPortsFlagStr, ",") {
if fp == "" {
continue
}
fpc, err := vm.ParsePortForwardString(fp)
if err != nil {
slog.Error("Failed to parse port forward string", "index", i, "value", fp, "error", err)
os.Exit(1)
}
forwardPortsConfig = append(forwardPortsConfig, fpc)
}
os.Exit(runVM(passthroughArg, func(ctx context.Context, i *vm.Instance, fm *vm.FileManager) int {
sc, err := i.DialSSH()
if err != nil {
@ -103,14 +120,16 @@ var shellCmd = &cobra.Command{
}
return 0
}, nil))
}, forwardPortsConfig, unrestrictedNetworkingFlag))
return nil
},
}
var forwardPortsFlagStr string
var unrestrictedNetworkingFlag bool
func init() {
shellCmd.Flags().BoolVar(&unrestrictedNetworkingFlag, "unsafe-unrestricted-networking", false, "(UNSAFE) Enable unrestricted networking. This will allow the VM to connect to the internet.")
shellCmd.Flags().StringVar(&forwardPortsFlagStr, "forward-ports", "", "Extra TCP port forwarding rules. Syntax: '<HOST PORT>:<VM PORT>' OR '<HOST BIND IP>:<HOST PORT>:<VM PORT>'. Multiple rules split by comma are accepted.")
}