-
Notifications
You must be signed in to change notification settings - Fork 384
/
push.go
110 lines (89 loc) · 2.8 KB
/
push.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
package main
import (
"context"
"encoding/base64"
"flag"
"fmt"
"github.com/peterbourgon/ff/v3/ffcli"
"berty.tech/berty/v2/go/internal/cryptoutil"
"berty.tech/berty/v2/go/pkg/authtypes"
"berty.tech/berty/v2/go/pkg/bertypushrelay"
"berty.tech/berty/v2/go/pkg/pushtypes"
)
func pushServerCommand() *ffcli.Command {
var (
apns *string
fcm *string
sk *string
)
fsBuilder := func() (*flag.FlagSet, error) {
fs := flag.NewFlagSet("berty push-server", flag.ExitOnError)
apns = fs.String("apns", "", "Apple's apns certs path, comma-separated")
fcm = fs.String("fcm", "", "Firebase's FCM API keys, formatted like app_id:api_key and comma-separated")
sk = fs.String("push-private-key", "", "Push server private key, base64 formatted")
manager.SetupLoggingFlags(fs) // also available at root level
manager.SetupProtocolAuth(fs)
manager.SetupDefaultGRPCListenersFlags(fs)
// set serviceid for needed by push server
manager.Node.Protocol.ServiceID = authtypes.ServicePushID
return fs, nil
}
return &ffcli.Command{
Name: "push-server",
ShortHelp: "push relay server",
ShortUsage: "berty [global flags] push-server [flags]",
FlagSetBuilder: fsBuilder,
UsageFunc: usageFunc,
Options: ffSubcommandOptions(),
Exec: func(ctx context.Context, args []string) error {
if len(args) > 0 {
return flag.ErrHelp
}
logger, err := manager.GetLogger()
if err != nil {
return err
}
if manager.Node.Protocol.AuthSecret == "" {
return fmt.Errorf("node.auth-secret cannot be empty")
}
if manager.Node.Protocol.AuthPublicKey == "" {
return fmt.Errorf("node.auth-pk cannot be empty")
}
if sk == nil || *sk == "" {
return fmt.Errorf("push-private-key cannot be empty")
}
keyBytes, err := base64.RawURLEncoding.DecodeString(*sk)
if err != nil {
return fmt.Errorf("invalid private key supplied")
}
key, err := cryptoutil.KeySliceToArray(keyBytes)
if err != nil {
return err
}
dispatchers := []bertypushrelay.PushDispatcher{}
apnsDispatchers, err := bertypushrelay.PushDispatcherLoadAPNSCertificates(logger, apns)
if err != nil {
return err
}
dispatchers = append(dispatchers, apnsDispatchers...)
fcmDispatchers, err := bertypushrelay.PushDispatcherLoadFirebaseAPIKey(logger, fcm)
if err != nil {
return err
}
dispatchers = append(dispatchers, fcmDispatchers...)
server, mux, err := manager.GetGRPCServer()
if err != nil {
return err
}
pushService, err := bertypushrelay.NewPushService(key, dispatchers, logger)
if err != nil {
return err
}
pushtypes.RegisterPushServiceServer(server, pushService)
if err := pushtypes.RegisterPushServiceHandlerServer(ctx, mux, pushService); err != nil {
return err
}
return manager.RunWorkers()
},
}
}