mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-08-27 14:11:04 +02:00
lnd: only restrict slots for inbound connections
For outbound connections, since they are initialized by the users, we can relax on the restriction. A future global limit may be added - as for now, we will let them to be managed by the users.
This commit is contained in:
committed by
Olaoluwa Osuntokun
parent
bc6008f854
commit
bd99924383
28
accessman.go
28
accessman.go
@@ -545,7 +545,7 @@ func (a *accessMan) checkIncomingConnBanScore(remotePub *btcec.PublicKey) (
|
|||||||
// addPeerAccess tracks a peer's access in the maps. This should be called when
|
// addPeerAccess tracks a peer's access in the maps. This should be called when
|
||||||
// the peer has fully connected.
|
// the peer has fully connected.
|
||||||
func (a *accessMan) addPeerAccess(remotePub *btcec.PublicKey,
|
func (a *accessMan) addPeerAccess(remotePub *btcec.PublicKey,
|
||||||
access peerAccessStatus) {
|
access peerAccessStatus, inbound bool) {
|
||||||
|
|
||||||
ctx := btclog.WithCtx(
|
ctx := btclog.WithCtx(
|
||||||
context.TODO(), lnutils.LogPubKey("peer", remotePub),
|
context.TODO(), lnutils.LogPubKey("peer", remotePub),
|
||||||
@@ -561,15 +561,37 @@ func (a *accessMan) addPeerAccess(remotePub *btcec.PublicKey,
|
|||||||
|
|
||||||
a.peerScores[peerMapKey] = peerSlotStatus{state: access}
|
a.peerScores[peerMapKey] = peerSlotStatus{state: access}
|
||||||
|
|
||||||
// Increment numRestricted.
|
// Exit early if this is not a restricted peer.
|
||||||
if access == peerStatusRestricted {
|
if access != peerStatusRestricted {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Increment numRestricted if this is an inbound connection.
|
||||||
|
if inbound {
|
||||||
oldRestricted := a.numRestricted
|
oldRestricted := a.numRestricted
|
||||||
a.numRestricted++
|
a.numRestricted++
|
||||||
|
|
||||||
acsmLog.DebugS(ctx, "Incremented restricted slots",
|
acsmLog.DebugS(ctx, "Incremented restricted slots",
|
||||||
"old_restricted", oldRestricted,
|
"old_restricted", oldRestricted,
|
||||||
"new_restricted", a.numRestricted)
|
"new_restricted", a.numRestricted)
|
||||||
|
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Otherwise, this is a newly created outbound connection. We won't
|
||||||
|
// place any restriction on it, instead, we will do a hot upgrade here
|
||||||
|
// to move it from restricted to temporary.
|
||||||
|
peerCount := channeldb.ChanCount{
|
||||||
|
HasOpenOrClosedChan: false,
|
||||||
|
PendingOpenCount: 0,
|
||||||
|
}
|
||||||
|
|
||||||
|
a.peerCounts[peerMapKey] = peerCount
|
||||||
|
a.peerScores[peerMapKey] = peerSlotStatus{
|
||||||
|
state: peerStatusTemporary,
|
||||||
|
}
|
||||||
|
|
||||||
|
acsmLog.InfoS(ctx, "Upgraded outbound peer: restricted -> temporary")
|
||||||
}
|
}
|
||||||
|
|
||||||
// removePeerAccess removes the peer's access from the maps. This should be
|
// removePeerAccess removes the peer's access from the maps. This should be
|
||||||
|
@@ -24,7 +24,7 @@ func assertInboundConnection(t *testing.T, a *accessMan,
|
|||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.Equal(t, status, peerAccess)
|
require.Equal(t, status, peerAccess)
|
||||||
|
|
||||||
a.addPeerAccess(remotePub, peerAccess)
|
a.addPeerAccess(remotePub, peerAccess, true)
|
||||||
peerScore, ok := a.peerScores[remotePubSer]
|
peerScore, ok := a.peerScores[remotePubSer]
|
||||||
require.True(t, ok)
|
require.True(t, ok)
|
||||||
require.Equal(t, status, peerScore.state)
|
require.Equal(t, status, peerScore.state)
|
||||||
|
@@ -4316,12 +4316,14 @@ func (s *server) peerConnected(conn net.Conn, connReq *connmgr.ConnReq,
|
|||||||
addr := conn.RemoteAddr()
|
addr := conn.RemoteAddr()
|
||||||
pubKey := brontideConn.RemotePub()
|
pubKey := brontideConn.RemotePub()
|
||||||
|
|
||||||
// If the remote node's public key is banned, drop the connection.
|
// Only restrict access for inbound connections, which means if the
|
||||||
|
// remote node's public key is banned or the restricted slots are used
|
||||||
|
// up, we will drop the connection.
|
||||||
//
|
//
|
||||||
// TODO(yy): Consider perform this check in
|
// TODO(yy): Consider perform this check in
|
||||||
// `peerAccessMan.addPeerAccess`.
|
// `peerAccessMan.addPeerAccess`.
|
||||||
access, err := s.peerAccessMan.assignPeerPerms(pubKey)
|
access, err := s.peerAccessMan.assignPeerPerms(pubKey)
|
||||||
if err != nil {
|
if inbound && err != nil {
|
||||||
pubSer := pubKey.SerializeCompressed()
|
pubSer := pubKey.SerializeCompressed()
|
||||||
|
|
||||||
// Clean up the persistent peer maps if we're dropping this
|
// Clean up the persistent peer maps if we're dropping this
|
||||||
@@ -4474,7 +4476,7 @@ func (s *server) peerConnected(conn net.Conn, connReq *connmgr.ConnReq,
|
|||||||
p := peer.NewBrontide(pCfg)
|
p := peer.NewBrontide(pCfg)
|
||||||
|
|
||||||
// Update the access manager with the access permission for this peer.
|
// Update the access manager with the access permission for this peer.
|
||||||
s.peerAccessMan.addPeerAccess(pubKey, access)
|
s.peerAccessMan.addPeerAccess(pubKey, access, inbound)
|
||||||
|
|
||||||
// TODO(roasbeef): update IP address for link-node
|
// TODO(roasbeef): update IP address for link-node
|
||||||
// * also mark last-seen, do it one single transaction?
|
// * also mark last-seen, do it one single transaction?
|
||||||
|
Reference in New Issue
Block a user