This repository was archived by the owner on Dec 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhost.go
60 lines (53 loc) · 1.35 KB
/
host.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
package dione
import (
"fmt"
"github.com/libp2p/go-libp2p"
libp2p_crypto "github.com/libp2p/go-libp2p-core/crypto"
"github.com/libp2p/go-libp2p-core/host"
"github.com/libp2p/go-libp2p-core/routing"
"github.com/libp2p/go-libp2p-kad-dht"
noise "github.com/libp2p/go-libp2p-noise"
routedhost "github.com/libp2p/go-libp2p/p2p/host/routed"
)
type keyPair struct {
PrivateKey libp2p_crypto.PrivKey
PublicKey libp2p_crypto.PubKey
}
func newKeyPair() (keyPair, error) {
priv, pub, err := libp2p_crypto.GenerateKeyPair(
libp2p_crypto.Ed25519,
256,
)
ret := new(keyPair)
ret.PrivateKey = priv
ret.PublicKey = pub
return *ret, err
}
func NewHost(port int) (host.Host, *dht.IpfsDHT) {
var internalDht *dht.IpfsDHT
kP, err := newKeyPair()
if err != nil {
panic(err)
}
tcpString := fmt.Sprintf("/ip4/0.0.0.0/tcp/%v", port)
quicString := fmt.Sprintf("/ip4/0.0.0.0/udp/%v/quic", port)
h, err := libp2p.New(
libp2p.Identity(kP.PrivateKey),
libp2p.ListenAddrStrings(
tcpString,
quicString),
libp2p.Security(noise.ID, noise.New),
libp2p.DefaultTransports,
libp2p.NATPortMap(),
libp2p.Routing(func(h host.Host) (routing.PeerRouting, error) {
internalDht = newDht(h)
return internalDht, err
}),
libp2p.EnableAutoRelay(),
)
if err != nil {
panic(err)
}
routedHost := routedhost.Wrap(h, internalDht)
return routedHost, internalDht
}