cmd_merge.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. package main
  2. import (
  3. "bytes"
  4. "os"
  5. "path/filepath"
  6. "strings"
  7. C "github.com/sagernet/sing-box/constant"
  8. "github.com/sagernet/sing-box/log"
  9. "github.com/sagernet/sing-box/option"
  10. "github.com/sagernet/sing/common"
  11. E "github.com/sagernet/sing/common/exceptions"
  12. "github.com/sagernet/sing/common/json"
  13. "github.com/sagernet/sing/common/rw"
  14. "github.com/spf13/cobra"
  15. )
  16. var commandMerge = &cobra.Command{
  17. Use: "merge <output>",
  18. Short: "Merge configurations",
  19. Run: func(cmd *cobra.Command, args []string) {
  20. err := merge(args[0])
  21. if err != nil {
  22. log.Fatal(err)
  23. }
  24. },
  25. Args: cobra.ExactArgs(1),
  26. }
  27. func init() {
  28. mainCommand.AddCommand(commandMerge)
  29. }
  30. func merge(outputPath string) error {
  31. mergedOptions, err := readConfigAndMerge()
  32. if err != nil {
  33. return err
  34. }
  35. err = mergePathResources(&mergedOptions)
  36. if err != nil {
  37. return err
  38. }
  39. buffer := new(bytes.Buffer)
  40. encoder := json.NewEncoder(buffer)
  41. encoder.SetIndent("", " ")
  42. err = encoder.Encode(mergedOptions)
  43. if err != nil {
  44. return E.Cause(err, "encode config")
  45. }
  46. if existsContent, err := os.ReadFile(outputPath); err != nil {
  47. if string(existsContent) == buffer.String() {
  48. return nil
  49. }
  50. }
  51. err = rw.WriteFile(outputPath, buffer.Bytes())
  52. if err != nil {
  53. return err
  54. }
  55. outputPath, _ = filepath.Abs(outputPath)
  56. os.Stderr.WriteString(outputPath + "\n")
  57. return nil
  58. }
  59. func mergePathResources(options *option.Options) error {
  60. for index, inbound := range options.Inbounds {
  61. rawOptions, err := inbound.RawOptions()
  62. if err != nil {
  63. return err
  64. }
  65. if tlsOptions, containsTLSOptions := rawOptions.(option.InboundTLSOptionsWrapper); containsTLSOptions {
  66. tlsOptions.ReplaceInboundTLSOptions(mergeTLSInboundOptions(tlsOptions.TakeInboundTLSOptions()))
  67. }
  68. options.Inbounds[index] = inbound
  69. }
  70. for index, outbound := range options.Outbounds {
  71. rawOptions, err := outbound.RawOptions()
  72. if err != nil {
  73. return err
  74. }
  75. switch outbound.Type {
  76. case C.TypeSSH:
  77. outbound.SSHOptions = mergeSSHOutboundOptions(outbound.SSHOptions)
  78. }
  79. if tlsOptions, containsTLSOptions := rawOptions.(option.OutboundTLSOptionsWrapper); containsTLSOptions {
  80. tlsOptions.ReplaceOutboundTLSOptions(mergeTLSOutboundOptions(tlsOptions.TakeOutboundTLSOptions()))
  81. }
  82. options.Outbounds[index] = outbound
  83. }
  84. return nil
  85. }
  86. func mergeTLSInboundOptions(options *option.InboundTLSOptions) *option.InboundTLSOptions {
  87. if options == nil {
  88. return nil
  89. }
  90. if options.CertificatePath != "" {
  91. if content, err := os.ReadFile(options.CertificatePath); err == nil {
  92. options.Certificate = trimStringArray(strings.Split(string(content), "\n"))
  93. }
  94. }
  95. if options.KeyPath != "" {
  96. if content, err := os.ReadFile(options.KeyPath); err == nil {
  97. options.Key = trimStringArray(strings.Split(string(content), "\n"))
  98. }
  99. }
  100. if options.ECH != nil {
  101. if options.ECH.KeyPath != "" {
  102. if content, err := os.ReadFile(options.ECH.KeyPath); err == nil {
  103. options.ECH.Key = trimStringArray(strings.Split(string(content), "\n"))
  104. }
  105. }
  106. }
  107. return options
  108. }
  109. func mergeTLSOutboundOptions(options *option.OutboundTLSOptions) *option.OutboundTLSOptions {
  110. if options == nil {
  111. return nil
  112. }
  113. if options.CertificatePath != "" {
  114. if content, err := os.ReadFile(options.CertificatePath); err == nil {
  115. options.Certificate = trimStringArray(strings.Split(string(content), "\n"))
  116. }
  117. }
  118. if options.ECH != nil {
  119. if options.ECH.ConfigPath != "" {
  120. if content, err := os.ReadFile(options.ECH.ConfigPath); err == nil {
  121. options.ECH.Config = trimStringArray(strings.Split(string(content), "\n"))
  122. }
  123. }
  124. }
  125. return options
  126. }
  127. func mergeSSHOutboundOptions(options option.SSHOutboundOptions) option.SSHOutboundOptions {
  128. if options.PrivateKeyPath != "" {
  129. if content, err := os.ReadFile(os.ExpandEnv(options.PrivateKeyPath)); err == nil {
  130. options.PrivateKey = trimStringArray(strings.Split(string(content), "\n"))
  131. }
  132. }
  133. return options
  134. }
  135. func trimStringArray(array []string) []string {
  136. return common.Filter(array, func(it string) bool {
  137. return strings.TrimSpace(it) != ""
  138. })
  139. }