/
bridge.go
100 lines (84 loc) · 2.12 KB
/
bridge.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
package core
import (
"context"
"fmt"
"berty.tech/core/api/helper"
"berty.tech/core/daemon"
network_config "berty.tech/core/network/config"
"google.golang.org/grpc"
)
var sqlConfig = &daemon.SQLConfig{
Name: "berty.state.db",
Key: "s3cur3",
}
var config = &daemon.Config{
SqlOpts: sqlConfig,
GrpcBind: ":0",
GqlBind: ":0",
HideBanner: true,
DropDatabase: false,
InitOnly: false,
WithBot: false,
Notification: true,
ApnsCerts: []string{},
ApnsDevVoipCerts: []string{},
FcmAPIKeys: []string{},
PrivateKeyFile: "",
PeerCache: true,
Identity: "",
Bootstrap: network_config.DefaultBootstrap,
NoP2P: false,
BindP2P: []string{},
TransportP2P: []string{},
Hop: false,
Ble: true,
Mdns: true,
DhtServer: false,
PrivateNetwork: true,
SwarmKeyPath: "",
}
type NativeBridge struct {
bridge *daemon.Daemon
server *grpc.Server
conn *grpc.ClientConn
}
// @FIXME: NewNativeBridge must not panic, for now Initialize and Dial (should) never
// return an error so it safe, but keep an eye on it.
func NewNativeBridge() *NativeBridge {
bridge := daemon.New()
if _, err := bridge.Initialize(context.Background(), config); err != nil {
panic(err)
}
iogrpc := helper.NewIOGrpc()
dialer := iogrpc.NewDialer()
listener := iogrpc.Listener()
dialOpts := append([]grpc.DialOption{
grpc.WithInsecure(),
grpc.WithDialer(dialer),
})
gs := grpc.NewServer()
daemon.RegisterDaemonServer(gs, bridge)
conn, err := grpc.Dial("", dialOpts...)
if err != nil {
panic(err)
}
go func() {
if err := gs.Serve(listener); err != nil {
fmt.Errorf("serve error %s", err)
}
}()
return &NativeBridge{
bridge: bridge,
server: gs,
conn: conn,
}
}
func (n *NativeBridge) Invoke(method string, msgIn string) (string, error) {
in, err := helper.NewLazyMessage().FromBase64(msgIn)
if err != nil {
return "", err
}
out := helper.NewLazyMessage()
err = n.conn.Invoke(context.TODO(), method, in, out, helper.GrpcCallWithLazyCodec())
return out.Base64(), err
}