Skip to content

Commit 24adecb

Browse files
authored
client handler: allow to set/get application specific data (#416)
1 parent 0b1ad95 commit 24adecb

4 files changed

Lines changed: 51 additions & 0 deletions

File tree

client_handler.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ type clientHandler struct {
106106
isTransferOpen bool // indicate if the transfer connection is opened
107107
isTransferAborted bool // indicate if the transfer was aborted
108108
tlsRequirement TLSRequirement // TLS requirement to respect
109+
extra any // Additional application-specific data
109110
paramsMutex sync.RWMutex // mutex to protect the parameters exposed to the library users
110111
}
111112

@@ -245,6 +246,14 @@ func (c *clientHandler) HasTLSForTransfers() bool {
245246
return c.transferTLS
246247
}
247248

249+
func (c *clientHandler) SetExtra(extra any) {
250+
c.extra = extra
251+
}
252+
253+
func (c *clientHandler) Extra() any {
254+
return c.extra
255+
}
256+
248257
func (c *clientHandler) setTLSForTransfer(value bool) {
249258
c.paramsMutex.Lock()
250259
defer c.paramsMutex.Unlock()

client_handler_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,3 +456,36 @@ func TestDataConnectionRequirements(t *testing.T) {
456456
assert.Contains(t, err.Error(), "unhandled data connection requirement")
457457
}
458458
}
459+
460+
func TestExtraData(t *testing.T) {
461+
driver := &TestServerDriver{
462+
Debug: false,
463+
}
464+
s := NewTestServerWithDriver(t, driver)
465+
466+
conf := goftp.Config{
467+
User: authUser,
468+
Password: authPass,
469+
}
470+
471+
c, err := goftp.DialConfig(conf, s.Addr())
472+
require.NoError(t, err, "Couldn't connect")
473+
474+
defer func() { panicOnError(c.Close()) }()
475+
476+
raw, err := c.OpenRawConn()
477+
require.NoError(t, err)
478+
479+
defer func() { require.NoError(t, raw.Close()) }()
480+
481+
info := driver.GetClientsInfo()
482+
require.Len(t, info, 1)
483+
484+
for k, v := range info {
485+
ccInfo, ok := v.(map[string]interface{})
486+
require.True(t, ok)
487+
extra, ok := ccInfo["extra"].(uint32)
488+
require.True(t, ok)
489+
require.Equal(t, k, extra)
490+
}
491+
}

driver.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,12 @@ type ClientContext interface {
188188
// If you want to enforce the same requirement for all
189189
// clients, use the TLSRequired parameter defined in server settings instead
190190
SetTLSRequirement(requirement TLSRequirement) error
191+
192+
// SetExtra allows to set application specific data
193+
SetExtra(extra any)
194+
195+
// Extra returns application specific data set using SetExtra
196+
Extra() any
191197
}
192198

193199
// FileTransfer defines the inferface for file transfers.

driver_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,8 @@ func (driver *TestServerDriver) ClientConnected(cc ClientContext) (string, error
214214
}
215215

216216
cc.SetDebug(driver.Debug)
217+
// we set the client id as extra data just for testing
218+
cc.SetExtra(cc.ID())
217219
driver.Clients = append(driver.Clients, cc)
218220
// This will remain the official name for now
219221
return "TEST Server", err
@@ -267,6 +269,7 @@ func (driver *TestServerDriver) GetClientsInfo() map[uint32]interface{} {
267269
ccInfo["hasTLSForTransfers"] = cc.HasTLSForTransfers()
268270
ccInfo["lastCommand"] = cc.GetLastCommand()
269271
ccInfo["debug"] = cc.Debug()
272+
ccInfo["extra"] = cc.Extra()
270273

271274
info[cc.ID()] = ccInfo
272275
}

0 commit comments

Comments
 (0)