From f5a67ad2a7ea32026aa98dec22f16b1593501a80 Mon Sep 17 00:00:00 2001 From: Matt McGee <85582517+mmcgeefeedo@users.noreply.github.com> Date: Tue, 27 Jul 2021 17:12:05 -0400 Subject: [PATCH] Add data channel (active/passive) information to the ClientContext interface (#248) * GetLastDataChannel returns the last data channel mode (passive or active) * use the paramsMutex for lastDataChannel updates * update README.md with GetLastDataChannel update to ClientContext * convert data channel to enums --- README.md | 3 +++ client_handler.go | 25 +++++++++++++++++++++++++ client_handler_test.go | 5 +++++ driver.go | 3 +++ transfer_active.go | 1 + transfer_pasv.go | 1 + 6 files changed, 38 insertions(+) diff --git a/README.md b/README.md index 0a5dd380..f4e92413 100644 --- a/README.md +++ b/README.md @@ -114,6 +114,9 @@ type ClientContext interface { // GetLastCommand returns the last received command GetLastCommand() string + + // GetLastDataChannel returns the last data channel mode + GetLastDataChannel() DataChannel } // Settings define all the server settings diff --git a/client_handler.go b/client_handler.go index 6710f834..995b8ed9 100644 --- a/client_handler.go +++ b/client_handler.go @@ -34,6 +34,15 @@ const ( TransferTypeBinary ) +// DataChannel is the enumerable that represents the data channel (active or passive) +type DataChannel int + +// Supported data channel types +const ( + DataChannelPassive DataChannel = iota + 1 + DataChannelActive +) + const ( maxCommandSize = 4096 ) @@ -91,6 +100,7 @@ type clientHandler struct { transferWg sync.WaitGroup // wait group for command that open a transfer connection transferMu sync.Mutex // this mutex will protect the transfer parameters transfer transferHandler // Transfer connection (passive or active)s + lastDataChannel DataChannel // Last data channel mode (passive or active) isTransferOpen bool // indicate if the transfer connection is opened isTransferAborted bool // indicate if the transfer was aborted paramsMutex sync.RWMutex // mutex to protect the parameters exposed to the library users @@ -231,6 +241,14 @@ func (c *clientHandler) GetLastCommand() string { return c.command } +// GetLastDataChannel returns the last data channel mode +func (c *clientHandler) GetLastDataChannel() DataChannel { + c.paramsMutex.RLock() + defer c.paramsMutex.RUnlock() + + return c.lastDataChannel +} + func (c *clientHandler) setLastCommand(cmd string) { c.paramsMutex.Lock() defer c.paramsMutex.Unlock() @@ -238,6 +256,13 @@ func (c *clientHandler) setLastCommand(cmd string) { c.command = cmd } +func (c *clientHandler) setLastDataChannel(channel DataChannel) { + c.paramsMutex.Lock() + defer c.paramsMutex.Unlock() + + c.lastDataChannel = channel +} + func (c *clientHandler) closeTransfer() error { var err error if c.transfer != nil { diff --git a/client_handler_test.go b/client_handler_test.go index e36ce027..f4d746df 100644 --- a/client_handler_test.go +++ b/client_handler_test.go @@ -86,6 +86,11 @@ func TestLastCommand(t *testing.T) { assert.Empty(t, cc.GetLastCommand()) } +func TestLastDataChannel(t *testing.T) { + cc := clientHandler{lastDataChannel: DataChannelPassive} + assert.Equal(t, DataChannelPassive, cc.GetLastDataChannel()) +} + func TestTransferOpenError(t *testing.T) { s := NewTestServer(t, true) conf := goftp.Config{ diff --git a/driver.go b/driver.go index 5b9267ce..c1491681 100644 --- a/driver.go +++ b/driver.go @@ -147,6 +147,9 @@ type ClientContext interface { // GetLastCommand returns the last received command GetLastCommand() string + + // GetLastDataChannel returns the last data channel mode + GetLastDataChannel() DataChannel } // FileTransfer defines the inferface for file transfers. diff --git a/transfer_active.go b/transfer_active.go index c9cac20a..bc27551d 100644 --- a/transfer_active.go +++ b/transfer_active.go @@ -55,6 +55,7 @@ func (c *clientHandler) handlePORT(param string) error { } c.transferMu.Unlock() + c.setLastDataChannel(DataChannelActive) c.writeMessage(StatusOK, command+" command successful") diff --git a/transfer_pasv.go b/transfer_pasv.go index e9da39b8..4172ad94 100644 --- a/transfer_pasv.go +++ b/transfer_pasv.go @@ -184,6 +184,7 @@ func (c *clientHandler) handlePASV(param string) error { c.transferMu.Lock() c.transfer = p c.transferMu.Unlock() + c.setLastDataChannel(DataChannelPassive) return nil }