Fix security check message

This commit is contained in:
世界
2026-07-20 22:00:35 +08:00
parent 15be1ee2f0
commit 5cad5ad42d
12 changed files with 50 additions and 30 deletions
+2 -2
View File
@@ -7,7 +7,7 @@ import (
)
type SecurityPolicy interface {
CheckFeature(feature string) error
CheckFeature(ctx context.Context, feature string) error
}
func CheckSecurityFeature(ctx context.Context, feature string) error {
@@ -15,5 +15,5 @@ func CheckSecurityFeature(ctx context.Context, feature string) error {
if policy == nil {
return nil
}
return policy.CheckFeature(feature)
return policy.CheckFeature(ctx, feature)
}
+14 -7
View File
@@ -10,6 +10,7 @@ import (
"github.com/sagernet/sing-box/common/urltest"
C "github.com/sagernet/sing-box/constant"
"github.com/sagernet/sing-box/experimental/deprecated"
"github.com/sagernet/sing-box/experimental/locale"
"github.com/sagernet/sing-box/log"
"github.com/sagernet/sing-box/option"
"github.com/sagernet/sing/common"
@@ -34,12 +35,14 @@ type Instance struct {
logFactory log.Factory
}
func (s *StartedService) CheckConfig(configContent string) error {
options, err := parseConfig(s.ctx, configContent)
func (s *StartedService) CheckConfig(ctx context.Context, configContent string) error {
selectedLocale := locale.FromContext(ctx)
ctx, _ = locale.ContextWithLocale(s.ctx, selectedLocale.Locale)
options, err := parseConfig(ctx, configContent)
if err != nil {
return err
}
ctx, cancel := context.WithCancel(s.ctx)
ctx, cancel := context.WithCancel(ctx)
defer cancel()
instance, err := box.New(box.Options{
Context: ctx,
@@ -51,8 +54,10 @@ func (s *StartedService) CheckConfig(configContent string) error {
return err
}
func (s *StartedService) FormatConfig(configContent string) (string, error) {
options, err := parseConfig(s.ctx, configContent)
func (s *StartedService) FormatConfig(ctx context.Context, configContent string) (string, error) {
selectedLocale := locale.FromContext(ctx)
ctx, _ = locale.ContextWithLocale(s.ctx, selectedLocale.Locale)
options, err := parseConfig(ctx, configContent)
if err != nil {
return "", err
}
@@ -72,8 +77,10 @@ type OverrideOptions struct {
ExcludePackage []string
}
func (s *StartedService) newInstance(profileContent string, overrideOptions *OverrideOptions) (*Instance, error) {
ctx := service.ExtendContext(s.ctx)
func (s *StartedService) newInstance(ctx context.Context, profileContent string, overrideOptions *OverrideOptions) (*Instance, error) {
selectedLocale := locale.FromContext(ctx)
ctx, _ = locale.ContextWithLocale(s.ctx, selectedLocale.Locale)
ctx = service.ExtendContext(ctx)
service.MustRegister[deprecated.Manager](ctx, new(deprecatedManager))
ctx, cancel := context.WithCancel(ctx)
options, err := parseConfig(ctx, profileContent)
+1 -1
View File
@@ -44,7 +44,7 @@ func (s *ManagedService) StopService(ctx context.Context, empty *emptypb.Empty)
}
func (s *ManagedService) ReloadService(ctx context.Context, empty *emptypb.Empty) (*emptypb.Empty, error) {
err := s.handler.ServiceReload()
err := s.handler.ServiceReload(ctx)
if err != nil {
return nil, err
}
+3 -1
View File
@@ -1,5 +1,7 @@
package daemon
import "context"
type PlatformHandler interface {
WriteDebugMessage(message string)
ConnectSSHAgent() (int32, error)
@@ -7,7 +9,7 @@ type PlatformHandler interface {
type ManagedHandler interface {
ServiceStop() error
ServiceReload() error
ServiceReload(ctx context.Context) error
SystemProxyStatus() (*SystemProxyStatus, error)
SetSystemProxyEnabled(enabled bool) error
TriggerNativeCrash() error
+4 -3
View File
@@ -188,7 +188,7 @@ func (s *StartedService) waitForStarted(ctx context.Context) error {
}
}
func (s *StartedService) StartOrReloadService(profileContent string, options *OverrideOptions) error {
func (s *StartedService) StartOrReloadService(ctx context.Context, profileContent string, options *OverrideOptions) error {
s.serviceAccess.Lock()
switch s.serviceStatus.Status {
case ServiceStatus_IDLE, ServiceStatus_STARTED, ServiceStatus_STARTING, ServiceStatus_FATAL:
@@ -207,7 +207,7 @@ func (s *StartedService) StartOrReloadService(profileContent string, options *Ov
}
s.updateStatus(ServiceStatus_STARTING)
s.resetLogs()
instance, err := s.newInstance(profileContent, options)
instance, err := s.newInstance(ctx, profileContent, options)
if err != nil {
return s.updateStatusError(err)
}
@@ -1037,10 +1037,11 @@ func (s *StartedService) GetDeprecatedWarnings(ctx context.Context, empty *empty
return &DeprecatedWarnings{}, nil
}
notes := manager.Get()
selectedLocale := locale.FromContext(ctx)
return &DeprecatedWarnings{
Warnings: common.Map(notes, func(it deprecated.Note) *DeprecatedWarning {
return &DeprecatedWarning{
Message: it.Message(),
Message: it.MessageForLocale(selectedLocale),
Impending: it.Impending(),
MigrationLink: it.MigrationLink,
Description: it.Description,
+2 -2
View File
@@ -23,7 +23,7 @@ type applicationService struct {
}
func (s *applicationService) CheckConfig(ctx context.Context, request *ConfigContent) (*emptypb.Empty, error) {
err := s.startedService.CheckConfig(request.Content)
err := s.startedService.CheckConfig(ctx, request.Content)
if err != nil {
return nil, status.Error(codes.InvalidArgument, err.Error())
}
@@ -31,7 +31,7 @@ func (s *applicationService) CheckConfig(ctx context.Context, request *ConfigCon
}
func (s *applicationService) FormatConfig(ctx context.Context, request *ConfigContent) (*ConfigContent, error) {
content, err := s.startedService.FormatConfig(request.Content)
content, err := s.startedService.FormatConfig(ctx, request.Content)
if err != nil {
return nil, status.Error(codes.InvalidArgument, err.Error())
}
+1 -1
View File
@@ -87,7 +87,7 @@ func (s *desktopService) StartService(ctx context.Context, request *StartService
mergedOptions.OOMKillerDisabled = request.Options.OomKillerDisabled
mergedOptions.OOMMemoryLimit = request.Options.OomMemoryLimit
}
err = s.daemon.startServiceLocked(identity.UserID, request.ConfigContent, mergedOptions)
err = s.daemon.startServiceLocked(ctx, identity.UserID, request.ConfigContent, mergedOptions)
if err != nil {
return nil, s.daemon.cleanFailedStartLocked(identity.UserID, mergedOptions, err)
}
@@ -3,6 +3,7 @@
package main
import (
"context"
"fmt"
"os"
"path/filepath"
@@ -12,19 +13,19 @@ import (
E "github.com/sagernet/sing/common/exceptions"
)
func insecureFeatureError(feature string) error {
return E.New(fmt.Sprintf(locale.Current().InsecureFeatureMessage, feature, insecureModePlatformName()))
func insecureFeatureError(ctx context.Context, feature string) error {
return E.New(fmt.Sprintf(locale.FromContext(ctx).InsecureFeatureMessage, feature, insecureModePlatformName()))
}
type daemonSecurityPolicy struct {
daemon *Daemon
}
func (p *daemonSecurityPolicy) CheckFeature(feature string) error {
func (p *daemonSecurityPolicy) CheckFeature(ctx context.Context, feature string) error {
if p.daemon.insecureModeEnabled() {
return nil
}
return insecureFeatureError(feature)
return insecureFeatureError(ctx, feature)
}
type restrictedFileManager struct {
+3 -2
View File
@@ -1,6 +1,7 @@
package main
import (
"context"
"os"
"github.com/sagernet/sing-box/daemon"
@@ -27,7 +28,7 @@ func (h *managedHandler) ServiceStop() error {
return h.daemon.stopServiceLocked(ownerUserID)
}
func (h *managedHandler) ServiceReload() error {
func (h *managedHandler) ServiceReload(ctx context.Context) error {
if h.daemon.closed {
return os.ErrClosed
}
@@ -43,7 +44,7 @@ func (h *managedHandler) ServiceReload() error {
if err != nil {
return err
}
err = h.daemon.startServiceLocked(ownerUserID, configContent, options)
err = h.daemon.startServiceLocked(ctx, ownerUserID, configContent, options)
if err != nil {
return err
}
+5 -3
View File
@@ -27,6 +27,7 @@ import (
)
type Daemon struct {
ctx context.Context
logger log.ContextLogger
startedService *daemon.StartedService
server *grpc.Server
@@ -41,6 +42,7 @@ type Daemon struct {
func newDaemon() (*Daemon, error) {
ctx := include.Context(context.Background())
d := &Daemon{
ctx: ctx,
logger: log.StdLogger(),
runtimeWorkingDirectory: workingDirectory,
}
@@ -164,7 +166,7 @@ func (d *Daemon) restore() {
return
}
d.logger.Info("restoring service")
err = d.startServiceLocked(ownerUserID, configContent, options)
err = d.startServiceLocked(d.ctx, ownerUserID, configContent, options)
if err != nil {
d.logger.Error("restore service: ", err)
}
@@ -196,7 +198,7 @@ func (d *Daemon) configureWorkingDirectoryLocked(directory string) error {
return nil
}
func (d *Daemon) startServiceLocked(ownerUserID string, configContent string, options startOptions) error {
func (d *Daemon) startServiceLocked(ctx context.Context, ownerUserID string, configContent string, options startOptions) error {
directory := userWorkingDirectory(ownerUserID)
err := d.configureWorkingDirectoryLocked(directory)
if err != nil {
@@ -216,7 +218,7 @@ func (d *Daemon) startServiceLocked(ownerUserID string, configContent string, op
return err
}
}
err = d.startedService.StartOrReloadService(configContent, nil)
err = d.startedService.StartOrReloadService(ctx, configContent, nil)
if err != nil && d.platform != nil {
return E.Errors(err, d.platform.ResetPlatformOptions())
}
+6 -2
View File
@@ -36,10 +36,14 @@ func (n Note) Impending() bool {
}
func (n Note) Message() string {
return n.MessageForLocale(locale.Current())
}
func (n Note) MessageForLocale(selectedLocale *locale.Locale) string {
if n.MigrationLink != "" {
return fmt.Sprintf(locale.Current().DeprecatedMessage, n.Description, n.DeprecatedVersion, n.ScheduledVersion)
return fmt.Sprintf(selectedLocale.DeprecatedMessage, n.Description, n.DeprecatedVersion, n.ScheduledVersion)
} else {
return fmt.Sprintf(locale.Current().DeprecatedMessageNoLink, n.Description, n.DeprecatedVersion, n.ScheduledVersion)
return fmt.Sprintf(selectedLocale.DeprecatedMessageNoLink, n.Description, n.DeprecatedVersion, n.ScheduledVersion)
}
}
+4 -2
View File
@@ -29,6 +29,7 @@ import (
type CommandServer struct {
*daemon.StartedService
ctx context.Context
managedService *daemon.ManagedService
handler CommandServerHandler
platformInterface PlatformInterface
@@ -56,6 +57,7 @@ func NewCommandServer(handler CommandServerHandler, platformInterface PlatformIn
}
service.MustRegister[adapter.PlatformInterface](ctx, platformWrapper)
server := &CommandServer{
ctx: ctx,
handler: handler,
platformInterface: platformInterface,
platformWrapper: platformWrapper,
@@ -191,7 +193,7 @@ type OverrideOptions struct {
func (s *CommandServer) StartOrReloadService(configContent string, options *OverrideOptions) error {
saveConfigSnapshot(configContent)
err := s.StartedService.StartOrReloadService(configContent, &daemon.OverrideOptions{
err := s.StartedService.StartOrReloadService(s.ctx, configContent, &daemon.OverrideOptions{
AutoRedirect: options.AutoRedirect,
IncludePackage: iteratorToArray(options.IncludePackage),
ExcludePackage: iteratorToArray(options.ExcludePackage),
@@ -277,7 +279,7 @@ func (h *platformHandler) ServiceStop() error {
return (*CommandServer)(h).handler.ServiceStop()
}
func (h *platformHandler) ServiceReload() error {
func (h *platformHandler) ServiceReload(ctx context.Context) error {
return (*CommandServer)(h).handler.ServiceReload()
}