2014-11-16 20:13:20 +00:00
|
|
|
// Copyright (C) 2014 The Syncthing Authors.
|
2014-09-29 19:43:32 +00:00
|
|
|
//
|
2015-03-07 20:36:35 +00:00
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
2017-02-09 06:52:18 +00:00
|
|
|
// You can obtain one at https://mozilla.org/MPL/2.0/.
|
2014-09-04 06:31:38 +00:00
|
|
|
|
2019-03-26 19:53:58 +00:00
|
|
|
package ur
|
2014-06-11 18:04:23 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2017-04-26 00:15:23 +00:00
|
|
|
"context"
|
2015-09-10 12:08:40 +00:00
|
|
|
"crypto/tls"
|
2014-06-11 18:04:23 +00:00
|
|
|
"encoding/json"
|
2021-03-02 18:17:20 +00:00
|
|
|
"math/rand"
|
2017-10-15 07:45:15 +00:00
|
|
|
"net"
|
2014-06-11 18:04:23 +00:00
|
|
|
"net/http"
|
|
|
|
"runtime"
|
2015-09-06 14:36:09 +00:00
|
|
|
"sort"
|
2016-05-04 19:38:12 +00:00
|
|
|
"strings"
|
2019-08-25 21:43:28 +00:00
|
|
|
"sync"
|
2014-06-11 18:04:23 +00:00
|
|
|
"time"
|
|
|
|
|
2019-02-12 06:58:24 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/build"
|
2015-08-06 09:29:25 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/config"
|
2017-10-12 06:16:46 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/connections"
|
2021-07-27 19:27:52 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/db"
|
2015-10-12 18:30:14 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/dialer"
|
2015-09-22 17:38:46 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/protocol"
|
2017-01-23 21:56:43 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/scanner"
|
2015-09-06 14:36:09 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/upgrade"
|
2020-06-23 08:47:15 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/ur/contract"
|
2014-06-11 18:04:23 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Current version number of the usage report, for acceptance purposes. If
|
|
|
|
// fields are added or changed this integer must be incremented so that users
|
|
|
|
// are prompted for acceptance of the new report.
|
2019-03-26 19:53:58 +00:00
|
|
|
const Version = 3
|
2014-06-11 18:04:23 +00:00
|
|
|
|
2021-03-12 09:35:10 +00:00
|
|
|
var StartTime = time.Now().Truncate(time.Second)
|
2019-03-26 19:53:58 +00:00
|
|
|
|
2021-07-27 19:27:52 +00:00
|
|
|
type Model interface {
|
|
|
|
DBSnapshot(folder string) (*db.Snapshot, error)
|
|
|
|
UsageReportingStats(report *contract.Report, version int, preview bool)
|
|
|
|
}
|
|
|
|
|
2019-03-26 19:53:58 +00:00
|
|
|
type Service struct {
|
|
|
|
cfg config.Wrapper
|
2021-07-27 19:27:52 +00:00
|
|
|
model Model
|
2019-03-26 19:53:58 +00:00
|
|
|
connectionsService connections.Service
|
|
|
|
noUpgrade bool
|
|
|
|
forceRun chan struct{}
|
|
|
|
}
|
|
|
|
|
2021-07-27 19:27:52 +00:00
|
|
|
func New(cfg config.Wrapper, m Model, connectionsService connections.Service, noUpgrade bool) *Service {
|
2020-11-17 12:19:04 +00:00
|
|
|
return &Service{
|
2019-03-26 19:53:58 +00:00
|
|
|
cfg: cfg,
|
|
|
|
model: m,
|
|
|
|
connectionsService: connectionsService,
|
|
|
|
noUpgrade: noUpgrade,
|
2019-07-09 09:40:30 +00:00
|
|
|
forceRun: make(chan struct{}, 1), // Buffered to prevent locking
|
2019-03-26 19:53:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReportData returns the data to be sent in a usage report with the currently
|
|
|
|
// configured usage reporting version.
|
2020-06-23 08:47:15 +00:00
|
|
|
func (s *Service) ReportData(ctx context.Context) (*contract.Report, error) {
|
2019-06-11 06:19:11 +00:00
|
|
|
urVersion := s.cfg.Options().URAccepted
|
2020-02-24 20:57:15 +00:00
|
|
|
return s.reportData(ctx, urVersion, false)
|
2019-03-26 19:53:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ReportDataPreview returns a preview of the data to be sent in a usage report
|
|
|
|
// with the given version.
|
2020-06-23 08:47:15 +00:00
|
|
|
func (s *Service) ReportDataPreview(ctx context.Context, urVersion int) (*contract.Report, error) {
|
2020-02-24 20:57:15 +00:00
|
|
|
return s.reportData(ctx, urVersion, true)
|
2019-03-26 19:53:58 +00:00
|
|
|
}
|
|
|
|
|
2020-06-23 08:47:15 +00:00
|
|
|
func (s *Service) reportData(ctx context.Context, urVersion int, preview bool) (*contract.Report, error) {
|
2019-03-26 19:53:58 +00:00
|
|
|
opts := s.cfg.Options()
|
2021-02-04 20:10:41 +00:00
|
|
|
defaultFolder := s.cfg.DefaultFolder()
|
2014-06-11 18:04:23 +00:00
|
|
|
|
|
|
|
var totFiles, maxFiles int
|
|
|
|
var totBytes, maxBytes int64
|
2019-03-26 19:53:58 +00:00
|
|
|
for folderID := range s.cfg.Folders() {
|
2020-01-21 17:23:08 +00:00
|
|
|
snap, err := s.model.DBSnapshot(folderID)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
global := snap.GlobalSize()
|
|
|
|
snap.Release()
|
2017-12-14 09:51:17 +00:00
|
|
|
totFiles += int(global.Files)
|
2016-10-17 12:10:17 +00:00
|
|
|
totBytes += global.Bytes
|
2017-12-14 09:51:17 +00:00
|
|
|
if int(global.Files) > maxFiles {
|
|
|
|
maxFiles = int(global.Files)
|
2014-06-11 18:04:23 +00:00
|
|
|
}
|
2016-10-17 12:10:17 +00:00
|
|
|
if global.Bytes > maxBytes {
|
|
|
|
maxBytes = global.Bytes
|
2014-06-11 18:04:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var mem runtime.MemStats
|
|
|
|
runtime.ReadMemStats(&mem)
|
|
|
|
|
2020-06-23 08:47:15 +00:00
|
|
|
report := contract.New()
|
|
|
|
|
|
|
|
report.URVersion = urVersion
|
|
|
|
report.UniqueID = opts.URUniqueID
|
|
|
|
report.Version = build.Version
|
|
|
|
report.LongVersion = build.LongVersion
|
|
|
|
report.Platform = runtime.GOOS + "-" + runtime.GOARCH
|
|
|
|
report.NumFolders = len(s.cfg.Folders())
|
|
|
|
report.NumDevices = len(s.cfg.Devices())
|
|
|
|
report.TotFiles = totFiles
|
|
|
|
report.FolderMaxFiles = maxFiles
|
|
|
|
report.TotMiB = int(totBytes / 1024 / 1024)
|
|
|
|
report.FolderMaxMiB = int(maxBytes / 1024 / 1024)
|
|
|
|
report.MemoryUsageMiB = int((mem.Sys - mem.HeapReleased) / 1024 / 1024)
|
|
|
|
report.SHA256Perf = CpuBench(ctx, 5, 125*time.Millisecond, false)
|
|
|
|
report.HashPerf = CpuBench(ctx, 5, 125*time.Millisecond, true)
|
|
|
|
report.MemorySize = int(memorySize() / 1024 / 1024)
|
|
|
|
report.NumCPU = runtime.NumCPU()
|
|
|
|
|
2019-03-26 19:53:58 +00:00
|
|
|
for _, cfg := range s.cfg.Folders() {
|
2020-06-23 08:47:15 +00:00
|
|
|
report.RescanIntvs = append(report.RescanIntvs, cfg.RescanIntervalS)
|
2015-09-06 14:36:09 +00:00
|
|
|
|
2018-05-13 07:58:00 +00:00
|
|
|
switch cfg.Type {
|
|
|
|
case config.FolderTypeSendOnly:
|
2020-06-23 08:47:15 +00:00
|
|
|
report.FolderUses.SendOnly++
|
2018-05-13 07:58:00 +00:00
|
|
|
case config.FolderTypeSendReceive:
|
2020-06-23 08:47:15 +00:00
|
|
|
report.FolderUses.SendReceive++
|
2018-09-09 11:55:19 +00:00
|
|
|
case config.FolderTypeReceiveOnly:
|
2020-06-23 08:47:15 +00:00
|
|
|
report.FolderUses.ReceiveOnly++
|
2015-09-06 14:36:09 +00:00
|
|
|
}
|
|
|
|
if cfg.IgnorePerms {
|
2020-06-23 08:47:15 +00:00
|
|
|
report.FolderUses.IgnorePerms++
|
2015-09-06 14:36:09 +00:00
|
|
|
}
|
|
|
|
if cfg.IgnoreDelete {
|
2020-06-23 08:47:15 +00:00
|
|
|
report.FolderUses.IgnoreDelete++
|
2015-09-06 14:36:09 +00:00
|
|
|
}
|
|
|
|
if cfg.AutoNormalize {
|
2020-06-23 08:47:15 +00:00
|
|
|
report.FolderUses.AutoNormalize++
|
2015-09-06 14:36:09 +00:00
|
|
|
}
|
2020-06-23 08:47:15 +00:00
|
|
|
switch cfg.Versioning.Type {
|
|
|
|
case "":
|
|
|
|
// None
|
|
|
|
case "simple":
|
|
|
|
report.FolderUses.SimpleVersioning++
|
|
|
|
case "staggered":
|
|
|
|
report.FolderUses.StaggeredVersioning++
|
|
|
|
case "external":
|
|
|
|
report.FolderUses.ExternalVersioning++
|
|
|
|
case "trashcan":
|
|
|
|
report.FolderUses.TrashcanVersioning++
|
|
|
|
default:
|
|
|
|
l.Warnf("Unhandled versioning type for usage reports: %s", cfg.Versioning.Type)
|
2016-02-13 07:18:40 +00:00
|
|
|
}
|
2015-09-06 14:36:09 +00:00
|
|
|
}
|
2020-06-23 08:47:15 +00:00
|
|
|
sort.Ints(report.RescanIntvs)
|
|
|
|
|
2019-03-26 19:53:58 +00:00
|
|
|
for _, cfg := range s.cfg.Devices() {
|
2015-09-06 14:36:09 +00:00
|
|
|
if cfg.Introducer {
|
2020-06-23 08:47:15 +00:00
|
|
|
report.DeviceUses.Introducer++
|
2015-09-06 14:36:09 +00:00
|
|
|
}
|
|
|
|
if cfg.CertName != "" && cfg.CertName != "syncthing" {
|
2020-06-23 08:47:15 +00:00
|
|
|
report.DeviceUses.CustomCertName++
|
2015-09-06 14:36:09 +00:00
|
|
|
}
|
2020-06-23 08:47:15 +00:00
|
|
|
switch cfg.Compression {
|
2020-10-02 06:07:05 +00:00
|
|
|
case protocol.CompressionAlways:
|
2020-06-23 08:47:15 +00:00
|
|
|
report.DeviceUses.CompressAlways++
|
2020-10-02 06:07:05 +00:00
|
|
|
case protocol.CompressionMetadata:
|
2020-06-23 08:47:15 +00:00
|
|
|
report.DeviceUses.CompressMetadata++
|
2020-10-02 06:07:05 +00:00
|
|
|
case protocol.CompressionNever:
|
2020-06-23 08:47:15 +00:00
|
|
|
report.DeviceUses.CompressNever++
|
|
|
|
default:
|
|
|
|
l.Warnf("Unhandled versioning type for usage reports: %s", cfg.Compression)
|
2015-09-06 14:36:09 +00:00
|
|
|
}
|
2020-06-23 08:47:15 +00:00
|
|
|
|
2015-09-06 14:36:09 +00:00
|
|
|
for _, addr := range cfg.Addresses {
|
|
|
|
if addr == "dynamic" {
|
2020-06-23 08:47:15 +00:00
|
|
|
report.DeviceUses.DynamicAddr++
|
2015-09-06 14:36:09 +00:00
|
|
|
} else {
|
2020-06-23 08:47:15 +00:00
|
|
|
report.DeviceUses.StaticAddr++
|
2015-09-06 14:36:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-23 08:47:15 +00:00
|
|
|
report.Announce.GlobalEnabled = opts.GlobalAnnEnabled
|
|
|
|
report.Announce.LocalEnabled = opts.LocalAnnEnabled
|
2019-11-26 16:07:25 +00:00
|
|
|
for _, addr := range opts.RawGlobalAnnServers {
|
2015-11-09 14:35:14 +00:00
|
|
|
if addr == "default" || addr == "default-v4" || addr == "default-v6" {
|
2020-06-23 08:47:15 +00:00
|
|
|
report.Announce.DefaultServersDNS++
|
2015-09-21 08:54:21 +00:00
|
|
|
} else {
|
2020-06-23 08:47:15 +00:00
|
|
|
report.Announce.OtherServers++
|
2015-09-06 14:36:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-23 08:47:15 +00:00
|
|
|
report.Relays.Enabled = opts.RelaysEnabled
|
2019-11-26 16:07:25 +00:00
|
|
|
for _, addr := range s.cfg.Options().ListenAddresses() {
|
2016-05-04 19:38:12 +00:00
|
|
|
switch {
|
|
|
|
case addr == "dynamic+https://relays.syncthing.net/endpoint":
|
2020-06-23 08:47:15 +00:00
|
|
|
report.Relays.DefaultServers++
|
2016-05-04 19:38:12 +00:00
|
|
|
case strings.HasPrefix(addr, "relay://") || strings.HasPrefix(addr, "dynamic+http"):
|
2020-06-23 08:47:15 +00:00
|
|
|
report.Relays.OtherServers++
|
|
|
|
|
2015-09-06 19:15:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-23 08:47:15 +00:00
|
|
|
report.UsesRateLimit = opts.MaxRecvKbps > 0 || opts.MaxSendKbps > 0
|
|
|
|
report.UpgradeAllowedManual = !(upgrade.DisabledByCompilation || s.noUpgrade)
|
2020-08-18 07:26:33 +00:00
|
|
|
report.UpgradeAllowedAuto = !(upgrade.DisabledByCompilation || s.noUpgrade) && opts.AutoUpgradeEnabled()
|
|
|
|
report.UpgradeAllowedPre = !(upgrade.DisabledByCompilation || s.noUpgrade) && opts.AutoUpgradeEnabled() && opts.UpgradeToPreReleases
|
2015-09-06 14:36:09 +00:00
|
|
|
|
2020-06-23 08:47:15 +00:00
|
|
|
// V3
|
2014-06-12 18:47:46 +00:00
|
|
|
|
2019-03-26 19:53:58 +00:00
|
|
|
if urVersion >= 3 {
|
2020-06-23 08:47:15 +00:00
|
|
|
report.Uptime = s.UptimeS()
|
|
|
|
report.NATType = s.connectionsService.NATType()
|
|
|
|
report.AlwaysLocalNets = len(opts.AlwaysLocalNets) > 0
|
|
|
|
report.CacheIgnoredFiles = opts.CacheIgnoredFiles
|
|
|
|
report.OverwriteRemoteDeviceNames = opts.OverwriteRemoteDevNames
|
|
|
|
report.ProgressEmitterEnabled = opts.ProgressUpdateIntervalS > -1
|
2021-02-04 20:10:41 +00:00
|
|
|
report.CustomDefaultFolderPath = defaultFolder.Path != "~"
|
2020-06-23 08:47:15 +00:00
|
|
|
report.CustomTrafficClass = opts.TrafficClass != 0
|
|
|
|
report.CustomTempIndexMinBlocks = opts.TempIndexMinBlocks != 10
|
|
|
|
report.TemporariesDisabled = opts.KeepTemporariesH == 0
|
|
|
|
report.TemporariesCustom = opts.KeepTemporariesH != 24
|
|
|
|
report.LimitBandwidthInLan = opts.LimitBandwidthInLan
|
2023-03-22 21:57:26 +00:00
|
|
|
report.CustomReleaseURL = opts.ReleasesURL != "https://upgrades.syncthing.net/meta.json"
|
2020-06-23 08:47:15 +00:00
|
|
|
report.CustomStunServers = len(opts.RawStunServers) != 1 || opts.RawStunServers[0] != "default"
|
|
|
|
|
2019-03-26 19:53:58 +00:00
|
|
|
for _, cfg := range s.cfg.Folders() {
|
2017-10-15 07:45:15 +00:00
|
|
|
if cfg.ScanProgressIntervalS < 0 {
|
2020-06-23 08:47:15 +00:00
|
|
|
report.FolderUsesV3.ScanProgressDisabled++
|
2017-10-15 07:45:15 +00:00
|
|
|
}
|
|
|
|
if cfg.MaxConflicts == 0 {
|
2020-06-23 08:47:15 +00:00
|
|
|
report.FolderUsesV3.ConflictsDisabled++
|
2017-10-15 07:45:15 +00:00
|
|
|
} else if cfg.MaxConflicts < 0 {
|
2020-06-23 08:47:15 +00:00
|
|
|
report.FolderUsesV3.ConflictsUnlimited++
|
2017-10-15 07:45:15 +00:00
|
|
|
} else {
|
2020-06-23 08:47:15 +00:00
|
|
|
report.FolderUsesV3.ConflictsOther++
|
2017-10-15 07:45:15 +00:00
|
|
|
}
|
|
|
|
if cfg.DisableSparseFiles {
|
2020-06-23 08:47:15 +00:00
|
|
|
report.FolderUsesV3.DisableSparseFiles++
|
2017-10-15 07:45:15 +00:00
|
|
|
}
|
|
|
|
if cfg.DisableTempIndexes {
|
2020-06-23 08:47:15 +00:00
|
|
|
report.FolderUsesV3.DisableTempIndexes++
|
2017-10-15 07:45:15 +00:00
|
|
|
}
|
|
|
|
if cfg.WeakHashThresholdPct < 0 {
|
2020-06-23 08:47:15 +00:00
|
|
|
report.FolderUsesV3.AlwaysWeakHash++
|
2017-10-15 07:45:15 +00:00
|
|
|
} else if cfg.WeakHashThresholdPct != 25 {
|
2020-06-23 08:47:15 +00:00
|
|
|
report.FolderUsesV3.CustomWeakHashThreshold++
|
2017-10-15 07:45:15 +00:00
|
|
|
}
|
2017-10-20 16:25:20 +00:00
|
|
|
if cfg.FSWatcherEnabled {
|
2020-06-23 08:47:15 +00:00
|
|
|
report.FolderUsesV3.FsWatcherEnabled++
|
2017-10-20 16:25:20 +00:00
|
|
|
}
|
2020-06-23 08:47:15 +00:00
|
|
|
report.FolderUsesV3.PullOrder[cfg.Order.String()]++
|
|
|
|
report.FolderUsesV3.FilesystemType[cfg.FilesystemType.String()]++
|
2023-03-18 07:50:38 +00:00
|
|
|
report.FolderUsesV3.FsWatcherDelays = append(report.FolderUsesV3.FsWatcherDelays, int(cfg.FSWatcherDelayS))
|
2020-06-28 18:35:22 +00:00
|
|
|
if cfg.MarkerName != config.DefaultMarkerName {
|
|
|
|
report.FolderUsesV3.CustomMarkerName++
|
|
|
|
}
|
|
|
|
if cfg.CopyOwnershipFromParent {
|
|
|
|
report.FolderUsesV3.CopyOwnershipFromParent++
|
|
|
|
}
|
|
|
|
report.FolderUsesV3.ModTimeWindowS = append(report.FolderUsesV3.ModTimeWindowS, int(cfg.ModTimeWindow().Seconds()))
|
|
|
|
report.FolderUsesV3.MaxConcurrentWrites = append(report.FolderUsesV3.MaxConcurrentWrites, cfg.MaxConcurrentWrites)
|
|
|
|
if cfg.DisableFsync {
|
|
|
|
report.FolderUsesV3.DisableFsync++
|
|
|
|
}
|
|
|
|
report.FolderUsesV3.BlockPullOrder[cfg.BlockPullOrder.String()]++
|
|
|
|
report.FolderUsesV3.CopyRangeMethod[cfg.CopyRangeMethod.String()]++
|
2020-07-28 09:13:15 +00:00
|
|
|
if cfg.CaseSensitiveFS {
|
|
|
|
report.FolderUsesV3.CaseSensitiveFS++
|
|
|
|
}
|
2021-03-10 22:26:56 +00:00
|
|
|
if cfg.Type == config.FolderTypeReceiveEncrypted {
|
|
|
|
report.FolderUsesV3.ReceiveEncrypted++
|
|
|
|
}
|
2017-10-15 07:45:15 +00:00
|
|
|
}
|
2020-06-23 08:47:15 +00:00
|
|
|
sort.Ints(report.FolderUsesV3.FsWatcherDelays)
|
2017-10-15 07:45:15 +00:00
|
|
|
|
2020-11-09 14:33:32 +00:00
|
|
|
for _, cfg := range s.cfg.Devices() {
|
|
|
|
if cfg.Untrusted {
|
|
|
|
report.DeviceUsesV3.Untrusted++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-26 19:53:58 +00:00
|
|
|
guiCfg := s.cfg.GUI()
|
2017-10-15 07:45:15 +00:00
|
|
|
// Anticipate multiple GUI configs in the future, hence store counts.
|
|
|
|
if guiCfg.Enabled {
|
2020-06-23 08:47:15 +00:00
|
|
|
report.GUIStats.Enabled++
|
2017-10-15 07:45:15 +00:00
|
|
|
if guiCfg.UseTLS() {
|
2020-06-23 08:47:15 +00:00
|
|
|
report.GUIStats.UseTLS++
|
2017-10-15 07:45:15 +00:00
|
|
|
}
|
|
|
|
if len(guiCfg.User) > 0 && len(guiCfg.Password) > 0 {
|
2020-06-23 08:47:15 +00:00
|
|
|
report.GUIStats.UseAuth++
|
2017-10-15 07:45:15 +00:00
|
|
|
}
|
|
|
|
if guiCfg.InsecureAdminAccess {
|
2020-06-23 08:47:15 +00:00
|
|
|
report.GUIStats.InsecureAdminAccess++
|
2017-10-15 07:45:15 +00:00
|
|
|
}
|
|
|
|
if guiCfg.Debugging {
|
2020-06-23 08:47:15 +00:00
|
|
|
report.GUIStats.Debugging++
|
2017-10-15 07:45:15 +00:00
|
|
|
}
|
|
|
|
if guiCfg.InsecureSkipHostCheck {
|
2020-06-23 08:47:15 +00:00
|
|
|
report.GUIStats.InsecureSkipHostCheck++
|
2017-10-15 07:45:15 +00:00
|
|
|
}
|
|
|
|
if guiCfg.InsecureAllowFrameLoading {
|
2020-06-23 08:47:15 +00:00
|
|
|
report.GUIStats.InsecureAllowFrameLoading++
|
2017-10-15 07:45:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
addr, err := net.ResolveTCPAddr("tcp", guiCfg.Address())
|
|
|
|
if err == nil {
|
|
|
|
if addr.IP.IsLoopback() {
|
2020-06-23 08:47:15 +00:00
|
|
|
report.GUIStats.ListenLocal++
|
2017-10-15 07:45:15 +00:00
|
|
|
} else if addr.IP.IsUnspecified() {
|
2020-06-23 08:47:15 +00:00
|
|
|
report.GUIStats.ListenUnspecified++
|
2017-10-15 07:45:15 +00:00
|
|
|
}
|
|
|
|
}
|
2020-06-23 08:47:15 +00:00
|
|
|
report.GUIStats.Theme[guiCfg.Theme]++
|
2017-10-15 07:45:15 +00:00
|
|
|
}
|
2017-10-12 06:16:46 +00:00
|
|
|
}
|
|
|
|
|
2020-06-23 08:47:15 +00:00
|
|
|
s.model.UsageReportingStats(report, urVersion, preview)
|
|
|
|
|
|
|
|
if err := report.ClearForVersion(urVersion); err != nil {
|
|
|
|
return nil, err
|
2017-10-12 06:16:46 +00:00
|
|
|
}
|
|
|
|
|
2020-06-23 08:47:15 +00:00
|
|
|
return report, nil
|
2014-06-11 18:04:23 +00:00
|
|
|
}
|
|
|
|
|
2022-07-28 15:32:45 +00:00
|
|
|
func (*Service) UptimeS() int {
|
2023-07-22 21:25:03 +00:00
|
|
|
// Handle nonexistent or wildly incorrect system clock.
|
|
|
|
// This code was written in 2023, it can't run in the past.
|
|
|
|
if StartTime.Year() < 2023 {
|
|
|
|
return 0
|
|
|
|
}
|
2019-03-26 19:53:58 +00:00
|
|
|
return int(time.Since(StartTime).Seconds())
|
2015-09-29 18:05:22 +00:00
|
|
|
}
|
|
|
|
|
2020-02-24 20:57:15 +00:00
|
|
|
func (s *Service) sendUsageReport(ctx context.Context) error {
|
2020-06-23 08:47:15 +00:00
|
|
|
d, err := s.ReportData(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-06-11 18:04:23 +00:00
|
|
|
var b bytes.Buffer
|
2019-02-02 09:11:42 +00:00
|
|
|
if err := json.NewEncoder(&b).Encode(d); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-06-11 23:05:00 +00:00
|
|
|
|
2015-11-24 19:46:07 +00:00
|
|
|
client := &http.Client{
|
|
|
|
Transport: &http.Transport{
|
2019-11-26 07:39:51 +00:00
|
|
|
DialContext: dialer.DialContext,
|
|
|
|
Proxy: http.ProxyFromEnvironment,
|
2015-11-24 19:46:07 +00:00
|
|
|
TLSClientConfig: &tls.Config{
|
|
|
|
InsecureSkipVerify: s.cfg.Options().URPostInsecurely,
|
|
|
|
},
|
|
|
|
},
|
2015-09-10 12:08:40 +00:00
|
|
|
}
|
2020-06-23 08:47:15 +00:00
|
|
|
req, err := http.NewRequestWithContext(ctx, "POST", s.cfg.Options().URURL, &b)
|
2020-04-06 07:53:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2020-02-24 20:57:15 +00:00
|
|
|
}
|
2020-04-06 07:53:37 +00:00
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
resp.Body.Close()
|
|
|
|
return nil
|
2014-06-11 18:04:23 +00:00
|
|
|
}
|
|
|
|
|
2020-11-17 12:19:04 +00:00
|
|
|
func (s *Service) Serve(ctx context.Context) error {
|
2019-12-04 06:15:00 +00:00
|
|
|
s.cfg.Subscribe(s)
|
|
|
|
defer s.cfg.Unsubscribe(s)
|
|
|
|
|
2017-10-12 06:16:46 +00:00
|
|
|
t := time.NewTimer(time.Duration(s.cfg.Options().URInitialDelayS) * time.Second)
|
2014-06-11 18:04:23 +00:00
|
|
|
for {
|
|
|
|
select {
|
2019-11-21 07:41:15 +00:00
|
|
|
case <-ctx.Done():
|
2020-11-17 12:19:04 +00:00
|
|
|
return ctx.Err()
|
2017-10-12 06:16:46 +00:00
|
|
|
case <-s.forceRun:
|
|
|
|
t.Reset(0)
|
2014-06-11 18:04:23 +00:00
|
|
|
case <-t.C:
|
2017-10-12 06:16:46 +00:00
|
|
|
if s.cfg.Options().URAccepted >= 2 {
|
2020-02-24 20:57:15 +00:00
|
|
|
err := s.sendUsageReport(ctx)
|
2017-10-12 06:16:46 +00:00
|
|
|
if err != nil {
|
|
|
|
l.Infoln("Usage report:", err)
|
|
|
|
} else {
|
|
|
|
l.Infof("Sent usage report (version %d)", s.cfg.Options().URAccepted)
|
|
|
|
}
|
2014-06-11 23:05:00 +00:00
|
|
|
}
|
2015-05-12 07:35:37 +00:00
|
|
|
t.Reset(24 * time.Hour) // next report tomorrow
|
2014-06-11 18:04:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-26 19:53:58 +00:00
|
|
|
func (s *Service) CommitConfiguration(from, to config.Configuration) bool {
|
2017-10-12 06:16:46 +00:00
|
|
|
if from.Options.URAccepted != to.Options.URAccepted || from.Options.URUniqueID != to.Options.URUniqueID || from.Options.URURL != to.Options.URURL {
|
2018-11-07 10:05:07 +00:00
|
|
|
select {
|
|
|
|
case s.forceRun <- struct{}{}:
|
2019-07-09 09:40:30 +00:00
|
|
|
default:
|
|
|
|
// s.forceRun is one buffered, so even though nothing
|
|
|
|
// was sent, a run will still happen after this point.
|
2018-11-07 10:05:07 +00:00
|
|
|
}
|
2017-10-12 06:16:46 +00:00
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2019-03-26 19:53:58 +00:00
|
|
|
func (*Service) String() string {
|
|
|
|
return "ur.Service"
|
2014-06-11 18:04:23 +00:00
|
|
|
}
|
|
|
|
|
2019-08-25 21:43:28 +00:00
|
|
|
var (
|
|
|
|
blocksResult []protocol.BlockInfo // so the result is not optimized away
|
|
|
|
blocksResultMut sync.Mutex
|
|
|
|
)
|
|
|
|
|
2019-03-26 19:53:58 +00:00
|
|
|
// CpuBench returns CPU performance as a measure of single threaded SHA-256 MiB/s
|
2020-02-24 20:57:15 +00:00
|
|
|
func CpuBench(ctx context.Context, iterations int, duration time.Duration, useWeakHash bool) float64 {
|
2019-08-25 21:43:28 +00:00
|
|
|
blocksResultMut.Lock()
|
|
|
|
defer blocksResultMut.Unlock()
|
|
|
|
|
2018-04-16 18:08:50 +00:00
|
|
|
dataSize := 16 * protocol.MinBlockSize
|
2017-02-06 12:42:39 +00:00
|
|
|
bs := make([]byte, dataSize)
|
2021-03-02 18:17:20 +00:00
|
|
|
r := rand.New(rand.NewSource(time.Now().UnixNano()))
|
|
|
|
r.Read(bs)
|
2017-02-06 12:42:39 +00:00
|
|
|
|
2015-10-20 06:27:22 +00:00
|
|
|
var perf float64
|
|
|
|
for i := 0; i < iterations; i++ {
|
2020-02-24 20:57:15 +00:00
|
|
|
if v := cpuBenchOnce(ctx, duration, useWeakHash, bs); v > perf {
|
2015-10-20 06:27:22 +00:00
|
|
|
perf = v
|
|
|
|
}
|
|
|
|
}
|
2021-03-22 14:07:41 +00:00
|
|
|
// not looking at the blocksResult makes it unused from a static
|
|
|
|
// analysis / compiler standpoint...
|
|
|
|
// blocksResult may be nil at this point if the context is cancelled
|
|
|
|
if blocksResult != nil {
|
|
|
|
blocksResult = nil
|
2021-03-17 21:23:12 +00:00
|
|
|
}
|
2015-10-20 06:27:22 +00:00
|
|
|
return perf
|
|
|
|
}
|
|
|
|
|
2020-02-24 20:57:15 +00:00
|
|
|
func cpuBenchOnce(ctx context.Context, duration time.Duration, useWeakHash bool, bs []byte) float64 {
|
2014-06-11 18:04:23 +00:00
|
|
|
t0 := time.Now()
|
|
|
|
b := 0
|
2020-02-24 20:57:15 +00:00
|
|
|
var err error
|
2015-10-20 06:27:22 +00:00
|
|
|
for time.Since(t0) < duration {
|
2017-01-23 21:56:43 +00:00
|
|
|
r := bytes.NewReader(bs)
|
2020-02-24 20:57:15 +00:00
|
|
|
blocksResult, err = scanner.Blocks(ctx, r, protocol.MinBlockSize, int64(len(bs)), nil, useWeakHash)
|
|
|
|
if err != nil {
|
|
|
|
return 0 // Context done
|
|
|
|
}
|
2017-02-06 12:42:39 +00:00
|
|
|
b += len(bs)
|
2014-06-11 18:04:23 +00:00
|
|
|
}
|
|
|
|
d := time.Since(t0)
|
|
|
|
return float64(int(float64(b)/d.Seconds()/(1<<20)*100)) / 100
|
|
|
|
}
|