add connection id

This commit is contained in:
Jakob Borg 2023-05-22 08:55:24 +02:00
parent 716b42103a
commit a933bcfebc
5 changed files with 219 additions and 1 deletions

View File

@ -8,7 +8,10 @@ package connections
import (
"context"
"crypto/sha256"
"crypto/tls"
"encoding/base32"
"encoding/binary"
"fmt"
"io"
"net"
@ -42,6 +45,7 @@ type internalConn struct {
isLocal bool
priority int
establishedAt time.Time
connectionID string
}
type connType int
@ -88,12 +92,26 @@ 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: time.Now().Truncate(time.Second),
establishedAt: now.Truncate(time.Second),
connectionID: id,
}
}
@ -138,6 +156,10 @@ func (c internalConn) EstablishedAt() time.Time {
return c.establishedAt
}
func (c internalConn) ConnectionID() string {
return c.connectionID
}
func (c internalConn) String() string {
t := "WAN"
if c.isLocal {

View File

@ -8,6 +8,16 @@ import (
)
type mockedConnectionInfo struct {
ConnectionIDStub func() string
connectionIDMutex sync.RWMutex
connectionIDArgsForCall []struct {
}
connectionIDReturns struct {
result1 string
}
connectionIDReturnsOnCall map[int]struct {
result1 string
}
CryptoStub func() string
cryptoMutex sync.RWMutex
cryptoArgsForCall []struct {
@ -92,6 +102,59 @@ type mockedConnectionInfo struct {
invocationsMutex sync.RWMutex
}
func (fake *mockedConnectionInfo) ConnectionID() string {
fake.connectionIDMutex.Lock()
ret, specificReturn := fake.connectionIDReturnsOnCall[len(fake.connectionIDArgsForCall)]
fake.connectionIDArgsForCall = append(fake.connectionIDArgsForCall, struct {
}{})
stub := fake.ConnectionIDStub
fakeReturns := fake.connectionIDReturns
fake.recordInvocation("ConnectionID", []interface{}{})
fake.connectionIDMutex.Unlock()
if stub != nil {
return stub()
}
if specificReturn {
return ret.result1
}
return fakeReturns.result1
}
func (fake *mockedConnectionInfo) ConnectionIDCallCount() int {
fake.connectionIDMutex.RLock()
defer fake.connectionIDMutex.RUnlock()
return len(fake.connectionIDArgsForCall)
}
func (fake *mockedConnectionInfo) ConnectionIDCalls(stub func() string) {
fake.connectionIDMutex.Lock()
defer fake.connectionIDMutex.Unlock()
fake.ConnectionIDStub = stub
}
func (fake *mockedConnectionInfo) ConnectionIDReturns(result1 string) {
fake.connectionIDMutex.Lock()
defer fake.connectionIDMutex.Unlock()
fake.ConnectionIDStub = nil
fake.connectionIDReturns = struct {
result1 string
}{result1}
}
func (fake *mockedConnectionInfo) ConnectionIDReturnsOnCall(i int, result1 string) {
fake.connectionIDMutex.Lock()
defer fake.connectionIDMutex.Unlock()
fake.ConnectionIDStub = nil
if fake.connectionIDReturnsOnCall == nil {
fake.connectionIDReturnsOnCall = make(map[int]struct {
result1 string
})
}
fake.connectionIDReturnsOnCall[i] = struct {
result1 string
}{result1}
}
func (fake *mockedConnectionInfo) Crypto() string {
fake.cryptoMutex.Lock()
ret, specificReturn := fake.cryptoReturnsOnCall[len(fake.cryptoArgsForCall)]
@ -519,6 +582,8 @@ func (fake *mockedConnectionInfo) TypeReturnsOnCall(i int, result1 string) {
func (fake *mockedConnectionInfo) Invocations() map[string][][]interface{} {
fake.invocationsMutex.RLock()
defer fake.invocationsMutex.RUnlock()
fake.connectionIDMutex.RLock()
defer fake.connectionIDMutex.RUnlock()
fake.cryptoMutex.RLock()
defer fake.cryptoMutex.RUnlock()
fake.establishedAtMutex.RLock()

View File

@ -31,6 +31,16 @@ type Connection struct {
clusterConfigArgsForCall []struct {
arg1 protocol.ClusterConfig
}
ConnectionIDStub func() string
connectionIDMutex sync.RWMutex
connectionIDArgsForCall []struct {
}
connectionIDReturns struct {
result1 string
}
connectionIDReturnsOnCall map[int]struct {
result1 string
}
CryptoStub func() string
cryptoMutex sync.RWMutex
cryptoArgsForCall []struct {
@ -315,6 +325,59 @@ func (fake *Connection) ClusterConfigArgsForCall(i int) protocol.ClusterConfig {
return argsForCall.arg1
}
func (fake *Connection) ConnectionID() string {
fake.connectionIDMutex.Lock()
ret, specificReturn := fake.connectionIDReturnsOnCall[len(fake.connectionIDArgsForCall)]
fake.connectionIDArgsForCall = append(fake.connectionIDArgsForCall, struct {
}{})
stub := fake.ConnectionIDStub
fakeReturns := fake.connectionIDReturns
fake.recordInvocation("ConnectionID", []interface{}{})
fake.connectionIDMutex.Unlock()
if stub != nil {
return stub()
}
if specificReturn {
return ret.result1
}
return fakeReturns.result1
}
func (fake *Connection) ConnectionIDCallCount() int {
fake.connectionIDMutex.RLock()
defer fake.connectionIDMutex.RUnlock()
return len(fake.connectionIDArgsForCall)
}
func (fake *Connection) ConnectionIDCalls(stub func() string) {
fake.connectionIDMutex.Lock()
defer fake.connectionIDMutex.Unlock()
fake.ConnectionIDStub = stub
}
func (fake *Connection) ConnectionIDReturns(result1 string) {
fake.connectionIDMutex.Lock()
defer fake.connectionIDMutex.Unlock()
fake.ConnectionIDStub = nil
fake.connectionIDReturns = struct {
result1 string
}{result1}
}
func (fake *Connection) ConnectionIDReturnsOnCall(i int, result1 string) {
fake.connectionIDMutex.Lock()
defer fake.connectionIDMutex.Unlock()
fake.ConnectionIDStub = nil
if fake.connectionIDReturnsOnCall == nil {
fake.connectionIDReturnsOnCall = make(map[int]struct {
result1 string
})
}
fake.connectionIDReturnsOnCall[i] = struct {
result1 string
}{result1}
}
func (fake *Connection) Crypto() string {
fake.cryptoMutex.Lock()
ret, specificReturn := fake.cryptoReturnsOnCall[len(fake.cryptoArgsForCall)]
@ -1162,6 +1225,8 @@ func (fake *Connection) Invocations() map[string][][]interface{} {
defer fake.closedMutex.RUnlock()
fake.clusterConfigMutex.RLock()
defer fake.clusterConfigMutex.RUnlock()
fake.connectionIDMutex.RLock()
defer fake.connectionIDMutex.RUnlock()
fake.cryptoMutex.RLock()
defer fake.cryptoMutex.RUnlock()
fake.downloadProgressMutex.RLock()

View File

@ -10,6 +10,16 @@ import (
)
type ConnectionInfo struct {
ConnectionIDStub func() string
connectionIDMutex sync.RWMutex
connectionIDArgsForCall []struct {
}
connectionIDReturns struct {
result1 string
}
connectionIDReturnsOnCall map[int]struct {
result1 string
}
CryptoStub func() string
cryptoMutex sync.RWMutex
cryptoArgsForCall []struct {
@ -94,6 +104,59 @@ type ConnectionInfo struct {
invocationsMutex sync.RWMutex
}
func (fake *ConnectionInfo) ConnectionID() string {
fake.connectionIDMutex.Lock()
ret, specificReturn := fake.connectionIDReturnsOnCall[len(fake.connectionIDArgsForCall)]
fake.connectionIDArgsForCall = append(fake.connectionIDArgsForCall, struct {
}{})
stub := fake.ConnectionIDStub
fakeReturns := fake.connectionIDReturns
fake.recordInvocation("ConnectionID", []interface{}{})
fake.connectionIDMutex.Unlock()
if stub != nil {
return stub()
}
if specificReturn {
return ret.result1
}
return fakeReturns.result1
}
func (fake *ConnectionInfo) ConnectionIDCallCount() int {
fake.connectionIDMutex.RLock()
defer fake.connectionIDMutex.RUnlock()
return len(fake.connectionIDArgsForCall)
}
func (fake *ConnectionInfo) ConnectionIDCalls(stub func() string) {
fake.connectionIDMutex.Lock()
defer fake.connectionIDMutex.Unlock()
fake.ConnectionIDStub = stub
}
func (fake *ConnectionInfo) ConnectionIDReturns(result1 string) {
fake.connectionIDMutex.Lock()
defer fake.connectionIDMutex.Unlock()
fake.ConnectionIDStub = nil
fake.connectionIDReturns = struct {
result1 string
}{result1}
}
func (fake *ConnectionInfo) ConnectionIDReturnsOnCall(i int, result1 string) {
fake.connectionIDMutex.Lock()
defer fake.connectionIDMutex.Unlock()
fake.ConnectionIDStub = nil
if fake.connectionIDReturnsOnCall == nil {
fake.connectionIDReturnsOnCall = make(map[int]struct {
result1 string
})
}
fake.connectionIDReturnsOnCall[i] = struct {
result1 string
}{result1}
}
func (fake *ConnectionInfo) Crypto() string {
fake.cryptoMutex.Lock()
ret, specificReturn := fake.cryptoReturnsOnCall[len(fake.cryptoArgsForCall)]
@ -521,6 +584,8 @@ func (fake *ConnectionInfo) TypeReturnsOnCall(i int, result1 string) {
func (fake *ConnectionInfo) Invocations() map[string][][]interface{} {
fake.invocationsMutex.RLock()
defer fake.invocationsMutex.RUnlock()
fake.connectionIDMutex.RLock()
defer fake.connectionIDMutex.RUnlock()
fake.cryptoMutex.RLock()
defer fake.cryptoMutex.RUnlock()
fake.establishedAtMutex.RLock()

View File

@ -166,6 +166,7 @@ type ConnectionInfo interface {
String() string
Crypto() string
EstablishedAt() time.Time
ConnectionID() string
}
type rawConnection struct {