-
Notifications
You must be signed in to change notification settings - Fork 507
/
userd.go
61 lines (49 loc) · 1.12 KB
/
userd.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
package daemon
import (
"context"
"strconv"
"strings"
"google.golang.org/grpc"
"github.com/telepresenceio/telepresence/rpc/v2/connector"
)
type UserClient struct {
connector.ConnectorClient
Conn *grpc.ClientConn
Remote bool
}
type Session struct {
UserClient
Info *connector.ConnectInfo
Started bool
}
type userDaemonKey struct{}
func GetUserClient(ctx context.Context) *UserClient {
if ud, ok := ctx.Value(userDaemonKey{}).(*UserClient); ok {
return ud
}
return nil
}
func WithUserClient(ctx context.Context, ud *UserClient) context.Context {
return context.WithValue(ctx, userDaemonKey{}, ud)
}
type sessionKey struct{}
func GetSession(ctx context.Context) *Session {
if s, ok := ctx.Value(sessionKey{}).(*Session); ok {
return s
}
return nil
}
func WithSession(ctx context.Context, s *Session) context.Context {
return context.WithValue(ctx, sessionKey{}, s)
}
func (ud *UserClient) DaemonPort() int {
if ud.Remote {
addr := ud.Conn.Target()
if lc := strings.LastIndexByte(addr, ':'); lc >= 0 {
if port, err := strconv.Atoi(addr[lc+1:]); err == nil {
return port
}
}
}
return -1
}