Skip to content

Commit

Permalink
Update register remote service API (#46)
Browse files Browse the repository at this point in the history
Change `RegisterRemoteSKI` api call with a 2nd argument to provide the
SHIP-ID if known. If provided (as a non empty string), upon SHIP
handshake it will check the reported SHIP-ID from the remote service,
and if it differs, will close the connection. The SHIP-ID is also made
available via mDNS, and therefor should be known at all times.

The implementation of `ServiceShipIDUpdate` of `HubReaderInterface` is
called to report the SHIP-ID, if it wasn’t known yet.
  • Loading branch information
DerAndereAndi authored Jan 22, 2025
2 parents d388b85 + 45edf34 commit dd44eeb
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 21 deletions.
15 changes: 12 additions & 3 deletions api/hub.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
17 changes: 13 additions & 4 deletions hub/hub_pairing.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down
10 changes: 5 additions & 5 deletions hub/hub_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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))
}

Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
19 changes: 10 additions & 9 deletions mocks/HubInterface.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit dd44eeb

Please sign in to comment.