main.go 3.2 KB
Newer Older
Jeromy's avatar
Jeromy committed
1
2
3
package main

import (
4
	"context"
Jeromy's avatar
Jeromy committed
5
6
7
8
	"flag"
	"fmt"
	"io/ioutil"
	"log"
Jeromy's avatar
Jeromy committed
9
	"strings"
Jeromy's avatar
Jeromy committed
10

Jeromy's avatar
Jeromy committed
11
	peer "github.com/ipfs/go-libp2p-peer"
Jeromy's avatar
Jeromy committed
12
	pstore "github.com/ipfs/go-libp2p-peerstore"
13
14
	host "github.com/libp2p/go-libp2p/p2p/host"
	bhost "github.com/libp2p/go-libp2p/p2p/host/basic"
Jeromy's avatar
Jeromy committed
15
	inet "github.com/libp2p/go-libp2p/p2p/net"
16
17
18
	net "github.com/libp2p/go-libp2p/p2p/net"
	swarm "github.com/libp2p/go-libp2p/p2p/net/swarm"
	testutil "github.com/libp2p/go-libp2p/testutil"
Jeromy's avatar
Jeromy committed
19
20
21
22
23

	ma "github.com/jbenet/go-multiaddr"
)

// create a 'Host' with a random peer to listen on the given address
Jeromy's avatar
Jeromy committed
24
func makeDummyHost(listen string, secio bool) (host.Host, error) {
Jeromy's avatar
Jeromy committed
25
26
27
28
29
	addr, err := ma.NewMultiaddr(listen)
	if err != nil {
		return nil, err
	}

Jeromy's avatar
Jeromy committed
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
	ps := pstore.NewPeerstore()
	var pid peer.ID

	if secio {
		ident, err := testutil.RandIdentity()
		if err != nil {
			return nil, err
		}

		ident.PrivateKey()
		ps.AddPrivKey(ident.ID(), ident.PrivateKey())
		ps.AddPubKey(ident.ID(), ident.PublicKey())
		pid = ident.ID()
	} else {
		fakepid, err := testutil.RandPeerID()
		if err != nil {
			return nil, err
		}
		pid = fakepid
Jeromy's avatar
Jeromy committed
49
50
	}

Jeromy's avatar
Jeromy committed
51
	ctx := context.Background()
Jeromy's avatar
Jeromy committed
52
53

	// create a new swarm to be used by the service host
Jeromy's avatar
Jeromy committed
54
	netw, err := swarm.NewNetwork(ctx, []ma.Multiaddr{addr}, pid, ps, nil)
Jeromy's avatar
Jeromy committed
55
56
57
58
59
60
61
62
63
64
65
	if err != nil {
		return nil, err
	}

	log.Printf("I am %s/ipfs/%s\n", addr, pid.Pretty())
	return bhost.New(netw), nil
}

func main() {
	listenF := flag.Int("l", 0, "wait for incoming connections")
	target := flag.String("d", "", "target peer to dial")
Jeromy's avatar
Jeromy committed
66
67
	secio := flag.Bool("secio", false, "enable secio")

Jeromy's avatar
Jeromy committed
68
69
70
71
	flag.Parse()

	listenaddr := fmt.Sprintf("/ip4/0.0.0.0/tcp/%d", *listenF)

Jeromy's avatar
Jeromy committed
72
	ha, err := makeDummyHost(listenaddr, *secio)
Jeromy's avatar
Jeromy committed
73
74
75
76
77
	if err != nil {
		log.Fatal(err)
	}

	// Set a stream handler on host A
Jeromy's avatar
Jeromy committed
78
79
	ha.SetStreamHandler("/echo/1.0.0", func(s net.Stream) {
		log.Println("Got a new stream!")
Jeromy's avatar
Jeromy committed
80
81
82
83
		defer s.Close()
	})

	if *target == "" {
Jeromy's avatar
Jeromy committed
84
85
		log.Println("listening for connections...")
		select {} // hang forever
Jeromy's avatar
Jeromy committed
86
87
	}

Jeromy's avatar
Jeromy committed
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
	ipfsaddr, err := ma.NewMultiaddr(*target)
	if err != nil {
		log.Fatalln(err)
	}

	pid, err := ipfsaddr.ValueForProtocol(ma.P_IPFS)
	if err != nil {
		log.Fatalln(err)
	}

	peerid, err := peer.IDB58Decode(pid)
	if err != nil {
		log.Fatalln(err)
	}

	tptaddr := strings.Split(ipfsaddr.String(), "/ipfs/")[0]
	tptmaddr, err := ma.NewMultiaddr(tptaddr)
Jeromy's avatar
Jeromy committed
105
106
107
108
109
	if err != nil {
		log.Fatalln(err)
	}

	pi := pstore.PeerInfo{
Jeromy's avatar
Jeromy committed
110
111
		ID:    peerid,
		Addrs: []ma.Multiaddr{tptmaddr},
Jeromy's avatar
Jeromy committed
112
113
114
115
116
117
118
119
120
121
122
	}

	log.Println("connecting to target")
	err = ha.Connect(context.Background(), pi)
	if err != nil {
		log.Fatalln(err)
	}

	log.Println("opening stream...")
	// make a new stream from host B to host A
	// it should be handled on host A by the handler we set
Jeromy's avatar
Jeromy committed
123
	s, err := ha.NewStream(context.Background(), peerid, "/hello/1.0.0")
Jeromy's avatar
Jeromy committed
124
125
126
127
	if err != nil {
		log.Fatalln(err)
	}

Jeromy's avatar
Jeromy committed
128
129
130
131
132
	_, err = s.Write([]byte("Hello world of peer two peer"))
	if err != nil {
		log.Fatalln(err)
	}

Jeromy's avatar
Jeromy committed
133
134
135
136
137
138
139
	out, err := ioutil.ReadAll(s)
	if err != nil {
		log.Fatalln(err)
	}

	log.Println("GOT: ", string(out))
}
Jeromy's avatar
Jeromy committed
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157

func doEcho(s inet.Stream) {
	buf := make([]byte, 1024)
	for {
		n, err := s.Read(buf)
		if err != nil {
			log.Println(err)
			return
		}

		log.Printf("read data: %q\n", buf[:n])
		_, err = s.Write(buf[:n])
		if err != nil {
			log.Println(err)
			return
		}
	}
}