tls.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. package inbound
  2. import (
  3. "context"
  4. "crypto/tls"
  5. "os"
  6. "github.com/sagernet/sing-box/adapter"
  7. "github.com/sagernet/sing-box/log"
  8. "github.com/sagernet/sing-box/option"
  9. "github.com/sagernet/sing/common"
  10. E "github.com/sagernet/sing/common/exceptions"
  11. "github.com/fsnotify/fsnotify"
  12. )
  13. var _ adapter.Service = (*TLSConfig)(nil)
  14. type TLSConfig struct {
  15. config *tls.Config
  16. logger log.Logger
  17. acmeService adapter.Service
  18. certificate []byte
  19. key []byte
  20. certificatePath string
  21. keyPath string
  22. watcher *fsnotify.Watcher
  23. }
  24. func (c *TLSConfig) Config() *tls.Config {
  25. return c.config
  26. }
  27. func (c *TLSConfig) Start() error {
  28. if c.acmeService != nil {
  29. return c.acmeService.Start()
  30. } else {
  31. if c.certificatePath == "" && c.keyPath == "" {
  32. return nil
  33. }
  34. err := c.startWatcher()
  35. if err != nil {
  36. c.logger.Warn("create fsnotify watcher: ", err)
  37. }
  38. return nil
  39. }
  40. }
  41. func (c *TLSConfig) startWatcher() error {
  42. watcher, err := fsnotify.NewWatcher()
  43. if err != nil {
  44. return err
  45. }
  46. if c.certificatePath != "" {
  47. err = watcher.Add(c.certificatePath)
  48. if err != nil {
  49. return err
  50. }
  51. }
  52. if c.keyPath != "" {
  53. err = watcher.Add(c.keyPath)
  54. if err != nil {
  55. return err
  56. }
  57. }
  58. c.watcher = watcher
  59. go c.loopUpdate()
  60. return nil
  61. }
  62. func (c *TLSConfig) loopUpdate() {
  63. for {
  64. select {
  65. case event, ok := <-c.watcher.Events:
  66. if !ok {
  67. return
  68. }
  69. if event.Op&fsnotify.Write != fsnotify.Write {
  70. continue
  71. }
  72. err := c.reloadKeyPair()
  73. if err != nil {
  74. c.logger.Error(E.Cause(err, "reload TLS key pair"))
  75. }
  76. case err, ok := <-c.watcher.Errors:
  77. if !ok {
  78. return
  79. }
  80. c.logger.Error(E.Cause(err, "fsnotify error"))
  81. }
  82. }
  83. }
  84. func (c *TLSConfig) reloadKeyPair() error {
  85. if c.certificatePath != "" {
  86. certificate, err := os.ReadFile(c.certificatePath)
  87. if err != nil {
  88. return E.Cause(err, "reload certificate from ", c.certificatePath)
  89. }
  90. c.certificate = certificate
  91. }
  92. if c.keyPath != "" {
  93. key, err := os.ReadFile(c.keyPath)
  94. if err != nil {
  95. return E.Cause(err, "reload key from ", c.keyPath)
  96. }
  97. c.key = key
  98. }
  99. keyPair, err := tls.X509KeyPair(c.certificate, c.key)
  100. if err != nil {
  101. return E.Cause(err, "reload key pair")
  102. }
  103. c.config.Certificates = []tls.Certificate{keyPair}
  104. c.logger.Info("reloaded TLS certificate")
  105. return nil
  106. }
  107. func (c *TLSConfig) Close() error {
  108. if c.acmeService != nil {
  109. return c.acmeService.Close()
  110. }
  111. if c.watcher != nil {
  112. return c.watcher.Close()
  113. }
  114. return nil
  115. }
  116. func NewTLSConfig(ctx context.Context, logger log.Logger, options option.InboundTLSOptions) (*TLSConfig, error) {
  117. if !options.Enabled {
  118. return nil, nil
  119. }
  120. var tlsConfig *tls.Config
  121. var acmeService adapter.Service
  122. var err error
  123. if options.ACME != nil && len(options.ACME.Domain) > 0 {
  124. tlsConfig, acmeService, err = startACME(ctx, common.PtrValueOrDefault(options.ACME))
  125. if err != nil {
  126. return nil, err
  127. }
  128. } else {
  129. tlsConfig = &tls.Config{}
  130. }
  131. if options.ServerName != "" {
  132. tlsConfig.ServerName = options.ServerName
  133. }
  134. if len(options.ALPN) > 0 {
  135. tlsConfig.NextProtos = append(tlsConfig.NextProtos, options.ALPN...)
  136. }
  137. if options.MinVersion != "" {
  138. minVersion, err := option.ParseTLSVersion(options.MinVersion)
  139. if err != nil {
  140. return nil, E.Cause(err, "parse min_version")
  141. }
  142. tlsConfig.MinVersion = minVersion
  143. }
  144. if options.MaxVersion != "" {
  145. maxVersion, err := option.ParseTLSVersion(options.MaxVersion)
  146. if err != nil {
  147. return nil, E.Cause(err, "parse max_version")
  148. }
  149. tlsConfig.MaxVersion = maxVersion
  150. }
  151. if options.CipherSuites != nil {
  152. find:
  153. for _, cipherSuite := range options.CipherSuites {
  154. for _, tlsCipherSuite := range tls.CipherSuites() {
  155. if cipherSuite == tlsCipherSuite.Name {
  156. tlsConfig.CipherSuites = append(tlsConfig.CipherSuites, tlsCipherSuite.ID)
  157. continue find
  158. }
  159. }
  160. return nil, E.New("unknown cipher_suite: ", cipherSuite)
  161. }
  162. }
  163. var certificate []byte
  164. var key []byte
  165. if acmeService == nil {
  166. if options.Certificate != "" {
  167. certificate = []byte(options.Certificate)
  168. } else if options.CertificatePath != "" {
  169. content, err := os.ReadFile(options.CertificatePath)
  170. if err != nil {
  171. return nil, E.Cause(err, "read certificate")
  172. }
  173. certificate = content
  174. }
  175. if options.Key != "" {
  176. key = []byte(options.Key)
  177. } else if options.KeyPath != "" {
  178. content, err := os.ReadFile(options.KeyPath)
  179. if err != nil {
  180. return nil, E.Cause(err, "read key")
  181. }
  182. key = content
  183. }
  184. if certificate == nil {
  185. return nil, E.New("missing certificate")
  186. }
  187. if key == nil {
  188. return nil, E.New("missing key")
  189. }
  190. keyPair, err := tls.X509KeyPair(certificate, key)
  191. if err != nil {
  192. return nil, E.Cause(err, "parse x509 key pair")
  193. }
  194. tlsConfig.Certificates = []tls.Certificate{keyPair}
  195. }
  196. return &TLSConfig{
  197. config: tlsConfig,
  198. logger: logger,
  199. acmeService: acmeService,
  200. certificate: certificate,
  201. key: key,
  202. certificatePath: options.CertificatePath,
  203. keyPath: options.KeyPath,
  204. }, nil
  205. }