This repository has been archived by the owner on May 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 42
gate QUIC connections via new ConnectionGater #152
Merged
Merged
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
bc2b6e0
gating in QUIC
aarshkshah1992 21b5936
changes as per review
aarshkshah1992 135bb88
fix import order
aarshkshah1992 d960369
add test for intercept secured
aarshkshah1992 cb8b875
go mod tidy.
raulk 6326c75
naming nits; upgrade deps.
raulk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,20 +8,51 @@ import ( | |
"io/ioutil" | ||
mrand "math/rand" | ||
"net" | ||
"sync" | ||
"sync/atomic" | ||
"time" | ||
|
||
"github.com/libp2p/go-libp2p-core/control" | ||
ic "github.com/libp2p/go-libp2p-core/crypto" | ||
"github.com/libp2p/go-libp2p-core/network" | ||
"github.com/libp2p/go-libp2p-core/peer" | ||
tpt "github.com/libp2p/go-libp2p-core/transport" | ||
filter "github.com/libp2p/go-maddr-filter" | ||
|
||
quicproxy "github.com/lucas-clemente/quic-go/integrationtests/tools/proxy" | ||
ma "github.com/multiformats/go-multiaddr" | ||
manet "github.com/multiformats/go-multiaddr-net" | ||
|
||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
type localhostMockGater struct { | ||
lk sync.Mutex | ||
allowAll bool | ||
} | ||
|
||
func (c *localhostMockGater) InterceptAccept(addrs network.ConnMultiaddrs) bool { | ||
c.lk.Lock() | ||
defer c.lk.Unlock() | ||
return c.allowAll || !manet.IsIPLoopback(addrs.RemoteMultiaddr()) | ||
} | ||
|
||
func (c *localhostMockGater) InterceptPeerDial(p peer.ID) (allow bool) { | ||
return true | ||
} | ||
|
||
func (c *localhostMockGater) InterceptAddrDial(peer.ID, ma.Multiaddr) (allow bool) { | ||
return true | ||
} | ||
|
||
func (c *localhostMockGater) InterceptSecured(network.Direction, peer.ID, network.ConnMultiaddrs) (allow bool) { | ||
return true | ||
} | ||
|
||
func (c *localhostMockGater) InterceptUpgraded(network.Conn) (allow bool, reason control.DisconnectReason) { | ||
return true, 0 | ||
} | ||
|
||
var _ = Describe("Connection", func() { | ||
var ( | ||
serverKey, clientKey ic.PrivKey | ||
|
@@ -166,17 +197,12 @@ var _ = Describe("Connection", func() { | |
}) | ||
|
||
It("filters addresses", func() { | ||
filters := filter.NewFilters() | ||
ipNet := net.IPNet{ | ||
IP: net.IPv4(127, 0, 0, 1), | ||
Mask: net.IPv4Mask(255, 255, 255, 255), | ||
} | ||
filters.AddFilter(ipNet, filter.ActionDeny) | ||
testMA, err := ma.NewMultiaddr("/ip4/127.0.0.1/udp/1234/quic") | ||
Expect(err).ToNot(HaveOccurred()) | ||
Expect(filters.AddrBlocked(testMA)).To(BeTrue()) | ||
cg := &localhostMockGater{} | ||
Expect(cg.InterceptAccept(&connAddrs{rmAddr: testMA})).To(BeFalse()) | ||
raulk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
serverTransport, err := NewTransport(serverKey, nil, filters) | ||
serverTransport, err := NewTransport(serverKey, nil, cg) | ||
Expect(err).ToNot(HaveOccurred()) | ||
ln := runServer(serverTransport, "/ip4/127.0.0.1/udp/0/quic") | ||
defer ln.Close() | ||
|
@@ -192,7 +218,9 @@ var _ = Describe("Connection", func() { | |
|
||
// now allow the address and make sure the connection goes through | ||
clientTransport.(*transport).clientConfig.HandshakeTimeout = 2 * time.Second | ||
filters.AddFilter(ipNet, filter.ActionAccept) | ||
cg.lk.Lock() | ||
cg.allowAll = true | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A test for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is done. |
||
cg.lk.Unlock() | ||
conn, err := clientTransport.Dial(context.Background(), ln.Multiaddr(), serverID) | ||
Expect(err).ToNot(HaveOccurred()) | ||
conn.Close() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,9 +3,13 @@ package libp2pquic | |
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"io" | ||
"net" | ||
|
||
"github.com/libp2p/go-libp2p-core/connmgr" | ||
n "github.com/libp2p/go-libp2p-core/network" | ||
|
||
"github.com/minio/sha256-simd" | ||
"golang.org/x/crypto/hkdf" | ||
|
||
|
@@ -15,7 +19,6 @@ import ( | |
"github.com/libp2p/go-libp2p-core/pnet" | ||
tpt "github.com/libp2p/go-libp2p-core/transport" | ||
p2ptls "github.com/libp2p/go-libp2p-tls" | ||
filter "github.com/libp2p/go-maddr-filter" | ||
quic "github.com/lucas-clemente/quic-go" | ||
ma "github.com/multiformats/go-multiaddr" | ||
mafmt "github.com/multiformats/go-multiaddr-fmt" | ||
|
@@ -43,9 +46,9 @@ type connManager struct { | |
reuseUDP6 *reuse | ||
} | ||
|
||
func newConnManager(filters *filter.Filters) (*connManager, error) { | ||
reuseUDP4 := newReuse(filters) | ||
reuseUDP6 := newReuse(filters) | ||
func newConnManager(gater connmgr.ConnectionGater) (*connManager, error) { | ||
reuseUDP4 := newReuse(gater) | ||
reuseUDP6 := newReuse(gater) | ||
|
||
return &connManager{ | ||
reuseUDP4: reuseUDP4, | ||
|
@@ -88,12 +91,13 @@ type transport struct { | |
connManager *connManager | ||
serverConfig *quic.Config | ||
clientConfig *quic.Config | ||
gater connmgr.ConnectionGater | ||
} | ||
|
||
var _ tpt.Transport = &transport{} | ||
|
||
// NewTransport creates a new QUIC transport | ||
func NewTransport(key ic.PrivKey, psk pnet.PSK, filters *filter.Filters) (tpt.Transport, error) { | ||
func NewTransport(key ic.PrivKey, psk pnet.PSK, gater connmgr.ConnectionGater) (tpt.Transport, error) { | ||
if len(psk) > 0 { | ||
log.Error("QUIC doesn't support private networks yet.") | ||
return nil, errors.New("QUIC doesn't support private networks yet") | ||
|
@@ -106,7 +110,7 @@ func NewTransport(key ic.PrivKey, psk pnet.PSK, filters *filter.Filters) (tpt.Tr | |
if err != nil { | ||
return nil, err | ||
} | ||
connManager, err := newConnManager(filters) | ||
connManager, err := newConnManager(gater) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
@@ -128,6 +132,7 @@ func NewTransport(key ic.PrivKey, psk pnet.PSK, filters *filter.Filters) (tpt.Tr | |
connManager: connManager, | ||
serverConfig: config, | ||
clientConfig: config.Clone(), | ||
gater: gater, | ||
} | ||
t.serverConfig.GetLogWriter = getLogWriterFor("server") | ||
t.clientConfig.GetLogWriter = getLogWriterFor("client") | ||
|
@@ -178,6 +183,13 @@ func (t *transport) Dial(ctx context.Context, raddr ma.Multiaddr, p peer.ID) (tp | |
pconn.DecreaseCount() | ||
return nil, err | ||
} | ||
|
||
connaddrs := &connAddrs{lmAddr: localMultiaddr, rmAddr: remoteMultiaddr} | ||
if t.gater != nil && !t.gater.InterceptSecured(n.DirOutbound, p, connaddrs) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need to close the connection at this point. Otherwise the peer will never know that we closed the connection. As from quic-go's point of view the connection is fully established, there won't be any connection-level timeout here either. |
||
pconn.DecreaseCount() | ||
return nil, fmt.Errorf("secured connection gated") | ||
} | ||
|
||
return &conn{ | ||
sess: sess, | ||
transport: t, | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@raulk I know this will make you cringe and add some entropy to thy life. But, I have added a TODO at libp2p/go-libp2p#930 to replace this with the "Functional Gater" that we plan to implement.
For all that you hold dear on this good Earth, rest assured, this shall be fixed.