/
main.go
390 lines (330 loc) · 9.37 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
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
package main
import (
"context"
"crypto/rand"
"crypto/rsa"
"crypto/tls"
"crypto/x509"
"encoding/pem"
"fmt"
"math/big"
"net"
"net/http"
"os"
"github.com/Filecoin-Titan/titan/api/types"
"github.com/Filecoin-Titan/titan/node"
"github.com/Filecoin-Titan/titan/api"
"github.com/Filecoin-Titan/titan/build"
lcli "github.com/Filecoin-Titan/titan/cli"
"github.com/Filecoin-Titan/titan/lib/titanlog"
"github.com/Filecoin-Titan/titan/node/config"
"github.com/Filecoin-Titan/titan/node/modules/dtypes"
"github.com/Filecoin-Titan/titan/node/repo"
"github.com/Filecoin-Titan/titan/node/secret"
"github.com/filecoin-project/go-jsonrpc"
"github.com/filecoin-project/go-jsonrpc/auth"
"github.com/gbrlsnchs/jwt/v3"
"github.com/quic-go/quic-go"
"github.com/quic-go/quic-go/http3"
"golang.org/x/xerrors"
logging "github.com/ipfs/go-log/v2"
manet "github.com/multiformats/go-multiaddr/net"
"github.com/urfave/cli/v2"
)
var log = logging.Logger("main")
const (
// FlagSchedulerRepo Flag
FlagSchedulerRepo = "scheduler-repo"
// FlagSchedulerRepoDeprecation Flag
FlagSchedulerRepoDeprecation = "schedulerrepo"
)
func main() {
types.RunningNodeType = types.NodeScheduler
titanlog.SetupLogLevels()
local := []*cli.Command{
initCmd,
runCmd,
getAPIKeyCmd,
}
local = append(local, lcli.SchedulerCMDs...)
local = append(local, lcli.CommonCommands...)
app := &cli.App{
Name: "titan-scheduler",
Usage: "Titan scheduler node",
Version: build.UserVersion(),
EnableBashCompletion: true,
Flags: []cli.Flag{
&cli.StringFlag{
Name: FlagSchedulerRepo,
Aliases: []string{FlagSchedulerRepoDeprecation},
EnvVars: []string{"TITAN_SCHEDULER_PATH", "SCHEDULER_PATH"},
Value: "~/.titanscheduler", // TODO: Consider XDG_DATA_HOME
Usage: fmt.Sprintf("Specify scheduler repo path. flag %s and env TITAN_SCHEDULER_PATH are DEPRECATION, will REMOVE SOON", FlagSchedulerRepoDeprecation),
},
&cli.StringFlag{
Name: "panic-reports",
EnvVars: []string{"TITAN_PANIC_REPORT_PATH"},
Hidden: true,
Value: "~/.titanscheduler", // should follow --repo default
},
},
After: func(c *cli.Context) error {
if r := recover(); r != nil {
// Generate report in TITAN_SCHEDULER_PATH and re-raise panic
build.GeneratePanicReport(c.String("panic-reports"), c.String(FlagSchedulerRepo), c.App.Name)
log.Panic(r)
}
return nil
},
Commands: local,
}
app.Setup()
app.Metadata["repoType"] = repo.Scheduler
if err := app.Run(os.Args); err != nil {
log.Warnf("%+v", err)
return
}
}
type jwtPayload struct {
Allow []auth.Permission
}
var initCmd = &cli.Command{
Name: "init",
Usage: "Initialize a titan scheduler repo",
Action: func(cctx *cli.Context) error {
log.Info("Initializing titan scheduler")
repoPath := cctx.String(FlagSchedulerRepo)
r, err := repo.NewFS(repoPath)
if err != nil {
return err
}
ok, err := r.Exists()
if err != nil {
return err
}
if ok {
return xerrors.Errorf("repo at '%s' is already initialized", cctx.String(FlagSchedulerRepo))
}
log.Info("Initializing repo")
if err := r.Init(repo.Scheduler); err != nil {
return err
}
return nil
},
}
var getAPIKeyCmd = &cli.Command{
Name: "get-api-key",
Usage: "Generate API Key",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "perm",
Usage: "permission to assign to the token, one of: web, candidate, edge, locator,admin",
Value: "",
},
},
Action: func(cctx *cli.Context) error {
lr, err := openRepo(cctx)
if err != nil {
return err
}
defer lr.Close() //nolint
perm := cctx.String("perm")
p := jwtPayload{}
p.Allow = []auth.Permission{auth.Permission(perm)}
authKey, err := secret.APISecret(lr)
if err != nil {
return xerrors.Errorf("setting up api secret: %w", err)
}
k, err := jwt.Sign(&p, authKey)
if err != nil {
return xerrors.Errorf("jwt sign: %w", err)
}
fmt.Println(string(k))
return nil
},
}
var runCmd = &cli.Command{
Name: "run",
Usage: "Start titan scheduler node",
Flags: []cli.Flag{},
Before: func(cctx *cli.Context) error {
return nil
},
Action: func(cctx *cli.Context) error {
log.Info("Starting titan scheduler node")
repoPath := cctx.String(FlagSchedulerRepo)
r, err := repo.NewFS(repoPath)
if err != nil {
return err
}
ok, err := r.Exists()
if err != nil {
return err
}
if !ok {
if err := r.Init(repo.Scheduler); err != nil {
return err
}
}
lr, err := r.Lock(repo.Scheduler)
if err != nil {
return err
}
cfg, err := lr.Config()
if err != nil {
return err
}
schedulerCfg := cfg.(*config.SchedulerCfg)
err = lr.Close()
if err != nil {
return err
}
udpPacketConn, err := net.ListenPacket("udp", schedulerCfg.ListenAddress)
if err != nil {
return err
}
defer func() {
err = udpPacketConn.Close()
if err != nil {
log.Errorf("udpPacketConn Close err:%s", err.Error())
}
}()
transport := &quic.Transport{Conn: udpPacketConn}
var shutdownChan = make(chan struct{})
var schedulerAPI api.Scheduler
stop, err := node.New(cctx.Context,
node.Scheduler(&schedulerAPI),
node.Base(),
node.Repo(r),
node.Override(new(*quic.Transport), transport),
node.Override(new(dtypes.ShutdownChan), shutdownChan),
node.Override(node.SetApiEndpointKey, func(lr repo.LockedRepo) error {
return setEndpointAPI(lr, schedulerCfg.ListenAddress)
}),
)
if err != nil {
return xerrors.Errorf("creating node: %w", err)
}
// Populate JSON-RPC options.
serverOptions := []jsonrpc.ServerOption{jsonrpc.WithServerErrors(api.RPCErrors)}
if maxRequestSize := cctx.Int("api-max-req-size"); maxRequestSize != 0 {
serverOptions = append(serverOptions, jsonrpc.WithMaxRequestSize(int64(maxRequestSize)))
}
// Instantiate the scheduler handler.
h, err := node.SchedulerHandler(schedulerAPI, true, serverOptions...)
if err != nil {
return fmt.Errorf("failed to instantiate rpc handler: %s", err.Error())
}
var stopHTTP3Server = func(context.Context) error {
return transport.Close()
}
go startHTTP3Server(transport, h, schedulerCfg) //nolint:errcheck
// Serve the RPC.
rpcStopper, err := node.ServeRPC(h, "scheduler", schedulerCfg.ListenAddress)
if err != nil {
return fmt.Errorf("failed to start json-rpc endpoint: %s", err.Error())
}
log.Info("titan scheduler listen with:", schedulerCfg.ListenAddress)
// Monitor for shutdown.
finishCh := node.MonitorShutdown(shutdownChan,
node.ShutdownHandler{Component: "rpc server", StopFunc: rpcStopper},
node.ShutdownHandler{Component: "node", StopFunc: stop},
node.ShutdownHandler{Component: "http3 server", StopFunc: stopHTTP3Server},
)
<-finishCh // fires when shutdown is complete.
return nil
},
}
func openRepo(cctx *cli.Context) (repo.LockedRepo, error) {
repoPath := cctx.String(FlagSchedulerRepo)
r, err := repo.NewFS(repoPath)
if err != nil {
return nil, err
}
ok, err := r.Exists()
if err != nil {
return nil, err
}
if !ok {
if err := r.Init(repo.Scheduler); err != nil {
return nil, err
}
}
lr, err := r.Lock(repo.Scheduler)
if err != nil {
return nil, err
}
return lr, nil
}
func startHTTP3Server(transport *quic.Transport, handler http.Handler, schedulerCfg *config.SchedulerCfg) error {
var tlsConfig *tls.Config
if len(schedulerCfg.CertificatePath) == 0 || len(schedulerCfg.PrivateKeyPath) == 0 {
config, err := defaultTLSConfig()
if err != nil {
log.Errorf("startUDPServer, defaultTLSConfig error:%s", err.Error())
return err
}
tlsConfig = config
} else {
cert, err := tls.LoadX509KeyPair(schedulerCfg.CertificatePath, schedulerCfg.PrivateKeyPath)
if err != nil {
log.Errorf("startUDPServer, LoadX509KeyPair error:%s", err.Error())
return err
}
tlsConfig = &tls.Config{
MinVersion: tls.VersionTLS12,
Certificates: []tls.Certificate{cert},
NextProtos: []string{"h2", "h3"},
InsecureSkipVerify: false,
}
}
ln, err := transport.ListenEarly(tlsConfig, nil)
if err != nil {
return err
}
srv := http3.Server{
TLSConfig: tlsConfig,
Handler: handler,
}
if err = srv.ServeListener(ln); err != nil {
log.Warn("http3 server ", err.Error())
}
return err
}
func defaultTLSConfig() (*tls.Config, error) {
key, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
return nil, err
}
template := x509.Certificate{SerialNumber: big.NewInt(1)}
certDER, err := x509.CreateCertificate(rand.Reader, &template, &template, &key.PublicKey, key)
if err != nil {
return nil, err
}
keyPEM := pem.EncodeToMemory(&pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(key)})
certPEM := pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: certDER})
tlsCert, err := tls.X509KeyPair(certPEM, keyPEM)
if err != nil {
return nil, err
}
return &tls.Config{
MinVersion: tls.VersionTLS12,
Certificates: []tls.Certificate{tlsCert},
NextProtos: []string{"h2", "h3"},
InsecureSkipVerify: true, //nolint:gosec // skip verify in default config
}, nil
}
func setEndpointAPI(lr repo.LockedRepo, address string) error {
a, err := net.ResolveTCPAddr("tcp", address)
if err != nil {
return xerrors.Errorf("parsing address: %w", err)
}
ma, err := manet.FromNetAddr(a)
if err != nil {
return xerrors.Errorf("creating api multiaddress: %w", err)
}
if err := lr.SetAPIEndpoint(ma); err != nil {
return xerrors.Errorf("setting api endpoint: %w", err)
}
return nil
}