github.com/devops-filetransfer/sshego@v7.0.4+incompatible/ud.go (about)

     1  package sshego
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  	"net"
     7  	"time"
     8  
     9  	"github.com/glycerine/sshego/xendor/github.com/glycerine/xcryptossh"
    10  )
    11  
    12  const MUX_C_OPEN_FWD = 0x10000006 // 268435462
    13  
    14  // openssh-7.4 says use -2 as the port to mean the host is actually
    15  // a unix-domain socket path.
    16  /*
    17  5. Requesting establishment of port forwards
    18  
    19  A client may request the master to establish a port forward:
    20  
    21  	uint32	MUX_C_OPEN_FWD
    22  	uint32	request id
    23  	uint32	forwarding type
    24  	string	listen host
    25  	uint32	listen port
    26  	string	connect host
    27  	uint32	connect port
    28  
    29  forwarding type may be MUX_FWD_LOCAL, MUX_FWD_REMOTE, MUX_FWD_DYNAMIC.
    30  
    31  If listen port is (unsigned int) -2, then the listen host is treated as
    32  a unix socket path name.
    33  
    34  If connect port is (unsigned int) -2, then the connect host is treated
    35  as a unix socket path name.
    36  */
    37  
    38  // DialRemoteUnixDomain initiates a connection to
    39  // udpath from the remote host using c as the
    40  // ssh client. Here udpath is a unixDomain socket
    41  // path in the remote filesystem.
    42  // The resulting connection has a zero LocalAddr() and RemoteAddr().
    43  func DialRemoteUnixDomain(ctx context.Context, c *ssh.Client, udpath string, parentHalt *ssh.Halter) (net.Conn, error) {
    44  	// Use a zero address for local and remote address.
    45  	zeroAddr := &net.TCPAddr{
    46  		IP:   net.IPv4zero,
    47  		Port: 0,
    48  	}
    49  	ch, err := dialDirect(ctx, c, net.IPv4zero.String(), 0, udpath, -2, parentHalt)
    50  	if err != nil {
    51  		return nil, err
    52  	}
    53  	return &unixDomainChanConn{
    54  		Channel: ch,
    55  		laddr:   zeroAddr,
    56  		raddr:   zeroAddr,
    57  	}, nil
    58  }
    59  
    60  // unixDomainChanConn fulfills the net.Conn interface without
    61  // the tcpChan having to hold laddr or raddr directly.
    62  // From github.com/glycerine/xcryptossh/tcpip.go
    63  type unixDomainChanConn struct {
    64  	ssh.Channel
    65  	laddr, raddr net.Addr
    66  }
    67  
    68  // LocalAddr returns the local network address.
    69  func (t *unixDomainChanConn) LocalAddr() net.Addr {
    70  	return t.laddr
    71  }
    72  
    73  // RemoteAddr returns the remote network address.
    74  func (t *unixDomainChanConn) RemoteAddr() net.Addr {
    75  	return t.raddr
    76  }
    77  
    78  // SetDeadline sets the read and write deadlines associated
    79  // with the connection.
    80  func (t *unixDomainChanConn) SetDeadline(deadline time.Time) error {
    81  	if err := t.SetReadDeadline(deadline); err != nil {
    82  		return err
    83  	}
    84  	return t.SetWriteDeadline(deadline)
    85  }
    86  
    87  // SetReadDeadline sets the read deadline.
    88  // A zero value for t means Read will not time out.
    89  // After the deadline, the error from Read will implement net.Error
    90  // with Timeout() == true.
    91  func (t *unixDomainChanConn) SetReadDeadline(deadline time.Time) error {
    92  	return errors.New("ssh: unixDomainChanConn: deadline not supported")
    93  }
    94  
    95  // SetWriteDeadline exists to satisfy the net.Conn interface
    96  // but is not implemented by this type.  It always returns an error.
    97  func (t *unixDomainChanConn) SetWriteDeadline(deadline time.Time) error {
    98  	return errors.New("ssh: unixDomainChanConn: deadline not supported")
    99  }