Skip to content

feat: add port-forward subcommand #1350

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

Merged
merged 12 commits into from
May 18, 2022
Prev Previous commit
Next Next commit
feat: add port-forward subcommand
  • Loading branch information
deansheather committed May 17, 2022
commit e3eb83968777c3afdc0ffcf28d3d359ad8841864
31 changes: 28 additions & 3 deletions agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,14 @@ func (a *agent) handleDial(ctx context.Context, label string, conn net.Conn) {

network := u.Scheme
addr := u.Host + u.Path
if strings.HasPrefix(network, "unix") {
addr, err = ExpandPath(addr)
if err != nil {
_ = writeError(xerrors.Errorf("expand path %q: %w", addr, err))
return
}
}

nconn, err := net.Dial(network, addr)
if err != nil {
_ = writeError(xerrors.Errorf("dial '%v://%v': %w", network, addr, err))
Expand All @@ -668,7 +676,7 @@ func (a *agent) handleDial(ctx context.Context, label string, conn net.Conn) {
return
}

bicopy(ctx, conn, nconn)
Bicopy(ctx, conn, nconn)
}

// isClosed returns whether the API is closed or not.
Expand Down Expand Up @@ -717,10 +725,10 @@ func (r *reconnectingPTY) Close() {
r.timeout.Stop()
}

// bicopy copies all of the data between the two connections and will close them
// Bicopy copies all of the data between the two connections and will close them
// after one or both of them are done writing. If the context is canceled, both
// of the connections will be closed.
func bicopy(ctx context.Context, c1, c2 io.ReadWriteCloser) {
func Bicopy(ctx context.Context, c1, c2 io.ReadWriteCloser) {
defer c1.Close()
defer c2.Close()

Expand All @@ -736,3 +744,20 @@ func bicopy(ctx context.Context, c1, c2 io.ReadWriteCloser) {

<-ctx.Done()
}

// ExpandPath expands the tilde at the beggining of a path to the current user's
// home directory and returns a full absolute path.
func ExpandPath(in string) (string, error) {
usr, err := user.Current()
if err != nil {
return "", xerrors.Errorf("get current user details: %w", err)
}

if in == "~" {
in = usr.HomeDir
} else if strings.HasPrefix(in, "~/") {
in = filepath.Join(usr.HomeDir, in[2:])
}

return filepath.Abs(in)
}
3 changes: 2 additions & 1 deletion agent/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ func (c *Conn) DialContext(ctx context.Context, network string, addr string) (ne
}

channel, err := c.CreateChannel(ctx, u.String(), &peer.ChannelOptions{
Protocol: "dial",
Protocol: "dial",
Unordered: strings.HasPrefix(network, "udp"),
})
if err != nil {
return nil, xerrors.Errorf("create datachannel: %w", err)
Expand Down
Loading