-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce "orchard {port-forward, vnc} worker WORKER_NAME" (#140)
* Fix potential NPE in Client.wsRequest() * Introduce "orchard {port-forward, vnc} worker WORKER_NAME" * portspec.go: simplify logic and respect [LOCAL_PORT]:REMOTE_PORT format
- Loading branch information
Showing
16 changed files
with
405 additions
and
89 deletions.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package portforward | ||
|
||
import ( | ||
"fmt" | ||
"github.com/cirruslabs/orchard/internal/proxy" | ||
"github.com/cirruslabs/orchard/pkg/client" | ||
"github.com/spf13/cobra" | ||
"net" | ||
) | ||
|
||
func newPortForwardWorkerCommand() *cobra.Command { | ||
command := &cobra.Command{ | ||
Use: "worker WORKER_NAME [LOCAL_PORT]:REMOTE_PORT", | ||
Short: "Forward TCP port to the worker", | ||
Args: cobra.ExactArgs(2), | ||
RunE: runPortForwardWorkerCommand, | ||
} | ||
|
||
return command | ||
} | ||
|
||
func runPortForwardWorkerCommand(cmd *cobra.Command, args []string) (err error) { | ||
name := args[0] | ||
portSpecRaw := args[1] | ||
|
||
portSpec, err := NewPortSpec(portSpecRaw) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
client, err := client.New() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
listener, err := net.Listen("tcp", fmt.Sprintf("127.0.0.1:%d", portSpec.LocalPort)) | ||
if err != nil { | ||
return err | ||
} | ||
defer func() { | ||
if listenerErr := listener.Close(); listenerErr != nil && err == nil { | ||
err = listenerErr | ||
} | ||
}() | ||
|
||
fmt.Printf("forwarding %s -> %s:%d...\n", listener.Addr(), name, portSpec.RemotePort) | ||
|
||
errCh := make(chan error, 1) | ||
|
||
go func() { | ||
for { | ||
conn, err := listener.Accept() | ||
if err != nil { | ||
errCh <- err | ||
|
||
return | ||
} | ||
|
||
go func() { | ||
defer conn.Close() | ||
|
||
wsConn, err := client.Workers().PortForward(cmd.Context(), name, portSpec.RemotePort) | ||
if err != nil { | ||
fmt.Printf("failed to forward port: %v\n", err) | ||
|
||
return | ||
} | ||
defer wsConn.Close() | ||
|
||
if err := proxy.Connections(wsConn, conn); err != nil { | ||
fmt.Printf("failed to forward port: %v\n", err) | ||
} | ||
}() | ||
} | ||
}() | ||
|
||
select { | ||
case <-cmd.Context().Done(): | ||
return cmd.Context().Err() | ||
case err := <-errCh: | ||
return err | ||
} | ||
} |
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.