listener.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package transport
  2. import (
  3. "crypto/tls"
  4. "net"
  5. "time"
  6. "github.com/cloudflare/cfssl/log"
  7. )
  8. // A Listener is a TCP network listener for TLS-secured connections.
  9. type Listener struct {
  10. *Transport
  11. net.Listener
  12. }
  13. // Listen sets up a new server. If an error is returned, it means
  14. // the server isn't ready to begin listening.
  15. func Listen(address string, tr *Transport) (*Listener, error) {
  16. var err error
  17. l := &Listener{Transport: tr}
  18. config, err := tr.getConfig()
  19. if err != nil {
  20. return nil, err
  21. }
  22. l.Listener, err = tls.Listen("tcp", address, config)
  23. return l, err
  24. }
  25. func (tr *Transport) getConfig() (*tls.Config, error) {
  26. if tr.ClientTrustStore != nil {
  27. log.Info("using client auth")
  28. return tr.TLSClientAuthServerConfig()
  29. }
  30. log.Info("not using client auth")
  31. return tr.TLSServerConfig()
  32. }
  33. // PollInterval is how often to check whether a new certificate has
  34. // been found.
  35. var PollInterval = 30 * time.Second
  36. func pollWait(target time.Time) {
  37. for {
  38. <-time.After(PollInterval)
  39. if time.Now().After(target) {
  40. break
  41. }
  42. }
  43. }
  44. // AutoUpdate will automatically update the listener. If a non-nil
  45. // certUpdates chan is provided, it will receive timestamps for
  46. // reissued certificates. If errChan is non-nil, any errors that occur
  47. // in the updater will be passed along.
  48. func (l *Listener) AutoUpdate(certUpdates chan<- time.Time, errChan chan<- error) {
  49. defer func() {
  50. if r := recover(); r != nil {
  51. log.Criticalf("AutoUpdate panicked: %v", r)
  52. }
  53. }()
  54. for {
  55. // Wait until it's time to update the certificate.
  56. target := time.Now().Add(l.Lifespan())
  57. if PollInterval == 0 {
  58. <-time.After(l.Lifespan())
  59. } else {
  60. pollWait(target)
  61. }
  62. // Keep trying to update the certificate until it's
  63. // ready.
  64. for {
  65. log.Debug("refreshing certificate")
  66. err := l.RefreshKeys()
  67. if err == nil {
  68. break
  69. }
  70. delay := l.Transport.Backoff.Duration()
  71. log.Debugf("failed to update certificate, will try again in %s", delay)
  72. if errChan != nil {
  73. errChan <- err
  74. }
  75. <-time.After(delay)
  76. }
  77. if certUpdates != nil {
  78. certUpdates <- time.Now()
  79. }
  80. config, err := l.getConfig()
  81. if err != nil {
  82. log.Debugf("immediately after getting a new certificate, the Transport is reporting errors: %v", err)
  83. if errChan != nil {
  84. errChan <- err
  85. }
  86. }
  87. address := l.Listener.Addr().String()
  88. lnet := l.Listener.Addr().Network()
  89. l.Listener, err = tls.Listen(lnet, address, config)
  90. if err != nil {
  91. log.Debugf("immediately after getting a new certificate, the Transport is reporting errors: %v", err)
  92. if errChan != nil {
  93. errChan <- err
  94. }
  95. }
  96. log.Debug("listener: auto update of certificate complete")
  97. l.Transport.Backoff.Reset()
  98. }
  99. }