Giter Club home page Giter Club logo

go-libp2p-transport-upgrader's Introduction

DEPRECATION NOTICE

This package has moved into go-libp2p as a sub-package, github.com/libp2p/go-libp2p/p2p/net/upgrader.

go-libp2p-transport-upgrader

GoDoc Build Status Discourse posts

Add encryption and multiplexing capabilities to libp2p transport connections

This package is a component of libp2p, a modular networking stack for building peer-to-peer applications.

For two libp2p peers to communicate, the connection between them must be secure, and each peer must be able to open multiple independent streams of communication over a single channel. We call connections with these features "capable" connections.

Many of the underlying transport protocols that are used by libp2p do not provide the required capabilities "out of the box." go-libp2p-transport-upgrader provides the necessary logic to upgrade connections and listeners into fully capable connections and connection listeners.

In order to be upgraded, the underlying connection or listener must be a multiaddr-net Conn or Listener. The multiaddr-net types integrate the Go standard library connection types with multiaddr, an extensible addressing format used throughout libp2p.

As well as the mandatory capabilities of security and multiplexing, the upgrader can optionally apply a Protector for private networking, as well as an address filter to prevent connections to specific addresses.

Install

Most people building applications with libp2p will have no need to install go-libp2p-transport-upgrader directly. It is included as a dependency of the main go-libp2p "entry point" module and is integrated into the libp2p Host.

For users who do not depend on go-libp2p and are managing their libp2p module dependencies in a more manual fashion, go-libp2p-transport-upgrader is a standard Go module which can be installed with:

go get github.com/libp2p/go-libp2p-transport-upgrader

Usage

To use, construct a new Upgrader with:

In practice, most users will not need to construct an Upgrader directly. Instead, when constructing a libp2p Host, you can pass in some combination of the PrivateNetwork, Filters, Security, and Muxer Options. This will configure the Upgrader that is created and used by the Host internally.

Example

Below is a simplified TCP transport implementation using the transport upgrader. In practice, you'll want to use go-tcp-transport, which is optimized for production usage.

package tcptransport

import (
	"context"

	tptu "github.com/libp2p/go-libp2p-transport-upgrader"

	ma "github.com/multiformats/go-multiaddr"
	mafmt "github.com/multiformats/go-multiaddr-fmt"
	manet "github.com/multiformats/go-multiaddr/net"
	tpt "github.com/libp2p/go-libp2p-core/transport"
	peer "github.com/libp2p/go-libp2p-core/peer"
)

// TcpTransport is a simple TCP transport.
type TcpTransport struct {
	// Connection upgrader for upgrading insecure stream connections to
	// secure multiplex connections.
	Upgrader *tptu.Upgrader
}

var _ tpt.Transport = &TcpTransport{}

// NewTCPTransport creates a new TCP transport instance.
func NewTCPTransport(upgrader *tptu.Upgrader) *TcpTransport {
	return &TcpTransport{Upgrader: upgrader}
}

// CanDial returns true if this transport believes it can dial the given
// multiaddr.
func (t *TcpTransport) CanDial(addr ma.Multiaddr) bool {
	return mafmt.TCP.Matches(addr)
}

// Dial dials the peer at the remote address.
func (t *TcpTransport) Dial(ctx context.Context, raddr ma.Multiaddr, p peer.ID) (tpt.CapableConn, error) {
	var dialer manet.Dialer
	conn, err := dialer.DialContext(ctx, raddr)
	if err != nil {
		return nil, err
	}
	return t.Upgrader.UpgradeOutbound(ctx, t, conn, p)
}

// Listen listens on the given multiaddr.
func (t *TcpTransport) Listen(laddr ma.Multiaddr) (tpt.Listener, error) {
	list, err := manet.Listen(laddr)
	if err != nil {
		return nil, err
	}
	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}
}

// Proxy always returns false for the TCP transport.
func (t *TcpTransport) Proxy() bool {
	return false
}

Contribute

Feel free to join in. All welcome. Open an issue!

This repository falls under the libp2p Code of Conduct.

Want to hack on libp2p?

License

MIT


The last gx published version of this module was: 0.1.28: QmeqC5shQjEBRG9B8roZqQCJ9xb7Pq6AbWxJFMyLgqBBWh

go-libp2p-transport-upgrader's People

Contributors

aarshkshah1992 avatar dependabot-preview[bot] avatar hannahhoward avatar hsanjuan avatar kevina avatar kubuxu avatar marten-seemann avatar raulk avatar stebalien avatar vyzo avatar web3-bot avatar yusefnapora avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

