main.go 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183
  1. // Copyright (C) 2014 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 http://mozilla.org/MPL/2.0/.
  6. package main
  7. import (
  8. "crypto/tls"
  9. "flag"
  10. "fmt"
  11. "io/ioutil"
  12. "log"
  13. "net"
  14. "net/http"
  15. _ "net/http/pprof"
  16. "net/url"
  17. "os"
  18. "path/filepath"
  19. "regexp"
  20. "runtime"
  21. "runtime/pprof"
  22. "strconv"
  23. "strings"
  24. "time"
  25. "github.com/calmh/logger"
  26. "github.com/juju/ratelimit"
  27. "github.com/syncthing/protocol"
  28. "github.com/syncthing/syncthing/lib/config"
  29. "github.com/syncthing/syncthing/lib/db"
  30. "github.com/syncthing/syncthing/lib/discover"
  31. "github.com/syncthing/syncthing/lib/events"
  32. "github.com/syncthing/syncthing/lib/model"
  33. "github.com/syncthing/syncthing/lib/osutil"
  34. "github.com/syncthing/syncthing/lib/relay"
  35. "github.com/syncthing/syncthing/lib/symlinks"
  36. "github.com/syncthing/syncthing/lib/tlsutil"
  37. "github.com/syncthing/syncthing/lib/upgrade"
  38. "github.com/syndtr/goleveldb/leveldb"
  39. "github.com/syndtr/goleveldb/leveldb/errors"
  40. "github.com/syndtr/goleveldb/leveldb/opt"
  41. "github.com/thejerf/suture"
  42. "golang.org/x/crypto/bcrypt"
  43. )
  44. var (
  45. Version = "unknown-dev"
  46. Codename = "Beryllium Bedbug"
  47. BuildEnv = "default"
  48. BuildStamp = "0"
  49. BuildDate time.Time
  50. BuildHost = "unknown"
  51. BuildUser = "unknown"
  52. IsRelease bool
  53. IsBeta bool
  54. LongVersion string
  55. )
  56. const (
  57. exitSuccess = 0
  58. exitError = 1
  59. exitNoUpgradeAvailable = 2
  60. exitRestarting = 3
  61. exitUpgrading = 4
  62. )
  63. const (
  64. bepProtocolName = "bep/1.0"
  65. tlsDefaultCommonName = "syncthing"
  66. tlsRSABits = 3072
  67. pingEventInterval = time.Minute
  68. )
  69. var l = logger.DefaultLogger
  70. func init() {
  71. if Version != "unknown-dev" {
  72. // If not a generic dev build, version string should come from git describe
  73. exp := regexp.MustCompile(`^v\d+\.\d+\.\d+(-[a-z0-9]+)*(\+\d+-g[0-9a-f]+)?(-dirty)?$`)
  74. if !exp.MatchString(Version) {
  75. l.Fatalf("Invalid version string %q;\n\tdoes not match regexp %v", Version, exp)
  76. }
  77. }
  78. // Check for a clean release build. A release is something like "v0.1.2",
  79. // with an optional suffix of letters and dot separated numbers like
  80. // "-beta3.47". If there's more stuff, like a plus sign and a commit hash
  81. // and so on, then it's not a release. If there's a dash anywhere in
  82. // there, it's some kind of beta or prerelease version.
  83. exp := regexp.MustCompile(`^v\d+\.\d+\.\d+(-[a-z]+[\d\.]+)?$`)
  84. IsRelease = exp.MatchString(Version)
  85. IsBeta = strings.Contains(Version, "-")
  86. stamp, _ := strconv.Atoi(BuildStamp)
  87. BuildDate = time.Unix(int64(stamp), 0)
  88. date := BuildDate.UTC().Format("2006-01-02 15:04:05 MST")
  89. LongVersion = fmt.Sprintf(`syncthing %s "%s" (%s %s-%s %s) %s@%s %s`, Version, Codename, runtime.Version(), runtime.GOOS, runtime.GOARCH, BuildEnv, BuildUser, BuildHost, date)
  90. if os.Getenv("STTRACE") != "" {
  91. logFlags = log.Ltime | log.Ldate | log.Lmicroseconds | log.Lshortfile
  92. }
  93. }
  94. var (
  95. cfg *config.Wrapper
  96. myID protocol.DeviceID
  97. confDir string
  98. logFlags = log.Ltime
  99. writeRateLimit *ratelimit.Bucket
  100. readRateLimit *ratelimit.Bucket
  101. stop = make(chan int)
  102. relaySvc *relay.Svc
  103. cert tls.Certificate
  104. lans []*net.IPNet
  105. )
  106. const (
  107. usage = "syncthing [options]"
  108. extraUsage = `
  109. The default configuration directory is:
  110. %s
  111. The -logflags value is a sum of the following:
  112. 1 Date
  113. 2 Time
  114. 4 Microsecond time
  115. 8 Long filename
  116. 16 Short filename
  117. I.e. to prefix each log line with date and time, set -logflags=3 (1 + 2 from
  118. above). The value 0 is used to disable all of the above. The default is to
  119. show time only (2).
  120. Development Settings
  121. --------------------
  122. The following environment variables modify syncthing's behavior in ways that
  123. are mostly useful for developers. Use with care.
  124. STGUIASSETS Directory to load GUI assets from. Overrides compiled in assets.
  125. STTRACE A comma separated string of facilities to trace. The valid
  126. facility strings are:
  127. - "beacon" (the beacon package)
  128. - "discover" (the discover package)
  129. - "events" (the events package)
  130. - "files" (the files package)
  131. - "http" (the main package; HTTP requests)
  132. - "locks" (the sync package; trace long held locks)
  133. - "net" (the main package; connections & network messages)
  134. - "model" (the model package)
  135. - "scanner" (the scanner package)
  136. - "stats" (the stats package)
  137. - "suture" (the suture package; service management)
  138. - "upnp" (the upnp package)
  139. - "xdr" (the xdr package)
  140. - "all" (all of the above)
  141. STPROFILER Set to a listen address such as "127.0.0.1:9090" to start the
  142. profiler with HTTP access.
  143. STCPUPROFILE Write a CPU profile to cpu-$pid.pprof on exit.
  144. STHEAPPROFILE Write heap profiles to heap-$pid-$timestamp.pprof each time
  145. heap usage increases.
  146. STBLOCKPROFILE Write block profiles to block-$pid-$timestamp.pprof every 20
  147. seconds.
  148. STPERFSTATS Write running performance statistics to perf-$pid.csv. Not
  149. supported on Windows.
  150. STNOUPGRADE Disable automatic upgrades.
  151. GOMAXPROCS Set the maximum number of CPU cores to use. Defaults to all
  152. available CPU cores.
  153. GOGC Percentage of heap growth at which to trigger GC. Default is
  154. 100. Lower numbers keep peak memory usage down, at the price
  155. of CPU usage (ie. performance).`
  156. )
  157. // Command line and environment options
  158. var (
  159. reset bool
  160. showVersion bool
  161. doUpgrade bool
  162. doUpgradeCheck bool
  163. upgradeTo string
  164. noBrowser bool
  165. noConsole bool
  166. generateDir string
  167. logFile string
  168. auditEnabled bool
  169. verbose bool
  170. paused bool
  171. noRestart = os.Getenv("STNORESTART") != ""
  172. noUpgrade = os.Getenv("STNOUPGRADE") != ""
  173. guiAddress = os.Getenv("STGUIADDRESS") // legacy
  174. guiAuthentication = os.Getenv("STGUIAUTH") // legacy
  175. guiAPIKey = os.Getenv("STGUIAPIKEY") // legacy
  176. profiler = os.Getenv("STPROFILER")
  177. guiAssets = os.Getenv("STGUIASSETS")
  178. cpuProfile = os.Getenv("STCPUPROFILE") != ""
  179. stRestarting = os.Getenv("STRESTART") != ""
  180. innerProcess = os.Getenv("STNORESTART") != "" || os.Getenv("STMONITORED") != ""
  181. )
  182. func main() {
  183. if runtime.GOOS == "windows" {
  184. // On Windows, we use a log file by default. Setting the -logfile flag
  185. // to "-" disables this behavior.
  186. flag.StringVar(&logFile, "logfile", "", "Log file name (use \"-\" for stdout)")
  187. // We also add an option to hide the console window
  188. flag.BoolVar(&noConsole, "no-console", false, "Hide console window")
  189. } else {
  190. flag.StringVar(&logFile, "logfile", "-", "Log file name (use \"-\" for stdout)")
  191. }
  192. flag.StringVar(&generateDir, "generate", "", "Generate key and config in specified dir, then exit")
  193. flag.StringVar(&guiAddress, "gui-address", guiAddress, "Override GUI address")
  194. flag.StringVar(&guiAuthentication, "gui-authentication", guiAuthentication, "Override GUI authentication; username:password")
  195. flag.StringVar(&guiAPIKey, "gui-apikey", guiAPIKey, "Override GUI API key")
  196. flag.StringVar(&confDir, "home", "", "Set configuration directory")
  197. flag.IntVar(&logFlags, "logflags", logFlags, "Select information in log line prefix")
  198. flag.BoolVar(&noBrowser, "no-browser", false, "Do not start browser")
  199. flag.BoolVar(&noRestart, "no-restart", noRestart, "Do not restart; just exit")
  200. flag.BoolVar(&reset, "reset", false, "Reset the database")
  201. flag.BoolVar(&doUpgrade, "upgrade", false, "Perform upgrade")
  202. flag.BoolVar(&doUpgradeCheck, "upgrade-check", false, "Check for available upgrade")
  203. flag.BoolVar(&showVersion, "version", false, "Show version")
  204. flag.StringVar(&upgradeTo, "upgrade-to", upgradeTo, "Force upgrade directly from specified URL")
  205. flag.BoolVar(&auditEnabled, "audit", false, "Write events to audit file")
  206. flag.BoolVar(&verbose, "verbose", false, "Print verbose log output")
  207. flag.BoolVar(&paused, "paused", false, "Start with all devices paused")
  208. flag.Usage = usageFor(flag.CommandLine, usage, fmt.Sprintf(extraUsage, baseDirs["config"]))
  209. flag.Parse()
  210. if noConsole {
  211. osutil.HideConsole()
  212. }
  213. if confDir != "" {
  214. // Not set as default above because the string can be really long.
  215. baseDirs["config"] = confDir
  216. }
  217. if err := expandLocations(); err != nil {
  218. l.Fatalln(err)
  219. }
  220. if guiAssets == "" {
  221. guiAssets = locations[locGUIAssets]
  222. }
  223. if logFile == "" {
  224. // Use the default log file location
  225. logFile = locations[locLogFile]
  226. }
  227. if showVersion {
  228. fmt.Println(LongVersion)
  229. return
  230. }
  231. l.SetFlags(logFlags)
  232. if generateDir != "" {
  233. dir, err := osutil.ExpandTilde(generateDir)
  234. if err != nil {
  235. l.Fatalln("generate:", err)
  236. }
  237. info, err := os.Stat(dir)
  238. if err == nil && !info.IsDir() {
  239. l.Fatalln(dir, "is not a directory")
  240. }
  241. if err != nil && os.IsNotExist(err) {
  242. err = osutil.MkdirAll(dir, 0700)
  243. if err != nil {
  244. l.Fatalln("generate:", err)
  245. }
  246. }
  247. certFile, keyFile := filepath.Join(dir, "cert.pem"), filepath.Join(dir, "key.pem")
  248. cert, err := tls.LoadX509KeyPair(certFile, keyFile)
  249. if err == nil {
  250. l.Warnln("Key exists; will not overwrite.")
  251. l.Infoln("Device ID:", protocol.NewDeviceID(cert.Certificate[0]))
  252. } else {
  253. cert, err = tlsutil.NewCertificate(certFile, keyFile, tlsDefaultCommonName, tlsRSABits)
  254. if err != nil {
  255. l.Fatalln("Create certificate:", err)
  256. }
  257. myID = protocol.NewDeviceID(cert.Certificate[0])
  258. if err != nil {
  259. l.Fatalln("Load certificate:", err)
  260. }
  261. if err == nil {
  262. l.Infoln("Device ID:", protocol.NewDeviceID(cert.Certificate[0]))
  263. }
  264. }
  265. cfgFile := filepath.Join(dir, "config.xml")
  266. if _, err := os.Stat(cfgFile); err == nil {
  267. l.Warnln("Config exists; will not overwrite.")
  268. return
  269. }
  270. var myName, _ = os.Hostname()
  271. var newCfg = defaultConfig(myName)
  272. var cfg = config.Wrap(cfgFile, newCfg)
  273. err = cfg.Save()
  274. if err != nil {
  275. l.Warnln("Failed to save config", err)
  276. }
  277. return
  278. }
  279. if info, err := os.Stat(baseDirs["config"]); err == nil && !info.IsDir() {
  280. l.Fatalln("Config directory", baseDirs["config"], "is not a directory")
  281. }
  282. // Ensure that our home directory exists.
  283. ensureDir(baseDirs["config"], 0700)
  284. if upgradeTo != "" {
  285. err := upgrade.ToURL(upgradeTo)
  286. if err != nil {
  287. l.Fatalln("Upgrade:", err) // exits 1
  288. }
  289. l.Okln("Upgraded from", upgradeTo)
  290. return
  291. }
  292. if doUpgrade || doUpgradeCheck {
  293. rel, err := upgrade.LatestRelease(cfg.Options().ReleasesURL, Version)
  294. if err != nil {
  295. l.Fatalln("Upgrade:", err) // exits 1
  296. }
  297. if upgrade.CompareVersions(rel.Tag, Version) <= 0 {
  298. l.Infof("No upgrade available (current %q >= latest %q).", Version, rel.Tag)
  299. os.Exit(exitNoUpgradeAvailable)
  300. }
  301. l.Infof("Upgrade available (current %q < latest %q)", Version, rel.Tag)
  302. if doUpgrade {
  303. // Use leveldb database locks to protect against concurrent upgrades
  304. _, err = leveldb.OpenFile(locations[locDatabase], &opt.Options{OpenFilesCacheCapacity: 100})
  305. if err != nil {
  306. l.Infoln("Attempting upgrade through running Syncthing...")
  307. err = upgradeViaRest()
  308. if err != nil {
  309. l.Fatalln("Upgrade:", err)
  310. }
  311. l.Okln("Syncthing upgrading")
  312. return
  313. }
  314. err = upgrade.To(rel)
  315. if err != nil {
  316. l.Fatalln("Upgrade:", err) // exits 1
  317. }
  318. l.Okf("Upgraded to %q", rel.Tag)
  319. }
  320. return
  321. }
  322. if reset {
  323. resetDB()
  324. return
  325. }
  326. if noRestart {
  327. syncthingMain()
  328. } else {
  329. monitorMain()
  330. }
  331. }
  332. func upgradeViaRest() error {
  333. cfg, err := config.Load(locations[locConfigFile], protocol.LocalDeviceID)
  334. if err != nil {
  335. return err
  336. }
  337. target := cfg.GUI().Address
  338. if cfg.GUI().UseTLS {
  339. target = "https://" + target
  340. } else {
  341. target = "http://" + target
  342. }
  343. r, _ := http.NewRequest("POST", target+"/rest/system/upgrade", nil)
  344. r.Header.Set("X-API-Key", cfg.GUI().APIKey)
  345. tr := &http.Transport{
  346. TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
  347. }
  348. client := &http.Client{
  349. Transport: tr,
  350. Timeout: 60 * time.Second,
  351. }
  352. resp, err := client.Do(r)
  353. if err != nil {
  354. return err
  355. }
  356. if resp.StatusCode != 200 {
  357. bs, err := ioutil.ReadAll(resp.Body)
  358. defer resp.Body.Close()
  359. if err != nil {
  360. return err
  361. }
  362. return errors.New(string(bs))
  363. }
  364. return err
  365. }
  366. func syncthingMain() {
  367. // Create a main service manager. We'll add things to this as we go along.
  368. // We want any logging it does to go through our log system.
  369. mainSvc := suture.New("main", suture.Spec{
  370. Log: func(line string) {
  371. if debugSuture {
  372. l.Debugln(line)
  373. }
  374. },
  375. })
  376. mainSvc.ServeBackground()
  377. // Set a log prefix similar to the ID we will have later on, or early log
  378. // lines look ugly.
  379. l.SetPrefix("[start] ")
  380. if auditEnabled {
  381. startAuditing(mainSvc)
  382. }
  383. if verbose {
  384. mainSvc.Add(newVerboseSvc())
  385. }
  386. // Event subscription for the API; must start early to catch the early events.
  387. apiSub := events.NewBufferedSubscription(events.Default.Subscribe(events.AllEvents), 1000)
  388. if len(os.Getenv("GOMAXPROCS")) == 0 {
  389. runtime.GOMAXPROCS(runtime.NumCPU())
  390. }
  391. // Attempt to increase the limit on number of open files to the maximum
  392. // allowed, in case we have many peers. We don't really care enough to
  393. // report the error if there is one.
  394. osutil.MaximizeOpenFileLimit()
  395. // Ensure that that we have a certificate and key.
  396. cert, err := tls.LoadX509KeyPair(locations[locCertFile], locations[locKeyFile])
  397. if err != nil {
  398. l.Infof("Generating RSA key and certificate for %s...", tlsDefaultCommonName)
  399. cert, err = tlsutil.NewCertificate(locations[locCertFile], locations[locKeyFile], tlsDefaultCommonName, tlsRSABits)
  400. if err != nil {
  401. l.Fatalln(err)
  402. }
  403. }
  404. // We reinitialize the predictable RNG with our device ID, to get a
  405. // sequence that is always the same but unique to this syncthing instance.
  406. predictableRandom.Seed(seedFromBytes(cert.Certificate[0]))
  407. myID = protocol.NewDeviceID(cert.Certificate[0])
  408. l.SetPrefix(fmt.Sprintf("[%s] ", myID.String()[:5]))
  409. l.Infoln(LongVersion)
  410. l.Infoln("My ID:", myID)
  411. // Emit the Starting event, now that we know who we are.
  412. events.Default.Log(events.Starting, map[string]string{
  413. "home": baseDirs["config"],
  414. "myID": myID.String(),
  415. })
  416. // Prepare to be able to save configuration
  417. cfgFile := locations[locConfigFile]
  418. var myName string
  419. // Load the configuration file, if it exists.
  420. // If it does not, create a template.
  421. if info, err := os.Stat(cfgFile); err == nil {
  422. if !info.Mode().IsRegular() {
  423. l.Fatalln("Config file is not a file?")
  424. }
  425. cfg, err = config.Load(cfgFile, myID)
  426. if err == nil {
  427. myCfg := cfg.Devices()[myID]
  428. if myCfg.Name == "" {
  429. myName, _ = os.Hostname()
  430. } else {
  431. myName = myCfg.Name
  432. }
  433. } else {
  434. l.Fatalln("Configuration:", err)
  435. }
  436. } else {
  437. l.Infoln("No config file; starting with empty defaults")
  438. myName, _ = os.Hostname()
  439. newCfg := defaultConfig(myName)
  440. cfg = config.Wrap(cfgFile, newCfg)
  441. cfg.Save()
  442. l.Infof("Edit %s to taste or use the GUI\n", cfgFile)
  443. }
  444. if cfg.Raw().OriginalVersion != config.CurrentVersion {
  445. l.Infoln("Archiving a copy of old config file format")
  446. // Archive a copy
  447. osutil.Rename(cfgFile, cfgFile+fmt.Sprintf(".v%d", cfg.Raw().OriginalVersion))
  448. // Save the new version
  449. cfg.Save()
  450. }
  451. if err := checkShortIDs(cfg); err != nil {
  452. l.Fatalln("Short device IDs are in conflict. Unlucky!\n Regenerate the device ID of one if the following:\n ", err)
  453. }
  454. if len(profiler) > 0 {
  455. go func() {
  456. l.Debugln("Starting profiler on", profiler)
  457. runtime.SetBlockProfileRate(1)
  458. err := http.ListenAndServe(profiler, nil)
  459. if err != nil {
  460. l.Fatalln(err)
  461. }
  462. }()
  463. }
  464. // The TLS configuration is used for both the listening socket and outgoing
  465. // connections.
  466. tlsCfg := &tls.Config{
  467. Certificates: []tls.Certificate{cert},
  468. NextProtos: []string{bepProtocolName},
  469. ClientAuth: tls.RequestClientCert,
  470. SessionTicketsDisabled: true,
  471. InsecureSkipVerify: true,
  472. MinVersion: tls.VersionTLS12,
  473. CipherSuites: []uint16{
  474. tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
  475. tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
  476. tls.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,
  477. tls.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,
  478. tls.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,
  479. tls.TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,
  480. },
  481. }
  482. // If the read or write rate should be limited, set up a rate limiter for it.
  483. // This will be used on connections created in the connect and listen routines.
  484. opts := cfg.Options()
  485. if !opts.SymlinksEnabled {
  486. symlinks.Supported = false
  487. }
  488. protocol.PingTimeout = time.Duration(opts.PingTimeoutS) * time.Second
  489. protocol.PingIdleTime = time.Duration(opts.PingIdleTimeS) * time.Second
  490. if opts.MaxSendKbps > 0 {
  491. writeRateLimit = ratelimit.NewBucketWithRate(float64(1000*opts.MaxSendKbps), int64(5*1000*opts.MaxSendKbps))
  492. }
  493. if opts.MaxRecvKbps > 0 {
  494. readRateLimit = ratelimit.NewBucketWithRate(float64(1000*opts.MaxRecvKbps), int64(5*1000*opts.MaxRecvKbps))
  495. }
  496. if (opts.MaxRecvKbps > 0 || opts.MaxSendKbps > 0) && !opts.LimitBandwidthInLan {
  497. lans, _ = osutil.GetLans()
  498. networks := make([]string, 0, len(lans))
  499. for _, lan := range lans {
  500. networks = append(networks, lan.String())
  501. }
  502. for _, lan := range opts.AlwaysLocalNets {
  503. _, ipnet, err := net.ParseCIDR(lan)
  504. if err != nil {
  505. l.Infoln("Network", lan, "is malformed:", err)
  506. continue
  507. }
  508. networks = append(networks, ipnet.String())
  509. }
  510. l.Infoln("Local networks:", strings.Join(networks, ", "))
  511. }
  512. dbFile := locations[locDatabase]
  513. ldb, err := leveldb.OpenFile(dbFile, dbOpts())
  514. if leveldbIsCorrupted(err) {
  515. ldb, err = leveldb.RecoverFile(dbFile, dbOpts())
  516. }
  517. if leveldbIsCorrupted(err) {
  518. // The database is corrupted, and we've tried to recover it but it
  519. // didn't work. At this point there isn't much to do beyond dropping
  520. // the database and reindexing...
  521. l.Infoln("Database corruption detected, unable to recover. Reinitializing...")
  522. if err := resetDB(); err != nil {
  523. l.Fatalln("Remove database:", err)
  524. }
  525. ldb, err = leveldb.OpenFile(dbFile, dbOpts())
  526. }
  527. if err != nil {
  528. l.Fatalln("Cannot open database:", err, "- Is another copy of Syncthing already running?")
  529. }
  530. // Remove database entries for folders that no longer exist in the config
  531. folders := cfg.Folders()
  532. for _, folder := range db.ListFolders(ldb) {
  533. if _, ok := folders[folder]; !ok {
  534. l.Infof("Cleaning data for dropped folder %q", folder)
  535. db.DropFolder(ldb, folder)
  536. }
  537. }
  538. m := model.NewModel(cfg, myID, myName, "syncthing", Version, ldb)
  539. cfg.Subscribe(m)
  540. if t := os.Getenv("STDEADLOCKTIMEOUT"); len(t) > 0 {
  541. it, err := strconv.Atoi(t)
  542. if err == nil {
  543. m.StartDeadlockDetector(time.Duration(it) * time.Second)
  544. }
  545. } else if !IsRelease || IsBeta {
  546. m.StartDeadlockDetector(20 * time.Minute)
  547. }
  548. if paused {
  549. for device := range cfg.Devices() {
  550. m.PauseDevice(device)
  551. }
  552. }
  553. // Clear out old indexes for other devices. Otherwise we'll start up and
  554. // start needing a bunch of files which are nowhere to be found. This
  555. // needs to be changed when we correctly do persistent indexes.
  556. for _, folderCfg := range cfg.Folders() {
  557. m.AddFolder(folderCfg)
  558. for _, device := range folderCfg.DeviceIDs() {
  559. if device == myID {
  560. continue
  561. }
  562. m.Index(device, folderCfg.ID, nil, 0, nil)
  563. }
  564. // Routine to pull blocks from other devices to synchronize the local
  565. // folder. Does not run when we are in read only (publish only) mode.
  566. if folderCfg.ReadOnly {
  567. m.StartFolderRO(folderCfg.ID)
  568. } else {
  569. m.StartFolderRW(folderCfg.ID)
  570. }
  571. }
  572. mainSvc.Add(m)
  573. // The default port we announce, possibly modified by setupUPnP next.
  574. uri, err := url.Parse(opts.ListenAddress[0])
  575. if err != nil {
  576. l.Fatalf("Failed to parse listen address %s: %v", opts.ListenAddress[0], err)
  577. }
  578. addr, err := net.ResolveTCPAddr("tcp", uri.Host)
  579. if err != nil {
  580. l.Fatalln("Bad listen address:", err)
  581. }
  582. // The externalAddr tracks our external addresses for discovery purposes.
  583. var addrList *addressLister
  584. // Start UPnP. The UPnP service will restart global discovery if the
  585. // external port changes.
  586. if opts.UPnPEnabled {
  587. upnpSvc := newUPnPSvc(cfg, addr.Port)
  588. mainSvc.Add(upnpSvc)
  589. // The external address tracker needs to know about the UPnP service
  590. // so it can check for an external mapped port.
  591. addrList = newAddressLister(upnpSvc, cfg)
  592. } else {
  593. addrList = newAddressLister(nil, cfg)
  594. }
  595. // Start discovery
  596. discoverer := discovery(addrList, relaySvc)
  597. // GUI
  598. setupGUI(mainSvc, cfg, m, apiSub, discoverer)
  599. // Start relay management
  600. var relaySvc *relay.Svc
  601. if opts.RelaysEnabled && (opts.GlobalAnnEnabled || opts.RelayWithoutGlobalAnn) {
  602. relaySvc = relay.NewSvc(cfg, tlsCfg)
  603. mainSvc.Add(relaySvc)
  604. }
  605. // Start connection management
  606. connectionSvc := newConnectionSvc(cfg, myID, m, tlsCfg, discoverer, relaySvc)
  607. mainSvc.Add(connectionSvc)
  608. if cpuProfile {
  609. f, err := os.Create(fmt.Sprintf("cpu-%d.pprof", os.Getpid()))
  610. if err != nil {
  611. log.Fatal(err)
  612. }
  613. pprof.StartCPUProfile(f)
  614. }
  615. for _, device := range cfg.Devices() {
  616. if len(device.Name) > 0 {
  617. l.Infof("Device %s is %q at %v", device.DeviceID, device.Name, device.Addresses)
  618. }
  619. }
  620. if opts.URAccepted > 0 && opts.URAccepted < usageReportVersion {
  621. l.Infoln("Anonymous usage report has changed; revoking acceptance")
  622. opts.URAccepted = 0
  623. opts.URUniqueID = ""
  624. cfg.SetOptions(opts)
  625. }
  626. if opts.URAccepted >= usageReportVersion {
  627. if opts.URUniqueID == "" {
  628. // Previously the ID was generated from the node ID. We now need
  629. // to generate a new one.
  630. opts.URUniqueID = randomString(8)
  631. cfg.SetOptions(opts)
  632. cfg.Save()
  633. }
  634. }
  635. // The usageReportingManager registers itself to listen to configuration
  636. // changes, and there's nothing more we need to tell it from the outside.
  637. // Hence we don't keep the returned pointer.
  638. newUsageReportingManager(m, cfg)
  639. if opts.RestartOnWakeup {
  640. go standbyMonitor()
  641. }
  642. if opts.AutoUpgradeIntervalH > 0 {
  643. if noUpgrade {
  644. l.Infof("No automatic upgrades; STNOUPGRADE environment variable defined.")
  645. } else if IsRelease {
  646. go autoUpgrade()
  647. } else {
  648. l.Infof("No automatic upgrades; %s is not a release version.", Version)
  649. }
  650. }
  651. events.Default.Log(events.StartupComplete, map[string]string{
  652. "myID": myID.String(),
  653. })
  654. go generatePingEvents()
  655. cleanConfigDirectory()
  656. code := <-stop
  657. mainSvc.Stop()
  658. l.Okln("Exiting")
  659. if cpuProfile {
  660. pprof.StopCPUProfile()
  661. }
  662. os.Exit(code)
  663. }
  664. func dbOpts() *opt.Options {
  665. // Calculate a suitable database block cache capacity.
  666. // Default is 8 MiB.
  667. blockCacheCapacity := 8 << 20
  668. // Increase block cache up to this maximum:
  669. const maxCapacity = 64 << 20
  670. // ... which we reach when the box has this much RAM:
  671. const maxAtRAM = 8 << 30
  672. if v := cfg.Options().DatabaseBlockCacheMiB; v != 0 {
  673. // Use the value from the config, if it's set.
  674. blockCacheCapacity = v << 20
  675. } else if bytes, err := memorySize(); err == nil {
  676. // We start at the default of 8 MiB and use larger values for machines
  677. // with more memory.
  678. if bytes > maxAtRAM {
  679. // Cap the cache at maxCapacity when we reach maxAtRam amount of memory
  680. blockCacheCapacity = maxCapacity
  681. } else if bytes > maxAtRAM/maxCapacity*int64(blockCacheCapacity) {
  682. // Grow from the default to maxCapacity at maxAtRam amount of memory
  683. blockCacheCapacity = int(bytes * maxCapacity / maxAtRAM)
  684. }
  685. l.Infoln("Database block cache capacity", blockCacheCapacity/1024, "KiB")
  686. }
  687. return &opt.Options{
  688. OpenFilesCacheCapacity: 100,
  689. BlockCacheCapacity: blockCacheCapacity,
  690. WriteBuffer: 4 << 20,
  691. }
  692. }
  693. func startAuditing(mainSvc *suture.Supervisor) {
  694. auditFile := timestampedLoc(locAuditLog)
  695. fd, err := os.OpenFile(auditFile, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0600)
  696. if err != nil {
  697. l.Fatalln("Audit:", err)
  698. }
  699. auditSvc := newAuditSvc(fd)
  700. mainSvc.Add(auditSvc)
  701. // We wait for the audit service to fully start before we return, to
  702. // ensure we capture all events from the start.
  703. auditSvc.WaitForStart()
  704. l.Infoln("Audit log in", auditFile)
  705. }
  706. func setupGUI(mainSvc *suture.Supervisor, cfg *config.Wrapper, m *model.Model, apiSub *events.BufferedSubscription, discoverer *discover.Discoverer) {
  707. opts := cfg.Options()
  708. guiCfg := overrideGUIConfig(cfg.GUI(), guiAddress, guiAuthentication, guiAPIKey)
  709. if guiCfg.Enabled && guiCfg.Address != "" {
  710. addr, err := net.ResolveTCPAddr("tcp", guiCfg.Address)
  711. if err != nil {
  712. l.Fatalf("Cannot start GUI on %q: %v", guiCfg.Address, err)
  713. } else {
  714. var hostOpen, hostShow string
  715. switch {
  716. case addr.IP == nil:
  717. hostOpen = "localhost"
  718. hostShow = "0.0.0.0"
  719. case addr.IP.IsUnspecified():
  720. hostOpen = "localhost"
  721. hostShow = addr.IP.String()
  722. default:
  723. hostOpen = addr.IP.String()
  724. hostShow = hostOpen
  725. }
  726. var proto = "http"
  727. if guiCfg.UseTLS {
  728. proto = "https"
  729. }
  730. urlShow := fmt.Sprintf("%s://%s/", proto, net.JoinHostPort(hostShow, strconv.Itoa(addr.Port)))
  731. l.Infoln("Starting web GUI on", urlShow)
  732. api, err := newAPISvc(myID, guiCfg, guiAssets, m, apiSub, discoverer)
  733. if err != nil {
  734. l.Fatalln("Cannot start GUI:", err)
  735. }
  736. cfg.Subscribe(api)
  737. mainSvc.Add(api)
  738. if opts.StartBrowser && !noBrowser && !stRestarting {
  739. urlOpen := fmt.Sprintf("%s://%s/", proto, net.JoinHostPort(hostOpen, strconv.Itoa(addr.Port)))
  740. // Can potentially block if the utility we are invoking doesn't
  741. // fork, and just execs, hence keep it in it's own routine.
  742. go openURL(urlOpen)
  743. }
  744. }
  745. }
  746. }
  747. func defaultConfig(myName string) config.Configuration {
  748. newCfg := config.New(myID)
  749. newCfg.Folders = []config.FolderConfiguration{
  750. {
  751. ID: "default",
  752. RawPath: locations[locDefFolder],
  753. RescanIntervalS: 60,
  754. MinDiskFreePct: 1,
  755. Devices: []config.FolderDeviceConfiguration{{DeviceID: myID}},
  756. },
  757. }
  758. newCfg.Devices = []config.DeviceConfiguration{
  759. {
  760. DeviceID: myID,
  761. Addresses: []string{"dynamic"},
  762. Name: myName,
  763. },
  764. }
  765. port, err := getFreePort("127.0.0.1", 8384)
  766. if err != nil {
  767. l.Fatalln("get free port (GUI):", err)
  768. }
  769. newCfg.GUI.Address = fmt.Sprintf("127.0.0.1:%d", port)
  770. port, err = getFreePort("0.0.0.0", 22000)
  771. if err != nil {
  772. l.Fatalln("get free port (BEP):", err)
  773. }
  774. newCfg.Options.ListenAddress = []string{fmt.Sprintf("tcp://0.0.0.0:%d", port)}
  775. return newCfg
  776. }
  777. func generatePingEvents() {
  778. for {
  779. time.Sleep(pingEventInterval)
  780. events.Default.Log(events.Ping, nil)
  781. }
  782. }
  783. func resetDB() error {
  784. return os.RemoveAll(locations[locDatabase])
  785. }
  786. func restart() {
  787. l.Infoln("Restarting")
  788. stop <- exitRestarting
  789. }
  790. func shutdown() {
  791. l.Infoln("Shutting down")
  792. stop <- exitSuccess
  793. }
  794. func discovery(addrList *addressLister, relaySvc *relay.Svc) *discover.Discoverer {
  795. opts := cfg.Options()
  796. disc := discover.NewDiscoverer(myID, opts.ListenAddress, relaySvc)
  797. if opts.LocalAnnEnabled {
  798. l.Infoln("Starting local discovery announcements")
  799. disc.StartLocal(opts.LocalAnnPort, opts.LocalAnnMCAddr)
  800. }
  801. if opts.GlobalAnnEnabled {
  802. go func() {
  803. // Defer starting global announce server, giving time to connect
  804. // to relay servers.
  805. time.Sleep(5 * time.Second)
  806. l.Infoln("Starting global discovery announcements")
  807. disc.StartGlobal(opts.GlobalAnnServers, addrList)
  808. }()
  809. }
  810. return disc
  811. }
  812. func ensureDir(dir string, mode int) {
  813. fi, err := os.Stat(dir)
  814. if os.IsNotExist(err) {
  815. err := osutil.MkdirAll(dir, 0700)
  816. if err != nil {
  817. l.Fatalln(err)
  818. }
  819. } else if mode >= 0 && err == nil && int(fi.Mode()&0777) != mode {
  820. err := os.Chmod(dir, os.FileMode(mode))
  821. // This can fail on crappy filesystems, nothing we can do about it.
  822. if err != nil {
  823. l.Warnln(err)
  824. }
  825. }
  826. }
  827. // getFreePort returns a free TCP port fort listening on. The ports given are
  828. // tried in succession and the first to succeed is returned. If none succeed,
  829. // a random high port is returned.
  830. func getFreePort(host string, ports ...int) (int, error) {
  831. for _, port := range ports {
  832. c, err := net.Listen("tcp", fmt.Sprintf("%s:%d", host, port))
  833. if err == nil {
  834. c.Close()
  835. return port, nil
  836. }
  837. }
  838. c, err := net.Listen("tcp", host+":0")
  839. if err != nil {
  840. return 0, err
  841. }
  842. addr := c.Addr().(*net.TCPAddr)
  843. c.Close()
  844. return addr.Port, nil
  845. }
  846. func overrideGUIConfig(cfg config.GUIConfiguration, address, authentication, apikey string) config.GUIConfiguration {
  847. if address != "" {
  848. cfg.Enabled = true
  849. if !strings.Contains(address, "//") {
  850. // Assume just an IP was given. Don't touch he TLS setting.
  851. cfg.Address = address
  852. } else {
  853. parsed, err := url.Parse(address)
  854. if err != nil {
  855. l.Fatalln(err)
  856. }
  857. cfg.Address = parsed.Host
  858. switch parsed.Scheme {
  859. case "http":
  860. cfg.UseTLS = false
  861. case "https":
  862. cfg.UseTLS = true
  863. default:
  864. l.Fatalln("Unknown scheme:", parsed.Scheme)
  865. }
  866. }
  867. }
  868. if authentication != "" {
  869. authenticationParts := strings.SplitN(authentication, ":", 2)
  870. hash, err := bcrypt.GenerateFromPassword([]byte(authenticationParts[1]), 0)
  871. if err != nil {
  872. l.Fatalln("Invalid GUI password:", err)
  873. }
  874. cfg.User = authenticationParts[0]
  875. cfg.Password = string(hash)
  876. }
  877. if apikey != "" {
  878. cfg.APIKey = apikey
  879. }
  880. return cfg
  881. }
  882. func standbyMonitor() {
  883. restartDelay := time.Duration(60 * time.Second)
  884. now := time.Now()
  885. for {
  886. time.Sleep(10 * time.Second)
  887. if time.Since(now) > 2*time.Minute {
  888. l.Infof("Paused state detected, possibly woke up from standby. Restarting in %v.", restartDelay)
  889. // We most likely just woke from standby. If we restart
  890. // immediately chances are we won't have networking ready. Give
  891. // things a moment to stabilize.
  892. time.Sleep(restartDelay)
  893. restart()
  894. return
  895. }
  896. now = time.Now()
  897. }
  898. }
  899. func autoUpgrade() {
  900. timer := time.NewTimer(0)
  901. sub := events.Default.Subscribe(events.DeviceConnected)
  902. for {
  903. select {
  904. case event := <-sub.C():
  905. data, ok := event.Data.(map[string]string)
  906. if !ok || data["clientName"] != "syncthing" || upgrade.CompareVersions(data["clientVersion"], Version) != upgrade.Newer {
  907. continue
  908. }
  909. l.Infof("Connected to device %s with a newer version (current %q < remote %q). Checking for upgrades.", data["id"], Version, data["clientVersion"])
  910. case <-timer.C:
  911. }
  912. rel, err := upgrade.LatestRelease(cfg.Options().ReleasesURL, Version)
  913. if err == upgrade.ErrUpgradeUnsupported {
  914. events.Default.Unsubscribe(sub)
  915. return
  916. }
  917. if err != nil {
  918. // Don't complain too loudly here; we might simply not have
  919. // internet connectivity, or the upgrade server might be down.
  920. l.Infoln("Automatic upgrade:", err)
  921. timer.Reset(time.Duration(cfg.Options().AutoUpgradeIntervalH) * time.Hour)
  922. continue
  923. }
  924. if upgrade.CompareVersions(rel.Tag, Version) != upgrade.Newer {
  925. // Skip equal, older or majorly newer (incompatible) versions
  926. timer.Reset(time.Duration(cfg.Options().AutoUpgradeIntervalH) * time.Hour)
  927. continue
  928. }
  929. l.Infof("Automatic upgrade (current %q < latest %q)", Version, rel.Tag)
  930. err = upgrade.To(rel)
  931. if err != nil {
  932. l.Warnln("Automatic upgrade:", err)
  933. timer.Reset(time.Duration(cfg.Options().AutoUpgradeIntervalH) * time.Hour)
  934. continue
  935. }
  936. events.Default.Unsubscribe(sub)
  937. l.Warnf("Automatically upgraded to version %q. Restarting in 1 minute.", rel.Tag)
  938. time.Sleep(time.Minute)
  939. stop <- exitUpgrading
  940. return
  941. }
  942. }
  943. // cleanConfigDirectory removes old, unused configuration and index formats, a
  944. // suitable time after they have gone out of fashion.
  945. func cleanConfigDirectory() {
  946. patterns := map[string]time.Duration{
  947. "panic-*.log": 7 * 24 * time.Hour, // keep panic logs for a week
  948. "audit-*.log": 7 * 24 * time.Hour, // keep audit logs for a week
  949. "index": 14 * 24 * time.Hour, // keep old index format for two weeks
  950. "config.xml.v*": 30 * 24 * time.Hour, // old config versions for a month
  951. "*.idx.gz": 30 * 24 * time.Hour, // these should for sure no longer exist
  952. "backup-of-v0.8": 30 * 24 * time.Hour, // these neither
  953. }
  954. for pat, dur := range patterns {
  955. pat = filepath.Join(baseDirs["config"], pat)
  956. files, err := osutil.Glob(pat)
  957. if err != nil {
  958. l.Infoln("Cleaning:", err)
  959. continue
  960. }
  961. for _, file := range files {
  962. info, err := osutil.Lstat(file)
  963. if err != nil {
  964. l.Infoln("Cleaning:", err)
  965. continue
  966. }
  967. if time.Since(info.ModTime()) > dur {
  968. if err = os.RemoveAll(file); err != nil {
  969. l.Infoln("Cleaning:", err)
  970. } else {
  971. l.Infoln("Cleaned away old file", filepath.Base(file))
  972. }
  973. }
  974. }
  975. }
  976. }
  977. // checkShortIDs verifies that the configuration won't result in duplicate
  978. // short ID:s; that is, that the devices in the cluster all have unique
  979. // initial 64 bits.
  980. func checkShortIDs(cfg *config.Wrapper) error {
  981. exists := make(map[uint64]protocol.DeviceID)
  982. for deviceID := range cfg.Devices() {
  983. shortID := deviceID.Short()
  984. if otherID, ok := exists[shortID]; ok {
  985. return fmt.Errorf("%v in conflict with %v", deviceID, otherID)
  986. }
  987. exists[shortID] = deviceID
  988. }
  989. return nil
  990. }
  991. // A "better" version of leveldb's errors.IsCorrupted.
  992. func leveldbIsCorrupted(err error) bool {
  993. switch {
  994. case err == nil:
  995. return false
  996. case errors.IsCorrupted(err):
  997. return true
  998. case strings.Contains(err.Error(), "corrupted"):
  999. return true
  1000. }
  1001. return false
  1002. }