From f26a98895a4bf503471ce5de07ea662ef0caaf05 Mon Sep 17 00:00:00 2001 From: Sarah Jamie Lewis Date: Tue, 14 Jul 2020 16:35:21 -0700 Subject: [PATCH 1/3] Add Metrics (Fix: #21) --- networks/tor/BaseOnionService.go | 20 ++++++++++++++++++++ service.go | 6 ++++++ 2 files changed, 26 insertions(+) diff --git a/networks/tor/BaseOnionService.go b/networks/tor/BaseOnionService.go index fe9c2e5..feaf56a 100644 --- a/networks/tor/BaseOnionService.go +++ b/networks/tor/BaseOnionService.go @@ -23,6 +23,26 @@ type BaseOnionService struct { lock sync.Mutex } +// Metrics provides a report of useful information about the status of the service e.g. the number of active +// connections +func (s *BaseOnionService) Metrics() tapir.ServiceMetrics { + s.lock.Lock() + defer s.lock.Unlock() + + count := 0 + s.connections.Range(func(key, value interface{}) bool { + connection := value.(tapir.Connection) + if !connection.IsClosed() { + count++ + } + return true + }) + + return tapir.ServiceMetrics{ + ConnectionCount: count, + } +} + // Init initializes a BaseOnionService with a given private key and identity // The private key is needed to initialize the Onion listen socket, ideally we could just pass an Identity in here. func (s *BaseOnionService) Init(acn connectivity.ACN, sk ed25519.PrivateKey, id *primitives.Identity) { diff --git a/service.go b/service.go index 4534644..0c1a9fd 100644 --- a/service.go +++ b/service.go @@ -12,12 +12,18 @@ import ( "sync" ) +// ServiceMetrics outlines higher level information about the service e.g. counts of connections +type ServiceMetrics struct { + ConnectionCount int +} + // Service defines the interface for a Tapir Service type Service interface { Init(acn connectivity.ACN, privateKey ed25519.PrivateKey, identity *primitives.Identity) Connect(hostname string, application Application) (bool, error) Listen(application Application) error GetConnection(connectionID string) (Connection, error) + Metrics() ServiceMetrics Broadcast(message []byte, capability Capability) error WaitForCapabilityOrClose(connectionID string, capability Capability) (Connection, error) Shutdown() -- 2.25.1 From c0b675b01189ac50cf0feff30441eb7307d5c936 Mon Sep 17 00:00:00 2001 From: Sarah Jamie Lewis Date: Mon, 20 Jul 2020 17:41:50 -0700 Subject: [PATCH 2/3] Use hmac.Write instead of hmac.Sum when verifying tokens --- primitives/privacypass/token.go | 3 ++- primitives/privacypass/tokenserver.go | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/primitives/privacypass/token.go b/primitives/privacypass/token.go index 8bc9a77..d8dcdd5 100644 --- a/primitives/privacypass/token.go +++ b/primitives/privacypass/token.go @@ -72,7 +72,8 @@ func (t *Token) unblindSignedToken(token SignedToken) { func (t *Token) SpendToken(data []byte) SpentToken { key := sha3.Sum256(append(t.t, t.W.Encode(nil)...)) mac := hmac.New(sha3.New512, key[:]) - return SpentToken{t.t, mac.Sum(data)} + mac.Write(data) + return SpentToken{t.t, mac.Sum(nil)} } // GenerateBlindedTokenBatch generates a batch of blinded tokens (and their unblinded equivalents) diff --git a/primitives/privacypass/tokenserver.go b/primitives/privacypass/tokenserver.go index c5036d2..0945499 100644 --- a/primitives/privacypass/tokenserver.go +++ b/primitives/privacypass/tokenserver.go @@ -136,7 +136,8 @@ func (ts *TokenServer) SpendToken(token SpentToken, data []byte) error { W := new(ristretto.Element).ScalarMult(ts.k, T) key := sha3.Sum256(append(token.T, W.Encode(nil)...)) mac := hmac.New(sha3.New512, key[:]) - computedMAC := mac.Sum(data) + mac.Write(data) + computedMAC := mac.Sum(nil) result := hmac.Equal(token.MAC, computedMAC) if result == true { if ts.persistanceService == nil { -- 2.25.1 From 9fba459adc35f8471909ba0e6d61bdbd9f20250a Mon Sep 17 00:00:00 2001 From: Sarah Jamie Lewis Date: Thu, 29 Oct 2020 15:45:07 -0700 Subject: [PATCH 3/3] Check if Listener exists before trying to close it --- networks/tor/BaseOnionService.go | 13 +++++++++++-- scratch/main.go | 0 2 files changed, 11 insertions(+), 2 deletions(-) create mode 100644 scratch/main.go diff --git a/networks/tor/BaseOnionService.go b/networks/tor/BaseOnionService.go index feaf56a..43667dc 100644 --- a/networks/tor/BaseOnionService.go +++ b/networks/tor/BaseOnionService.go @@ -21,6 +21,7 @@ type BaseOnionService struct { privateKey ed25519.PrivateKey ls connectivity.ListenService lock sync.Mutex + port int } // Metrics provides a report of useful information about the status of the service e.g. the number of active @@ -51,6 +52,12 @@ func (s *BaseOnionService) Init(acn connectivity.ACN, sk ed25519.PrivateKey, id s.acn = acn s.id = id s.privateKey = sk + s.port = 9878 +} + +// SetPort configures the port that the service uses. +func (s *BaseOnionService) SetPort(port int) { + s.port = port } // WaitForCapabilityOrClose blocks until the connection has the given capability or the underlying connection is closed @@ -146,7 +153,7 @@ func (s *BaseOnionService) Listen(app tapir.Application) error { // accepts a new connection // spins off to a connection struct s.lock.Lock() - ls, err := s.acn.Listen(s.privateKey, 9878) + ls, err := s.acn.Listen(s.privateKey, s.port) s.ls = ls log.Debugf("Starting a service on %v ", ls.AddressFull()) s.lock.Unlock() @@ -172,7 +179,9 @@ func (s *BaseOnionService) Listen(app tapir.Application) error { func (s *BaseOnionService) Shutdown() { s.lock.Lock() defer s.lock.Unlock() - s.ls.Close() + if s.ls != nil { + s.ls.Close() + } s.connections.Range(func(key, value interface{}) bool { connection := value.(tapir.Connection) connection.Close() diff --git a/scratch/main.go b/scratch/main.go new file mode 100644 index 0000000..e69de29 -- 2.25.1