diff --git a/cmd/sing-box/cmd_merge.go b/cmd/sing-box/cmd_merge.go index 82c92a45..0aff7501 100644 --- a/cmd/sing-box/cmd_merge.go +++ b/cmd/sing-box/cmd_merge.go @@ -10,6 +10,7 @@ import ( C "github.com/sagernet/sing-box/constant" "github.com/sagernet/sing-box/log" "github.com/sagernet/sing-box/option" + "github.com/sagernet/sing/common" E "github.com/sagernet/sing/common/exceptions" "github.com/sagernet/sing/common/rw" @@ -120,18 +121,18 @@ func mergeTLSInboundOptions(options *option.InboundTLSOptions) *option.InboundTL } if options.CertificatePath != "" { if content, err := os.ReadFile(options.CertificatePath); err == nil { - options.Certificate = strings.Split(string(content), "\n") + options.Certificate = trimStringArray(strings.Split(string(content), "\n")) } } if options.KeyPath != "" { if content, err := os.ReadFile(options.KeyPath); err == nil { - options.Key = strings.Split(string(content), "\n") + options.Key = trimStringArray(strings.Split(string(content), "\n")) } } if options.ECH != nil { if options.ECH.KeyPath != "" { if content, err := os.ReadFile(options.ECH.KeyPath); err == nil { - options.ECH.Key = strings.Split(string(content), "\n") + options.ECH.Key = trimStringArray(strings.Split(string(content), "\n")) } } } @@ -144,13 +145,13 @@ func mergeTLSOutboundOptions(options *option.OutboundTLSOptions) *option.Outboun } if options.CertificatePath != "" { if content, err := os.ReadFile(options.CertificatePath); err == nil { - options.Certificate = strings.Split(string(content), "\n") + options.Certificate = trimStringArray(strings.Split(string(content), "\n")) } } if options.ECH != nil { if options.ECH.ConfigPath != "" { if content, err := os.ReadFile(options.ECH.ConfigPath); err == nil { - options.ECH.Config = strings.Split(string(content), "\n") + options.ECH.Config = trimStringArray(strings.Split(string(content), "\n")) } } } @@ -159,9 +160,15 @@ func mergeTLSOutboundOptions(options *option.OutboundTLSOptions) *option.Outboun func mergeSSHOutboundOptions(options option.SSHOutboundOptions) option.SSHOutboundOptions { if options.PrivateKeyPath != "" { - if content, err := os.ReadFile(options.PrivateKeyPath); err == nil { - options.PrivateKey = strings.Split(string(content), "\n") + if content, err := os.ReadFile(os.ExpandEnv(options.PrivateKeyPath)); err == nil { + options.PrivateKey = trimStringArray(strings.Split(string(content), "\n")) } } return options } + +func trimStringArray(array []string) []string { + return common.Filter(array, func(it string) bool { + return strings.TrimSpace(it) != "" + }) +}