Skip to content

Commit 04fcd46

Browse files
committed
first draft
1 parent 2d1b353 commit 04fcd46

File tree

1 file changed

+45
-5
lines changed

1 file changed

+45
-5
lines changed

cli/remoteforward.go

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,24 @@ type cookieAddr struct {
2323

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

28-
func validateRemoteForward(flag string) bool {
29-
return remoteForwardRegex.MatchString(flag)
28+
// remote_socket_path:local_socket_path (both absolute paths)
29+
var remoteForwardRegexUnixSocket = regexp.MustCompile(`^(\\.+):(\\.+)$`)
30+
31+
func remoteForwardTCP(flag string) bool {
32+
return remoteForwardRegexTCP.MatchString(flag)
3033
}
3134

32-
func parseRemoteForward(flag string) (net.Addr, net.Addr, error) {
33-
matches := remoteForwardRegex.FindStringSubmatch(flag)
35+
func remoteForwardUnixSocket(flag string) bool {
36+
return remoteForwardRegexUnixSocket.MatchString(flag)
37+
}
3438

39+
func validateRemoteForward(flag string) bool {
40+
return remoteForwardTCP(flag) || remoteForwardUnixSocket(flag)
41+
}
42+
43+
func parseRemoteForwardTCP(matches []string) (net.Addr, net.Addr, error) {
3544
remotePort, err := strconv.Atoi(matches[1])
3645
if err != nil {
3746
return nil, nil, xerrors.Errorf("remote port is invalid: %w", err)
@@ -57,6 +66,37 @@ func parseRemoteForward(flag string) (net.Addr, net.Addr, error) {
5766
return localAddr, remoteAddr, nil
5867
}
5968

69+
func parseRemoteForwardUnixSocket(matches []string) (net.Addr, net.Addr, error) {
70+
remoteSocket := matches[1]
71+
localSocket := matches[2]
72+
73+
remoteAddr := &net.UnixAddr{
74+
Name: remoteSocket,
75+
Net: "unix",
76+
}
77+
78+
localAddr := &net.UnixAddr{
79+
Name: localSocket,
80+
Net: "unix",
81+
}
82+
return localAddr, remoteAddr, nil
83+
}
84+
85+
func parseRemoteForward(flag string) (net.Addr, net.Addr, error) {
86+
tcpMatches := remoteForwardRegexTCP.FindStringSubmatch(flag)
87+
88+
if len(tcpMatches) > 0 {
89+
return parseRemoteForwardTCP(tcpMatches)
90+
}
91+
92+
unixSocketMatches := remoteForwardRegexUnixSocket.FindStringSubmatch(flag)
93+
if len(unixSocketMatches) > 0 {
94+
return parseRemoteForwardUnixSocket(unixSocketMatches)
95+
}
96+
97+
return nil, nil, xerrors.New("Could not match forward arguments")
98+
}
99+
60100
// sshRemoteForward starts forwarding connections from a remote listener to a
61101
// local address via SSH in a goroutine.
62102
//

0 commit comments

Comments
 (0)