-
Notifications
You must be signed in to change notification settings - Fork 666
/
app.go
270 lines (231 loc) · 7.26 KB
/
app.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
// Copyright (C) 2019-2023, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package app
import (
"fmt"
"os"
"os/signal"
"sync"
"syscall"
"go.uber.org/zap"
"golang.org/x/sync/errgroup"
"github.com/ava-labs/avalanchego/nat"
"github.com/ava-labs/avalanchego/node"
"github.com/ava-labs/avalanchego/utils/constants"
"github.com/ava-labs/avalanchego/utils/ips"
"github.com/ava-labs/avalanchego/utils/logging"
"github.com/ava-labs/avalanchego/utils/perms"
"github.com/ava-labs/avalanchego/utils/ulimit"
)
const (
Header = ` _____ .__ .__
/ _ \___ _______ | | _____ ____ ____ | |__ ____ ,_ o
/ /_\ \ \/ /\__ \ | | \__ \ / \_/ ___\| | \_/ __ \ / //\,
/ | \ / / __ \| |__/ __ \| | \ \___| Y \ ___/ \>> |
\____|__ /\_/ (____ /____(____ /___| /\___ >___| /\___ > \\
\/ \/ \/ \/ \/ \/ \/`
)
var (
stakingPortName = fmt.Sprintf("%s-staking", constants.AppName)
httpPortName = fmt.Sprintf("%s-http", constants.AppName)
_ App = (*app)(nil)
)
type App interface {
// Start kicks off the application and returns immediately.
// Start should only be called once.
Start() error
// Stop notifies the application to exit and returns immediately.
// Stop should only be called after [Start].
// It is safe to call Stop multiple times.
Stop() error
// ExitCode should only be called after [Start] returns with no error. It
// should block until the application finishes
ExitCode() (int, error)
}
func New(config node.Config) App {
return &app{
config: config,
node: &node.Node{},
}
}
func Run(app App) int {
// start running the application
if err := app.Start(); err != nil {
return 1
}
// register signals to kill the application
signals := make(chan os.Signal, 1)
signal.Notify(signals, syscall.SIGINT)
signal.Notify(signals, syscall.SIGTERM)
// start up a new go routine to handle attempts to kill the application
var eg errgroup.Group
eg.Go(func() error {
for range signals {
return app.Stop()
}
return nil
})
// wait for the app to exit and get the exit code response
exitCode, err := app.ExitCode()
// shut down the signal go routine
signal.Stop(signals)
close(signals)
// if there was an error closing or running the application, report that error
if eg.Wait() != nil || err != nil {
return 1
}
// return the exit code that the application reported
return exitCode
}
// app is a wrapper around a node that runs in this process
type app struct {
config node.Config
node *node.Node
exitWG sync.WaitGroup
}
// Start the business logic of the node (as opposed to config reading, etc).
// Does not block until the node is done. Errors returned from this method
// are not logged.
func (a *app) Start() error {
// Set the data directory permissions to be read write.
if err := perms.ChmodR(a.config.DatabaseConfig.Path, true, perms.ReadWriteExecute); err != nil {
return fmt.Errorf("failed to restrict the permissions of the database directory with: %w", err)
}
if err := perms.ChmodR(a.config.LoggingConfig.Directory, true, perms.ReadWriteExecute); err != nil {
return fmt.Errorf("failed to restrict the permissions of the log directory with: %w", err)
}
// we want to create the logger after the plugin has started the app
logFactory := logging.NewFactory(a.config.LoggingConfig)
log, err := logFactory.Make("main")
if err != nil {
logFactory.Close()
return err
}
// update fd limit
fdLimit := a.config.FdLimit
if err := ulimit.Set(fdLimit, log); err != nil {
log.Fatal("failed to set fd-limit",
zap.Error(err),
)
logFactory.Close()
return err
}
// Track if sybil control is enforced
if !a.config.SybilProtectionEnabled {
log.Warn("sybil control is not enforced")
}
// TODO move this to config
// SupportsNAT() for NoRouter is false.
// Which means we tried to perform a NAT activity but we were not successful.
if a.config.AttemptedNATTraversal && !a.config.Nat.SupportsNAT() {
log.Warn("UPnP and NAT-PMP router attach failed, you may not be listening publicly. " +
"Please confirm the settings in your router")
}
if ip := a.config.IPPort.IPPort().IP; ip.IsLoopback() || ip.IsPrivate() {
log.Warn("P2P IP is private, you will not be publicly discoverable",
zap.Stringer("ip", ip),
)
}
// An empty host is treated as a wildcard to match all addresses, so it is
// considered public.
hostIsPublic := a.config.HTTPHost == ""
if !hostIsPublic {
ip, err := ips.Lookup(a.config.HTTPHost)
if err != nil {
log.Fatal("failed to lookup HTTP host",
zap.String("host", a.config.HTTPHost),
zap.Error(err),
)
logFactory.Close()
return err
}
hostIsPublic = !ip.IsLoopback() && !ip.IsPrivate()
log.Debug("finished HTTP host lookup",
zap.String("host", a.config.HTTPHost),
zap.Stringer("ip", ip),
zap.Bool("isPublic", hostIsPublic),
)
}
mapper := nat.NewPortMapper(log, a.config.Nat)
// Open staking port we want for NAT traversal to have the external port
// (config.IP.Port) to connect to our internal listening port
// (config.InternalStakingPort) which should be the same in most cases.
if port := a.config.IPPort.IPPort().Port; port != 0 {
mapper.Map(
port,
port,
stakingPortName,
a.config.IPPort,
a.config.IPResolutionFreq,
)
}
// Don't open the HTTP port if the HTTP server is private
if hostIsPublic {
log.Warn("HTTP server is binding to a potentially public host. "+
"You may be vulnerable to a DoS attack if your HTTP port is publicly accessible",
zap.String("host", a.config.HTTPHost),
)
// For NAT traversal we want to route from the external port
// (config.ExternalHTTPPort) to our internal port (config.HTTPPort).
if a.config.HTTPPort != 0 {
mapper.Map(
a.config.HTTPPort,
a.config.HTTPPort,
httpPortName,
nil,
a.config.IPResolutionFreq,
)
}
}
// Regularly update our public IP.
// Note that if the node config said to not dynamically resolve and
// update our public IP, [p.config.IPUdater] is a no-op implementation.
go a.config.IPUpdater.Dispatch(log)
if err := a.node.Initialize(&a.config, log, logFactory); err != nil {
log.Fatal("error initializing node",
zap.Error(err),
)
mapper.UnmapAllPorts()
a.config.IPUpdater.Stop()
log.Stop()
logFactory.Close()
return err
}
// [p.ExitCode] will block until [p.exitWG.Done] is called
a.exitWG.Add(1)
go func() {
defer func() {
if r := recover(); r != nil {
fmt.Println("caught panic", r)
}
log.Stop()
logFactory.Close()
a.exitWG.Done()
}()
defer func() {
mapper.UnmapAllPorts()
a.config.IPUpdater.Stop()
// If [p.node.Dispatch()] panics, then we should log the panic and
// then re-raise the panic. This is why the above defer is broken
// into two parts.
log.StopOnPanic()
}()
err := a.node.Dispatch()
log.Debug("dispatch returned",
zap.Error(err),
)
}()
return nil
}
// Stop attempts to shutdown the currently running node. This function will
// return immediately.
func (a *app) Stop() error {
a.node.Shutdown(0)
return nil
}
// ExitCode returns the exit code that the node is reporting. This function
// blocks until the node has been shut down.
func (a *app) ExitCode() (int, error) {
a.exitWG.Wait()
return a.node.ExitCode(), nil
}