Unverified Commit aedbe954 authored by Abhishek Upperwal's avatar Abhishek Upperwal Committed by GitHub
Browse files

Merge pull request #1 from libp2p/master

Update
parents d91fc254 3b8f2275
5.0.15: QmXGfPjhnro8tgANHDUg4gGgLGYnAz1zcDPAgNeUkzbsN1 5.0.17: QmWsV6kzPaYGBDVyuUfWBvyQygEc9Qrv9vzo8vZ7X4mdLN
...@@ -242,9 +242,7 @@ func addAddrToPeerstore(h host.Host, addr string) peer.ID { ...@@ -242,9 +242,7 @@ func addAddrToPeerstore(h host.Host, addr string) peer.ID {
return peerid return peerid
} }
func main() { const help = `
flag.Usage = func() {
fmt.Println(`
This example creates a simple HTTP Proxy using two libp2p peers. The first peer 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 provides an HTTP server locally which tunnels the HTTP requests with libp2p
to a remote peer. The remote peer performs the requests and to a remote peer. The remote peer performs the requests and
...@@ -256,7 +254,11 @@ Usage: Start remote peer first with: ./proxy ...@@ -256,7 +254,11 @@ Usage: Start remote peer first with: ./proxy
Then you can do something like: curl -x "localhost:9900" "http://ipfs.io". 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 This proxies sends the request through the local peer, which proxies it to
the remote peer, which makes it and sends the response back. the remote peer, which makes it and sends the response back.
`) `
func main() {
flag.Usage = func() {
fmt.Println(help)
flag.PrintDefaults() flag.PrintDefaults()
} }
......
...@@ -9,7 +9,7 @@ import ( ...@@ -9,7 +9,7 @@ import (
inet "github.com/libp2p/go-libp2p-net" inet "github.com/libp2p/go-libp2p-net"
"github.com/libp2p/go-libp2p-host" "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" protobufCodec "github.com/multiformats/go-multicodec/protobuf"
uuid "github.com/satori/go.uuid" uuid "github.com/satori/go.uuid"
) )
...@@ -19,13 +19,13 @@ const echoRequest = "/echo/echoreq/0.0.1" ...@@ -19,13 +19,13 @@ const echoRequest = "/echo/echoreq/0.0.1"
const echoResponse = "/echo/echoresp/0.0.1" const echoResponse = "/echo/echoresp/0.0.1"
type EchoProtocol struct { type EchoProtocol struct {
node *Node // local host node *Node // local host
requests map[string]*p2p.EchoRequest // used to access request data from response handlers 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 done chan bool // only for demo purposes to hold main from terminating
} }
func NewEchoProtocol(node *Node, done chan bool) *EchoProtocol { 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(echoRequest, e.onEchoRequest)
node.SetStreamHandler(echoResponse, e.onEchoResponse) node.SetStreamHandler(echoResponse, e.onEchoResponse)
...@@ -38,7 +38,7 @@ func NewEchoProtocol(node *Node, done chan bool) *EchoProtocol { ...@@ -38,7 +38,7 @@ func NewEchoProtocol(node *Node, done chan bool) *EchoProtocol {
// remote peer requests handler // remote peer requests handler
func (e *EchoProtocol) onEchoRequest(s inet.Stream) { func (e *EchoProtocol) onEchoRequest(s inet.Stream) {
// get request data // get request data
data := &p2p.EchoRequest{} data := &pb.EchoRequest{}
decoder := protobufCodec.Multicodec(nil).Decoder(bufio.NewReader(s)) decoder := protobufCodec.Multicodec(nil).Decoder(bufio.NewReader(s))
err := decoder.Decode(data) err := decoder.Decode(data)
if err != nil { if err != nil {
...@@ -59,7 +59,7 @@ func (e *EchoProtocol) onEchoRequest(s inet.Stream) { ...@@ -59,7 +59,7 @@ func (e *EchoProtocol) onEchoRequest(s inet.Stream) {
// send response to the request using the message string he provided // 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), MessageData: e.node.NewMessageData(data.MessageData.Id, false),
Message: data.Message} Message: data.Message}
...@@ -88,7 +88,7 @@ func (e *EchoProtocol) onEchoRequest(s inet.Stream) { ...@@ -88,7 +88,7 @@ func (e *EchoProtocol) onEchoRequest(s inet.Stream) {
// remote echo response handler // remote echo response handler
func (e *EchoProtocol) onEchoResponse(s inet.Stream) { func (e *EchoProtocol) onEchoResponse(s inet.Stream) {
data := &p2p.EchoResponse{} data := &pb.EchoResponse{}
decoder := protobufCodec.Multicodec(nil).Decoder(bufio.NewReader(s)) decoder := protobufCodec.Multicodec(nil).Decoder(bufio.NewReader(s))
err := decoder.Decode(data) err := decoder.Decode(data)
if err != nil { if err != nil {
...@@ -125,7 +125,7 @@ func (e *EchoProtocol) Echo(host host.Host) bool { ...@@ -125,7 +125,7 @@ func (e *EchoProtocol) Echo(host host.Host) bool {
log.Printf("%s: Sending echo to: %s....", e.node.ID(), host.ID()) log.Printf("%s: Sending echo to: %s....", e.node.ID(), host.ID())
// create message data // create message data
req := &p2p.EchoRequest{ req := &pb.EchoRequest{
MessageData: e.node.NewMessageData(uuid.Must(uuid.NewV4()).String(), false), MessageData: e.node.NewMessageData(uuid.Must(uuid.NewV4()).String(), false),
Message: fmt.Sprintf("Echo from %s", e.node.ID())} Message: fmt.Sprintf("Echo from %s", e.node.ID())}
......
...@@ -31,6 +31,7 @@ type Config struct { ...@@ -31,6 +31,7 @@ type Config struct {
Protector pnet.Protector Protector pnet.Protector
Reporter metrics.Reporter Reporter metrics.Reporter
DisableSecio bool DisableSecio bool
EnableNAT bool
} }
type Option func(cfg *Config) error type Option func(cfg *Config) error
...@@ -92,6 +93,13 @@ func NoEncryption() Option { ...@@ -92,6 +93,13 @@ func NoEncryption() Option {
return TransportEncryption(EncPlaintext) return TransportEncryption(EncPlaintext)
} }
func NATPortMap() Option {
return func(cfg *Config) error {
cfg.EnableNAT = true
return nil
}
}
func Muxer(m mux.Transport) Option { func Muxer(m mux.Transport) Option {
return func(cfg *Config) error { return func(cfg *Config) error {
if cfg.Muxer != nil { if cfg.Muxer != nil {
...@@ -199,7 +207,13 @@ func newWithCfg(ctx context.Context, cfg *Config) (host.Host, error) { ...@@ -199,7 +207,13 @@ func newWithCfg(ctx context.Context, cfg *Config) (host.Host, error) {
netw := (*swarm.Network)(swrm) 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 { func DefaultMuxer() mux.Transport {
......
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...)
}
...@@ -250,7 +250,7 @@ func (h *BasicHost) newStreamHandler(s inet.Stream) { ...@@ -250,7 +250,7 @@ func (h *BasicHost) newStreamHandler(s inet.Stream) {
} }
logf("protocol EOF: %s (took %s)", s.Conn().RemotePeer(), took) logf("protocol EOF: %s (took %s)", s.Conn().RemotePeer(), took)
} else { } else {
log.Warningf("protocol mux failed: %s (took %s)", err, took) log.Infof("protocol mux failed: %s (took %s)", err, took)
} }
s.Reset() s.Reset()
return return
......
...@@ -49,9 +49,9 @@ ...@@ -49,9 +49,9 @@
"version": "1.0.0" "version": "1.0.0"
}, },
{ {
"hash": "QmRb5jh8z2E8hMGN2tkvs1yHynUanqnZ3UeKwgN1i9P1F8", "hash": "QmTG23dvpBCBjqQwyDxV8CQT6jmS4PSftNr1VqHhE3MLy7",
"name": "go-log", "name": "go-log",
"version": "1.4.0" "version": "1.4.1"
}, },
{ {
"hash": "QmRK2LxanhK2gZq6k6R7vk5ZoYZk8ULSSTB7FzDsMUX6CB", "hash": "QmRK2LxanhK2gZq6k6R7vk5ZoYZk8ULSSTB7FzDsMUX6CB",
...@@ -104,9 +104,9 @@ ...@@ -104,9 +104,9 @@
"version": "0.0.0" "version": "0.0.0"
}, },
{ {
"hash": "QmWp2mA7eab53PS4NdyW4uvBf73ZB7wSB7eN64K5pS1AKg", "hash": "QmVXXxPsnDY16szK4gPy1oz4qKd8HHshemX1miZR2frtJo",
"name": "go-peerstream", "name": "go-peerstream",
"version": "2.1.4" "version": "2.1.5"
}, },
{ {
"author": "whyrusleeping", "author": "whyrusleeping",
...@@ -122,33 +122,33 @@ ...@@ -122,33 +122,33 @@
}, },
{ {
"author": "whyrusleeping", "author": "whyrusleeping",
"hash": "Qmf9JgVLz46pxPXwG2eWSJpkqVCcjD4rp7zCRi2KP6GTNB", "hash": "QmPDZJxtWGfcwLPazJxD4h3v3aDs43V7UNAVs3Jz1Wo7o4",
"name": "go-libp2p-loggables", "name": "go-libp2p-loggables",
"version": "1.1.13" "version": "1.1.14"
}, },
{ {
"author": "whyrusleeping", "author": "whyrusleeping",
"hash": "QmT8TkDNBDyBsnZ4JJ2ecHU7qN184jkw1tY8y4chFfeWsy", "hash": "QmP47neqyP4NR9CKbjVogZ8U9Gybxfcfsa8HtPSPSxwiA8",
"name": "go-libp2p-secio", "name": "go-libp2p-secio",
"version": "1.2.6" "version": "1.2.7"
}, },
{ {
"author": "whyrusleeping", "author": "whyrusleeping",
"hash": "QmXauCuJzmzapetmC6W4TuDJLL1yFFrVzSHoWv8YdbmnxH", "hash": "QmdeiKhUy1TVGBaKxt7y1QmBDLBdisSrLJ1x58Eoj4PXUh",
"name": "go-libp2p-peerstore", "name": "go-libp2p-peerstore",
"version": "1.4.15" "version": "1.4.17"
}, },
{ {
"author": "whyrusleeping", "author": "whyrusleeping",
"hash": "QmVxtCwKFMmwcjhQXsGj6m4JAW7nGb9hRoErH9jpgqcLxA", "hash": "QmPUHzTLPZFYqv8WqcBTuMFYTgeom4uHHEaxzk7bd5GYZB",
"name": "go-libp2p-transport", "name": "go-libp2p-transport",
"version": "2.2.13" "version": "2.2.14"
}, },
{ {
"author": "whyrusleeping", "author": "whyrusleeping",
"hash": "QmRGZvAy3LRSjj4H2riZ1XJogFYfz3YZLp4Q59nU8MmYKx", "hash": "QmdxKHpkZCTV3C7xdE1iJdPfFm5LVvMPvirdFmKu1TimzY",
"name": "go-tcp-transport", "name": "go-tcp-transport",
"version": "1.2.8" "version": "1.2.9"
}, },
{ {
"author": "whyrusleeping", "author": "whyrusleeping",
...@@ -164,75 +164,75 @@ ...@@ -164,75 +164,75 @@
}, },
{ {
"author": "whyrusleeping", "author": "whyrusleeping",
"hash": "QmNSWW3Sb4eju4o2djPQ1L1c2Zj9XN9sMYJL8r1cbxdc6b", "hash": "QmTGSre9j1otFgsr1opCUQDXTPSM6BTZnMWwPeA5nYJM7w",
"name": "go-addr-util", "name": "go-addr-util",
"version": "1.2.6" "version": "1.2.7"
}, },
{ {
"author": "whyrusleeping", "author": "whyrusleeping",
"hash": "QmVvkK7s5imCiq3JVbL3pGfnhcCnf3LrFJPF4GE2sAoGZf", "hash": "QmUJzxQQ2kzwQubsMqBTr1NGDpLfh7pGA2E1oaJULcKDPq",
"name": "go-testutil", "name": "go-testutil",
"version": "1.1.15" "version": "1.2.1"
}, },
{ {
"author": "whyrusleeping", "author": "whyrusleeping",
"hash": "QmRvZscvtJhcRJhKPrRqoR76pmsQ8MnCqUjk3FNpm1D8Wa", "hash": "QmaHsbK8b39AzQWEwDsysCutdJXyfa3k9oFh1cr6dfMhHT",
"name": "go-libp2p-conn", "name": "go-libp2p-conn",
"version": "1.7.5" "version": "1.7.6"
}, },
{ {
"author": "whyrusleeping", "author": "whyrusleeping",
"hash": "QmXfkENeeBvh3zYA51MaSdGUdBjhQ99cP5WQe8zgr6wchG", "hash": "QmXoz9o2PT3tEzf7hicegwex5UgVP54n3k82K7jrWFyN86",
"name": "go-libp2p-net", "name": "go-libp2p-net",
"version": "2.0.5" "version": "2.0.7"
}, },
{ {
"author": "whyrusleeping", "author": "whyrusleeping",
"hash": "QmdeBtQGXjSt7cb97nx9JyLHHv5va2LyEAue7Q5tDFzpLy", "hash": "QmVvu4bS5QLfS19ePkp5Wgzn2ZUma5oXTT9BgDFyQLxUZF",
"name": "go-libp2p-metrics", "name": "go-libp2p-metrics",
"version": "2.0.4" "version": "2.0.6"
}, },
{ {
"author": "whyrusleeping", "author": "whyrusleeping",
"hash": "QmToCvh5eJtoDheMggre7b2zeFCJ6tAyB82YVs457cqoUE", "hash": "QmYDNqBAMWVMHKndYR35Sd8PfEVWBiDmpHYkuRJTunJDeJ",
"name": "go-libp2p-interface-conn", "name": "go-libp2p-interface-conn",
"version": "0.4.12" "version": "0.4.13"
}, },
{ {
"author": "whyrusleeping", "author": "whyrusleeping",
"hash": "QmNmJZL7FQySMtE2BQuLMuZg2EB2CLEunJJUSVSc9YnnbV", "hash": "QmfZTdmunzKzAGJrSvXXQbQ5kLLUiEMX5vdwux7iXkdk7D",
"name": "go-libp2p-host", "name": "go-libp2p-host",
"version": "2.1.5" "version": "2.1.7"
}, },
{ {
"author": "whyrusleeping", "author": "whyrusleeping",
"hash": "QmSwZMWwFZSUpe5muU2xgTUwppH24KfMwdPXiwbEp2c6G5", "hash": "QmRqfgh56f8CrqpwH7D2s6t8zQRsvPoftT3sp5Y6SUhNA3",
"name": "go-libp2p-swarm", "name": "go-libp2p-swarm",
"version": "2.1.5" "version": "2.1.7"
}, },
{ {
"author": "whyrusleeping", "author": "whyrusleeping",
"hash": "QmesX7qFSxES4ebn9pFpJsjzu8NN6vcKVH3wkypmbGmkqG", "hash": "QmXtFH52dAPCq5i4iYjr1g8xVFVJD3fwKWWyNHjVB4sHRp",
"name": "go-libp2p-nat", "name": "go-libp2p-nat",
"version": "0.0.7" "version": "0.0.8"
}, },
{ {
"author": "whyrusleeping", "author": "whyrusleeping",
"hash": "QmYVR3C8DWPHdHxvLtNFYfjsXgaRAdh6hPMNH3KiwCgu4o", "hash": "Qmb6BsZf6Y3kxffXMNTubGPF1w1bkHtpvhfYbmnwP3NQyw",
"name": "go-libp2p-netutil", "name": "go-libp2p-netutil",
"version": "0.3.9" "version": "0.3.11"
}, },
{ {
"author": "whyrusleeping", "author": "whyrusleeping",
"hash": "QmQr1j6UvdhpponAaqSdswqRpdzsFwNop2N8kXLNw8afem", "hash": "Qmc64U41EEB4nPG7wxjEqFwKJajS2f8kk5q2TvUrQf78Xu",
"name": "go-libp2p-blankhost", "name": "go-libp2p-blankhost",
"version": "0.2.5" "version": "0.2.7"
}, },
{ {
"author": "whyrusleeping", "author": "whyrusleeping",
"hash": "QmaPbCnUMBohSGo3KnxEa2bHqyJVVeEEcwtqJAYxerieBo", "hash": "Qme1knMqwt1hKZbc1BmQFmnm9f36nyQGwXxPGVpVJ9rMK5",
"name": "go-libp2p-crypto", "name": "go-libp2p-crypto",
"version": "1.5.0" "version": "1.6.2"
}, },
{ {
"author": "whyrusleeping", "author": "whyrusleeping",
...@@ -254,15 +254,15 @@ ...@@ -254,15 +254,15 @@
}, },
{ {
"author": "whyrusleeping", "author": "whyrusleeping",
"hash": "QmZoWKhxUmZ2seW4BzX6fJkNR8hh9PsGModr7q171yq2SS", "hash": "QmcJukH2sAFjY3HdBKq35WDzWoL3UUu2gt9wdfqZTUyM74",
"name": "go-libp2p-peer", "name": "go-libp2p-peer",
"version": "2.2.3" "version": "2.3.2"
}, },
{ {
"author": "vyzo", "author": "vyzo",
"hash": "QmVTnHzuyECV9JzbXXfZRj1pKtgknp1esamUb2EH33mJkA", "hash": "QmZRbCo2gw7ghw5m7L77a8FvvQTVr62J4hmy8ozpdq7dHF",
"name": "go-libp2p-circuit", "name": "go-libp2p-circuit",
"version": "2.0.10" "version": "2.0.12"
}, },
{ {
"author": "lgierth", "author": "lgierth",
...@@ -272,15 +272,15 @@ ...@@ -272,15 +272,15 @@
}, },
{ {
"author": "why", "author": "why",
"hash": "Qmax8X1Kfahf5WfSB68EWDG3d3qyS3Sqs1v412fjPTfRwx", "hash": "QmfQNieWBPwmnUjXWPZbjJPzhNwFFabTb5RQ79dyVWGujQ",
"name": "go-libp2p-interface-connmgr", "name": "go-libp2p-interface-connmgr",
"version": "0.0.6" "version": "0.0.8"
}, },
{ {
"author": "whyrusleeping", "author": "whyrusleeping",
"hash": "Qmc14vuKyGqX27RvBhekYytxSFJpaEgQVuVJgKSm69MEix", "hash": "QmenmFuirGzv8S1R3DyvbZ6tFmQapkGeDCebgYzni1Ntn3",
"name": "go-smux-multiplex", "name": "go-smux-multiplex",
"version": "3.0.5" "version": "3.0.6"
}, },
{ {
"author": "multiformats", "author": "multiformats",
...@@ -300,6 +300,6 @@ ...@@ -300,6 +300,6 @@
"license": "MIT", "license": "MIT",
"name": "go-libp2p", "name": "go-libp2p",
"releaseCmd": "git commit -a -m \"gx publish $VERSION\"", "releaseCmd": "git commit -a -m \"gx publish $VERSION\"",
"version": "5.0.15" "version": "5.0.17"
} }
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment