diff --git a/internal/therealssh/chan_list.go b/internal/therealssh/chan_list.go index d8bd69668..5f76ac10d 100644 --- a/internal/therealssh/chan_list.go +++ b/internal/therealssh/chan_list.go @@ -5,14 +5,14 @@ import "sync" type chanList struct { sync.Mutex - chans []*SshChannel + chans []*SSHChannel } func newChanList() *chanList { - return &chanList{chans: []*SshChannel{}} + return &chanList{chans: []*SSHChannel{}} } -func (c *chanList) add(sshCh *SshChannel) uint32 { +func (c *chanList) add(sshCh *SSHChannel) uint32 { c.Lock() defer c.Unlock() @@ -27,7 +27,7 @@ func (c *chanList) add(sshCh *SshChannel) uint32 { return uint32(len(c.chans) - 1) } -func (c *chanList) getChannel(id uint32) *SshChannel { +func (c *chanList) getChannel(id uint32) *SSHChannel { c.Lock() defer c.Unlock() @@ -38,10 +38,10 @@ func (c *chanList) getChannel(id uint32) *SshChannel { return nil } -func (c *chanList) dropAll() []*SshChannel { +func (c *chanList) dropAll() []*SSHChannel { c.Lock() defer c.Unlock() - var r []*SshChannel + var r []*SSHChannel for _, ch := range c.chans { if ch == nil { diff --git a/internal/therealssh/channel.go b/internal/therealssh/channel.go index 97ae4f9df..79418fa9f 100644 --- a/internal/therealssh/channel.go +++ b/internal/therealssh/channel.go @@ -25,8 +25,8 @@ const Port = 2 // Debug enables debug messages. var Debug = false -// SshChannel defines communication channel parameters. -type SshChannel struct { +// SSHChannel defines communication channel parameters. +type SSHChannel struct { RemoteID uint32 RemoteAddr *app.Addr @@ -38,25 +38,25 @@ type SshChannel struct { dataCh chan []byte } -// OpenChannel constructs new SshChannel with empty Session. -func OpenChannel(remoteID uint32, remoteAddr *app.Addr, conn net.Conn) *SshChannel { - return &SshChannel{RemoteID: remoteID, conn: conn, RemoteAddr: remoteAddr, msgCh: make(chan []byte), dataCh: make(chan []byte)} +// OpenChannel constructs new SSHChannel with empty Session. +func OpenChannel(remoteID uint32, remoteAddr *app.Addr, conn net.Conn) *SSHChannel { + return &SSHChannel{RemoteID: remoteID, conn: conn, RemoteAddr: remoteAddr, msgCh: make(chan []byte), dataCh: make(chan []byte)} } -// OpenClientChannel constructs new client SshChannel with empty Session. -func OpenClientChannel(remoteID uint32, remotePK cipher.PubKey, conn net.Conn) *SshChannel { +// OpenClientChannel constructs new client SSHChannel with empty Session. +func OpenClientChannel(remoteID uint32, remotePK cipher.PubKey, conn net.Conn) *SSHChannel { ch := OpenChannel(remoteID, &app.Addr{PubKey: remotePK, Port: Port}, conn) return ch } // Send sends command message. -func (sshCh *SshChannel) Send(cmd CommandType, payload []byte) error { +func (sshCh *SSHChannel) Send(cmd CommandType, payload []byte) error { data := appendU32([]byte{byte(cmd)}, sshCh.RemoteID) _, err := sshCh.conn.Write(append(data, payload...)) return err } -func (sshCh *SshChannel) Read(p []byte) (int, error) { +func (sshCh *SSHChannel) Read(p []byte) (int, error) { data, more := <-sshCh.dataCh if !more { return 0, io.EOF @@ -65,14 +65,14 @@ func (sshCh *SshChannel) Read(p []byte) (int, error) { return copy(p, data), nil } -func (sshCh *SshChannel) Write(p []byte) (n int, err error) { +func (sshCh *SSHChannel) Write(p []byte) (n int, err error) { n = len(p) err = sshCh.Send(CmdChannelData, p) return } // Request sends request message and waits for response. -func (sshCh *SshChannel) Request(requestType RequestType, payload []byte) ([]byte, error) { +func (sshCh *SSHChannel) Request(requestType RequestType, payload []byte) ([]byte, error) { debug("sending request %x", requestType) req := append([]byte{byte(requestType)}, payload...) @@ -89,7 +89,7 @@ func (sshCh *SshChannel) Request(requestType RequestType, payload []byte) ([]byt } // Serve starts request handling loop. -func (sshCh *SshChannel) Serve() error { +func (sshCh *SSHChannel) Serve() error { for data := range sshCh.msgCh { var err error debug("new request %x", data[0]) @@ -136,12 +136,12 @@ func (sshCh *SshChannel) Serve() error { // SocketPath returns unix socket location. This socket is normally // used by the CLI to exchange PTY data with a client app. -func (sshCh *SshChannel) SocketPath() string { +func (sshCh *SSHChannel) SocketPath() string { return filepath.Join(os.TempDir(), fmt.Sprintf("therealsshd-%d", sshCh.RemoteID)) } // ServeSocket starts socket handling loop. -func (sshCh *SshChannel) ServeSocket() error { +func (sshCh *SSHChannel) ServeSocket() error { os.Remove(sshCh.SocketPath()) debug("waiting for new socket connections on: %s", sshCh.SocketPath()) l, err := net.ListenUnix("unix", &net.UnixAddr{Name: sshCh.SocketPath(), Net: "unix"}) @@ -178,7 +178,7 @@ func (sshCh *SshChannel) ServeSocket() error { } // OpenPTY creates new PTY Session for the Channel. -func (sshCh *SshChannel) OpenPTY(user *user.User, sz *pty.Winsize) (err error) { +func (sshCh *SSHChannel) OpenPTY(user *user.User, sz *pty.Winsize) (err error) { if sshCh.session != nil { return errors.New("session is already started") } @@ -194,12 +194,12 @@ func (sshCh *SshChannel) OpenPTY(user *user.User, sz *pty.Winsize) (err error) { } // Shell starts shell process on Channel's PTY session. -func (sshCh *SshChannel) Shell() error { +func (sshCh *SSHChannel) Shell() error { return sshCh.Start("shell") } // Start executes provided command on Channel's PTY session. -func (sshCh *SshChannel) Start(command string) error { +func (sshCh *SSHChannel) Start(command string) error { if sshCh.session == nil { return errors.New("session is not started") } @@ -214,7 +214,7 @@ func (sshCh *SshChannel) Start(command string) error { return sshCh.session.Start(command) } -func (sshCh *SshChannel) serveSession() error { +func (sshCh *SSHChannel) serveSession() error { defer func() { sshCh.Send(CmdChannelServerClose, nil) // nolint sshCh.Close() @@ -236,7 +236,7 @@ func (sshCh *SshChannel) serveSession() error { } // WindowChange resize PTY Session size. -func (sshCh *SshChannel) WindowChange(sz *pty.Winsize) error { +func (sshCh *SSHChannel) WindowChange(sz *pty.Winsize) error { if sshCh.session == nil { return errors.New("session is not started") } @@ -245,7 +245,7 @@ func (sshCh *SshChannel) WindowChange(sz *pty.Winsize) error { } // Close safely closes Channel resources. -func (sshCh *SshChannel) Close() error { +func (sshCh *SSHChannel) Close() error { select { case <-sshCh.dataCh: default: diff --git a/internal/therealssh/client.go b/internal/therealssh/client.go index f6f2503c1..5c4264be9 100644 --- a/internal/therealssh/client.go +++ b/internal/therealssh/client.go @@ -46,7 +46,7 @@ func NewClient(rpcAddr string, d Dialer) (net.Listener, *Client, error) { } // OpenChannel requests new Channel on the remote Server. -func (c *Client) OpenChannel(remotePK cipher.PubKey) (localID uint32, sshCh *SshChannel, cErr error) { +func (c *Client) OpenChannel(remotePK cipher.PubKey) (localID uint32, sshCh *SSHChannel, cErr error) { conn, err := c.dialer.Dial(&app.Addr{PubKey: remotePK, Port: Port}) if err != nil { cErr = fmt.Errorf("dial failed: %s", err) @@ -80,7 +80,7 @@ func (c *Client) OpenChannel(remotePK cipher.PubKey) (localID uint32, sshCh *Ssh return localID, sshCh, cErr } -func (c *Client) resolveChannel(remotePK cipher.PubKey, localID uint32) (*SshChannel, error) { +func (c *Client) resolveChannel(remotePK cipher.PubKey, localID uint32) (*SSHChannel, error) { sshCh := c.chans.getChannel(localID) if sshCh == nil { return nil, errors.New("channel is not opened") diff --git a/internal/therealssh/client_test.go b/internal/therealssh/client_test.go index f5a7643aa..999abce61 100644 --- a/internal/therealssh/client_test.go +++ b/internal/therealssh/client_test.go @@ -19,7 +19,7 @@ func TestClientOpenChannel(t *testing.T) { c := &Client{dialer, newChanList()} type data struct { - ch *SshChannel + ch *SSHChannel err error } resCh := make(chan data)