README.md 7.18 KB
Newer Older
tavit ohanian's avatar
tavit ohanian committed
1 2
# go-libp2p-transport-upgrader

Yusef Napora's avatar
Yusef Napora committed
3 4 5
[![](https://img.shields.io/badge/made%20by-Protocol%20Labs-blue.svg?style=flat-square)](https://protocol.ai)
[![](https://img.shields.io/badge/project-libp2p-yellow.svg?style=flat-square)](https://libp2p.io/)
[![](https://img.shields.io/badge/freenode-%23libp2p-yellow.svg?style=flat-square)](http://webchat.freenode.net/?channels=%23libp2p)
Steven Allen's avatar
Steven Allen committed
6 7
[![GoDoc](https://godoc.org/github.com/libp2p/go-libp2p-transport-upgrader?status.svg)](https://godoc.org/github.com/libp2p/go-libp2p-transport-upgrader)
[![Build Status](https://travis-ci.org/libp2p/go-libp2p-transport-upgrader.svg?branch=master)](https://travis-ci.org/libp2p/go-libp2p-transport-upgrader)
Yusef Napora's avatar
Yusef Napora committed
8
[![Discourse posts](https://img.shields.io/discourse/https/discuss.libp2p.io/posts.svg)](https://discuss.libp2p.io)
Steven Allen's avatar
Steven Allen committed
9

10
> Add encryption and multiplexing capabilities to libp2p transport connections
Steven Allen's avatar
Steven Allen committed
11

12 13
This package is a component of [libp2p](https://libp2p.io), a modular networking
stack for building peer-to-peer applications.
Steven Allen's avatar
Steven Allen committed
14

15 16 17 18
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.
Steven Allen's avatar
Steven Allen committed
19

20 21 22 23 24
Many of the underlying [transport protocols][docs-transport] 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.
Steven Allen's avatar
Steven Allen committed
25

26 27 28 29 30
In order to be upgraded, the underlying connection or listener must be a
[`multiaddr-net`][manet] [`Conn`][manet-conn] or [`Listener`][manet-listener].
The `multiaddr-net` types integrate the Go standard library connection types
with [`multiaddr`][multiaddr], an extensible addressing format used throughout
libp2p.
Steven Allen's avatar
Steven Allen committed
31

32 33 34
As well as the mandatory capabilities of security and multiplexing, the upgrader
can optionally apply a `Protector` for [private networking][pnet], as well as an
[address filter][maddr-filter] to prevent connections to specific addresses.
Steven Allen's avatar
Steven Allen committed
35 36 37

## Install

38 39 40 41 42 43 44 45
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`][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:
Steven Allen's avatar
Steven Allen committed
46 47

```sh
Steven Allen's avatar
Steven Allen committed
48
go get github.com/libp2p/go-libp2p-transport-upgrader
Steven Allen's avatar
Steven Allen committed
49 50
```

51 52 53 54
This repo is [gomod](https://github.com/golang/go/wiki/Modules)-compatible, and users of
go 1.11 and later with modules enabled will automatically pull the latest tagged release
by referencing this package. Upgrades to future releases can be managed using `go get`,
or by editing your `go.mod` file as [described by the gomod documentation](https://github.com/golang/go/wiki/Modules#how-to-upgrade-and-downgrade-dependencies).
Steven Allen's avatar
Steven Allen committed
55 56 57

## Usage

58 59 60 61 62 63 64 65 66 67 68 69 70 71
To use, construct a new `Upgrader` with:

* An optional [pnet][pnet] `Protector`.
* An optional [go-maddr-filter][maddr-filter] address `Filter`.
* A mandatory [stream security transport][ss].
* A mandatory [stream multiplexer][smux].

In practice, most users will not need to construct an `Upgrader` directly.
Instead, when constructing a libp2p [`Host`][godoc-host], you can pass in some
combination of the [`PrivateNetwork`][godoc-pnet-option],
[`Filters`][godoc-filters-option], [`Security`][godoc-security-option], and
[`Muxer`][godoc-muxer-option] `Option`s. This will configure the `Upgrader` that
is created and used by the `Host` internally.

72 73
## Example

74 75 76
Below is a simplified TCP transport implementation using the transport upgrader.
In practice, you'll want to use
[go-tcp-transport](https://github.com/libp2p/go-tcp-transport), which is
tavit ohanian's avatar
tavit ohanian committed
77
optimized for production usage.
78 79 80 81 82 83 84 85 86 87

```go
package tcptransport

import (
	"context"

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

	ma "github.com/multiformats/go-multiaddr"
88
	mafmt "github.com/multiformats/go-multiaddr-fmt"
89
	manet "github.com/multiformats/go-multiaddr/net"
90 91
	tpt "github.com/libp2p/go-libp2p-core/transport"
	peer "github.com/libp2p/go-libp2p-core/peer"
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
)

// 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.
115 116 117
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)
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
	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
}
142

143
```
Steven Allen's avatar
Steven Allen committed
144 145 146

## Contribute

Steven Allen's avatar
Steven Allen committed
147
Feel free to join in. All welcome. Open an [issue](https://github.com/libp2p/go-libp2p-transport-upgrader/issues)!
Steven Allen's avatar
Steven Allen committed
148

149
This repository falls under the libp2p [Code of Conduct](https://github.com/libp2p/community/blob/master/code-of-conduct.md).
Steven Allen's avatar
Steven Allen committed
150

151
### Want to hack on libp2p?
Steven Allen's avatar
Steven Allen committed
152

153
[![](https://cdn.rawgit.com/libp2p/community/master/img/contribute.gif)](https://github.com/libp2p/community/blob/master/CONTRIBUTE.md)
Steven Allen's avatar
Steven Allen committed
154 155 156 157

## License

MIT
158 159 160 161

---

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

[tpt]: https://godoc.org/github.com/libp2p/go-libp2p-core/transport
164
[manet]: https://github.com/multiformats/go-multiaddr/
165 166 167
[ss]: https://godoc.org/github.com/libp2p/go-libp2p-core/sec
[smux]: https://godoc.org/github.com/libp2p/go-libp2p-core/mux
[pnet]: https://godoc.org/github.com/libp2p/go-libp2p-core/pnet
168 169 170
[manet-conn]: https://godoc.org/github.com/multiformats/go-multiaddr/net#Conn
[manet-listener]: https://godoc.org/github.com/multiformats/go-multiaddr/net#Listener
[maddr-filter]: https://github.com/multiformats/go-multiaddr
171 172 173 174 175 176 177 178
[docs-transport]: https://docs.libp2p.io/concepts/transport
[multiaddr]: https://github.com/multiformats/multiaddr
[go-libp2p]: https://github.com/lib2p2/go-libp2p
[godoc-host]: https://godoc.org/github.com/libp2p/go-libp2p-core/host#Host
[godoc-pnet-option]: https://godoc.org/github.com/libp2p/go-libp2p#PrivateNetwork
[godoc-filters-option]: https://godoc.org/github.com/libp2p/go-libp2p#Filters
[godoc-security-option]: https://godoc.org/github.com/libp2p/go-libp2p#Security
[godoc-muxer-option]: https://godoc.org/github.com/libp2p/go-libp2p#Muxer