This commit is contained in:
Jakob Borg 2023-05-24 12:34:17 +02:00
parent 463b5d7c6c
commit b64fbdd1c1
2 changed files with 15 additions and 36 deletions

View File

@ -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

View File

@ -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()),
}
}