mirror of
https://github.com/ProtonMail/proton-bridge.git
synced 2025-12-15 14:56:42 +00:00
feat(GODT-2255): Randomize the focus service port.
This commit is contained in:
@ -21,9 +21,11 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/Masterminds/semver/v3"
|
||||
"github.com/ProtonMail/proton-bridge/v3/internal/focus/proto"
|
||||
"github.com/ProtonMail/proton-bridge/v3/internal/service"
|
||||
"github.com/sirupsen/logrus"
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/credentials/insecure"
|
||||
@ -32,10 +34,10 @@ import (
|
||||
|
||||
// TryRaise tries to raise the application by dialing the focus service.
|
||||
// It returns true if the service is running and the application was told to raise.
|
||||
func TryRaise() bool {
|
||||
func TryRaise(settingsPath string) bool {
|
||||
var raised bool
|
||||
|
||||
if err := withClientConn(context.Background(), func(ctx context.Context, client proto.FocusClient) error {
|
||||
if err := withClientConn(context.Background(), settingsPath, func(ctx context.Context, client proto.FocusClient) error {
|
||||
if _, err := client.Raise(ctx, &emptypb.Empty{}); err != nil {
|
||||
return fmt.Errorf("failed to call client.Raise: %w", err)
|
||||
}
|
||||
@ -53,10 +55,10 @@ func TryRaise() bool {
|
||||
|
||||
// TryVersion tries to determine the version of the running application instance.
|
||||
// It returns the version and true if the version could be determined.
|
||||
func TryVersion() (*semver.Version, bool) {
|
||||
func TryVersion(settingsPath string) (*semver.Version, bool) {
|
||||
var version *semver.Version
|
||||
|
||||
if err := withClientConn(context.Background(), func(ctx context.Context, client proto.FocusClient) error {
|
||||
if err := withClientConn(context.Background(), settingsPath, func(ctx context.Context, client proto.FocusClient) error {
|
||||
raw, err := client.Version(ctx, &emptypb.Empty{})
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to call client.Version: %w", err)
|
||||
@ -78,10 +80,15 @@ func TryVersion() (*semver.Version, bool) {
|
||||
return version, true
|
||||
}
|
||||
|
||||
func withClientConn(ctx context.Context, fn func(context.Context, proto.FocusClient) error) error {
|
||||
func withClientConn(ctx context.Context, settingsPath string, fn func(context.Context, proto.FocusClient) error) error {
|
||||
var config = service.Config{}
|
||||
err := config.Load(filepath.Join(settingsPath, serverConfigFileName))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
cc, err := grpc.DialContext(
|
||||
ctx,
|
||||
net.JoinHostPort(Host, fmt.Sprint(Port)),
|
||||
net.JoinHostPort(Host, fmt.Sprint(config.Port)),
|
||||
grpc.WithTransportCredentials(insecure.NewCredentials()),
|
||||
)
|
||||
if err != nil {
|
||||
|
||||
@ -18,19 +18,25 @@
|
||||
package focus
|
||||
|
||||
import (
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/Masterminds/semver/v3"
|
||||
"github.com/ProtonMail/proton-bridge/v3/internal/locations"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestFocus_Raise(t *testing.T) {
|
||||
tmpDir := t.TempDir()
|
||||
locations := locations.New(newTestLocationsProvider(tmpDir), "config-name")
|
||||
// Start the focus service.
|
||||
service, err := NewService(semver.MustParse("1.2.3"))
|
||||
service, err := NewService(locations, semver.MustParse("1.2.3"))
|
||||
require.NoError(t, err)
|
||||
|
||||
settingsFolder, err := locations.ProvideSettingsPath()
|
||||
require.NoError(t, err)
|
||||
// Try to dial it, it should succeed.
|
||||
require.True(t, TryRaise())
|
||||
require.True(t, TryRaise(settingsFolder))
|
||||
|
||||
// The service should report a raise call.
|
||||
<-service.GetRaiseCh()
|
||||
@ -39,16 +45,60 @@ func TestFocus_Raise(t *testing.T) {
|
||||
service.Close()
|
||||
|
||||
// Try to dial it, it should fail.
|
||||
require.False(t, TryRaise())
|
||||
require.False(t, TryRaise(settingsFolder))
|
||||
}
|
||||
|
||||
func TestFocus_Version(t *testing.T) {
|
||||
tmpDir := t.TempDir()
|
||||
locations := locations.New(newTestLocationsProvider(tmpDir), "config-name")
|
||||
// Start the focus service.
|
||||
_, err := NewService(semver.MustParse("1.2.3"))
|
||||
_, err := NewService(locations, semver.MustParse("1.2.3"))
|
||||
require.NoError(t, err)
|
||||
|
||||
settingsFolder, err := locations.ProvideSettingsPath()
|
||||
require.NoError(t, err)
|
||||
|
||||
// Try to dial it, it should succeed.
|
||||
version, ok := TryVersion()
|
||||
version, ok := TryVersion(settingsFolder)
|
||||
require.True(t, ok)
|
||||
require.Equal(t, "1.2.3", version.String())
|
||||
}
|
||||
|
||||
type TestLocationsProvider struct {
|
||||
config, data, cache string
|
||||
}
|
||||
|
||||
func newTestLocationsProvider(dir string) *TestLocationsProvider {
|
||||
config, err := os.MkdirTemp(dir, "config")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
data, err := os.MkdirTemp(dir, "data")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
cache, err := os.MkdirTemp(dir, "cache")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return &TestLocationsProvider{
|
||||
config: config,
|
||||
data: data,
|
||||
cache: cache,
|
||||
}
|
||||
}
|
||||
|
||||
func (provider *TestLocationsProvider) UserConfig() string {
|
||||
return provider.config
|
||||
}
|
||||
|
||||
func (provider *TestLocationsProvider) UserData() string {
|
||||
return provider.data
|
||||
}
|
||||
|
||||
func (provider *TestLocationsProvider) UserCache() string {
|
||||
return provider.cache
|
||||
}
|
||||
|
||||
@ -25,16 +25,16 @@ import (
|
||||
|
||||
"github.com/Masterminds/semver/v3"
|
||||
"github.com/ProtonMail/proton-bridge/v3/internal/focus/proto"
|
||||
"github.com/ProtonMail/proton-bridge/v3/internal/service"
|
||||
"github.com/sirupsen/logrus"
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/protobuf/types/known/emptypb"
|
||||
)
|
||||
|
||||
// Host is the local host to listen on.
|
||||
const Host = "127.0.0.1"
|
||||
|
||||
// Port is the port to listen on.
|
||||
var Port = 1042 // nolint:gochecknoglobals
|
||||
const (
|
||||
Host = "127.0.0.1"
|
||||
serverConfigFileName = "grpcFocusServerConfig.json"
|
||||
)
|
||||
|
||||
// Service is a gRPC service that can be used to raise the application.
|
||||
type Service struct {
|
||||
@ -47,26 +47,39 @@ type Service struct {
|
||||
|
||||
// NewService creates a new focus service.
|
||||
// It listens on the local host and port 1042 (by default).
|
||||
func NewService(version *semver.Version) (*Service, error) {
|
||||
service := &Service{
|
||||
func NewService(locator service.Locator, version *semver.Version) (*Service, error) {
|
||||
serv := &Service{
|
||||
server: grpc.NewServer(),
|
||||
raiseCh: make(chan struct{}, 1),
|
||||
version: version,
|
||||
}
|
||||
|
||||
proto.RegisterFocusServer(service.server, service)
|
||||
proto.RegisterFocusServer(serv.server, serv)
|
||||
|
||||
if listener, err := net.Listen("tcp", net.JoinHostPort(Host, fmt.Sprint(Port))); err != nil {
|
||||
logrus.WithError(err).Warn("Failed to start focus service")
|
||||
if listener, err := net.Listen("tcp", net.JoinHostPort(Host, fmt.Sprint(0))); err != nil {
|
||||
logrus.WithError(err).Warn("Failed to start focus serv")
|
||||
} else {
|
||||
config := service.Config{}
|
||||
// retrieve the port assigned by the system, so that we can put it in the config file.
|
||||
address, ok := listener.Addr().(*net.TCPAddr)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("could not retrieve gRPC service listener address")
|
||||
}
|
||||
config.Port = address.Port
|
||||
if path, err := service.SaveGRPCServerConfigFile(locator, &config, serverConfigFileName); err != nil {
|
||||
logrus.WithError(err).WithField("path", path).Warn("Could not write focus gRPC service config file")
|
||||
} else {
|
||||
logrus.WithField("path", path).Info("Successfully saved gRPC Focus service config file")
|
||||
}
|
||||
|
||||
go func() {
|
||||
if err := service.server.Serve(listener); err != nil {
|
||||
if err := serv.server.Serve(listener); err != nil {
|
||||
fmt.Printf("failed to serve: %v", err)
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
return service, nil
|
||||
return serv, nil
|
||||
}
|
||||
|
||||
// Raise implements the gRPC FocusService interface; it raises the application.
|
||||
|
||||
Reference in New Issue
Block a user