Repository navigation
feat(server): allow forwarding VNC port locally #1515
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,115 @@ | ||||||
| package server | ||||||
|
|
||||||
| import ( | ||||||
| "context" | ||||||
| "fmt" | ||||||
| "net" | ||||||
| "os" | ||||||
| "os/signal" | ||||||
| "strconv" | ||||||
| "sync" | ||||||
|
|
||||||
| "github.com/spf13/cobra" | ||||||
|
|
||||||
| "github.com/hetznercloud/cli/internal/cmd/base" | ||||||
| "github.com/hetznercloud/cli/internal/cmd/cmpl" | ||||||
| "github.com/hetznercloud/cli/internal/cmd/util/vncutil" | ||||||
| "github.com/hetznercloud/cli/internal/hcapi2" | ||||||
| "github.com/hetznercloud/cli/internal/state" | ||||||
| "github.com/hetznercloud/hcloud-go/v2/hcloud" | ||||||
| ) | ||||||
|
|
||||||
| var VNCCmd = base.Cmd{ | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| BaseCobraCommand: func(client hcapi2.Client) *cobra.Command { | ||||||
| cmd := &cobra.Command{ | ||||||
| Use: "vnc-port-foward [options] <server>", | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| Short: "Forward the Server VNC console to a local port", | ||||||
| Long: `Forward the Server VNC console to a local port, so it can be accessed with any VNC client. | ||||||
|
|
||||||
| Note that the local VNC server has no password protection.`, | ||||||
| ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(client.Server().Names)), | ||||||
| TraverseChildren: true, | ||||||
| DisableFlagsInUseLine: true, | ||||||
| } | ||||||
| cmd.Flags().String("address", "127.0.0.1", "Local address to listen on") | ||||||
| cmd.Flags().IntP("port", "p", 5900, "Local port to listen on (0 picks a random free port)") | ||||||
| return cmd | ||||||
| }, | ||||||
| Experimental: base.ExperimentalWrapper("The vnc-port-foward command", "experimental", ""), | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| Run: func(s state.State, cmd *cobra.Command, args []string) error { | ||||||
| idOrName := args[0] | ||||||
| address, _ := cmd.Flags().GetString("address") | ||||||
| port, _ := cmd.Flags().GetInt("port") | ||||||
|
|
||||||
| if port < 0 || port > 65535 { | ||||||
| return fmt.Errorf("invalid port: %d", port) | ||||||
| } | ||||||
|
|
||||||
| server, _, err := s.Client().Server().Get(s, idOrName) | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we check if the server is running and return a nice error message? What happens if the server is off or rebuilding? |
||||||
| if err != nil { | ||||||
| return err | ||||||
| } | ||||||
| if server == nil { | ||||||
| return fmt.Errorf("Server not found: %s", idOrName) | ||||||
| } | ||||||
|
|
||||||
| // capture ctrl + c to stop forwarding | ||||||
| ctx, stop := signal.NotifyContext(s, os.Interrupt) | ||||||
|
|
||||||
| var lc net.ListenConfig | ||||||
| listener, err := lc.Listen(ctx, "tcp", net.JoinHostPort(address, strconv.Itoa(port))) | ||||||
| if err != nil { | ||||||
| stop() | ||||||
| return fmt.Errorf("could not listen on local address: %w", err) | ||||||
| } | ||||||
|
|
||||||
| var wg sync.WaitGroup | ||||||
| defer func() { | ||||||
| stop() | ||||||
| wg.Wait() | ||||||
| }() | ||||||
| context.AfterFunc(ctx, func() { | ||||||
| _ = listener.Close() | ||||||
| }) | ||||||
|
|
||||||
| cmd.Printf("Forwarding VNC console of Server %d to %s, press Ctrl+C to stop\n", server.ID, listener.Addr()) | ||||||
|
|
||||||
| for { | ||||||
| conn, err := listener.Accept() | ||||||
| if err != nil { | ||||||
| if ctx.Err() != nil { | ||||||
| // ctrl + c pressed | ||||||
| return nil | ||||||
| } | ||||||
| return err | ||||||
| } | ||||||
| wg.Go(func() { | ||||||
| if err := forwardVNC(ctx, s, cmd, server, conn); err != nil && ctx.Err() == nil { | ||||||
| cmd.PrintErrf("Error: connection from %s: %v\n", conn.RemoteAddr(), err) | ||||||
| } | ||||||
| }) | ||||||
| } | ||||||
| }, | ||||||
| } | ||||||
|
|
||||||
| func forwardVNC(ctx context.Context, s state.State, cmd *cobra.Command, server *hcloud.Server, conn net.Conn) error { | ||||||
| defer conn.Close() | ||||||
|
|
||||||
| result, _, err := s.Client().Server().RequestConsole(ctx, server) | ||||||
| if err != nil { | ||||||
| return err | ||||||
| } | ||||||
| if err := s.WaitForActions(ctx, cmd, result.Action); err != nil { | ||||||
| return err | ||||||
| } | ||||||
|
Comment on lines
+102
to
+104
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder if we should only log a simple "waiting" message, instead of the full blown waiting with prorgess bar. WDYT? |
||||||
|
|
||||||
| ws, err := vncutil.DialWebsocket(ctx, result.WSSURL) | ||||||
| if err != nil { | ||||||
| return err | ||||||
| } | ||||||
|
|
||||||
| cmd.Printf("Accepted connection from %s\n", conn.RemoteAddr()) | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't this be at the top of the function? I understand this log message as: We accepted the connection of a VNC client on port 5900, and then start the process of request the console URL, and opening the websocket connection. |
||||||
| defer cmd.Printf("Closed connection from %s\n", conn.RemoteAddr()) | ||||||
|
|
||||||
| return vncutil.ProxyWebsocket(ctx, conn, ws) | ||||||
| } | ||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| package vncutil | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "io" | ||
| "net" | ||
| "net/url" | ||
|
|
||
| "golang.org/x/net/websocket" | ||
| ) | ||
|
|
||
| func websocketOrigin(wsURL string) (string, error) { | ||
| u, err := url.Parse(wsURL) | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
| u.Scheme = "https" | ||
| return u.String(), nil | ||
| } | ||
|
|
||
| func DialWebsocket(ctx context.Context, wsURL string) (*websocket.Conn, error) { | ||
| origin, err := websocketOrigin(wsURL) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| cfg, err := websocket.NewConfig(wsURL, origin) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| ws, err := cfg.DialContext(ctx) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| ws.PayloadType = websocket.BinaryFrame | ||
|
|
||
| return ws, nil | ||
| } | ||
|
|
||
| func ProxyWebsocket(ctx context.Context, conn net.Conn, ws *websocket.Conn) error { | ||
| closeAll := func() { | ||
| _ = conn.Close() | ||
| _ = ws.Close() | ||
| } | ||
| stop := context.AfterFunc(ctx, closeAll) | ||
| defer stop() | ||
|
|
||
| errs := make(chan error, 2) | ||
| go func() { | ||
| _, err := io.Copy(ws, conn) | ||
| errs <- err | ||
| }() | ||
| go func() { | ||
| _, err := io.Copy(conn, ws) | ||
| errs <- err | ||
| }() | ||
|
|
||
| // close both connections if one side errors | ||
| err := <-errs | ||
| closeAll() | ||
| <-errs | ||
|
|
||
| if ctx.Err() != nil || errors.Is(err, net.ErrClosed) { | ||
| return nil | ||
| } | ||
| return err | ||
| } | ||
|
Comment on lines
+42
to
+69
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe some debug logs could be useful, in case something is not working as expected. |
||
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.
Should we rename this file to
vnc_port_foward.go?Could we also have some test for this command?