Skip to content

feat(cli): add reverse tunnelling SSH support for unix sockets #9976

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
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
first draft
  • Loading branch information
monika-canva committed Sep 29, 2023
commit 04fcd46a9d6dda9954da9a3c6c460513b19d1c11
50 changes: 45 additions & 5 deletions cli/remoteforward.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,24 @@ type cookieAddr struct {

// Format:
// remote_port:local_address:local_port
var remoteForwardRegex = regexp.MustCompile(`^(\d+):(.+):(\d+)$`)
var remoteForwardRegexTCP = regexp.MustCompile(`^(\d+):(.+):(\d+)$`)

func validateRemoteForward(flag string) bool {
return remoteForwardRegex.MatchString(flag)
// remote_socket_path:local_socket_path (both absolute paths)
var remoteForwardRegexUnixSocket = regexp.MustCompile(`^(\\.+):(\\.+)$`)

func remoteForwardTCP(flag string) bool {
return remoteForwardRegexTCP.MatchString(flag)
}

func parseRemoteForward(flag string) (net.Addr, net.Addr, error) {
matches := remoteForwardRegex.FindStringSubmatch(flag)
func remoteForwardUnixSocket(flag string) bool {
return remoteForwardRegexUnixSocket.MatchString(flag)
}

func validateRemoteForward(flag string) bool {
return remoteForwardTCP(flag) || remoteForwardUnixSocket(flag)
}

func parseRemoteForwardTCP(matches []string) (net.Addr, net.Addr, error) {
remotePort, err := strconv.Atoi(matches[1])
if err != nil {
return nil, nil, xerrors.Errorf("remote port is invalid: %w", err)
Expand All @@ -57,6 +66,37 @@ func parseRemoteForward(flag string) (net.Addr, net.Addr, error) {
return localAddr, remoteAddr, nil
}

func parseRemoteForwardUnixSocket(matches []string) (net.Addr, net.Addr, error) {
remoteSocket := matches[1]
localSocket := matches[2]

remoteAddr := &net.UnixAddr{
Name: remoteSocket,
Net: "unix",
}

localAddr := &net.UnixAddr{
Name: localSocket,
Net: "unix",
}
return localAddr, remoteAddr, nil
}

func parseRemoteForward(flag string) (net.Addr, net.Addr, error) {
tcpMatches := remoteForwardRegexTCP.FindStringSubmatch(flag)

if len(tcpMatches) > 0 {
return parseRemoteForwardTCP(tcpMatches)
}

unixSocketMatches := remoteForwardRegexUnixSocket.FindStringSubmatch(flag)
if len(unixSocketMatches) > 0 {
return parseRemoteForwardUnixSocket(unixSocketMatches)
}

return nil, nil, xerrors.New("Could not match forward arguments")
}

// sshRemoteForward starts forwarding connections from a remote listener to a
// local address via SSH in a goroutine.
//
Expand Down