/
libp2p.go
113 lines (86 loc) · 2.68 KB
/
libp2p.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
//go:build !IPFSLITE
// +build !IPFSLITE
package ipfs
import (
"context"
"crypto/ecdsa"
"fmt"
"github.com/costinm/meshauth"
"github.com/costinm/ugate"
"github.com/libp2p/go-libp2p/core/network"
"log"
"net"
"github.com/ipfs/boxo/routing/http/client"
"github.com/ipfs/boxo/routing/http/contentrouter"
"github.com/libp2p/go-libp2p"
"github.com/libp2p/go-libp2p/core/crypto"
"github.com/libp2p/go-libp2p/core/host"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/libp2p/go-libp2p/core/routing"
"github.com/multiformats/go-multiaddr"
)
// ConnectionGater, Server
type IPFS struct {
Host host.Host
}
func (ipfs *IPFS) FindPeer(ctx context.Context, id peer.ID) (peer.AddrInfo, error) {
return peer.AddrInfo{ID: id}, nil
}
func (ipfs *IPFS) DialContext(ctx context.Context, net string, addr string) (net.Conn, error) {
return nil, nil
}
func Init(ug *ugate.UGate) {
ug.ListenerProto["ipfs"] = func(gate *ugate.UGate, l *meshauth.PortListener) error {
InitIPFS(ug.Auth, l.GetPort())
return nil
}
}
// InitIPFS creates LibP2P compatible transport.
// Identity is based on the EC256 workload identity in auth.
//
// Routing is based on HTTP.
//
// Main purpose of this integration is to take advantage of public
// auto-relay code and infra, for control/signaling channels.
//
func InitIPFS(auth *meshauth.MeshAuth, p2pport int32) *IPFS {
p2p := &IPFS{
}
sk, _, _ := crypto.ECDSAKeyPairFromKey(auth.Cert.PrivateKey.(*ecdsa.PrivateKey))
la := []multiaddr.Multiaddr{}
listen, err := multiaddr.NewMultiaddr(fmt.Sprintf("/ip4/0.0.0.0/tcp/%d", p2pport))
la = append(la, listen)
finalOpts := []libp2p.Option{
libp2p.Identity(sk),
libp2p.ListenAddrs(la...),
libp2p.ConnectionGater(p2p),
}
// Use a HTTP gateway for resolution - https://ipfs.io or https://cloudflare-ipfs.com/
// Can also use mdns locally
finalOpts = append(finalOpts,
libp2p.Routing(func(h host.Host) (routing.PeerRouting, error) {
resClient, err := client.New("http://127.0.0.1:11015")
cr := contentrouter.NewContentRoutingClient(resClient)
return cr, err
}))
h, err := libp2p.New(finalOpts...)
if err != nil {
panic(err)
}
p2p.Host = h
// Add our stream handlers
h.SetStreamHandler(Protocol, streamHandler)
// Use the host 'event bus' to subscribe to all events.
InitEvent(p2p.Host)
for _, a := range h.Addrs() {
log.Println("IPFS Addr: ", a.String() + "/p2p/" + h.ID().String())
}
log.Println("IPFS CID: ", peer.ToCid(h.ID()).String())
return p2p
}
const Protocol = "/ugate/0.0.1"
func streamHandler(stream network.Stream) {
// Remember to close the stream when we are done.
defer stream.Close()
log.Println("NEW STREAM: ", stream.Conn().RemotePeer(), stream.Conn().RemotePublicKey())
}