-
Notifications
You must be signed in to change notification settings - Fork 110
/
dial.go
50 lines (42 loc) · 1.54 KB
/
dial.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
package grpc
import (
"context"
"strings"
"time"
"github.com/edaniels/golog"
"go.viam.com/utils/rpc"
)
// Dial dials a gRPC server.
func Dial(ctx context.Context, address string, logger golog.Logger, opts ...rpc.DialOption) (rpc.ClientConn, error) {
webrtcOpts := rpc.DialWebRTCOptions{
Config: &DefaultWebRTCConfiguration,
}
if signalingServerAddress, secure, ok := InferSignalingServerAddress(address); ok {
webrtcOpts.AllowAutoDetectAuthOptions = true
webrtcOpts.SignalingInsecure = !secure
webrtcOpts.SignalingServerAddress = signalingServerAddress
}
optsCopy := make([]rpc.DialOption, len(opts)+2)
optsCopy[0] = rpc.WithWebRTCOptions(webrtcOpts)
optsCopy[1] = rpc.WithAllowInsecureDowngrade()
copy(optsCopy[2:], opts)
ctx, timeoutCancel := context.WithTimeout(ctx, 20*time.Second)
defer timeoutCancel()
return rpc.Dial(ctx, address, logger, optsCopy...)
}
// InferSignalingServerAddress returns the appropriate WebRTC signaling server address
// if it can be detected. Returns the address, if the endpoint is secure, and if found.
// TODO(RSDK-235):
// remove hard coding of signaling server address and
// prefer SRV lookup instead.
func InferSignalingServerAddress(address string) (string, bool, bool) {
switch {
case strings.HasSuffix(address, ".viam.cloud"):
return "app.viam.com:443", true, true
case strings.HasSuffix(address, ".robot.viaminternal"):
return "app.viaminternal:8089", false, true // not secure
case strings.HasSuffix(address, ".viamstg.cloud"):
return "app.viam.dev:443", true, true
}
return "", false, false
}