GODT-1917: gRPC service should use random port.

WIP: bridge-gui wait and parse gRPC service config fie.
This commit is contained in:
Xavier Michelon
2022-09-30 11:12:56 +02:00
parent d1cbf4f06c
commit 20c802a1e5
15 changed files with 423 additions and 15 deletions

View File

@ -22,7 +22,9 @@ package grpc
import (
"context"
cryptotls "crypto/tls"
"fmt"
"net"
"path/filepath"
"strings"
"sync"
"time"
@ -31,6 +33,7 @@ import (
"github.com/ProtonMail/proton-bridge/v2/internal/config/settings"
"github.com/ProtonMail/proton-bridge/v2/internal/events"
"github.com/ProtonMail/proton-bridge/v2/internal/frontend/types"
"github.com/ProtonMail/proton-bridge/v2/internal/locations"
"github.com/ProtonMail/proton-bridge/v2/internal/updater"
"github.com/ProtonMail/proton-bridge/v2/internal/users"
"github.com/ProtonMail/proton-bridge/v2/pkg/keychain"
@ -43,6 +46,10 @@ import (
"google.golang.org/protobuf/types/known/emptypb"
)
const (
serviceConfigFileName = "grpcServiceConfig.json"
)
// Service is the RPC service struct.
type Service struct { // nolint:structcheck
UnimplementedBridgeServer
@ -68,6 +75,7 @@ type Service struct { // nolint:structcheck
initializing sync.WaitGroup
initializationDone sync.Once
firstTimeAutostart sync.Once
locations *locations.Locations
}
// NewService returns a new instance of the service.
@ -78,6 +86,7 @@ func NewService(
updater types.Updater,
bridge types.Bridger,
restarter types.Restarter,
locations *locations.Locations,
) *Service {
s := Service{
UnimplementedBridgeServer: UnimplementedBridgeServer{},
@ -92,6 +101,7 @@ func NewService(
initializing: sync.WaitGroup{},
initializationDone: sync.Once{},
firstTimeAutostart: sync.Once{},
locations: locations,
}
// Initializing.Done is only called sync.Once. Please keep the increment
@ -111,12 +121,18 @@ func NewService(
RegisterBridgeServer(s.grpcServer, &s)
s.listener, err = net.Listen("tcp", "127.0.0.1:9292") // Port should be configurable from the command-line.
s.listener, err = net.Listen("tcp", "127.0.0.1:0") // Port 0 means that the port is randomly picked by the system.
if err != nil {
s.log.WithError(err).Error("could not create listener")
panic(err)
s.log.WithError(err).Panic("could not create listener")
}
if err := s.saveGRPCServerConfigFile(); err != nil {
s.log.WithError(err).Panic("could not write gRPC service configuration file")
}
s.log.Info("gRPC server listening at ", s.listener.Addr())
return &s
}
@ -387,3 +403,19 @@ func (s *Service) installUpdate() {
_ = s.SendEvent(NewUpdateSilentRestartNeededEvent())
}
func (s *Service) saveGRPCServerConfigFile() error {
address, ok := s.listener.Addr().(*net.TCPAddr)
if !ok {
return fmt.Errorf("could not retrieve gRPC service listener address")
}
sc := config{Port: address.Port}
settingsPath, err := s.locations.ProvideSettingsPath()
if err != nil {
return err
}
return sc.save(filepath.Join(settingsPath, serviceConfigFileName))
}