Skip to content

Commit

Permalink
Merge pull request #1 from libp2p/master
Browse files Browse the repository at this point in the history
Update
  • Loading branch information
upperwal committed Apr 23, 2018
2 parents d91fc25 + 3b8f227 commit aedbe95
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 63 deletions.
2 changes: 1 addition & 1 deletion .gx/lastpubver
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5.0.15: QmXGfPjhnro8tgANHDUg4gGgLGYnAz1zcDPAgNeUkzbsN1
5.0.17: QmWsV6kzPaYGBDVyuUfWBvyQygEc9Qrv9vzo8vZ7X4mdLN
1 change: 1 addition & 0 deletions ci/Jenkinsfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
golang()
10 changes: 6 additions & 4 deletions examples/http-proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,9 +242,7 @@ func addAddrToPeerstore(h host.Host, addr string) peer.ID {
return peerid
}

func main() {
flag.Usage = func() {
fmt.Println(`
const help = `
This example creates a simple HTTP Proxy using two libp2p peers. The first peer
provides an HTTP server locally which tunnels the HTTP requests with libp2p
to a remote peer. The remote peer performs the requests and
Expand All @@ -256,7 +254,11 @@ Usage: Start remote peer first with: ./proxy
Then you can do something like: curl -x "localhost:9900" "http://ipfs.io".
This proxies sends the request through the local peer, which proxies it to
the remote peer, which makes it and sends the response back.
`)
`

func main() {
flag.Usage = func() {
fmt.Println(help)
flag.PrintDefaults()
}

Expand Down
18 changes: 9 additions & 9 deletions examples/multipro/echo.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
inet "github.com/libp2p/go-libp2p-net"

"github.com/libp2p/go-libp2p-host"
p2p "github.com/libp2p/go-libp2p/examples/multipro/pb"
pb "github.com/libp2p/go-libp2p/examples/multipro/pb"
protobufCodec "github.com/multiformats/go-multicodec/protobuf"
uuid "github.com/satori/go.uuid"
)
Expand All @@ -19,13 +19,13 @@ const echoRequest = "/echo/echoreq/0.0.1"
const echoResponse = "/echo/echoresp/0.0.1"

type EchoProtocol struct {
node *Node // local host
requests map[string]*p2p.EchoRequest // used to access request data from response handlers
done chan bool // only for demo purposes to hold main from terminating
node *Node // local host
requests map[string]*pb.EchoRequest // used to access request data from response handlers
done chan bool // only for demo purposes to hold main from terminating
}

func NewEchoProtocol(node *Node, done chan bool) *EchoProtocol {
e := EchoProtocol{node: node, requests: make(map[string]*p2p.EchoRequest), done: done}
e := EchoProtocol{node: node, requests: make(map[string]*pb.EchoRequest), done: done}
node.SetStreamHandler(echoRequest, e.onEchoRequest)
node.SetStreamHandler(echoResponse, e.onEchoResponse)

Expand All @@ -38,7 +38,7 @@ func NewEchoProtocol(node *Node, done chan bool) *EchoProtocol {
// remote peer requests handler
func (e *EchoProtocol) onEchoRequest(s inet.Stream) {
// get request data
data := &p2p.EchoRequest{}
data := &pb.EchoRequest{}
decoder := protobufCodec.Multicodec(nil).Decoder(bufio.NewReader(s))
err := decoder.Decode(data)
if err != nil {
Expand All @@ -59,7 +59,7 @@ func (e *EchoProtocol) onEchoRequest(s inet.Stream) {

// send response to the request using the message string he provided

resp := &p2p.EchoResponse{
resp := &pb.EchoResponse{
MessageData: e.node.NewMessageData(data.MessageData.Id, false),
Message: data.Message}

Expand Down Expand Up @@ -88,7 +88,7 @@ func (e *EchoProtocol) onEchoRequest(s inet.Stream) {

// remote echo response handler
func (e *EchoProtocol) onEchoResponse(s inet.Stream) {
data := &p2p.EchoResponse{}
data := &pb.EchoResponse{}
decoder := protobufCodec.Multicodec(nil).Decoder(bufio.NewReader(s))
err := decoder.Decode(data)
if err != nil {
Expand Down Expand Up @@ -125,7 +125,7 @@ func (e *EchoProtocol) Echo(host host.Host) bool {
log.Printf("%s: Sending echo to: %s....", e.node.ID(), host.ID())

// create message data
req := &p2p.EchoRequest{
req := &pb.EchoRequest{
MessageData: e.node.NewMessageData(uuid.Must(uuid.NewV4()).String(), false),
Message: fmt.Sprintf("Echo from %s", e.node.ID())}

Expand Down
16 changes: 15 additions & 1 deletion libp2p.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type Config struct {
Protector pnet.Protector
Reporter metrics.Reporter
DisableSecio bool
EnableNAT bool
}

type Option func(cfg *Config) error
Expand Down Expand Up @@ -92,6 +93,13 @@ func NoEncryption() Option {
return TransportEncryption(EncPlaintext)
}

func NATPortMap() Option {
return func(cfg *Config) error {
cfg.EnableNAT = true
return nil
}
}

func Muxer(m mux.Transport) Option {
return func(cfg *Config) error {
if cfg.Muxer != nil {
Expand Down Expand Up @@ -199,7 +207,13 @@ func newWithCfg(ctx context.Context, cfg *Config) (host.Host, error) {

netw := (*swarm.Network)(swrm)

return bhost.New(netw), nil
hostOpts := &bhost.HostOpts{}

if cfg.EnableNAT {
hostOpts.NATManager = bhost.NewNATManager(netw)
}

return bhost.NewHost(ctx, netw, hostOpts)
}

func DefaultMuxer() mux.Transport {
Expand Down
34 changes: 34 additions & 0 deletions libp2p_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package libp2p

import (
"context"
"fmt"
"testing"

crypto "github.com/libp2p/go-libp2p-crypto"
host "github.com/libp2p/go-libp2p-host"
)

func TestNewHost(t *testing.T) {
_, err := makeRandomHost(t, 9000)
if err != nil {
t.Fatal(err)
}
}

func makeRandomHost(t *testing.T, port int) (host.Host, error) {
ctx := context.Background()
priv, _, err := crypto.GenerateKeyPair(crypto.RSA, 2048)
if err != nil {
t.Fatal(err)
}

opts := []Option{
ListenAddrStrings(fmt.Sprintf("/ip4/127.0.0.1/tcp/%d", port)),
Identity(priv),
Muxer(DefaultMuxer()),
NATPortMap(),
}

return New(ctx, opts...)
}
2 changes: 1 addition & 1 deletion p2p/host/basic/basic_host.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ func (h *BasicHost) newStreamHandler(s inet.Stream) {
}
logf("protocol EOF: %s (took %s)", s.Conn().RemotePeer(), took)
} else {
log.Warningf("protocol mux failed: %s (took %s)", err, took)
log.Infof("protocol mux failed: %s (took %s)", err, took)
}
s.Reset()
return
Expand Down
94 changes: 47 additions & 47 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@
"version": "1.0.0"
},
{
"hash": "QmRb5jh8z2E8hMGN2tkvs1yHynUanqnZ3UeKwgN1i9P1F8",
"hash": "QmTG23dvpBCBjqQwyDxV8CQT6jmS4PSftNr1VqHhE3MLy7",
"name": "go-log",
"version": "1.4.0"
"version": "1.4.1"
},
{
"hash": "QmRK2LxanhK2gZq6k6R7vk5ZoYZk8ULSSTB7FzDsMUX6CB",
Expand Down Expand Up @@ -104,9 +104,9 @@
"version": "0.0.0"
},
{
"hash": "QmWp2mA7eab53PS4NdyW4uvBf73ZB7wSB7eN64K5pS1AKg",
"hash": "QmVXXxPsnDY16szK4gPy1oz4qKd8HHshemX1miZR2frtJo",
"name": "go-peerstream",
"version": "2.1.4"
"version": "2.1.5"
},
{
"author": "whyrusleeping",
Expand All @@ -122,33 +122,33 @@
},
{
"author": "whyrusleeping",
"hash": "Qmf9JgVLz46pxPXwG2eWSJpkqVCcjD4rp7zCRi2KP6GTNB",
"hash": "QmPDZJxtWGfcwLPazJxD4h3v3aDs43V7UNAVs3Jz1Wo7o4",
"name": "go-libp2p-loggables",
"version": "1.1.13"
"version": "1.1.14"
},
{
"author": "whyrusleeping",
"hash": "QmT8TkDNBDyBsnZ4JJ2ecHU7qN184jkw1tY8y4chFfeWsy",
"hash": "QmP47neqyP4NR9CKbjVogZ8U9Gybxfcfsa8HtPSPSxwiA8",
"name": "go-libp2p-secio",
"version": "1.2.6"
"version": "1.2.7"
},
{
"author": "whyrusleeping",
"hash": "QmXauCuJzmzapetmC6W4TuDJLL1yFFrVzSHoWv8YdbmnxH",
"hash": "QmdeiKhUy1TVGBaKxt7y1QmBDLBdisSrLJ1x58Eoj4PXUh",
"name": "go-libp2p-peerstore",
"version": "1.4.15"
"version": "1.4.17"
},
{
"author": "whyrusleeping",
"hash": "QmVxtCwKFMmwcjhQXsGj6m4JAW7nGb9hRoErH9jpgqcLxA",
"hash": "QmPUHzTLPZFYqv8WqcBTuMFYTgeom4uHHEaxzk7bd5GYZB",
"name": "go-libp2p-transport",
"version": "2.2.13"
"version": "2.2.14"
},
{
"author": "whyrusleeping",
"hash": "QmRGZvAy3LRSjj4H2riZ1XJogFYfz3YZLp4Q59nU8MmYKx",
"hash": "QmdxKHpkZCTV3C7xdE1iJdPfFm5LVvMPvirdFmKu1TimzY",
"name": "go-tcp-transport",
"version": "1.2.8"
"version": "1.2.9"
},
{
"author": "whyrusleeping",
Expand All @@ -164,75 +164,75 @@
},
{
"author": "whyrusleeping",
"hash": "QmNSWW3Sb4eju4o2djPQ1L1c2Zj9XN9sMYJL8r1cbxdc6b",
"hash": "QmTGSre9j1otFgsr1opCUQDXTPSM6BTZnMWwPeA5nYJM7w",
"name": "go-addr-util",
"version": "1.2.6"
"version": "1.2.7"
},
{
"author": "whyrusleeping",
"hash": "QmVvkK7s5imCiq3JVbL3pGfnhcCnf3LrFJPF4GE2sAoGZf",
"hash": "QmUJzxQQ2kzwQubsMqBTr1NGDpLfh7pGA2E1oaJULcKDPq",
"name": "go-testutil",
"version": "1.1.15"
"version": "1.2.1"
},
{
"author": "whyrusleeping",
"hash": "QmRvZscvtJhcRJhKPrRqoR76pmsQ8MnCqUjk3FNpm1D8Wa",
"hash": "QmaHsbK8b39AzQWEwDsysCutdJXyfa3k9oFh1cr6dfMhHT",
"name": "go-libp2p-conn",
"version": "1.7.5"
"version": "1.7.6"
},
{
"author": "whyrusleeping",
"hash": "QmXfkENeeBvh3zYA51MaSdGUdBjhQ99cP5WQe8zgr6wchG",
"hash": "QmXoz9o2PT3tEzf7hicegwex5UgVP54n3k82K7jrWFyN86",
"name": "go-libp2p-net",
"version": "2.0.5"
"version": "2.0.7"
},
{
"author": "whyrusleeping",
"hash": "QmdeBtQGXjSt7cb97nx9JyLHHv5va2LyEAue7Q5tDFzpLy",
"hash": "QmVvu4bS5QLfS19ePkp5Wgzn2ZUma5oXTT9BgDFyQLxUZF",
"name": "go-libp2p-metrics",
"version": "2.0.4"
"version": "2.0.6"
},
{
"author": "whyrusleeping",
"hash": "QmToCvh5eJtoDheMggre7b2zeFCJ6tAyB82YVs457cqoUE",
"hash": "QmYDNqBAMWVMHKndYR35Sd8PfEVWBiDmpHYkuRJTunJDeJ",
"name": "go-libp2p-interface-conn",
"version": "0.4.12"
"version": "0.4.13"
},
{
"author": "whyrusleeping",
"hash": "QmNmJZL7FQySMtE2BQuLMuZg2EB2CLEunJJUSVSc9YnnbV",
"hash": "QmfZTdmunzKzAGJrSvXXQbQ5kLLUiEMX5vdwux7iXkdk7D",
"name": "go-libp2p-host",
"version": "2.1.5"
"version": "2.1.7"
},
{
"author": "whyrusleeping",
"hash": "QmSwZMWwFZSUpe5muU2xgTUwppH24KfMwdPXiwbEp2c6G5",
"hash": "QmRqfgh56f8CrqpwH7D2s6t8zQRsvPoftT3sp5Y6SUhNA3",
"name": "go-libp2p-swarm",
"version": "2.1.5"
"version": "2.1.7"
},
{
"author": "whyrusleeping",
"hash": "QmesX7qFSxES4ebn9pFpJsjzu8NN6vcKVH3wkypmbGmkqG",
"hash": "QmXtFH52dAPCq5i4iYjr1g8xVFVJD3fwKWWyNHjVB4sHRp",
"name": "go-libp2p-nat",
"version": "0.0.7"
"version": "0.0.8"
},
{
"author": "whyrusleeping",
"hash": "QmYVR3C8DWPHdHxvLtNFYfjsXgaRAdh6hPMNH3KiwCgu4o",
"hash": "Qmb6BsZf6Y3kxffXMNTubGPF1w1bkHtpvhfYbmnwP3NQyw",
"name": "go-libp2p-netutil",
"version": "0.3.9"
"version": "0.3.11"
},
{
"author": "whyrusleeping",
"hash": "QmQr1j6UvdhpponAaqSdswqRpdzsFwNop2N8kXLNw8afem",
"hash": "Qmc64U41EEB4nPG7wxjEqFwKJajS2f8kk5q2TvUrQf78Xu",
"name": "go-libp2p-blankhost",
"version": "0.2.5"
"version": "0.2.7"
},
{
"author": "whyrusleeping",
"hash": "QmaPbCnUMBohSGo3KnxEa2bHqyJVVeEEcwtqJAYxerieBo",
"hash": "Qme1knMqwt1hKZbc1BmQFmnm9f36nyQGwXxPGVpVJ9rMK5",
"name": "go-libp2p-crypto",
"version": "1.5.0"
"version": "1.6.2"
},
{
"author": "whyrusleeping",
Expand All @@ -254,15 +254,15 @@
},
{
"author": "whyrusleeping",
"hash": "QmZoWKhxUmZ2seW4BzX6fJkNR8hh9PsGModr7q171yq2SS",
"hash": "QmcJukH2sAFjY3HdBKq35WDzWoL3UUu2gt9wdfqZTUyM74",
"name": "go-libp2p-peer",
"version": "2.2.3"
"version": "2.3.2"
},
{
"author": "vyzo",
"hash": "QmVTnHzuyECV9JzbXXfZRj1pKtgknp1esamUb2EH33mJkA",
"hash": "QmZRbCo2gw7ghw5m7L77a8FvvQTVr62J4hmy8ozpdq7dHF",
"name": "go-libp2p-circuit",
"version": "2.0.10"
"version": "2.0.12"
},
{
"author": "lgierth",
Expand All @@ -272,15 +272,15 @@
},
{
"author": "why",
"hash": "Qmax8X1Kfahf5WfSB68EWDG3d3qyS3Sqs1v412fjPTfRwx",
"hash": "QmfQNieWBPwmnUjXWPZbjJPzhNwFFabTb5RQ79dyVWGujQ",
"name": "go-libp2p-interface-connmgr",
"version": "0.0.6"
"version": "0.0.8"
},
{
"author": "whyrusleeping",
"hash": "Qmc14vuKyGqX27RvBhekYytxSFJpaEgQVuVJgKSm69MEix",
"hash": "QmenmFuirGzv8S1R3DyvbZ6tFmQapkGeDCebgYzni1Ntn3",
"name": "go-smux-multiplex",
"version": "3.0.5"
"version": "3.0.6"
},
{
"author": "multiformats",
Expand All @@ -300,6 +300,6 @@
"license": "MIT",
"name": "go-libp2p",
"releaseCmd": "git commit -a -m \"gx publish $VERSION\"",
"version": "5.0.15"
"version": "5.0.17"
}

0 comments on commit aedbe95

Please sign in to comment.