tls.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. tlsConfig.NextProtos = []string{}
  132. if options.ServerName != "" {
  133. tlsConfig.ServerName = options.ServerName
  134. }
  135. if len(options.ALPN) > 0 {
  136. tlsConfig.NextProtos = options.ALPN
  137. }
  138. if options.MinVersion != "" {
  139. minVersion, err := option.ParseTLSVersion(options.MinVersion)
  140. if err != nil {
  141. return nil, E.Cause(err, "parse min_version")
  142. }
  143. tlsConfig.MinVersion = minVersion
  144. }
  145. if options.MaxVersion != "" {
  146. maxVersion, err := option.ParseTLSVersion(options.MaxVersion)
  147. if err != nil {
  148. return nil, E.Cause(err, "parse max_version")
  149. }
  150. tlsConfig.MaxVersion = maxVersion
  151. }
  152. if options.CipherSuites != nil {
  153. find:
  154. for _, cipherSuite := range options.CipherSuites {
  155. for _, tlsCipherSuite := range tls.CipherSuites() {
  156. if cipherSuite == tlsCipherSuite.Name {
  157. tlsConfig.CipherSuites = append(tlsConfig.CipherSuites, tlsCipherSuite.ID)
  158. continue find
  159. }
  160. }
  161. return nil, E.New("unknown cipher_suite: ", cipherSuite)
  162. }
  163. }
  164. var certificate []byte
  165. var key []byte
  166. if acmeService == nil {
  167. if options.Certificate != "" {
  168. certificate = []byte(options.Certificate)
  169. } else if options.CertificatePath != "" {
  170. content, err := os.ReadFile(options.CertificatePath)
  171. if err != nil {
  172. return nil, E.Cause(err, "read certificate")
  173. }
  174. certificate = content
  175. }
  176. if options.Key != "" {
  177. key = []byte(options.Key)
  178. } else if options.KeyPath != "" {
  179. content, err := os.ReadFile(options.KeyPath)
  180. if err != nil {
  181. return nil, E.Cause(err, "read key")
  182. }
  183. key = content
  184. }
  185. if certificate == nil {
  186. return nil, E.New("missing certificate")
  187. }
  188. if key == nil {
  189. return nil, E.New("missing key")
  190. }
  191. keyPair, err := tls.X509KeyPair(certificate, key)
  192. if err != nil {
  193. return nil, E.Cause(err, "parse x509 key pair")
  194. }
  195. tlsConfig.Certificates = []tls.Certificate{keyPair}
  196. }
  197. return &TLSConfig{
  198. config: tlsConfig,
  199. logger: logger,
  200. acmeService: acmeService,
  201. certificate: certificate,
  202. key: key,
  203. certificatePath: options.CertificatePath,
  204. keyPath: options.KeyPath,
  205. }, nil
  206. }