-
Notifications
You must be signed in to change notification settings - Fork 629
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
limactl: add tunnel
command (experimental)
#2710
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
package main | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
"runtime" | ||
"strconv" | ||
|
||
"github.com/lima-vm/lima/pkg/freeport" | ||
"github.com/lima-vm/lima/pkg/sshutil" | ||
"github.com/lima-vm/lima/pkg/store" | ||
"github.com/mattn/go-shellwords" | ||
"github.com/sirupsen/logrus" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
const tunnelHelp = `Create a tunnel for Lima | ||
|
||
Create a SOCKS tunnel so that the host can join the guest network. | ||
` | ||
|
||
func newTunnelCommand() *cobra.Command { | ||
tunnelCmd := &cobra.Command{ | ||
Use: "tunnel [flags] INSTANCE", | ||
Short: "Create a tunnel for Lima", | ||
PersistentPreRun: func(*cobra.Command, []string) { | ||
logrus.Warn("`limactl tunnel` is experimental") | ||
}, | ||
Long: tunnelHelp, | ||
Args: WrapArgsError(cobra.ExactArgs(1)), | ||
RunE: tunnelAction, | ||
ValidArgsFunction: tunnelBashComplete, | ||
SilenceErrors: true, | ||
GroupID: advancedCommand, | ||
} | ||
|
||
tunnelCmd.Flags().SetInterspersed(false) | ||
// TODO: implement l2tp, ikev2, masque, ... | ||
tunnelCmd.Flags().String("type", "socks", "Tunnel type, currently only \"socks\" is implemented") | ||
tunnelCmd.Flags().Int("socks-port", 0, "SOCKS port, defaults to a random port") | ||
return tunnelCmd | ||
} | ||
|
||
func tunnelAction(cmd *cobra.Command, args []string) error { | ||
flags := cmd.Flags() | ||
tunnelType, err := flags.GetString("type") | ||
if err != nil { | ||
return err | ||
} | ||
if tunnelType != "socks" { | ||
return fmt.Errorf("unknown tunnel type: %q", tunnelType) | ||
} | ||
port, err := flags.GetInt("socks-port") | ||
if err != nil { | ||
return err | ||
} | ||
if port != 0 && (port < 1024 || port > 65535) { | ||
return fmt.Errorf("invalid socks port %d", port) | ||
} | ||
stdout, stderr := cmd.OutOrStdout(), cmd.ErrOrStderr() | ||
instName := args[0] | ||
inst, err := store.Inspect(instName) | ||
if err != nil { | ||
if errors.Is(err, os.ErrNotExist) { | ||
return fmt.Errorf("instance %q does not exist, run `limactl create %s` to create a new instance", instName, instName) | ||
} | ||
return err | ||
} | ||
if inst.Status == store.StatusStopped { | ||
return fmt.Errorf("instance %q is stopped, run `limactl start %s` to start the instance", instName, instName) | ||
} | ||
|
||
if port == 0 { | ||
port, err = freeport.TCP() | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
var ( | ||
arg0 string | ||
arg0Args []string | ||
) | ||
// FIXME: deduplicate the code clone across `limactl shell` and `limactl tunnel` | ||
if sshShell := os.Getenv(envShellSSH); sshShell != "" { | ||
sshShellFields, err := shellwords.Parse(sshShell) | ||
switch { | ||
case err != nil: | ||
logrus.WithError(err).Warnf("Failed to split %s variable into shell tokens. "+ | ||
"Falling back to 'ssh' command", envShellSSH) | ||
case len(sshShellFields) > 0: | ||
arg0 = sshShellFields[0] | ||
if len(sshShellFields) > 1 { | ||
arg0Args = sshShellFields[1:] | ||
} | ||
} | ||
} | ||
|
||
if arg0 == "" { | ||
arg0, err = exec.LookPath("ssh") | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
sshOpts, err := sshutil.SSHOpts( | ||
inst.Dir, | ||
*inst.Config.SSH.LoadDotSSHPubKeys, | ||
*inst.Config.SSH.ForwardAgent, | ||
*inst.Config.SSH.ForwardX11, | ||
*inst.Config.SSH.ForwardX11Trusted) | ||
if err != nil { | ||
return err | ||
} | ||
sshArgs := sshutil.SSHArgsFromOpts(sshOpts) | ||
sshArgs = append(sshArgs, []string{ | ||
"-q", // quiet | ||
"-f", // background | ||
"-N", // no command | ||
"-D", fmt.Sprintf("127.0.0.1:%d", port), | ||
"-p", strconv.Itoa(inst.SSHLocalPort), | ||
inst.SSHAddress, | ||
}...) | ||
sshCmd := exec.Command(arg0, append(arg0Args, sshArgs...)...) | ||
sshCmd.Stdout = stderr | ||
sshCmd.Stderr = stderr | ||
logrus.Debugf("executing ssh (may take a long)): %+v", sshCmd.Args) | ||
|
||
if err := sshCmd.Run(); err != nil { | ||
return err | ||
} | ||
|
||
switch runtime.GOOS { | ||
case "darwin": | ||
fmt.Fprintf(stdout, "Open <System Settings> → <Network> → <Wi-Fi> (or whatever) → <Details> → <Proxies> → <SOCKS proxy>,\n") | ||
fmt.Fprintf(stdout, "and specify the following configuration:\n") | ||
fmt.Fprintf(stdout, "- Server: 127.0.0.1\n") | ||
fmt.Fprintf(stdout, "- Port: %d\n", port) | ||
case "windows": | ||
fmt.Fprintf(stdout, "Open <Settings> → <Network & Internet> → <Proxy>,\n") | ||
fmt.Fprintf(stdout, "and specify the following configuration:\n") | ||
fmt.Fprintf(stdout, "- Address: socks=127.0.0.1\n") | ||
fmt.Fprintf(stdout, "- Port: %d\n", port) | ||
default: | ||
fmt.Fprintf(stdout, "Set `ALL_PROXY=socks5h://127.0.0.1:%d`, etc.\n", port) | ||
} | ||
fmt.Fprintf(stdout, "The instance can be connected from the host as <http://lima-%s.internal> via a web browser.\n", inst.Name) | ||
|
||
// TODO: show the port in `limactl list --json` ? | ||
// TODO: add `--stop` flag to shut down the tunnel | ||
return nil | ||
} | ||
|
||
func tunnelBashComplete(cmd *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) { | ||
return bashCompleteInstanceNames(cmd) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// Package freeport provides functions to find free localhost ports. | ||
package freeport | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
) | ||
|
||
func TCP() (int, error) { | ||
lAddr0, err := net.ResolveTCPAddr("tcp4", "127.0.0.1:0") | ||
if err != nil { | ||
return 0, err | ||
} | ||
l, err := net.ListenTCP("tcp4", lAddr0) | ||
if err != nil { | ||
return 0, err | ||
} | ||
defer l.Close() | ||
lAddr := l.Addr() | ||
lTCPAddr, ok := lAddr.(*net.TCPAddr) | ||
if !ok { | ||
return 0, fmt.Errorf("expected *net.TCPAddr, got %v", lAddr) | ||
} | ||
port := lTCPAddr.Port | ||
if port <= 0 { | ||
return 0, fmt.Errorf("unexpected port %d", port) | ||
} | ||
return port, nil | ||
} | ||
|
||
func UDP() (int, error) { | ||
lAddr0, err := net.ResolveUDPAddr("udp4", "127.0.0.1:0") | ||
if err != nil { | ||
return 0, err | ||
} | ||
l, err := net.ListenUDP("udp4", lAddr0) | ||
if err != nil { | ||
return 0, err | ||
} | ||
defer l.Close() | ||
lAddr := l.LocalAddr() | ||
lUDPAddr, ok := lAddr.(*net.UDPAddr) | ||
if !ok { | ||
return 0, fmt.Errorf("expected *net.UDPAddr, got %v", lAddr) | ||
} | ||
port := lUDPAddr.Port | ||
if port <= 0 { | ||
return 0, fmt.Errorf("unexpected port %d", port) | ||
} | ||
return port, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
//go:build !windows | ||
|
||
package freeport | ||
|
||
import "errors" | ||
|
||
func VSock() (int, error) { | ||
return 0, errors.New("freeport.VSock is not implemented for non-Windows hosts") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package freeport | ||
|
||
import "github.com/lima-vm/lima/pkg/windows" | ||
|
||
func VSock() (int, error) { | ||
return windows.GetRandomFreeVSockPort(0, 2147483647) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are we planning to address these todo now or later release ??
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Later.
So probably this should be marked as experimental
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated the PR to mark the command experimental