From b64fbdd1c1529d00788317872666c9e8d88d9a54 Mon Sep 17 00:00:00 2001 From: Jakob Borg Date: Wed, 24 May 2023 12:34:17 +0200 Subject: [PATCH] wip --- lib/connections/service.go | 31 +++++++++++++------------------ lib/connections/structs.go | 20 ++------------------ 2 files changed, 15 insertions(+), 36 deletions(-) diff --git a/lib/connections/service.go b/lib/connections/service.go index 8ec0816a7..f832c5685 100644 --- a/lib/connections/service.go +++ b/lib/connections/service.go @@ -172,8 +172,8 @@ type service struct { listeners map[string]genericListener listenerTokens map[string]suture.ServiceToken - connectionsMut sync.Mutex - connections map[protocol.DeviceID]int + numConnectionsMut sync.Mutex + numConnections map[protocol.DeviceID]int } func NewService(cfg config.Wrapper, myID protocol.DeviceID, mdl Model, tlsCfg *tls.Config, discoverer discover.Finder, bepProtocolName string, tlsDefaultCommonName string, evLogger events.Logger, registry *registry.Registry, keyGen *protocol.KeyGenerator) Service { @@ -206,8 +206,8 @@ func NewService(cfg config.Wrapper, myID protocol.DeviceID, mdl Model, tlsCfg *t listeners: make(map[string]genericListener), listenerTokens: make(map[string]suture.ServiceToken), - connectionsMut: sync.NewMutex(), - connections: make(map[protocol.DeviceID]int), + numConnectionsMut: sync.NewMutex(), + numConnections: make(map[protocol.DeviceID]int), } cfg.Subscribe(service) @@ -418,12 +418,7 @@ func (s *service) handleHellos(ctx context.Context) error { // connections are limited. rd, wr := s.limiter.getLimiters(remoteID, c, c.IsLocal()) - // Amazing hack: We need to pass the connection to the model, but we - // also need to pass the model to the connection. The - // connectionContextModel handles the mediation by being constructed - // in two stages... protoConn := protocol.NewConnection(remoteID, rd, wr, c, s.model, c, deviceCfg.Compression, s.cfg.FolderPasswords(remoteID), s.keyGen) - s.accountAddedConnection(remoteID) go func() { <-protoConn.Closed() @@ -1198,21 +1193,21 @@ func (s *service) validateIdentity(c internalConn, expectedID protocol.DeviceID) } func (s *service) accountAddedConnection(d protocol.DeviceID) { - s.connectionsMut.Lock() - defer s.connectionsMut.Unlock() - s.connections[d]++ + s.numConnectionsMut.Lock() + defer s.numConnectionsMut.Unlock() + s.numConnections[d]++ } func (s *service) accountRemovedConnection(d protocol.DeviceID) { - s.connectionsMut.Lock() - defer s.connectionsMut.Unlock() - s.connections[d]-- + s.numConnectionsMut.Lock() + defer s.numConnectionsMut.Unlock() + s.numConnections[d]-- } func (s *service) connectionsForDevice(d protocol.DeviceID) int { - s.connectionsMut.Lock() - defer s.connectionsMut.Unlock() - return s.connections[d] + s.numConnectionsMut.Lock() + defer s.numConnectionsMut.Unlock() + return s.numConnections[d] } type nextDialRegistry map[protocol.DeviceID]nextDialDevice diff --git a/lib/connections/structs.go b/lib/connections/structs.go index 328eeac7b..c43bcf5d1 100644 --- a/lib/connections/structs.go +++ b/lib/connections/structs.go @@ -8,10 +8,7 @@ package connections import ( "context" - "crypto/sha256" "crypto/tls" - "encoding/base32" - "encoding/binary" "fmt" "io" "net" @@ -92,26 +89,13 @@ func (t connType) Transport() string { } func newInternalConn(tc tlsConn, connType connType, isLocal bool, priority int) internalConn { - // Construct the connection ID. In principle it's an opaque string, but - // in practice it's the timestamp with added hash of connection details, - // meaning that 1) it's globally unique, and 2) it's sortable to - // determine a "primary" connection for a device. To ensure the - // sortability we use the base32 "hexencoding" which has the characters - // in the right order to sort lexicographically. - now := time.Now() - buf := binary.BigEndian.AppendUint64(nil, uint64(now.UnixNano())) - h := sha256.New() - fmt.Fprintf(h, "%v\n%v\n%v\n", connType, tc.LocalAddr(), tc.RemoteAddr()) - buf = h.Sum(buf) - id := base32.HexEncoding.WithPadding(base32.NoPadding).EncodeToString(buf) - return internalConn{ tlsConn: tc, connType: connType, isLocal: isLocal, priority: priority, - establishedAt: now.Truncate(time.Second), - connectionID: id, + establishedAt: time.Now().Truncate(time.Second), + connectionID: fmt.Sprintf("%v-%v-%v", tc.LocalAddr(), connType.Transport(), tc.RemoteAddr()), } }