github.com/telepresenceio/telepresence/v2@v2.20.0-pro.6.0.20240517030216-236ea954e789/pkg/client/cli/daemon/userd.go (about) 1 package daemon 2 3 import ( 4 "context" 5 "strconv" 6 "strings" 7 8 "google.golang.org/grpc" 9 10 "github.com/telepresenceio/telepresence/rpc/v2/connector" 11 ) 12 13 type UserClient struct { 14 connector.ConnectorClient 15 Conn *grpc.ClientConn 16 DaemonID *Identifier 17 } 18 19 type Session struct { 20 UserClient 21 Info *connector.ConnectInfo 22 Started bool 23 } 24 25 type userDaemonKey struct{} 26 27 func GetUserClient(ctx context.Context) *UserClient { 28 if ud, ok := ctx.Value(userDaemonKey{}).(*UserClient); ok { 29 return ud 30 } 31 return nil 32 } 33 34 func WithUserClient(ctx context.Context, ud *UserClient) context.Context { 35 return context.WithValue(ctx, userDaemonKey{}, ud) 36 } 37 38 type sessionKey struct{} 39 40 func GetSession(ctx context.Context) *Session { 41 if s, ok := ctx.Value(sessionKey{}).(*Session); ok { 42 return s 43 } 44 return nil 45 } 46 47 func WithSession(ctx context.Context, s *Session) context.Context { 48 return context.WithValue(ctx, sessionKey{}, s) 49 } 50 51 func (ud *UserClient) Containerized() bool { 52 return ud.DaemonID.Containerized 53 } 54 55 func (ud *UserClient) DaemonPort() int { 56 if ud.DaemonID.Containerized { 57 addr := ud.Conn.Target() 58 if lc := strings.LastIndexByte(addr, ':'); lc >= 0 { 59 if port, err := strconv.Atoi(addr[lc+1:]); err == nil { 60 return port 61 } 62 } 63 } 64 return -1 65 }