From fe3437611b282b8ede61fb04edf5ce5a988142de Mon Sep 17 00:00:00 2001 From: Marten Seemann Date: Mon, 20 Sep 2021 18:18:34 +0100 Subject: [PATCH] make Protocols return TCP and the security protocol, fix CanDial --- go.mod | 10 +++++----- go.sum | 31 +++++++++++++++-------------- tcp.go | 33 ++++++++++++++++++++++--------- tcp_test.go | 56 +++++++++++++++++++++++++++++++++++++++-------------- 4 files changed, 86 insertions(+), 44 deletions(-) diff --git a/go.mod b/go.mod index 1938935..79c9b27 100644 --- a/go.mod +++ b/go.mod @@ -4,17 +4,17 @@ go 1.16 require ( github.com/ipfs/go-log v1.0.4 - github.com/libp2p/go-conn-security-multistream v0.2.1 - github.com/libp2p/go-libp2p-core v0.8.6 + github.com/libp2p/go-conn-security-multistream v0.3.0 + github.com/libp2p/go-libp2p-core v0.10.1-0.20210921170543-f829c09c1ca0 github.com/libp2p/go-libp2p-mplex v0.4.1 - github.com/libp2p/go-libp2p-testing v0.4.2 - github.com/libp2p/go-libp2p-transport-upgrader v0.4.6 + github.com/libp2p/go-libp2p-testing v0.5.1-0.20210921173022-d2d7433a5068 + github.com/libp2p/go-libp2p-transport-upgrader v0.5.1-0.20210922105033-ad6ad0abd9c8 github.com/libp2p/go-netroute v0.1.5 // indirect github.com/libp2p/go-reuseport v0.0.2 github.com/libp2p/go-reuseport-transport v0.0.5 github.com/marten-seemann/tcp v0.0.0-20210406111302-dfbc87cc63fd github.com/mikioh/tcpinfo v0.0.0-20190314235526-30a79bb1804b - github.com/multiformats/go-multiaddr v0.3.3 + github.com/multiformats/go-multiaddr v0.4.1 github.com/multiformats/go-multiaddr-fmt v0.1.0 github.com/multiformats/go-multihash v0.0.15 // indirect github.com/prometheus/client_golang v1.10.0 diff --git a/go.sum b/go.sum index 50c5215..e4c888e 100644 --- a/go.sum +++ b/go.sum @@ -167,7 +167,6 @@ github.com/jbenet/go-cienv v0.1.0/go.mod h1:TqNnHUmJgXau0nCzC7kXWeotg3J9W34CUv5D github.com/jbenet/go-temp-err-catcher v0.1.0 h1:zpb3ZH6wIE8Shj2sKS+khgRvf7T7RABoLk/+KKHggpk= github.com/jbenet/go-temp-err-catcher v0.1.0/go.mod h1:0kJRvmDZXNMIiJirNPEYfhpPwbGVtZVWC34vc5WLsDk= github.com/jbenet/goprocess v0.1.3/go.mod h1:5yspPrukOVuOLORacaBi858NqyClJPQxYZlqdZVfqY4= -github.com/jbenet/goprocess v0.1.4 h1:DRGOFReOMqqDNXwW70QkacFW0YN9QnwLV0Vqk+3oU0o= github.com/jbenet/goprocess v0.1.4/go.mod h1:5yspPrukOVuOLORacaBi858NqyClJPQxYZlqdZVfqY4= github.com/jessevdk/go-flags v0.0.0-20141203071132-1679536dcc89/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI= github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI= @@ -197,27 +196,26 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/libp2p/go-buffer-pool v0.0.2 h1:QNK2iAFa8gjAe1SPz6mHSMuCcjs+X1wlHzeOSqcmlfs= github.com/libp2p/go-buffer-pool v0.0.2/go.mod h1:MvaB6xw5vOrDl8rYZGLFdKAuk/hRoRZd1Vi32+RXyFM= -github.com/libp2p/go-conn-security-multistream v0.2.1 h1:ft6/POSK7F+vl/2qzegnHDaXFU0iWB4yVTYrioC6Zy0= -github.com/libp2p/go-conn-security-multistream v0.2.1/go.mod h1:cR1d8gA0Hr59Fj6NhaTpFhJZrjSYuNmhpT2r25zYR70= +github.com/libp2p/go-conn-security-multistream v0.3.0 h1:9UCIKlBL1hC9u7nkMXpD1nkc/T53PKMAn3/k9ivBAVc= +github.com/libp2p/go-conn-security-multistream v0.3.0/go.mod h1:EEP47t4fw/bTelVmEzIDqSe69hO/ip52xBEhZMLWAHM= github.com/libp2p/go-flow-metrics v0.0.3/go.mod h1:HeoSNUrOJVK1jEpDqVEiUOIXqhbnS27omG0uWU5slZs= github.com/libp2p/go-libp2p-core v0.3.0/go.mod h1:ACp3DmS3/N64c2jDzcV429ukDpicbL6+TrrxANBjPGw= github.com/libp2p/go-libp2p-core v0.5.0/go.mod h1:49XGI+kc38oGVwqSBhDEwytaAxgZasHhFfQKibzTls0= -github.com/libp2p/go-libp2p-core v0.7.0/go.mod h1:FfewUH/YpvWbEB+ZY9AQRQ4TAD8sJBt/G1rVvhz5XT8= github.com/libp2p/go-libp2p-core v0.8.0/go.mod h1:FfewUH/YpvWbEB+ZY9AQRQ4TAD8sJBt/G1rVvhz5XT8= -github.com/libp2p/go-libp2p-core v0.8.1/go.mod h1:FfewUH/YpvWbEB+ZY9AQRQ4TAD8sJBt/G1rVvhz5XT8= -github.com/libp2p/go-libp2p-core v0.8.6 h1:3S8g006qG6Tjpj1JdRK2S+TWc2DJQKX/RG9fdLeiLSU= -github.com/libp2p/go-libp2p-core v0.8.6/go.mod h1:dgHr0l0hIKfWpGpqAMbpo19pen9wJfdCGv51mTmdpmM= +github.com/libp2p/go-libp2p-core v0.10.0/go.mod h1:ECdxehoYosLYHgDDFa2N4yE8Y7aQRAMf0sX9mf2sbGg= +github.com/libp2p/go-libp2p-core v0.10.1-0.20210921170543-f829c09c1ca0 h1:W1EqIm0+QVnfQ2SV138//D9NyAhseDxqLMoWvvhYsQQ= +github.com/libp2p/go-libp2p-core v0.10.1-0.20210921170543-f829c09c1ca0/go.mod h1:KlkHsZ0nKerWsXLZJm3LfFQwusI5k3iN4BgtYTE4IYE= github.com/libp2p/go-libp2p-mplex v0.4.1 h1:/pyhkP1nLwjG3OM+VuaNJkQT/Pqq73WzB3aDN3Fx1sc= github.com/libp2p/go-libp2p-mplex v0.4.1/go.mod h1:cmy+3GfqfM1PceHTLL7zQzAAYaryDu6iPSC+CIb094g= github.com/libp2p/go-libp2p-pnet v0.2.0 h1:J6htxttBipJujEjz1y0a5+eYoiPcFHhSYHH6na5f0/k= github.com/libp2p/go-libp2p-pnet v0.2.0/go.mod h1:Qqvq6JH/oMZGwqs3N1Fqhv8NVhrdYcO0BW4wssv21LA= github.com/libp2p/go-libp2p-testing v0.1.2-0.20200422005655-8775583591d8/go.mod h1:Qy8sAncLKpwXtS2dSnDOP8ktexIAHKu+J+pnZOFZLTc= -github.com/libp2p/go-libp2p-testing v0.3.0/go.mod h1:efZkql4UZ7OVsEfaxNHZPzIehtsBXMrXnCfJIgDti5g= github.com/libp2p/go-libp2p-testing v0.4.0/go.mod h1:Q+PFXYoiYFN5CAEG2w3gLPEzotlKsNSbKQ/lImlOWF0= -github.com/libp2p/go-libp2p-testing v0.4.2 h1:IOiA5mMigi+eEjf4J+B7fepDhsjtsoWA9QbsCqbNp5U= -github.com/libp2p/go-libp2p-testing v0.4.2/go.mod h1:Q+PFXYoiYFN5CAEG2w3gLPEzotlKsNSbKQ/lImlOWF0= -github.com/libp2p/go-libp2p-transport-upgrader v0.4.6 h1:SHt3g0FslnqIkEWF25YOB8UCOCTpGAVvHRWQYJ+veiI= -github.com/libp2p/go-libp2p-transport-upgrader v0.4.6/go.mod h1:JE0WQuQdy+uLZ5zOaI3Nw9dWGYJIA7mywEtP2lMvnyk= +github.com/libp2p/go-libp2p-testing v0.5.0/go.mod h1:QBk8fqIL1XNcno/l3/hhaIEn4aLRijpYOR+zVjjlh+A= +github.com/libp2p/go-libp2p-testing v0.5.1-0.20210921173022-d2d7433a5068 h1:Uhzao0ft5xsws67/qzZNlK1kvQKMwuDj/jr0RHfpU98= +github.com/libp2p/go-libp2p-testing v0.5.1-0.20210921173022-d2d7433a5068/go.mod h1:zcQ6B90GUqkXx0xdQ0EyBH8XUvlP36pxEdpUFtPtWNk= +github.com/libp2p/go-libp2p-transport-upgrader v0.5.1-0.20210922105033-ad6ad0abd9c8 h1:3I7js9jPQUmOx9Kyt4tRpp6Dt2tYasbWEkLsoiTOkQg= +github.com/libp2p/go-libp2p-transport-upgrader v0.5.1-0.20210922105033-ad6ad0abd9c8/go.mod h1:4T3Km0CC97JF4T4KH6BjSScdZLvGk/W0WKAm7MAlwYI= github.com/libp2p/go-maddr-filter v0.1.0/go.mod h1:VzZhTXkMucEGGEOSKddrwGiOv0tUhgnKqNEmIAz/bPU= github.com/libp2p/go-mplex v0.3.0 h1:U1T+vmCYJaEoDJPV1aq31N56hS+lJgb397GsylNSgrU= github.com/libp2p/go-mplex v0.3.0/go.mod h1:0Oy/A9PQlwBytDRp4wSkFnzHYDKcpLot35JQ6msjvYQ= @@ -291,8 +289,9 @@ github.com/multiformats/go-multiaddr v0.2.1/go.mod h1:s/Apk6IyxfvMjDafnhJgJ3/46z github.com/multiformats/go-multiaddr v0.2.2/go.mod h1:NtfXiOtHvghW9KojvtySjH5y0u0xW5UouOmQQrn6a3Y= github.com/multiformats/go-multiaddr v0.3.0/go.mod h1:dF9kph9wfJ+3VLAaeBqo9Of8x4fJxp6ggJGteB8HQTI= github.com/multiformats/go-multiaddr v0.3.1/go.mod h1:uPbspcUPd5AfaP6ql3ujFY+QWzmBD8uLLL4bXW0XfGc= -github.com/multiformats/go-multiaddr v0.3.3 h1:vo2OTSAqnENB2rLk79pLtr+uhj+VAzSe3uef5q0lRSs= github.com/multiformats/go-multiaddr v0.3.3/go.mod h1:lCKNGP1EQ1eZ35Za2wlqnabm9xQkib3fyB+nZXHLag0= +github.com/multiformats/go-multiaddr v0.4.1 h1:Pq37uLx3hsyNlTDir7FZyU8+cFCTqd5y1KiM2IzOutI= +github.com/multiformats/go-multiaddr v0.4.1/go.mod h1:3afI9HfVW8csiF8UZqtpYRiDyew8pRX7qLIGHu9FLuM= github.com/multiformats/go-multiaddr-fmt v0.1.0 h1:WLEFClPycPkp4fnIzoFoV9FVd49/eQsuaL3/CWe167E= github.com/multiformats/go-multiaddr-fmt v0.1.0/go.mod h1:hGtDIW4PU4BqJ50gW2quDuPVjyWNZxToGUh/HwTZYJo= github.com/multiformats/go-multiaddr-net v0.2.0/go.mod h1:gGdH3UXny6U3cKKYCvpXI5rnK7YaOIEOPVDI9tsJbEA= @@ -410,8 +409,9 @@ github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+ github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= -github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4= github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= +github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/tmc/grpc-websocket-proxy v0.0.0-20170815181823-89b8d40f7ca8/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U= github.com/urfave/cli v1.20.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA= github.com/urfave/cli v1.22.1/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0= @@ -599,8 +599,9 @@ gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.5/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= -gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU= gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= honnef.co/go/tools v0.0.0-20180728063816-88497007e858/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= diff --git a/tcp.go b/tcp.go index 5c94825..9b5ec75 100644 --- a/tcp.go +++ b/tcp.go @@ -99,6 +99,9 @@ type TcpTransport struct { // TCP connect timeout ConnectTimeout time.Duration + protos []ma.Protocol + dialMatcher mafmt.Pattern + reuse rtpt.Transport } @@ -107,15 +110,27 @@ var _ transport.Transport = &TcpTransport{} // NewTCPTransport creates a tcp transport object that tracks dialers and listeners // created. It represents an entire tcp stack (though it might not necessarily be) func NewTCPTransport(upgrader *tptu.Upgrader) *TcpTransport { - return &TcpTransport{Upgrader: upgrader, ConnectTimeout: DefaultConnectTimeout} + protos := []ma.Protocol{ma.ProtocolWithCode(ma.P_TCP)} + var dialMatcher mafmt.Pattern + // In case the upgrader muxes security protocols, it returns the zero value of ma.Protocol. + // This check can be removed once we remove support for non-security-enabled multiaddrs. + if secProto := upgrader.SecurityProtocol(); secProto.Code != 0 { + protos = append(protos, secProto) + dialMatcher = mafmt.And(mafmt.IP, mafmt.Base(ma.P_TCP), mafmt.Base(secProto.Code)) + } else { + dialMatcher = mafmt.And(mafmt.IP, mafmt.Base(ma.P_TCP)) + } + return &TcpTransport{ + Upgrader: upgrader, + ConnectTimeout: DefaultConnectTimeout, + protos: protos, + dialMatcher: dialMatcher, + } } -var dialMatcher = mafmt.And(mafmt.IP, mafmt.Base(ma.P_TCP)) - -// CanDial returns true if this transport believes it can dial the given -// multiaddr. +// CanDial returns true if this transport believes it can dial the given multiaddr. func (t *TcpTransport) CanDial(addr ma.Multiaddr) bool { - return dialMatcher.Matches(addr) + return t.dialMatcher.Matches(addr) } func (t *TcpTransport) maDial(ctx context.Context, raddr ma.Multiaddr) (manet.Conn, error) { @@ -173,9 +188,9 @@ func (t *TcpTransport) Listen(laddr ma.Multiaddr) (transport.Listener, error) { return t.Upgrader.UpgradeListener(t, list), nil } -// Protocols returns the list of terminal protocols this transport can dial. -func (t *TcpTransport) Protocols() []int { - return []int{ma.P_TCP} +// Protocols returns the protocols this transport can dial. +func (t *TcpTransport) Protocols() []ma.Protocol { + return t.protos } // Proxy always returns false for the TCP transport. diff --git a/tcp_test.go b/tcp_test.go index 3d9d1ad..a5cf7b4 100644 --- a/tcp_test.go +++ b/tcp_test.go @@ -15,18 +15,18 @@ import ( ma "github.com/multiformats/go-multiaddr" ) -func TestTcpTransport(t *testing.T) { +func TestTcpTransportWithSecureMuxer(t *testing.T) { for i := 0; i < 2; i++ { peerA, ia := makeInsecureMuxer(t) _, ib := makeInsecureMuxer(t) ta := NewTCPTransport(&tptu.Upgrader{ - Secure: ia, - Muxer: new(mplex.Transport), + SecureMuxer: ia, + Muxer: new(mplex.Transport), }) tb := NewTCPTransport(&tptu.Upgrader{ - Secure: ib, - Muxer: new(mplex.Transport), + SecureMuxer: ib, + Muxer: new(mplex.Transport), }) zero := "/ip4/127.0.0.1/tcp/0" @@ -37,6 +37,28 @@ func TestTcpTransport(t *testing.T) { envReuseportVal = true } +func TestTcpTransportWithSecureTransport(t *testing.T) { + for i := 0; i < 2; i++ { + peerA, ia := makeInsecureTransport(t) + _, ib := makeInsecureTransport(t) + + ta := NewTCPTransport(&tptu.Upgrader{ + SecureTransport: ia, + Muxer: new(mplex.Transport), + }) + tb := NewTCPTransport(&tptu.Upgrader{ + SecureTransport: ib, + Muxer: new(mplex.Transport), + }) + + zero := "/ip4/127.0.0.1/tcp/0/plaintextv2" + ttransport.SubtestTransport(t, ta, tb, zero, peerA) + + envReuseportVal = false + } + envReuseportVal = true +} + func TestTcpTransportCantDialDNS(t *testing.T) { for i := 0; i < 2; i++ { dnsa, err := ma.NewMultiaddr("/dns4/example.com/tcp/1234") @@ -46,8 +68,8 @@ func TestTcpTransportCantDialDNS(t *testing.T) { _, sm := makeInsecureMuxer(t) tpt := NewTCPTransport(&tptu.Upgrader{ - Secure: sm, - Muxer: new(mplex.Transport), + SecureMuxer: sm, + Muxer: new(mplex.Transport), }) if tpt.CanDial(dnsa) { @@ -68,12 +90,10 @@ func TestTcpTransportCantListenUtp(t *testing.T) { _, sm := makeInsecureMuxer(t) tpt := NewTCPTransport(&tptu.Upgrader{ - Secure: sm, - Muxer: new(mplex.Transport), + SecureMuxer: sm, + Muxer: new(mplex.Transport), }) - - _, err = tpt.Listen(utpa) - if err == nil { + if _, err := tpt.Listen(utpa); err == nil { t.Fatal("shouldnt be able to listen on utp addr with tcp transport") } @@ -83,6 +103,14 @@ func TestTcpTransportCantListenUtp(t *testing.T) { } func makeInsecureMuxer(t *testing.T) (peer.ID, sec.SecureMuxer) { + t.Helper() + id, tr := makeInsecureTransport(t) + var secMuxer csms.SSMuxer + secMuxer.AddTransport(insecure.ID, tr) + return id, &secMuxer +} + +func makeInsecureTransport(t *testing.T) (peer.ID, sec.SecureTransport) { t.Helper() priv, _, err := crypto.GenerateKeyPair(crypto.Ed25519, 256) if err != nil { @@ -92,7 +120,5 @@ func makeInsecureMuxer(t *testing.T) (peer.ID, sec.SecureMuxer) { if err != nil { t.Fatal(err) } - var secMuxer csms.SSMuxer - secMuxer.AddTransport(insecure.ID, insecure.NewWithIdentity(id, priv)) - return id, &secMuxer + return id, insecure.NewWithIdentity(id, priv) }