main.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. // Copyright (C) 2018 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at https://mozilla.org/MPL/2.0/.
  6. package main
  7. import (
  8. "context"
  9. "crypto/tls"
  10. "fmt"
  11. "log/slog"
  12. "net/http"
  13. "os"
  14. "os/signal"
  15. "runtime"
  16. "time"
  17. "github.com/alecthomas/kong"
  18. "github.com/prometheus/client_golang/prometheus/promhttp"
  19. "github.com/thejerf/suture/v4"
  20. "github.com/syncthing/syncthing/internal/blob"
  21. "github.com/syncthing/syncthing/internal/blob/s3"
  22. "github.com/syncthing/syncthing/internal/slogutil"
  23. "github.com/syncthing/syncthing/lib/build"
  24. "github.com/syncthing/syncthing/lib/protocol"
  25. "github.com/syncthing/syncthing/lib/rand"
  26. "github.com/syncthing/syncthing/lib/tlsutil"
  27. )
  28. const (
  29. addressExpiryTime = 2 * time.Hour
  30. // Reannounce-After is set to reannounceAfterSeconds +
  31. // random(reannounzeFuzzSeconds), similar for Retry-After
  32. reannounceAfterSeconds = 3300
  33. reannounzeFuzzSeconds = 300
  34. errorRetryAfterSeconds = 1500
  35. errorRetryFuzzSeconds = 300
  36. // Retry for not found is notFoundRetrySeenSeconds for records we have
  37. // seen an announcement for (but it's not active right now) and
  38. // notFoundRetryUnknownSeconds for records we have never seen (or not
  39. // seen within the last week).
  40. notFoundRetryUnknownMinSeconds = 60
  41. notFoundRetryUnknownMaxSeconds = 3600
  42. httpReadTimeout = 5 * time.Second
  43. httpWriteTimeout = 5 * time.Second
  44. httpMaxHeaderBytes = 1 << 10
  45. // Size of the replication outbox channel
  46. replicationOutboxSize = 10000
  47. )
  48. var debug = false
  49. type CLI struct {
  50. Cert string `group:"Listen" help:"Certificate file" default:"./cert.pem" env:"DISCOVERY_CERT_FILE"`
  51. Key string `group:"Listen" help:"Key file" default:"./key.pem" env:"DISCOVERY_KEY_FILE"`
  52. HTTP bool `group:"Listen" help:"Listen on HTTP (behind an HTTPS proxy)" env:"DISCOVERY_HTTP"`
  53. Compression bool `group:"Listen" help:"Enable GZIP compression of responses" env:"DISCOVERY_COMPRESSION"`
  54. Listen string `group:"Listen" help:"Listen address" default:":8443" env:"DISCOVERY_LISTEN"`
  55. MetricsListen string `group:"Listen" help:"Metrics listen address" env:"DISCOVERY_METRICS_LISTEN"`
  56. DesiredNotFoundRate float64 `group:"Listen" help:"Desired maximum rate of not-found replies (/s)" default:"1000"`
  57. DBDir string `group:"Database" help:"Database directory" default:"." env:"DISCOVERY_DB_DIR"`
  58. DBFlushInterval time.Duration `group:"Database" help:"Interval between database flushes" default:"5m" env:"DISCOVERY_DB_FLUSH_INTERVAL"`
  59. DBS3Endpoint string `name:"db-s3-endpoint" group:"Database (S3 backup)" hidden:"true" help:"S3 endpoint for database" env:"DISCOVERY_DB_S3_ENDPOINT"`
  60. DBS3Region string `name:"db-s3-region" group:"Database (S3 backup)" hidden:"true" help:"S3 region for database" env:"DISCOVERY_DB_S3_REGION"`
  61. DBS3Bucket string `name:"db-s3-bucket" group:"Database (S3 backup)" hidden:"true" help:"S3 bucket for database" env:"DISCOVERY_DB_S3_BUCKET"`
  62. DBS3AccessKeyID string `name:"db-s3-access-key-id" group:"Database (S3 backup)" hidden:"true" help:"S3 access key ID for database" env:"DISCOVERY_DB_S3_ACCESS_KEY_ID"`
  63. DBS3SecretKey string `name:"db-s3-secret-key" group:"Database (S3 backup)" hidden:"true" help:"S3 secret key for database" env:"DISCOVERY_DB_S3_SECRET_KEY"`
  64. AMQPAddress string `group:"AMQP replication" hidden:"true" help:"Address to AMQP broker" env:"DISCOVERY_AMQP_ADDRESS"`
  65. Debug bool `short:"d" help:"Print debug output" env:"DISCOVERY_DEBUG"`
  66. Version bool `short:"v" help:"Print version and exit"`
  67. }
  68. func main() {
  69. var cli CLI
  70. kong.Parse(&cli)
  71. level := slog.LevelInfo
  72. if cli.Debug {
  73. level = slog.LevelDebug
  74. }
  75. slogutil.SetDefaultLevel(level)
  76. if cli.Version {
  77. fmt.Println(build.LongVersionFor("stdiscosrv"))
  78. return
  79. }
  80. slog.Info(build.LongVersionFor("stdiscosrv"))
  81. buildInfo.WithLabelValues(build.Version, runtime.Version(), build.User, build.Date.UTC().Format("2006-01-02T15:04:05Z")).Set(1)
  82. var cert tls.Certificate
  83. if !cli.HTTP {
  84. var err error
  85. cert, err = tls.LoadX509KeyPair(cli.Cert, cli.Key)
  86. if os.IsNotExist(err) {
  87. slog.Info("Failed to load keypair. Generating one, this might take a while...")
  88. cert, err = tlsutil.NewCertificate(cli.Cert, cli.Key, "stdiscosrv", 20*365, false)
  89. if err != nil {
  90. slog.Error("Failed to generate X509 key pair", "error", err)
  91. os.Exit(1)
  92. }
  93. } else if err != nil {
  94. slog.Error("Failed to load keypair", "error", err)
  95. os.Exit(1)
  96. }
  97. devID := protocol.NewDeviceID(cert.Certificate[0])
  98. slog.Info("Loaded certificate keypair", "deviceId", devID.String())
  99. }
  100. // Root of the service tree.
  101. main := suture.New("main", suture.Spec{
  102. PassThroughPanics: true,
  103. Timeout: 2 * time.Minute,
  104. })
  105. // If configured, use blob storage for database backups.
  106. var blobs blob.Store
  107. var err error
  108. if cli.DBS3Endpoint != "" {
  109. blobs, err = s3.NewSession(cli.DBS3Endpoint, cli.DBS3Region, cli.DBS3Bucket, cli.DBS3AccessKeyID, cli.DBS3SecretKey)
  110. }
  111. if err != nil {
  112. slog.Error("Failed to create blob store", "error", err)
  113. os.Exit(1)
  114. }
  115. // Start the database.
  116. db := newInMemoryStore(cli.DBDir, cli.DBFlushInterval, blobs)
  117. main.Add(db)
  118. // If we have an AMQP broker for replication, start that
  119. var repl replicator
  120. if cli.AMQPAddress != "" {
  121. clientID := rand.String(10)
  122. kr := newAMQPReplicator(cli.AMQPAddress, clientID, db)
  123. main.Add(kr)
  124. repl = kr
  125. }
  126. // Start the main API server.
  127. qs := newAPISrv(cli.Listen, cert, db, repl, cli.HTTP, cli.Compression, cli.DesiredNotFoundRate)
  128. main.Add(qs)
  129. // If we have a metrics port configured, start a metrics handler.
  130. if cli.MetricsListen != "" {
  131. go func() {
  132. mux := http.NewServeMux()
  133. mux.Handle("/metrics", promhttp.Handler())
  134. err := http.ListenAndServe(cli.MetricsListen, mux)
  135. slog.Error("Failed to serve", "error", err)
  136. os.Exit(1)
  137. }()
  138. }
  139. ctx, cancel := context.WithCancel(context.Background())
  140. defer cancel()
  141. // Cancel on signal
  142. signalChan := make(chan os.Signal, 1)
  143. signal.Notify(signalChan, os.Interrupt)
  144. go func() {
  145. sig := <-signalChan
  146. slog.Info("Received signal; shutting down", "signal", sig)
  147. cancel()
  148. }()
  149. // Engage!
  150. main.Serve(ctx)
  151. }