Path: blob/main/components/local-app/pkg/bastion/service.go
2500 views
// Copyright (c) 2021 Gitpod GmbH. All rights reserved.1// Licensed under the GNU Affero General Public License (AGPL).2// See License.AGPL.txt in the project root for license information.34package bastion56import (7"context"89"google.golang.org/grpc/codes"10"google.golang.org/grpc/status"1112"github.com/gitpod-io/gitpod/local-app/api"13)1415type LocalAppService struct {16b *Bastion17s *SSHConfigWritingCallback18api.UnimplementedLocalAppServer19}2021func NewLocalAppService(b *Bastion, s *SSHConfigWritingCallback) *LocalAppService {22return &LocalAppService{23b: b,24s: s,25}26}2728func (s *LocalAppService) TunnelStatus(req *api.TunnelStatusRequest, srv api.LocalApp_TunnelStatusServer) error {29if !req.Observe {30return srv.Send(&api.TunnelStatusResponse{31Tunnels: s.b.Status(req.InstanceId),32})33}3435sub, err := s.b.Subscribe(req.InstanceId)36if err == ErrTooManySubscriptions {37return status.Error(codes.ResourceExhausted, "too many subscriptions")38}39if err != nil {40return status.Error(codes.Internal, err.Error())41}42defer sub.Close()4344for {45select {46case <-srv.Context().Done():47return nil48case update := <-sub.Updates():49if update == nil {50return nil51}52err := srv.Send(&api.TunnelStatusResponse{53Tunnels: update,54})55if err != nil {56return err57}58}59}60}6162func (s *LocalAppService) AutoTunnel(ctx context.Context, req *api.AutoTunnelRequest) (*api.AutoTunnelResponse, error) {63s.b.AutoTunnel(req.InstanceId, req.Enabled)64return &api.AutoTunnelResponse{}, nil65}6667func (s *LocalAppService) ResolveSSHConnection(ctx context.Context, req *api.ResolveSSHConnectionRequest) (*api.ResolveSSHConnectionResponse, error) {68ws := s.b.Update(req.WorkspaceId)69if ws == nil || ws.InstanceID != req.InstanceId {70return nil, status.Error(codes.NotFound, "workspace not found")71}72if ws.localSSHListener == nil {73return nil, status.Error(codes.NotFound, "workspace ssh tunnel not configured")74}75return &api.ResolveSSHConnectionResponse{76Host: ws.WorkspaceID,77ConfigFile: s.s.Path,78}, nil79}808182