Include trust level in features exchange

Add a 'Trusted' feature to the protocol exchange which indicates whether
the remote considers us to be reachable via a secure network; overlay
encryption will be disabled in the event that both peers agree. For
backwards compatibility purposes a default value of untrusted is assumed
if the feature is missing.
This commit is contained in:
Adam Harrison committed 2015-12-14 12:12:25 +00:00
1 parent b51b1adfcd
commit d434b50dfc
1 file changed
+35 -12
+35 -12
View File
@@ -40,17 +40,19 @@ type RemoteConnection struct {
type LocalConnection struct { type LocalConnection struct {
sync.RWMutex sync.RWMutex
RemoteConnection RemoteConnection
TCPConn *net.TCPConn TCPConn *net.TCPConn
version byte TrustRemote bool // is remote on a trusted subnet?
tcpSender TCPSender TrustedByRemote bool // does remote trust us?
SessionKey *[32]byte version byte
heartbeatTCP *time.Ticker tcpSender TCPSender
Router *Router SessionKey *[32]byte
uid uint64 heartbeatTCP *time.Ticker
actionChan chan<- ConnectionAction Router *Router
errorChan chan<- error uid uint64
finished <-chan struct{} // closed to signal that actorLoop has finished actionChan chan<- ConnectionAction
OverlayConn OverlayConnection errorChan chan<- error
finished <-chan struct{} // closed to signal that actorLoop has finished
OverlayConn OverlayConnection
} }
type ConnectionAction func() error type ConnectionAction func() error
@@ -94,6 +96,7 @@ func StartLocalConnection(connRemote *RemoteConnection, tcpConn *net.TCPConn, ro
RemoteConnection: *connRemote, // NB, we're taking a copy of connRemote here. RemoteConnection: *connRemote, // NB, we're taking a copy of connRemote here.
Router: router, Router: router,
TCPConn: tcpConn, TCPConn: tcpConn,
TrustRemote: false,
uid: randUint64(), uid: randUint64(),
actionChan: actionChan, actionChan: actionChan,
errorChan: errorChan, errorChan: errorChan,
@@ -189,13 +192,19 @@ func (conn *LocalConnection) run(actionChan <-chan ConnectionAction, errorChan <
conn.Log("connection ready; using protocol version", conn.version) conn.Log("connection ready; using protocol version", conn.version)
// only use negotiated session key for untrusted connections
var sessionKey *[32]byte
if conn.Untrusted() {
sessionKey = conn.SessionKey
}
params := OverlayConnectionParams{ params := OverlayConnectionParams{
RemotePeer: conn.remote, RemotePeer: conn.remote,
LocalAddr: conn.TCPConn.LocalAddr().(*net.TCPAddr), LocalAddr: conn.TCPConn.LocalAddr().(*net.TCPAddr),
RemoteAddr: conn.TCPConn.RemoteAddr().(*net.TCPAddr), RemoteAddr: conn.TCPConn.RemoteAddr().(*net.TCPAddr),
Outbound: conn.outbound, Outbound: conn.outbound,
ConnUID: conn.uid, ConnUID: conn.uid,
SessionKey: conn.SessionKey, SessionKey: sessionKey,
SendControlMessage: conn.sendOverlayControlMessage, SendControlMessage: conn.sendOverlayControlMessage,
Features: intro.Features, Features: intro.Features,
} }
@@ -253,6 +262,7 @@ func (conn *LocalConnection) makeFeatures() map[string]string {
"ShortID": fmt.Sprint(conn.local.ShortID), "ShortID": fmt.Sprint(conn.local.ShortID),
"UID": fmt.Sprint(conn.local.UID), "UID": fmt.Sprint(conn.local.UID),
"ConnID": fmt.Sprint(conn.uid), "ConnID": fmt.Sprint(conn.uid),
"Trusted": fmt.Sprint(conn.TrustRemote),
} }
conn.Router.Overlay.AddFeaturesTo(features) conn.Router.Overlay.AddFeaturesTo(features)
return features return features
@@ -300,6 +310,15 @@ func (conn *LocalConnection) parseFeatures(features features) (*Peer, error) {
} }
} }
var trusted bool
if trustedStr, present := features["Trusted"]; present {
trusted, err = strconv.ParseBool(trustedStr)
if err != nil {
return nil, err
}
}
conn.TrustedByRemote = trusted
uid, err := ParsePeerUID(features.Get("UID")) uid, err := ParsePeerUID(features.Get("UID"))
if err != nil { if err != nil {
return nil, err return nil, err
@@ -435,3 +454,7 @@ func (conn *LocalConnection) handleProtocolMsg(tag ProtocolTag, payload []byte)
func (conn *LocalConnection) extendReadDeadline() { func (conn *LocalConnection) extendReadDeadline() {
conn.TCPConn.SetReadDeadline(time.Now().Add(TCPHeartbeat * 2)) conn.TCPConn.SetReadDeadline(time.Now().Add(TCPHeartbeat * 2))
} }
func (conn *LocalConnection) Untrusted() bool {
return !conn.TrustRemote || !conn.TrustedByRemote
}