diff --git a/go.mod b/go.mod index 08d4c245b..67d18003b 100644 --- a/go.mod +++ b/go.mod @@ -38,6 +38,8 @@ require ( golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 golang.org/x/text v0.3.7 + google.golang.org/grpc v1.49.0 + google.golang.org/protobuf v1.27.1 gopkg.in/yaml.v3 v3.0.1 ) @@ -70,6 +72,7 @@ require ( github.com/yuin/goldmark-emoji v1.0.1 // indirect golang.org/x/net v0.0.0-20220722155237-a158d28d115b // indirect golang.org/x/oauth2 v0.0.0-20220309155454-6242fa91716a // indirect + google.golang.org/genproto v0.0.0-20200825200019-8632dd797987 // indirect gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect ) diff --git a/go.sum b/go.sum index f24d36e0a..4b79794c5 100644 --- a/go.sum +++ b/go.sum @@ -485,6 +485,7 @@ google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013/go.mod h1:NbSheEEY google.golang.org/genproto v0.0.0-20200618031413-b414f8b61790/go.mod h1:jDfRM7FcilCzHH/e9qn6dsT145K34l5v+OpcnNgKAAA= google.golang.org/genproto v0.0.0-20200729003335-053ba62fc06f/go.mod h1:FWY/as6DDZQgahTzZj3fqbO1CbirC29ZNUFHwi0/+no= google.golang.org/genproto v0.0.0-20200804131852-c06518451d9c/go.mod h1:FWY/as6DDZQgahTzZj3fqbO1CbirC29ZNUFHwi0/+no= +google.golang.org/genproto v0.0.0-20200825200019-8632dd797987 h1:PDIOdWxZ8eRizhKa1AAvY53xsvLB1cWorMjslvY3VA8= google.golang.org/genproto v0.0.0-20200825200019-8632dd797987/go.mod h1:FWY/as6DDZQgahTzZj3fqbO1CbirC29ZNUFHwi0/+no= google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c= google.golang.org/grpc v1.20.1/go.mod h1:10oTOabMzJvdu6/UiuZezV6QK5dSlG84ov/aaiqXj38= @@ -497,7 +498,10 @@ google.golang.org/grpc v1.27.1/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8 google.golang.org/grpc v1.28.0/go.mod h1:rpkK4SK4GF4Ach/+MFLZUBavHOvF2JJB5uozKKal+60= google.golang.org/grpc v1.29.1/go.mod h1:itym6AZVZYACWQqET3MqgPpjcuV5QH3BxFS3IjizoKk= google.golang.org/grpc v1.30.0/go.mod h1:N36X2cJ7JwdamYAgDz+s+rVMFjt3numwzf/HckM8pak= +google.golang.org/grpc v1.31.0 h1:T7P4R73V3SSDPhH7WW7ATbfViLtmamH0DKrP3f9AuDI= google.golang.org/grpc v1.31.0/go.mod h1:N36X2cJ7JwdamYAgDz+s+rVMFjt3numwzf/HckM8pak= +google.golang.org/grpc v1.49.0 h1:WTLtQzmQori5FUH25Pq4WT22oCsv8USpQ+F6rqtsmxw= +google.golang.org/grpc v1.49.0/go.mod h1:ZgQEeidpAuNRZ8iRrlBKXZQP1ghovWIVhdJRyCDK+GI= google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8= google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0= google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM= @@ -511,6 +515,8 @@ google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlba google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= google.golang.org/protobuf v1.26.0 h1:bxAC2xTBsZGibn2RTntX0oH50xLsqy1OxA9tTL3p/lk= google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= +google.golang.org/protobuf v1.27.1 h1:SnqbnDw1V7RiZcXPx5MEeqPv2s79L9i7BJUlG/+RurQ= +google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo= diff --git a/pkg/grpc/client.go b/pkg/grpc/client.go new file mode 100644 index 000000000..ddc50bb13 --- /dev/null +++ b/pkg/grpc/client.go @@ -0,0 +1,76 @@ +package grpc + +// gRPC client implementation to be able to connect to the gRPC server and perform the following operations: +// - Start a remote JupyterLab server + +import ( + "context" + "fmt" + "strconv" + "time" + + "github.com/cli/cli/v2/pkg/grpc/jupyter" + "google.golang.org/grpc" + "google.golang.org/grpc/credentials/insecure" + "google.golang.org/grpc/metadata" +) + +const ( + requestTimeout = 30 * time.Second +) + +type GrpcClient struct { + conn *grpc.ClientConn + token string + jupyterClient jupyter.JupyterServerHostClient +} + +func New() *GrpcClient { + return &GrpcClient{} +} + +// Connects to the gRPC server on the given port +func (g *GrpcClient) Connect(ctx context.Context, port int, token string) error { + // Attempt to connect to the given port + conn, err := grpc.Dial(fmt.Sprintf("localhost:%d", port), grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithBlock()) + + if err != nil { + return fmt.Errorf("Failed to connect to the internal server on port %d", port) + } + + g.conn = conn + g.token = token + g.jupyterClient = jupyter.NewJupyterServerHostClient(conn) + + return nil +} + +// Appends the authentication token to the gRPC context +func (g *GrpcClient) appendMetadata(ctx context.Context) context.Context { + return metadata.AppendToOutgoingContext(ctx, "Authorization", "Bearer "+g.token) +} + +// Starts a remote JupyterLab server to allow the user to connect to the codespace via JupyterLab in their browser +func (g *GrpcClient) GetRunningServer() (int, string, error) { + ctx, cancel := context.WithTimeout(context.Background(), requestTimeout) + ctx = g.appendMetadata(ctx) + defer cancel() + + response, err := g.jupyterClient.GetRunningServer(ctx, &jupyter.GetRunningServerRequest{}) + + if err != nil { + return 0, "", fmt.Errorf("failed to invoke JupyterLab RPC: %w", err) + } + + if !response.Result { + return 0, "", fmt.Errorf("failed to start JupyterLab: %s", response.Message) + } + + port, err := strconv.Atoi(response.Port) + + if err != nil { + return 0, "", fmt.Errorf("failed to parse JupyterLab port: %w", err) + } + + return port, response.ServerUrl, err +} diff --git a/pkg/grpc/jupyter/JupyterServerHostService.v1.pb.go b/pkg/grpc/jupyter/JupyterServerHostService.v1.pb.go new file mode 100644 index 000000000..c48f3d0cf --- /dev/null +++ b/pkg/grpc/jupyter/JupyterServerHostService.v1.pb.go @@ -0,0 +1,241 @@ +// Code generated by protoc-gen-go. DO NOT EDIT. +// versions: +// protoc-gen-go v1.28.0 +// protoc v3.21.3 +// source: JupyterServerHostService.v1.proto + +package jupyter + +import ( + protoreflect "google.golang.org/protobuf/reflect/protoreflect" + protoimpl "google.golang.org/protobuf/runtime/protoimpl" + reflect "reflect" + sync "sync" +) + +const ( + // Verify that this generated code is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion) + // Verify that runtime/protoimpl is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20) +) + +type GetRunningServerRequest struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields +} + +func (x *GetRunningServerRequest) Reset() { + *x = GetRunningServerRequest{} + if protoimpl.UnsafeEnabled { + mi := &file_JupyterServerHostService_v1_proto_msgTypes[0] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *GetRunningServerRequest) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*GetRunningServerRequest) ProtoMessage() {} + +func (x *GetRunningServerRequest) ProtoReflect() protoreflect.Message { + mi := &file_JupyterServerHostService_v1_proto_msgTypes[0] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use GetRunningServerRequest.ProtoReflect.Descriptor instead. +func (*GetRunningServerRequest) Descriptor() ([]byte, []int) { + return file_JupyterServerHostService_v1_proto_rawDescGZIP(), []int{0} +} + +type GetRunningServerResponse struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Result bool `protobuf:"varint,1,opt,name=Result,proto3" json:"Result,omitempty"` + Message string `protobuf:"bytes,2,opt,name=Message,proto3" json:"Message,omitempty"` + Port string `protobuf:"bytes,3,opt,name=Port,proto3" json:"Port,omitempty"` + ServerUrl string `protobuf:"bytes,4,opt,name=ServerUrl,proto3" json:"ServerUrl,omitempty"` +} + +func (x *GetRunningServerResponse) Reset() { + *x = GetRunningServerResponse{} + if protoimpl.UnsafeEnabled { + mi := &file_JupyterServerHostService_v1_proto_msgTypes[1] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *GetRunningServerResponse) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*GetRunningServerResponse) ProtoMessage() {} + +func (x *GetRunningServerResponse) ProtoReflect() protoreflect.Message { + mi := &file_JupyterServerHostService_v1_proto_msgTypes[1] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use GetRunningServerResponse.ProtoReflect.Descriptor instead. +func (*GetRunningServerResponse) Descriptor() ([]byte, []int) { + return file_JupyterServerHostService_v1_proto_rawDescGZIP(), []int{1} +} + +func (x *GetRunningServerResponse) GetResult() bool { + if x != nil { + return x.Result + } + return false +} + +func (x *GetRunningServerResponse) GetMessage() string { + if x != nil { + return x.Message + } + return "" +} + +func (x *GetRunningServerResponse) GetPort() string { + if x != nil { + return x.Port + } + return "" +} + +func (x *GetRunningServerResponse) GetServerUrl() string { + if x != nil { + return x.ServerUrl + } + return "" +} + +var File_JupyterServerHostService_v1_proto protoreflect.FileDescriptor + +var file_JupyterServerHostService_v1_proto_rawDesc = []byte{ + 0x0a, 0x21, 0x4a, 0x75, 0x70, 0x79, 0x74, 0x65, 0x72, 0x53, 0x65, 0x72, 0x76, 0x65, 0x72, 0x48, + 0x6f, 0x73, 0x74, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x2e, 0x76, 0x31, 0x2e, 0x70, 0x72, + 0x6f, 0x74, 0x6f, 0x12, 0x2b, 0x43, 0x6f, 0x64, 0x65, 0x73, 0x70, 0x61, 0x63, 0x65, 0x73, 0x2e, + 0x47, 0x72, 0x70, 0x63, 0x2e, 0x4a, 0x75, 0x70, 0x79, 0x74, 0x65, 0x72, 0x53, 0x65, 0x72, 0x76, + 0x65, 0x72, 0x48, 0x6f, 0x73, 0x74, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x2e, 0x76, 0x31, + 0x22, 0x19, 0x0a, 0x17, 0x47, 0x65, 0x74, 0x52, 0x75, 0x6e, 0x6e, 0x69, 0x6e, 0x67, 0x53, 0x65, + 0x72, 0x76, 0x65, 0x72, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x22, 0x7e, 0x0a, 0x18, 0x47, + 0x65, 0x74, 0x52, 0x75, 0x6e, 0x6e, 0x69, 0x6e, 0x67, 0x53, 0x65, 0x72, 0x76, 0x65, 0x72, 0x52, + 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x16, 0x0a, 0x06, 0x52, 0x65, 0x73, 0x75, 0x6c, + 0x74, 0x18, 0x01, 0x20, 0x01, 0x28, 0x08, 0x52, 0x06, 0x52, 0x65, 0x73, 0x75, 0x6c, 0x74, 0x12, + 0x18, 0x0a, 0x07, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, + 0x52, 0x07, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x12, 0x12, 0x0a, 0x04, 0x50, 0x6f, 0x72, + 0x74, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x50, 0x6f, 0x72, 0x74, 0x12, 0x1c, 0x0a, + 0x09, 0x53, 0x65, 0x72, 0x76, 0x65, 0x72, 0x55, 0x72, 0x6c, 0x18, 0x04, 0x20, 0x01, 0x28, 0x09, + 0x52, 0x09, 0x53, 0x65, 0x72, 0x76, 0x65, 0x72, 0x55, 0x72, 0x6c, 0x32, 0xb5, 0x01, 0x0a, 0x11, + 0x4a, 0x75, 0x70, 0x79, 0x74, 0x65, 0x72, 0x53, 0x65, 0x72, 0x76, 0x65, 0x72, 0x48, 0x6f, 0x73, + 0x74, 0x12, 0x9f, 0x01, 0x0a, 0x10, 0x47, 0x65, 0x74, 0x52, 0x75, 0x6e, 0x6e, 0x69, 0x6e, 0x67, + 0x53, 0x65, 0x72, 0x76, 0x65, 0x72, 0x12, 0x44, 0x2e, 0x43, 0x6f, 0x64, 0x65, 0x73, 0x70, 0x61, + 0x63, 0x65, 0x73, 0x2e, 0x47, 0x72, 0x70, 0x63, 0x2e, 0x4a, 0x75, 0x70, 0x79, 0x74, 0x65, 0x72, + 0x53, 0x65, 0x72, 0x76, 0x65, 0x72, 0x48, 0x6f, 0x73, 0x74, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, + 0x65, 0x2e, 0x76, 0x31, 0x2e, 0x47, 0x65, 0x74, 0x52, 0x75, 0x6e, 0x6e, 0x69, 0x6e, 0x67, 0x53, + 0x65, 0x72, 0x76, 0x65, 0x72, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x45, 0x2e, 0x43, + 0x6f, 0x64, 0x65, 0x73, 0x70, 0x61, 0x63, 0x65, 0x73, 0x2e, 0x47, 0x72, 0x70, 0x63, 0x2e, 0x4a, + 0x75, 0x70, 0x79, 0x74, 0x65, 0x72, 0x53, 0x65, 0x72, 0x76, 0x65, 0x72, 0x48, 0x6f, 0x73, 0x74, + 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x2e, 0x76, 0x31, 0x2e, 0x47, 0x65, 0x74, 0x52, 0x75, + 0x6e, 0x6e, 0x69, 0x6e, 0x67, 0x53, 0x65, 0x72, 0x76, 0x65, 0x72, 0x52, 0x65, 0x73, 0x70, 0x6f, + 0x6e, 0x73, 0x65, 0x42, 0x0b, 0x5a, 0x09, 0x2e, 0x2f, 0x6a, 0x75, 0x70, 0x79, 0x74, 0x65, 0x72, + 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, +} + +var ( + file_JupyterServerHostService_v1_proto_rawDescOnce sync.Once + file_JupyterServerHostService_v1_proto_rawDescData = file_JupyterServerHostService_v1_proto_rawDesc +) + +func file_JupyterServerHostService_v1_proto_rawDescGZIP() []byte { + file_JupyterServerHostService_v1_proto_rawDescOnce.Do(func() { + file_JupyterServerHostService_v1_proto_rawDescData = protoimpl.X.CompressGZIP(file_JupyterServerHostService_v1_proto_rawDescData) + }) + return file_JupyterServerHostService_v1_proto_rawDescData +} + +var file_JupyterServerHostService_v1_proto_msgTypes = make([]protoimpl.MessageInfo, 2) +var file_JupyterServerHostService_v1_proto_goTypes = []interface{}{ + (*GetRunningServerRequest)(nil), // 0: Codespaces.Grpc.JupyterServerHostService.v1.GetRunningServerRequest + (*GetRunningServerResponse)(nil), // 1: Codespaces.Grpc.JupyterServerHostService.v1.GetRunningServerResponse +} +var file_JupyterServerHostService_v1_proto_depIdxs = []int32{ + 0, // 0: Codespaces.Grpc.JupyterServerHostService.v1.JupyterServerHost.GetRunningServer:input_type -> Codespaces.Grpc.JupyterServerHostService.v1.GetRunningServerRequest + 1, // 1: Codespaces.Grpc.JupyterServerHostService.v1.JupyterServerHost.GetRunningServer:output_type -> Codespaces.Grpc.JupyterServerHostService.v1.GetRunningServerResponse + 1, // [1:2] is the sub-list for method output_type + 0, // [0:1] is the sub-list for method input_type + 0, // [0:0] is the sub-list for extension type_name + 0, // [0:0] is the sub-list for extension extendee + 0, // [0:0] is the sub-list for field type_name +} + +func init() { file_JupyterServerHostService_v1_proto_init() } +func file_JupyterServerHostService_v1_proto_init() { + if File_JupyterServerHostService_v1_proto != nil { + return + } + if !protoimpl.UnsafeEnabled { + file_JupyterServerHostService_v1_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*GetRunningServerRequest); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_JupyterServerHostService_v1_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*GetRunningServerResponse); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + } + type x struct{} + out := protoimpl.TypeBuilder{ + File: protoimpl.DescBuilder{ + GoPackagePath: reflect.TypeOf(x{}).PkgPath(), + RawDescriptor: file_JupyterServerHostService_v1_proto_rawDesc, + NumEnums: 0, + NumMessages: 2, + NumExtensions: 0, + NumServices: 1, + }, + GoTypes: file_JupyterServerHostService_v1_proto_goTypes, + DependencyIndexes: file_JupyterServerHostService_v1_proto_depIdxs, + MessageInfos: file_JupyterServerHostService_v1_proto_msgTypes, + }.Build() + File_JupyterServerHostService_v1_proto = out.File + file_JupyterServerHostService_v1_proto_rawDesc = nil + file_JupyterServerHostService_v1_proto_goTypes = nil + file_JupyterServerHostService_v1_proto_depIdxs = nil +} diff --git a/pkg/grpc/jupyter/JupyterServerHostService.v1.proto b/pkg/grpc/jupyter/JupyterServerHostService.v1.proto new file mode 100644 index 000000000..337e7cf41 --- /dev/null +++ b/pkg/grpc/jupyter/JupyterServerHostService.v1.proto @@ -0,0 +1,19 @@ +syntax = "proto3"; + +option go_package = "./jupyter"; + +package Codespaces.Grpc.JupyterServerHostService.v1; + +service JupyterServerHost { + rpc GetRunningServer (GetRunningServerRequest) returns (GetRunningServerResponse); +} + +message GetRunningServerRequest { +} + +message GetRunningServerResponse { + bool Result = 1; + string Message = 2; + string Port = 3; + string ServerUrl = 4; +} diff --git a/pkg/grpc/jupyter/JupyterServerHostService.v1_grpc.pb.go b/pkg/grpc/jupyter/JupyterServerHostService.v1_grpc.pb.go new file mode 100644 index 000000000..8ba53014d --- /dev/null +++ b/pkg/grpc/jupyter/JupyterServerHostService.v1_grpc.pb.go @@ -0,0 +1,105 @@ +// Code generated by protoc-gen-go-grpc. DO NOT EDIT. +// versions: +// - protoc-gen-go-grpc v1.2.0 +// - protoc v3.21.3 +// source: JupyterServerHostService.v1.proto + +package jupyter + +import ( + context "context" + grpc "google.golang.org/grpc" + codes "google.golang.org/grpc/codes" + status "google.golang.org/grpc/status" +) + +// This is a compile-time assertion to ensure that this generated file +// is compatible with the grpc package it is being compiled against. +// Requires gRPC-Go v1.32.0 or later. +const _ = grpc.SupportPackageIsVersion7 + +// JupyterServerHostClient is the client API for JupyterServerHost service. +// +// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://pkg.go.dev/google.golang.org/grpc/?tab=doc#ClientConn.NewStream. +type JupyterServerHostClient interface { + GetRunningServer(ctx context.Context, in *GetRunningServerRequest, opts ...grpc.CallOption) (*GetRunningServerResponse, error) +} + +type jupyterServerHostClient struct { + cc grpc.ClientConnInterface +} + +func NewJupyterServerHostClient(cc grpc.ClientConnInterface) JupyterServerHostClient { + return &jupyterServerHostClient{cc} +} + +func (c *jupyterServerHostClient) GetRunningServer(ctx context.Context, in *GetRunningServerRequest, opts ...grpc.CallOption) (*GetRunningServerResponse, error) { + out := new(GetRunningServerResponse) + err := c.cc.Invoke(ctx, "/Codespaces.Grpc.JupyterServerHostService.v1.JupyterServerHost/GetRunningServer", in, out, opts...) + if err != nil { + return nil, err + } + return out, nil +} + +// JupyterServerHostServer is the server API for JupyterServerHost service. +// All implementations must embed UnimplementedJupyterServerHostServer +// for forward compatibility +type JupyterServerHostServer interface { + GetRunningServer(context.Context, *GetRunningServerRequest) (*GetRunningServerResponse, error) + mustEmbedUnimplementedJupyterServerHostServer() +} + +// UnimplementedJupyterServerHostServer must be embedded to have forward compatible implementations. +type UnimplementedJupyterServerHostServer struct { +} + +func (UnimplementedJupyterServerHostServer) GetRunningServer(context.Context, *GetRunningServerRequest) (*GetRunningServerResponse, error) { + return nil, status.Errorf(codes.Unimplemented, "method GetRunningServer not implemented") +} +func (UnimplementedJupyterServerHostServer) mustEmbedUnimplementedJupyterServerHostServer() {} + +// UnsafeJupyterServerHostServer may be embedded to opt out of forward compatibility for this service. +// Use of this interface is not recommended, as added methods to JupyterServerHostServer will +// result in compilation errors. +type UnsafeJupyterServerHostServer interface { + mustEmbedUnimplementedJupyterServerHostServer() +} + +func RegisterJupyterServerHostServer(s grpc.ServiceRegistrar, srv JupyterServerHostServer) { + s.RegisterService(&JupyterServerHost_ServiceDesc, srv) +} + +func _JupyterServerHost_GetRunningServer_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(GetRunningServerRequest) + if err := dec(in); err != nil { + return nil, err + } + if interceptor == nil { + return srv.(JupyterServerHostServer).GetRunningServer(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: "/Codespaces.Grpc.JupyterServerHostService.v1.JupyterServerHost/GetRunningServer", + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(JupyterServerHostServer).GetRunningServer(ctx, req.(*GetRunningServerRequest)) + } + return interceptor(ctx, in, info, handler) +} + +// JupyterServerHost_ServiceDesc is the grpc.ServiceDesc for JupyterServerHost service. +// It's only intended for direct use with grpc.RegisterService, +// and not to be introspected or modified (even as a copy) +var JupyterServerHost_ServiceDesc = grpc.ServiceDesc{ + ServiceName: "Codespaces.Grpc.JupyterServerHostService.v1.JupyterServerHost", + HandlerType: (*JupyterServerHostServer)(nil), + Methods: []grpc.MethodDesc{ + { + MethodName: "GetRunningServer", + Handler: _JupyterServerHost_GetRunningServer_Handler, + }, + }, + Streams: []grpc.StreamDesc{}, + Metadata: "JupyterServerHostService.v1.proto", +} diff --git a/pkg/liveshare/client.go b/pkg/liveshare/client.go index 570e8615c..2fff9c766 100644 --- a/pkg/liveshare/client.go +++ b/pkg/liveshare/client.go @@ -15,13 +15,20 @@ import ( "crypto/tls" "errors" "fmt" + "net" "net/url" "strings" "time" + "github.com/cli/cli/v2/pkg/grpc" "github.com/opentracing/opentracing-go" ) +const ( + codespacesInternalPort = 16634 + codespacesInternalSessionName = "CodespacesInternal" +) + type logger interface { Println(v ...interface{}) Printf(f string, v ...interface{}) @@ -112,15 +119,49 @@ func Connect(ctx context.Context, opts Options) (*Session, error) { s := &Session{ ssh: ssh, rpc: rpc, + grpc: grpc.New(), clientName: opts.ClientName, keepAliveReason: make(chan string, 1), logger: opts.Logger, } go s.heartbeat(ctx, 1*time.Minute) + // Connect to the gRPC server so we can make requests anywhere we have access to the session + s.connectToGrpcServer(ctx, opts.SessionToken) + return s, nil } +// Connects to the gRPC server running on the host VM +func (s *Session) connectToGrpcServer(ctx context.Context, token string) error { + listen, err := net.Listen("tcp", fmt.Sprintf("127.0.0.1:%d", 0)) + if err != nil { + return err + } + + // Tunnel the remote gRPC server port to the local port + localGrpcServerPort := listen.Addr().(*net.TCPAddr).Port + internalTunnelClosed := make(chan error, 1) + go func() { + fwd := NewPortForwarder(s, codespacesInternalSessionName, codespacesInternalPort, true) + internalTunnelClosed <- fwd.ForwardToListener(ctx, listen) + }() + + // Make a connection to the gRPC server + err = s.grpc.Connect(ctx, localGrpcServerPort, token) + + if err != nil { + return err + } + + select { + case err := <-internalTunnelClosed: + return fmt.Errorf("internal tunnel closed: %w", err) + default: + return nil // success + } +} + type clientCapabilities struct { IsNonInteractive bool `json:"isNonInteractive"` } diff --git a/pkg/liveshare/session.go b/pkg/liveshare/session.go index f912fa5ea..b1c5ab7ca 100644 --- a/pkg/liveshare/session.go +++ b/pkg/liveshare/session.go @@ -8,6 +8,7 @@ import ( "strings" "time" + "github.com/cli/cli/v2/pkg/grpc" "github.com/opentracing/opentracing-go" "golang.org/x/crypto/ssh" "golang.org/x/sync/errgroup" @@ -21,8 +22,9 @@ type ChannelID struct { // A Session represents the session between a connected Live Share client and server. type Session struct { - ssh *sshSession - rpc *rpcClient + ssh *sshSession + rpc *rpcClient + grpc *grpc.GrpcClient clientName string keepAliveReason chan string @@ -100,27 +102,7 @@ func (s *Session) StartSSHServerWithOptions(ctx context.Context, options StartSS // StartJupyterServer starts a Juypyter server in the container and returns // the port on which it listens and the server URL. func (s *Session) StartJupyterServer(ctx context.Context) (int, string, error) { - var response struct { - Result bool `json:"result"` - Message string `json:"message"` - Port string `json:"port"` - ServerUrl string `json:"serverUrl"` - } - - if err := s.rpc.do(ctx, "IJupyterServerHostService.getRunningServer", []string{}, &response); err != nil { - return 0, "", fmt.Errorf("failed to invoke JupyterLab RPC: %w", err) - } - - if !response.Result { - return 0, "", fmt.Errorf("failed to start JupyterLab: %s", response.Message) - } - - port, err := strconv.Atoi(response.Port) - if err != nil { - return 0, "", fmt.Errorf("failed to parse JupyterLab port: %w", err) - } - - return port, response.ServerUrl, nil + return s.grpc.GetRunningServer() } // heartbeat runs until context cancellation, periodically checking whether there is a