boxdd: Add insecure mode
This commit is contained in:
@@ -1,42 +0,0 @@
|
||||
package daemon
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"os"
|
||||
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
)
|
||||
|
||||
func UnaryErrorInterceptor(ctx context.Context, request any, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (any, error) {
|
||||
response, err := handler(ctx, request)
|
||||
if err != nil {
|
||||
return nil, mapStatusError(err)
|
||||
}
|
||||
return response, nil
|
||||
}
|
||||
|
||||
func StreamErrorInterceptor(server any, stream grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
|
||||
err := handler(server, stream)
|
||||
if err != nil {
|
||||
return mapStatusError(err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func mapStatusError(err error) error {
|
||||
if _, loaded := status.FromError(err); loaded {
|
||||
return err
|
||||
}
|
||||
switch {
|
||||
case errors.Is(err, os.ErrInvalid):
|
||||
return status.Error(codes.FailedPrecondition, "service not started")
|
||||
case errors.Is(err, os.ErrClosed):
|
||||
return status.Error(codes.Unavailable, "service is closing")
|
||||
case errors.Is(err, context.Canceled), errors.Is(err, context.DeadlineExceeded):
|
||||
return status.FromContextError(err).Err()
|
||||
}
|
||||
return err
|
||||
}
|
||||
+2
-2
@@ -15,8 +15,8 @@ import (
|
||||
|
||||
func NewServer(startedService *StartedService, secret string) *grpc.Server {
|
||||
server := grpc.NewServer(
|
||||
grpc.ChainUnaryInterceptor(newUnaryAuthInterceptor(secret), UnaryErrorInterceptor),
|
||||
grpc.ChainStreamInterceptor(newStreamAuthInterceptor(secret), StreamErrorInterceptor),
|
||||
grpc.ChainUnaryInterceptor(newUnaryAuthInterceptor(secret)),
|
||||
grpc.ChainStreamInterceptor(newStreamAuthInterceptor(secret)),
|
||||
)
|
||||
healthServer := health.NewServer()
|
||||
RegisterStartedServiceServer(server, startedService)
|
||||
|
||||
@@ -196,6 +196,7 @@ func (s *StartedService) StartOrReloadService(profileContent string, options *Ov
|
||||
}
|
||||
oldInstance := s.instance
|
||||
if oldInstance != nil {
|
||||
s.instance = nil
|
||||
s.updateStatus(ServiceStatus_STOPPING)
|
||||
s.serviceAccess.Unlock()
|
||||
_ = oldInstance.Close()
|
||||
@@ -221,6 +222,8 @@ func (s *StartedService) StartOrReloadService(profileContent string, options *Ov
|
||||
return nil
|
||||
}
|
||||
if err != nil {
|
||||
s.instance = nil
|
||||
_ = instance.Close()
|
||||
return s.updateStatusError(err)
|
||||
}
|
||||
s.startedAt = time.Now()
|
||||
@@ -243,16 +246,13 @@ func (s *StartedService) CloseService() error {
|
||||
case ServiceStatus_STARTING, ServiceStatus_STARTED:
|
||||
default:
|
||||
s.serviceAccess.Unlock()
|
||||
return os.ErrInvalid
|
||||
return nil
|
||||
}
|
||||
s.updateStatus(ServiceStatus_STOPPING)
|
||||
instance := s.instance
|
||||
s.instance = nil
|
||||
if instance != nil {
|
||||
err := instance.Close()
|
||||
if err != nil {
|
||||
return s.updateStatusError(err)
|
||||
}
|
||||
_ = instance.Close()
|
||||
}
|
||||
s.startedAt = time.Time{}
|
||||
s.updateStatus(ServiceStatus_IDLE)
|
||||
|
||||
@@ -29,7 +29,7 @@ func (s *StartedService) ProvideUSBDevices(server grpc.BidiStreamingServer[USBPr
|
||||
instance := s.instance
|
||||
s.serviceAccess.RUnlock()
|
||||
if instance == nil {
|
||||
return E.New("service not started")
|
||||
return nil
|
||||
}
|
||||
serviceManager := service.FromContext[adapter.ServiceManager](instance.ctx)
|
||||
if serviceManager == nil {
|
||||
@@ -122,7 +122,7 @@ func (s *StartedService) SubscribeUSBIPServerStatus(
|
||||
instance := s.instance
|
||||
s.serviceAccess.RUnlock()
|
||||
if instance == nil {
|
||||
return E.New("service not started")
|
||||
return nil
|
||||
}
|
||||
serviceManager := service.FromContext[adapter.ServiceManager](instance.ctx)
|
||||
if serviceManager == nil {
|
||||
|
||||
Reference in New Issue
Block a user