go-libp2p-transport-upgrader's Issues

Data race in tests

Uncovered by new travis config that runs tests with the -race flag (#16):

=== RUN   TestGoLibp2pStreamTransport
Running Suite: GoLibp2pStreamTransport Suite
============================================
Random Seed: 1551460766
Will run 9 of 9 specs
••••••==================
WARNING: DATA RACE
Write at 0x00000111c4f8 by goroutine 8:
  github.com/libp2p/go-libp2p-transport-upgrader_test.glob..func1.4()
      /home/travis/gopath/src/github.com/libp2p/go-libp2p-transport-upgrader/listener_test.go:102 +0x3a
  github.com/onsi/ginkgo/internal/leafnodes.(*runner).runSync()
      /home/travis/gopath/pkg/mod/github.com/onsi/[email protected]/internal/leafnodes/runner.go:113 +0xbd
  github.com/onsi/ginkgo/internal/leafnodes.(*runner).run()
      /home/travis/gopath/pkg/mod/github.com/onsi/[email protected]/internal/leafnodes/runner.go:64 +0x16a
  github.com/onsi/ginkgo/internal/leafnodes.(*SetupNode).Run()
      /home/travis/gopath/pkg/mod/github.com/onsi/[email protected]/internal/leafnodes/setup_nodes.go:15 +0xa2
  github.com/onsi/ginkgo/internal/spec.(*Spec).runSample()
      /home/travis/gopath/pkg/mod/github.com/onsi/[email protected]/internal/spec/spec.go:193 +0x2c6
  github.com/onsi/ginkgo/internal/spec.(*Spec).Run()
      /home/travis/gopath/pkg/mod/github.com/onsi/[email protected]/internal/spec/spec.go:138 +0x145
  github.com/onsi/ginkgo/internal/specrunner.(*SpecRunner).runSpec()
      /home/travis/gopath/pkg/mod/github.com/onsi/[email protected]/internal/specrunner/spec_runner.go:200 +0x172
  github.com/onsi/ginkgo/internal/specrunner.(*SpecRunner).runSpecs()
      /home/travis/gopath/pkg/mod/github.com/onsi/[email protected]/internal/specrunner/spec_runner.go:170 +0x4b6
  github.com/onsi/ginkgo/internal/specrunner.(*SpecRunner).Run()
      /home/travis/gopath/pkg/mod/github.com/onsi/[email protected]/internal/specrunner/spec_runner.go:66 +0x149
  github.com/onsi/ginkgo/internal/suite.(*Suite).Run()
      /home/travis/gopath/pkg/mod/github.com/onsi/[email protected]/internal/suite/suite.go:62 +0x3e1
  github.com/onsi/ginkgo.RunSpecsWithCustomReporters()
      /home/travis/gopath/pkg/mod/github.com/onsi/[email protected]/ginkgo_dsl.go:221 +0x367
  github.com/onsi/ginkgo.RunSpecs()
      /home/travis/gopath/pkg/mod/github.com/onsi/[email protected]/ginkgo_dsl.go:202 +0x99
  github.com/libp2p/go-libp2p-transport-upgrader_test.TestGoLibp2pStreamTransport()
      /home/travis/gopath/src/github.com/libp2p/go-libp2p-transport-upgrader/go_libp2p_stream_transport_suite_test.go:12 +0xf6
  testing.tRunner()
      /home/travis/.gimme/versions/go1.11.5.linux.amd64/src/testing/testing.go:827 +0x162
Previous read at 0x00000111c4f8 by goroutine 87:
  github.com/libp2p/go-libp2p-transport-upgrader.(*listener).handleIncoming.func2()
      /home/travis/gopath/src/github.com/libp2p/go-libp2p-transport-upgrader/listener.go:100 +0xd3
Goroutine 8 (running) created at:
  testing.(*T).Run()
      /home/travis/.gimme/versions/go1.11.5.linux.amd64/src/testing/testing.go:878 +0x659
  testing.runTests.func1()
      /home/travis/.gimme/versions/go1.11.5.linux.amd64/src/testing/testing.go:1119 +0xa8
  testing.tRunner()
      /home/travis/.gimme/versions/go1.11.5.linux.amd64/src/testing/testing.go:827 +0x162
  testing.runTests()
      /home/travis/.gimme/versions/go1.11.5.linux.amd64/src/testing/testing.go:1117 +0x4ee
  testing.(*M).Run()
      /home/travis/.gimme/versions/go1.11.5.linux.amd64/src/testing/testing.go:1034 +0x2ee
  main.main()
      _testmain.go:98 +0x332
Goroutine 87 (running) created at:
  github.com/libp2p/go-libp2p-transport-upgrader.(*listener).handleIncoming()
      /home/travis/gopath/src/github.com/libp2p/go-libp2p-transport-upgrader/listener.go:97 +0x5a4
==================

undefined: pnet.Protector

Env: go 1.14 win

Probelm:

# github.com/libp2p/go-libp2p-transport-upgrader
..\..\go\pkg\mod\github.com\libp2p\go-libp2p-transport-upgrader@v0.1.1\upgrader.go:29:12: undefined: pnet.Protector

Why github.com/libp2p/go-libp2p-transport-upgrader

> go mod why github.com/libp2p/go-libp2p-transport-upgrader
# github.com/libp2p/go-libp2p-transport-upgrader
github.com/ngin-network/ngcore/ngp2p
github.com/libp2p/go-libp2p
github.com/libp2p/go-libp2p-circuit
github.com/libp2p/go-libp2p-transport-upgrader

in go.mod

	github.com/libp2p/go-libp2p v0.5.2

and then I checked godoc, there does not have pnet.Protector type.

timeout in TestAcceptQueueBacklogged test

=== RUN   TestAcceptQueueBacklogged
panic: test timed out after 10m0s

goroutine 890 [running]:
testing.(*M).startAlarm.func1()
	/opt/hostedtoolcache/go/1.16.7/x64/src/testing/testing.go:1700 +0x11f
created by time.goFunc
	/opt/hostedtoolcache/go/1.16.7/x64/src/time/sleep.go:180 +0x52

goroutine 1 [chan receive]:
testing.(*T).Run(0xc000086600, 0xa5d6d9, 0x19, 0xb947b0, 0x1)
	/opt/hostedtoolcache/go/1.16.7/x64/src/testing/testing.go:1239 +0x610
testing.runTests.func1(0xc000086600)
	/opt/hostedtoolcache/go/1.16.7/x64/src/testing/testing.go:1511 +0xa7
testing.tRunner(0xc000086600, 0xc00013fce0)
	/opt/hostedtoolcache/go/1.16.7/x64/src/testing/testing.go:1193 +0x203
testing.runTests(0xc0000b6570, 0xe7e3c0, 0xa, 0xa, 0xc0464e872a20275b, 0x8bb31a1d67, 0xe85360, 0xc0000a2148)
	/opt/hostedtoolcache/go/1.16.7/x64/src/testing/testing.go:1509 +0x613
testing.(*M).Run(0xc000140080, 0x0)
	/opt/hostedtoolcache/go/1.16.7/x64/src/testing/testing.go:1417 +0x3b4
main.main()
	_testmain.go:63 +0x237

goroutine 19 [select]:
github.com/ipfs/go-log/writer.(*MirrorWriter).logRoutine(0xc000097410)
	/home/runner/go/pkg/mod/github.com/ipfs/[email protected]/writer/writer.go:71 +0x193
created by github.com/ipfs/go-log/writer.NewMirrorWriter
	/home/runner/go/pkg/mod/github.com/ipfs/[email protected]/writer/writer.go:36 +0x13a

goroutine 13 [IO wait]:
internal/poll.runtime_pollWait(0x7f6a496d3880, 0x72, 0xe33610)
	/opt/hostedtoolcache/go/1.16.7/x64/src/runtime/netpoll.go:222 +0x55
internal/poll.(*pollDesc).wait(0xc000074198, 0x72, 0x1000, 0x1000, 0xffffffffffffffff)
	/opt/hostedtoolcache/go/1.16.7/x64/src/internal/poll/fd_poll_runtime.go:87 +0xe6
internal/poll.(*pollDesc).waitRead(...)
	/opt/hostedtoolcache/go/1.16.7/x64/src/internal/poll/fd_poll_runtime.go:92
internal/poll.(*FD).Read(0xc000074180, 0xc00007f000, 0x1000, 0x1000, 0x0, 0x0, 0x0)
	/opt/hostedtoolcache/go/1.16.7/x64/src/internal/poll/fd_unix.go:166 +0x28f
net.(*netFD).Read(0xc000074180, 0xc00007f000, 0x1000, 0x1000, 0x0, 0x0, 0x0)
	/opt/hostedtoolcache/go/1.16.7/x64/src/net/fd_posix.go:55 +0x69
net.(*conn).Read(0xc000010028, 0xc00007f000, 0x1000, 0x1000, 0x0, 0x0, 0x0)
	/opt/hostedtoolcache/go/1.16.7/x64/src/net/net.go:183 +0xec
bufio.(*Reader).fill(0xc000062600)
	/opt/hostedtoolcache/go/1.16.7/x64/src/bufio/bufio.go:101 +0x1af
bufio.(*Reader).ReadByte(0xc000062600, 0x38, 0x50, 0xc0001a2bf0)
	/opt/hostedtoolcache/go/1.16.7/x64/src/bufio/bufio.go:253 +0x5d
github.com/multiformats/go-varint.ReadUvarint(0xc07020, 0xc000062600, 0xc000012320, 0xc000062780, 0xc0001a2c98)
	/home/runner/go/pkg/mod/github.com/multiformats/[email protected]/varint.go:80 +0xa2
github.com/libp2p/go-mplex.(*Multiplex).readNextHeader(0xc000074400, 0xc000012301, 0x0, 0x0, 0xc00007c080)
	/home/runner/go/pkg/mod/github.com/libp2p/[email protected]/multiplex.go:508 +0x65
github.com/libp2p/go-mplex.(*Multiplex).handleIncoming(0xc000074400)
	/home/runner/go/pkg/mod/github.com/libp2p/[email protected]/multiplex.go:365 +0x14b
created by github.com/libp2p/go-mplex.NewMultiplex
	/home/runner/go/pkg/mod/github.com/libp2p/[email protected]/multiplex.go:105 +0x3c5

goroutine 14 [select]:
github.com/libp2p/go-mplex.(*Multiplex).handleOutgoing(0xc000074400)
	/home/runner/go/pkg/mod/github.com/libp2p/[email protected]/multiplex.go:191 +0x145
created by github.com/libp2p/go-mplex.NewMultiplex
	/home/runner/go/pkg/mod/github.com/libp2p/[email protected]/multiplex.go:106 +0x3e7

goroutine 11 [IO wait]:
internal/poll.runtime_pollWait(0x7f6a496d3798, 0x72, 0xe33610)
	/opt/hostedtoolcache/go/1.16.7/x64/src/runtime/netpoll.go:222 +0x55
internal/poll.(*pollDesc).wait(0xc000074218, 0x72, 0x1000, 0x1000, 0xffffffffffffffff)
	/opt/hostedtoolcache/go/1.16.7/x64/src/internal/poll/fd_poll_runtime.go:87 +0xe6
internal/poll.(*pollDesc).waitRead(...)
	/opt/hostedtoolcache/go/1.16.7/x64/src/internal/poll/fd_poll_runtime.go:92
internal/poll.(*FD).Read(0xc000074200, 0xc00007e000, 0x1000, 0x1000, 0x0, 0x0, 0x0)
	/opt/hostedtoolcache/go/1.16.7/x64/src/internal/poll/fd_unix.go:166 +0x28f
net.(*netFD).Read(0xc000074200, 0xc00007e000, 0x1000, 0x1000, 0xc00007a400, 0x100, 0x0)
	/opt/hostedtoolcache/go/1.16.7/x64/src/net/fd_posix.go:55 +0x69
net.(*conn).Read(0xc000010018, 0xc00007e000, 0x1000, 0x1000, 0x1, 0xc00007a400, 0x7f6a49e18308)
	/opt/hostedtoolcache/go/1.16.7/x64/src/net/net.go:183 +0xec
bufio.(*Reader).fill(0xc0000623c0)
	/opt/hostedtoolcache/go/1.16.7/x64/src/bufio/bufio.go:101 +0x1af
bufio.(*Reader).ReadByte(0xc0000623c0, 0x0, 0x0, 0x4)
	/opt/hostedtoolcache/go/1.16.7/x64/src/bufio/bufio.go:253 +0x5d
github.com/multiformats/go-varint.ReadUvarint(0xc07020, 0xc0000623c0, 0x4, 0x4d231f, 0xc00004dc98)
	/home/runner/go/pkg/mod/github.com/multiformats/[email protected]/varint.go:80 +0xa2
github.com/libp2p/go-mplex.(*Multiplex).readNextHeader(0xc000074300, 0xc00004dd01, 0xc00004dd70, 0x1, 0x3)
	/home/runner/go/pkg/mod/github.com/libp2p/[email protected]/multiplex.go:508 +0x65
github.com/libp2p/go-mplex.(*Multiplex).handleIncoming(0xc000074300)
	/home/runner/go/pkg/mod/github.com/libp2p/[email protected]/multiplex.go:365 +0x14b
created by github.com/libp2p/go-mplex.NewMultiplex
	/home/runner/go/pkg/mod/github.com/libp2p/[email protected]/multiplex.go:105 +0x3c5

goroutine 12 [select]:
github.com/libp2p/go-mplex.(*Multiplex).handleOutgoing(0xc000074300)
	/home/runner/go/pkg/mod/github.com/libp2p/[email protected]/multiplex.go:191 +0x145
created by github.com/libp2p/go-mplex.NewMultiplex
	/home/runner/go/pkg/mod/github.com/libp2p/[email protected]/multiplex.go:106 +0x3e7

goroutine 723 [chan receive]:
github.com/libp2p/go-libp2p-transport-upgrader.(*listener).Accept(0xc00043f2c0, 0x9d0820, 0xc00043f320, 0x10, 0x0)
	/home/runner/work/go-libp2p-transport-upgrader/go-libp2p-transport-upgrader/listener.go:149 +0x75
github.com/libp2p/go-libp2p-transport-upgrader_test.TestAcceptQueueBacklogged(0xc000435980)
	/home/runner/work/go-libp2p-transport-upgrader/go-libp2p-transport-upgrader/listener_test.go:307 +0x427
testing.tRunner(0xc000435980, 0xb947b0)
	/opt/hostedtoolcache/go/1.16.7/x64/src/testing/testing.go:1193 +0x203
created by testing.(*T).Run
	/opt/hostedtoolcache/go/1.16.7/x64/src/testing/testing.go:1238 +0x5d8

goroutine 724 [IO wait]:
internal/poll.runtime_pollWait(0x7f6a496d2cb8, 0x72, 0x72)
	/opt/hostedtoolcache/go/1.16.7/x64/src/runtime/netpoll.go:222 +0x55
internal/poll.(*pollDesc).wait(0xc00040ba98, 0x72, 0x0, 0x0, 0xa564c3)
	/opt/hostedtoolcache/go/1.16.7/x64/src/internal/poll/fd_poll_runtime.go:87 +0xe6
internal/poll.(*pollDesc).waitRead(...)
	/opt/hostedtoolcache/go/1.16.7/x64/src/internal/poll/fd_poll_runtime.go:92
internal/poll.(*FD).Accept(0xc00040ba80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0)
	/opt/hostedtoolcache/go/1.16.7/x64/src/internal/poll/fd_unix.go:401 +0x32f
net.(*netFD).accept(0xc00040ba80, 0x418710, 0x0, 0x0)
	/opt/hostedtoolcache/go/1.16.7/x64/src/net/fd_unix.go:172 +0x58
net.(*TCPListener).accept(0xc00027f350, 0xc0a120, 0xc00044c5d0, 0xe859e0)
	/opt/hostedtoolcache/go/1.16.7/x64/src/net/tcpsock_posix.go:139 +0x50
net.(*TCPListener).Accept(0xc00027f350, 0x43a6cf, 0xc0002aca87, 0x0, 0xc0001ccd50)
	/opt/hostedtoolcache/go/1.16.7/x64/src/net/tcpsock.go:261 +0x8e
github.com/multiformats/go-multiaddr/net.(*maListener).Accept(0xc0003462c0, 0x0, 0x0, 0xc00043f2c0, 0xc10970)
	/home/runner/go/pkg/mod/github.com/multiformats/[email protected]/net/net.go:243 +0x71
github.com/libp2p/go-libp2p-transport-upgrader.(*listener).handleIncoming(0xc00043f2c0)
	/home/runner/work/go-libp2p-transport-upgrader/go-libp2p-transport-upgrader/listener.go:71 +0x158
created by github.com/libp2p/go-libp2p-transport-upgrader.(*Upgrader).UpgradeListener
	/home/runner/work/go-libp2p-transport-upgrader/go-libp2p-transport-upgrader/upgrader.go:48 +0x36b
FAIL	github.com/libp2p/go-libp2p-transport-upgrader	600.029s
FAIL

Circular dependency

This package now depends on the tcp transport which depends on this package (for testing). We shouldn't do that.

flaky TestConnectionsClosedIfNotAccepted test

=== RUN   TestConnectionsClosedIfNotAccepted
    listener_test.go:124: connection closed earlier than expected. expected nothing on channel, got: <nil>
--- FAIL: TestConnectionsClosedIfNotAccepted (0.20s)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.