/
main.go
185 lines (141 loc) · 4.24 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package main
import (
"context"
"encoding/hex"
"flag"
"fmt"
"log"
"sync"
"time"
"github.com/HORNET-Storage/hornet-storage/lib/web"
"github.com/libp2p/go-libp2p"
"github.com/libp2p/go-libp2p/core/crypto"
"github.com/libp2p/go-libp2p/p2p/net/connmgr"
"github.com/libp2p/go-libp2p/p2p/security/noise"
libp2ptls "github.com/libp2p/go-libp2p/p2p/security/tls"
keys "github.com/HORNET-Storage/hornet-storage/lib/context"
"github.com/HORNET-Storage/hornet-storage/lib/handlers"
merkle_dag "github.com/HORNET-Storage/scionic-merkletree/dag"
//stores_bbolt "github.com/HORNET-Storage/hornet-storage/lib/stores/bbolt"
//stores_memory "github.com/HORNET-Storage/hornet-storage/lib/stores/memory"
stores_graviton "github.com/HORNET-Storage/hornet-storage/lib/stores/graviton"
)
const DefaultPort = "9000"
func main() {
ctx := context.Background()
wg := new(sync.WaitGroup)
keyFlag := flag.String("key", "", "Private key used to identify this node")
webFlag := flag.Bool("web", false, "Launch web server: true/false")
portFlag := flag.String("port", "", "Port to run the node on")
flag.Parse()
// Web Panel
if *webFlag {
wg.Add(1)
fmt.Println("Starting with web server enabled")
go func() {
err := web.StartServer()
if err != nil {
fmt.Println("Fatal error occured in web server")
}
wg.Done()
}()
}
// Port
port := DefaultPort
if portFlag != nil {
port = *portFlag
}
// Private key
var priv crypto.PrivKey
if keyFlag != nil {
bytes, err := hex.DecodeString(*keyFlag)
if err != nil {
log.Fatal(err)
}
privateKey, err := crypto.UnmarshalPrivateKey(bytes)
if err != nil {
log.Fatal(err)
}
priv = privateKey
} else {
privateKey, _, err := crypto.GenerateKeyPair(
crypto.Ed25519,
-1,
)
if err != nil {
log.Fatal(err)
}
priv = privateKey
}
// New storage implementation (will replace the above)
store := &stores_graviton.GravitonStore{}
store.InitStore()
ctx = context.WithValue(ctx, keys.Storage, store)
// This works but feels weird, open to better solutions
//defer store.UserDatabase.Db.Close()
//defer store.ContentDatabase.Db.Close()
// Setup libp2p Connection Manager
connmgr, err := connmgr.NewConnManager(
100, // Lowwater
400, // HighWater,
connmgr.WithGracePeriod(time.Minute),
)
if err != nil {
panic(err)
}
// Libp2p Host
listenAddress := fmt.Sprintf("/ip4/0.0.0.0/tcp/%s", port)
host, err := libp2p.New(
libp2p.Identity(priv),
// Multiple listen addresses
libp2p.ListenAddrStrings(
listenAddress,
),
// support TLS connections
libp2p.Security(libp2ptls.ID, libp2ptls.New),
// support noise connections
libp2p.Security(noise.ID, noise.New),
//libp2p.Transport(customQUICConstructor),
// support any other default transports (TCP)
libp2p.DefaultTransports,
//libp2p.Transport(transport),
// Let's prevent our peer from having too many
// connections by attaching a connection manager.
libp2p.ConnectionManager(connmgr),
// Attempt to open ports using uPNP for NATed hosts.
//libp2p.NATPortMap(),
// Let this host use the DHT to find other hosts
//libp2p.Routing(func(h host.Host) (routing.PeerRouting, error) {
// idht, err = dht.New(ctx, h)
// return idht, err
//}),
// If you want to help other peers to figure out if they are behind
// NATs, you can launch the server-side of AutoNAT too (AutoRelay
// already runs the client)
//
// This service is highly rate-limited and should not cause any
// performance issues.
//libp2p.EnableNATService(),
)
if err != nil {
log.Fatal(err)
}
log.Println("Adding download handler")
// Stream Handlers
handlers.AddDownloadHandler(host, store, func(rootLeaf *merkle_dag.DagLeaf, pubKey *string, signature *string) bool {
// Check keys or potential future permissions here
return true
})
log.Println("Adding upload handler")
handlers.AddUploadHandler(host, store, func(rootLeaf *merkle_dag.DagLeaf, pubKey *string, signature *string) bool {
// Check keys or potential future permissions here
return true
}, func(dag *merkle_dag.Dag, pubKey *string) {
// Don't need to do anything here right now
})
defer host.Close()
ctx = context.WithValue(ctx, keys.Host, host)
fmt.Printf("Host started with id: %s\n", host.ID())
keys.SetContext(ctx)
wg.Wait()
}