diff --git a/api/hub.go b/api/hub.go index 75ccfe9..b2bcbdb 100644 --- a/api/hub.go +++ b/api/hub.go @@ -24,8 +24,17 @@ type HubInterface interface { // Default: false SetAutoAccept(bool) - // Pair with the SKI - RegisterRemoteSKI(ski string) + // Pair a remote service based on the SKI + // + // Parameters: + // - ski: the SKI of the remote service (required) + // - shipID: the SHIP ID of the remote service (optional) + // + // Note: The SHIP ID is optional, but should be provided if available. + // if provided, it will be used to validate the remote service is + // providing this SHIP ID during the handshake process and will reject + // the connection if it does not match. + RegisterRemoteSKI(ski, shipID string) // Unpair the SKI UnregisterRemoteSKI(ski string) @@ -55,7 +64,7 @@ type HubReaderInterface interface { // Provides the SHIP ID the remote service reported during the handshake process // This needs to be persisted and passed on for future remote service connections - // when using `PairRemoteService` + // when using `RegisterRemoteSKI` ServiceShipIDUpdate(ski string, shipdID string) // Provides the current pairing state for the remote service diff --git a/hub/hub_pairing.go b/hub/hub_pairing.go index efabbbc..8d1e4f7 100644 --- a/hub/hub_pairing.go +++ b/hub/hub_pairing.go @@ -87,16 +87,24 @@ func (h *Hub) checkHasStarted() bool { return h.hasStarted } -// Sets the SKI as being paired or not -// Should be used for services which completed the pairing process and -// which were stored as having the process completed -func (h *Hub) RegisterRemoteSKI(ski string) { +// Pair a remote service based on the SKI +// +// Parameters: +// - ski: the SKI of the remote service (required) +// - shipID: the SHIP ID of the remote service (optional) +// +// Note: The SHIP ID is optional, but should be provided if available. +// if provided, it will be used to validate the remote service is +// providing this SHIP ID during the handshake process and will reject +// the connection if it does not match. +func (h *Hub) RegisterRemoteSKI(ski, shipID string) { ski = util.NormalizeSKI(ski) // if the hub has not started, simply add it if !h.checkHasStarted() { service := h.ServiceForSKI(ski) service.SetTrusted(true) + service.SetShipID(shipID) h.checkAutoReannounce() return @@ -107,6 +115,7 @@ func (h *Hub) RegisterRemoteSKI(ski string) { service := h.ServiceForSKI(ski) service.SetTrusted(true) + service.SetShipID(shipID) // remotely initiated? if conn != nil { diff --git a/hub/hub_test.go b/hub/hub_test.go index 1c2d7c0..6a0fb73 100644 --- a/hub/hub_test.go +++ b/hub/hub_test.go @@ -188,7 +188,7 @@ func (s *HubSuite) Test_IsRemoteSKIPaired() { assert.Equal(s.T(), false, paired) s.sut.registerConnection(s.shipConnection) - s.sut.RegisterRemoteSKI(s.remoteSki) + s.sut.RegisterRemoteSKI(s.remoteSki, "") paired = s.sut.IsRemoteServiceForSKIPaired(s.remoteSki) assert.Equal(s.T(), true, paired) @@ -219,11 +219,11 @@ func (s *HubSuite) Test_IsRemoteSKIPaired() { func (s *HubSuite) Test_RegisterRemoteSKI_AfterStart() { s.sut.hasStarted = true - s.sut.RegisterRemoteSKI(s.remoteSki) + s.sut.RegisterRemoteSKI(s.remoteSki, "") assert.Equal(s.T(), 0, len(s.sut.connections)) s.sut.registerConnection(s.shipConnection) - s.sut.RegisterRemoteSKI(s.remoteSki) + s.sut.RegisterRemoteSKI(s.remoteSki, "") assert.Equal(s.T(), 1, len(s.sut.connections)) } @@ -244,7 +244,7 @@ func (s *HubSuite) Test_Mdns() { assert.Equal(s.T(), 0, len(s.sut.connections)) assert.Equal(s.T(), 0, pairedServices) - s.sut.RegisterRemoteSKI(s.remoteSki) + s.sut.RegisterRemoteSKI(s.remoteSki, "") pairedServices = s.sut.numberPairedServices() assert.Equal(s.T(), 0, len(s.sut.connections)) assert.Equal(s.T(), 1, pairedServices) @@ -549,7 +549,7 @@ func (s *HubSuite) Test_InitiateConnection() { result = s.sut.initateConnection(service, entry) assert.Equal(s.T(), false, result) - s.sut.RegisterRemoteSKI(s.remoteSki) + s.sut.RegisterRemoteSKI(s.remoteSki, "") service.ConnectionStateDetail().SetState(api.ConnectionStateQueued) result = s.sut.initateConnection(service, entry) diff --git a/mocks/HubInterface.go b/mocks/HubInterface.go index 3f6a725..eaa04ba 100644 --- a/mocks/HubInterface.go +++ b/mocks/HubInterface.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.45.0. DO NOT EDIT. +// Code generated by mockery v2.46.3. DO NOT EDIT. package mocks @@ -135,9 +135,9 @@ func (_c *HubInterface_PairingDetailForSki_Call) RunAndReturn(run func(string) * return _c } -// RegisterRemoteSKI provides a mock function with given fields: ski -func (_m *HubInterface) RegisterRemoteSKI(ski string) { - _m.Called(ski) +// RegisterRemoteSKI provides a mock function with given fields: ski, shipID +func (_m *HubInterface) RegisterRemoteSKI(ski string, shipID string) { + _m.Called(ski, shipID) } // HubInterface_RegisterRemoteSKI_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'RegisterRemoteSKI' @@ -147,13 +147,14 @@ type HubInterface_RegisterRemoteSKI_Call struct { // RegisterRemoteSKI is a helper method to define mock.On call // - ski string -func (_e *HubInterface_Expecter) RegisterRemoteSKI(ski interface{}) *HubInterface_RegisterRemoteSKI_Call { - return &HubInterface_RegisterRemoteSKI_Call{Call: _e.mock.On("RegisterRemoteSKI", ski)} +// - shipID string +func (_e *HubInterface_Expecter) RegisterRemoteSKI(ski interface{}, shipID interface{}) *HubInterface_RegisterRemoteSKI_Call { + return &HubInterface_RegisterRemoteSKI_Call{Call: _e.mock.On("RegisterRemoteSKI", ski, shipID)} } -func (_c *HubInterface_RegisterRemoteSKI_Call) Run(run func(ski string)) *HubInterface_RegisterRemoteSKI_Call { +func (_c *HubInterface_RegisterRemoteSKI_Call) Run(run func(ski string, shipID string)) *HubInterface_RegisterRemoteSKI_Call { _c.Call.Run(func(args mock.Arguments) { - run(args[0].(string)) + run(args[0].(string), args[1].(string)) }) return _c } @@ -163,7 +164,7 @@ func (_c *HubInterface_RegisterRemoteSKI_Call) Return() *HubInterface_RegisterRe return _c } -func (_c *HubInterface_RegisterRemoteSKI_Call) RunAndReturn(run func(string)) *HubInterface_RegisterRemoteSKI_Call { +func (_c *HubInterface_RegisterRemoteSKI_Call) RunAndReturn(run func(string, string)) *HubInterface_RegisterRemoteSKI_Call { _c.Call.Return(run) return _c }