-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
160 lines (134 loc) · 4.7 KB
/
main.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package main
import (
"io"
"fmt"
"log"
"flag"
"strings"
"net/rpc"
"crypto/rand"
logr "github.com/sirupsen/logrus"
libp2p "github.com/libp2p/go-libp2p"
ma "github.com/multiformats/go-multiaddr"
crypto "github.com/libp2p/go-libp2p-crypto"
relay "github.com/libp2p/go-libp2p-circuit"
connmgr "github.com/libp2p/go-libp2p-connmgr"
c "github.com/libp2p/go-libp2p-daemon/p2pclient"
identify "github.com/libp2p/go-libp2p/p2p/protocol/identify"
)
func init() {
}
func main() {
identify.ClientVersion = "p2pd/0.1"
id := flag.String("id", "", "peer identity; private key file")
connMgr := flag.Bool("connManager", false, "Enables the Connection Manager")
connMgrLo := flag.Int("connLo", 256, "Connection Manager Low Water mark")
connMgrHi := flag.Int("connHi", 512, "Connection Manager High Water mark")
connMgrGrace := flag.Duration("connGrace", 120, "Connection Manager grace period (in seconds)")
natPortMap := flag.Bool("natPortMap", false, "Enables NAT port mapping")
pubsubRouter := flag.String("pubsubRouter", "gossipsub", "Specifies the pubsub router implementation")
pubsubSign := flag.Bool("pubsubSign", false, "Enables pubsub message signing")
pubsubSignStrict := flag.Bool("pubsubSignStrict", false, "Enables pubsub strict signature verification")
gossipsubHeartbeatInterval := flag.Duration("gossipsubHeartbeatInterval", 0, "Specifies the gossipsub heartbeat interval")
gossipsubHeartbeatInitialDelay := flag.Duration("gossipsubHeartbeatInitialDelay", 0, "Specifies the gossipsub initial heartbeat delay")
relayEnabled := flag.Bool("relay", true, "Enables circuit relay")
relayActive := flag.Bool("relayActive", false, "Enables active mode for relay")
relayHop := flag.Bool("relayHop", false, "Enables hop for relay")
hostAddrs := flag.String("hostAddrs", "", "comma separated list of multiaddrs the host should listen on")
announceAddrs := flag.String("announceAddrs", "", "comma separated list of multiaddrs the host should announce to the network")
noListen := flag.Bool("noListenAddrs", false, "sets the host to listen on no addresses")
flag.Parse()
var opts []libp2p.Option
if *id != "" {
var r io.Reader
r = rand.Reader
priv, _, err := crypto.GenerateEd25519Key(r)
if err != nil {
panic(err)
}
opts = append(opts, libp2p.Identity(priv))
}
if *hostAddrs != "" {
addrs := strings.Split(*hostAddrs, ",")
opts = append(opts, libp2p.ListenAddrStrings(addrs...))
}
if *announceAddrs != "" {
addrs := strings.Split(*announceAddrs, ",")
maddrs := make([]ma.Multiaddr, 0, len(addrs))
for _, a := range addrs {
maddr, err := ma.NewMultiaddr(a)
if err != nil {
log.Fatal(err)
}
maddrs = append(maddrs, maddr)
}
opts = append(opts, libp2p.AddrsFactory(func([]ma.Multiaddr) []ma.Multiaddr {
return maddrs
}))
}
if *connMgr {
cm := connmgr.NewConnManager(*connMgrLo, *connMgrHi, *connMgrGrace)
opts = append(opts, libp2p.ConnectionManager(cm))
}
if *natPortMap {
opts = append(opts, libp2p.NATPortMap())
}
if *relayEnabled {
var relayOpts []relay.RelayOpt
if *relayActive {
relayOpts = append(relayOpts, relay.OptActive)
}
if *relayHop {
relayOpts = append(relayOpts, relay.OptHop)
}
opts = append(opts, libp2p.EnableRelay(relayOpts...))
}
if *noListen {
opts = append(opts, libp2p.NoListenAddrs)
}
// Logrus provides JSON logs.
logr.SetFormatter(&logr.JSONFormatter{})
data := logr.Fields{
"id": *id,
"pubsubRouter": *pubsubRouter,
"gossipsubHeartbeatInterval": *gossipsubHeartbeatInterval,
"gossipsubHeartbeatInitialDelay": *gossipsubHeartbeatInitialDelay,
"relayEnabled": *relayEnabled,
"relayActive": *relayActive,
"relayHop": *relayHop,
"hostAddrs": *hostAddrs,
"announceAddrs": *announceAddrs,
}
fmt.Println(fmt.Printf("%#v",logr.WithFields(data)))
//start rpc server
msg := new(Message)
err := rpc.Register(msg)
fmt.Println(err)
//runs listener in background as a go function
go func() {
err = StartRpcServer()
fmt.Println(err)
}()
// gets the options to pass to the daemon
_, c1, closer, err := createDaemonClientPair(opts, *pubsubRouter, *pubsubSign, *pubsubSignStrict, *gossipsubHeartbeatInterval, *gossipsubHeartbeatInitialDelay)
if err != nil {
panic(err)
}
// fmt.Println(fmt.Printf("%#v",*d1))
// fmt.Println(fmt.Printf("%#v",*c1))
defer closer()
testProtos := []string{"/test"}
err = c1.NewStreamHandler(testProtos, func(info *c.StreamInfo, conn io.ReadWriteCloser) {
defer conn.Close()
buf := make([]byte, 1024)
_, err := conn.Read(buf)
if err != nil {
panic(err)
}
})
if err != nil {
panic(err)
}
fmt.Printf("Daemon started")
chanwait()
}