Skip to content

Commit 321685a

Browse files
committed
brontide+server.go: Listener ban-callback, introduce server caches
Here we introduce the server caches that will determine the access control status of our peers. Peers that have had their funding transaction confirm with us are protected. Peers that only have pending-open channels with us are temporary access and can have their access revoked. The rest of the peers are granted restricted access. The channelPeers map contains protected-access peers and the pendingChannelPeers map contains temporary-access peers.
1 parent 1cee6ea commit 321685a

File tree

7 files changed

+573
-51
lines changed

7 files changed

+573
-51
lines changed

brontide/listener.go

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package brontide
22

33
import (
4+
"encoding/hex"
45
"errors"
56
"fmt"
67
"io"
@@ -12,7 +13,7 @@ import (
1213

1314
// defaultHandshakes is the maximum number of handshakes that can be done in
1415
// parallel.
15-
const defaultHandshakes = 1000
16+
const defaultHandshakes = 50
1617

1718
// Listener is an implementation of a net.Conn which executes an authenticated
1819
// key exchange and message encryption protocol dubbed "Machine" after
@@ -24,6 +25,10 @@ type Listener struct {
2425

2526
tcp *net.TCPListener
2627

28+
// shouldAccept is a closure that determines if we should accept the
29+
// incoming connection or not based on its hex-encoded public key.
30+
shouldAccept func(string) (bool, error)
31+
2732
handshakeSema chan struct{}
2833
conns chan maybeConn
2934
quit chan struct{}
@@ -34,8 +39,8 @@ var _ net.Listener = (*Listener)(nil)
3439

3540
// NewListener returns a new net.Listener which enforces the Brontide scheme
3641
// during both initial connection establishment and data transfer.
37-
func NewListener(localStatic keychain.SingleKeyECDH,
38-
listenAddr string) (*Listener, error) {
42+
func NewListener(localStatic keychain.SingleKeyECDH, listenAddr string,
43+
shouldAccept func(string) (bool, error)) (*Listener, error) {
3944

4045
addr, err := net.ResolveTCPAddr("tcp", listenAddr)
4146
if err != nil {
@@ -50,6 +55,7 @@ func NewListener(localStatic keychain.SingleKeyECDH,
5055
brontideListener := &Listener{
5156
localStatic: localStatic,
5257
tcp: l,
58+
shouldAccept: shouldAccept,
5359
handshakeSema: make(chan struct{}, defaultHandshakes),
5460
conns: make(chan maybeConn),
5561
quit: make(chan struct{}),
@@ -193,6 +199,29 @@ func (l *Listener) doHandshake(conn net.Conn) {
193199
return
194200
}
195201

202+
// Call the shouldAccept closure to see if the remote node's public key
203+
// is allowed according to our banning heuristic. This is here because
204+
// we do not learn the remote node's public static key until we've
205+
// received and validated Act 3.
206+
remoteKey := brontideConn.RemotePub()
207+
if remoteKey == nil {
208+
connErr := fmt.Errorf("no remote pubkey")
209+
brontideConn.conn.Close()
210+
l.rejectConn(rejectedConnErr(connErr, remoteAddr))
211+
212+
return
213+
}
214+
215+
remoteHex := hex.EncodeToString(remoteKey.SerializeCompressed())
216+
accepted, acceptErr := l.shouldAccept(remoteHex)
217+
if !accepted {
218+
// Reject the connection.
219+
brontideConn.conn.Close()
220+
l.rejectConn(rejectedConnErr(acceptErr, remoteAddr))
221+
222+
return
223+
}
224+
196225
l.acceptConn(brontideConn)
197226
}
198227

@@ -255,3 +284,9 @@ func (l *Listener) Close() error {
255284
func (l *Listener) Addr() net.Addr {
256285
return l.tcp.Addr()
257286
}
287+
288+
// DisabledBanClosure is used in places that NewListener is invoked to bypass
289+
// the ban-scoring.
290+
func DisabledBanClosure(s string) (bool, error) {
291+
return true, nil
292+
}

brontide/noise_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func makeListener() (*Listener, *lnwire.NetAddress, error) {
3535
addr := "localhost:0"
3636

3737
// Our listener will be local, and the connection remote.
38-
listener, err := NewListener(localKeyECDH, addr)
38+
listener, err := NewListener(localKeyECDH, addr, DisabledBanClosure)
3939
if err != nil {
4040
return nil, nil, err
4141
}

discovery/syncer.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,9 @@ const (
181181
// requestBatchSize is the maximum number of channels we will query the
182182
// remote peer for in a QueryShortChanIDs message.
183183
requestBatchSize = 500
184+
185+
// syncerBufferSize is the size of the syncer's buffers.
186+
syncerBufferSize = 5
184187
)
185188

186189
var (
@@ -436,8 +439,8 @@ func newGossipSyncer(cfg gossipSyncerCfg, sema chan struct{}) *GossipSyncer {
436439
rateLimiter: rateLimiter,
437440
syncTransitionReqs: make(chan *syncTransitionReq),
438441
historicalSyncReqs: make(chan *historicalSyncReq),
439-
gossipMsgs: make(chan lnwire.Message, 100),
440-
queryMsgs: make(chan lnwire.Message, 100),
442+
gossipMsgs: make(chan lnwire.Message, syncerBufferSize),
443+
queryMsgs: make(chan lnwire.Message, syncerBufferSize),
441444
syncerSema: sema,
442445
quit: make(chan struct{}),
443446
}

funding/manager.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ const (
109109
// pendingChansLimit is the maximum number of pending channels that we
110110
// can have. After this point, pending channel opens will start to be
111111
// rejected.
112-
pendingChansLimit = 1_000
112+
pendingChansLimit = 50
113113
)
114114

115115
var (

peer/brontide.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,9 @@ const (
9090
// torTimeoutMultiplier is the scaling factor we use on network timeouts
9191
// for Tor peers.
9292
torTimeoutMultiplier = 3
93+
94+
// msgStreamSize is the size of the message streams.
95+
msgStreamSize = 5
9396
)
9497

9598
var (
@@ -1856,7 +1859,7 @@ func newChanMsgStream(p *Brontide, cid lnwire.ChannelID) *msgStream {
18561859
return newMsgStream(p,
18571860
fmt.Sprintf("Update stream for ChannelID(%x) created", cid[:]),
18581861
fmt.Sprintf("Update stream for ChannelID(%x) exiting", cid[:]),
1859-
1000,
1862+
msgStreamSize,
18601863
apply,
18611864
)
18621865
}
@@ -1875,7 +1878,7 @@ func newDiscMsgStream(p *Brontide) *msgStream {
18751878
p,
18761879
"Update stream for gossiper created",
18771880
"Update stream for gossiper exited",
1878-
1000,
1881+
msgStreamSize,
18791882
apply,
18801883
)
18811884
}

0 commit comments

Comments
 (0)