diff --git a/networks/tor/BaseOnionService.go b/networks/tor/BaseOnionService.go index c6a30a8..37e7cf9 100644 --- a/networks/tor/BaseOnionService.go +++ b/networks/tor/BaseOnionService.go @@ -21,6 +21,27 @@ 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 +// 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, + } } // Metrics provide metrics for services @@ -50,6 +71,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 @@ -145,7 +172,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() @@ -171,7 +198,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/primitives/privacypass/token.go b/primitives/privacypass/token.go index e4ffc0d..e7ce557 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 72a279b..7559a77 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 { diff --git a/scratch/main.go b/scratch/main.go new file mode 100644 index 0000000..e69de29 diff --git a/service.go b/service.go index 2ba5d65..e971ae8 100644 --- a/service.go +++ b/service.go @@ -12,7 +12,8 @@ import ( "sync" ) -// ServiceMetrics are metrics for servers... + +// ServiceMetrics outlines higher level information about the service e.g. counts of connections type ServiceMetrics struct { ConnectionCount int }