2014-11-16 21:13:20 +01:00
|
|
|
// Copyright (C) 2014 The Syncthing Authors.
|
2014-09-29 21:43:32 +02:00
|
|
|
//
|
2015-03-07 21:36:35 +01: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 07:52:18 +01:00
|
|
|
// You can obtain one at https://mozilla.org/MPL/2.0/.
|
2014-09-04 08:31:38 +02:00
|
|
|
|
2019-03-26 20:53:58 +01:00
|
|
|
package ur
|
2014-06-11 20:04:23 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2017-04-26 02:15:23 +02:00
|
|
|
"context"
|
2014-06-11 20:04:23 +02:00
|
|
|
"crypto/rand"
|
2015-09-10 14:08:40 +02:00
|
|
|
"crypto/tls"
|
2014-06-11 20:04:23 +02:00
|
|
|
"encoding/json"
|
2017-10-15 09:45:15 +02:00
|
|
|
"net"
|
2014-06-11 20:04:23 +02:00
|
|
|
"net/http"
|
|
|
|
"runtime"
|
2015-09-06 16:36:09 +02:00
|
|
|
"sort"
|
2016-05-04 21:38:12 +02:00
|
|
|
"strings"
|
2019-08-25 23:43:28 +02:00
|
|
|
"sync"
|
2014-06-11 20:04:23 +02:00
|
|
|
"time"
|
|
|
|
|
2019-02-12 07:58:24 +01:00
|
|
|
"github.com/syncthing/syncthing/lib/build"
|
2015-08-06 11:29:25 +02:00
|
|
|
"github.com/syncthing/syncthing/lib/config"
|
2017-10-12 08:16:46 +02:00
|
|
|
"github.com/syncthing/syncthing/lib/connections"
|
2015-10-12 20:30:14 +02:00
|
|
|
"github.com/syncthing/syncthing/lib/dialer"
|
2015-08-06 11:29:25 +02:00
|
|
|
"github.com/syncthing/syncthing/lib/model"
|
2015-09-22 19:38:46 +02:00
|
|
|
"github.com/syncthing/syncthing/lib/protocol"
|
2017-01-23 22:56:43 +01:00
|
|
|
"github.com/syncthing/syncthing/lib/scanner"
|
2015-09-06 16:36:09 +02:00
|
|
|
"github.com/syncthing/syncthing/lib/upgrade"
|
2019-07-09 11:40:30 +02:00
|
|
|
"github.com/syncthing/syncthing/lib/util"
|
|
|
|
|
|
|
|
"github.com/thejerf/suture"
|
2014-06-11 20:04:23 +02: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 20:53:58 +01:00
|
|
|
const Version = 3
|
2014-06-11 20:04:23 +02:00
|
|
|
|
2019-03-26 20:53:58 +01:00
|
|
|
var StartTime = time.Now()
|
|
|
|
|
|
|
|
type Service struct {
|
2019-07-09 11:40:30 +02:00
|
|
|
suture.Service
|
2019-03-26 20:53:58 +01:00
|
|
|
cfg config.Wrapper
|
|
|
|
model model.Model
|
|
|
|
connectionsService connections.Service
|
|
|
|
noUpgrade bool
|
|
|
|
forceRun chan struct{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func New(cfg config.Wrapper, m model.Model, connectionsService connections.Service, noUpgrade bool) *Service {
|
|
|
|
svc := &Service{
|
|
|
|
cfg: cfg,
|
|
|
|
model: m,
|
|
|
|
connectionsService: connectionsService,
|
|
|
|
noUpgrade: noUpgrade,
|
2019-07-09 11:40:30 +02:00
|
|
|
forceRun: make(chan struct{}, 1), // Buffered to prevent locking
|
2019-03-26 20:53:58 +01:00
|
|
|
}
|
2019-11-21 08:41:15 +01:00
|
|
|
svc.Service = util.AsService(svc.serve, svc.String())
|
2019-03-26 20:53:58 +01:00
|
|
|
return svc
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReportData returns the data to be sent in a usage report with the currently
|
|
|
|
// configured usage reporting version.
|
2020-02-24 21:57:15 +01:00
|
|
|
func (s *Service) ReportData(ctx context.Context) map[string]interface{} {
|
2019-06-11 08:19:11 +02:00
|
|
|
urVersion := s.cfg.Options().URAccepted
|
2020-02-24 21:57:15 +01:00
|
|
|
return s.reportData(ctx, urVersion, false)
|
2019-03-26 20:53:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ReportDataPreview returns a preview of the data to be sent in a usage report
|
|
|
|
// with the given version.
|
2020-02-24 21:57:15 +01:00
|
|
|
func (s *Service) ReportDataPreview(ctx context.Context, urVersion int) map[string]interface{} {
|
|
|
|
return s.reportData(ctx, urVersion, true)
|
2019-03-26 20:53:58 +01:00
|
|
|
}
|
|
|
|
|
2020-02-24 21:57:15 +01:00
|
|
|
func (s *Service) reportData(ctx context.Context, urVersion int, preview bool) map[string]interface{} {
|
2019-03-26 20:53:58 +01:00
|
|
|
opts := s.cfg.Options()
|
2014-06-11 20:04:23 +02:00
|
|
|
res := make(map[string]interface{})
|
2019-03-26 20:53:58 +01:00
|
|
|
res["urVersion"] = urVersion
|
2017-01-30 22:33:07 +01:00
|
|
|
res["uniqueID"] = opts.URUniqueID
|
2019-02-12 07:58:24 +01:00
|
|
|
res["version"] = build.Version
|
|
|
|
res["longVersion"] = build.LongVersion
|
2014-06-11 20:04:23 +02:00
|
|
|
res["platform"] = runtime.GOOS + "-" + runtime.GOARCH
|
2019-03-26 20:53:58 +01:00
|
|
|
res["numFolders"] = len(s.cfg.Folders())
|
|
|
|
res["numDevices"] = len(s.cfg.Devices())
|
2014-06-11 20:04:23 +02:00
|
|
|
|
|
|
|
var totFiles, maxFiles int
|
|
|
|
var totBytes, maxBytes int64
|
2019-03-26 20:53:58 +01:00
|
|
|
for folderID := range s.cfg.Folders() {
|
2020-01-21 18:23:08 +01:00
|
|
|
snap, err := s.model.DBSnapshot(folderID)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
global := snap.GlobalSize()
|
|
|
|
snap.Release()
|
2017-12-14 10:51:17 +01:00
|
|
|
totFiles += int(global.Files)
|
2016-10-17 14:10:17 +02:00
|
|
|
totBytes += global.Bytes
|
2017-12-14 10:51:17 +01:00
|
|
|
if int(global.Files) > maxFiles {
|
|
|
|
maxFiles = int(global.Files)
|
2014-06-11 20:04:23 +02:00
|
|
|
}
|
2016-10-17 14:10:17 +02:00
|
|
|
if global.Bytes > maxBytes {
|
|
|
|
maxBytes = global.Bytes
|
2014-06-11 20:04:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
res["totFiles"] = totFiles
|
2014-09-28 13:00:38 +02:00
|
|
|
res["folderMaxFiles"] = maxFiles
|
2014-06-11 20:04:23 +02:00
|
|
|
res["totMiB"] = totBytes / 1024 / 1024
|
2014-09-28 13:00:38 +02:00
|
|
|
res["folderMaxMiB"] = maxBytes / 1024 / 1024
|
2014-06-11 20:04:23 +02:00
|
|
|
|
|
|
|
var mem runtime.MemStats
|
|
|
|
runtime.ReadMemStats(&mem)
|
2014-08-05 23:13:55 +02:00
|
|
|
res["memoryUsageMiB"] = (mem.Sys - mem.HeapReleased) / 1024 / 1024
|
2020-02-24 21:57:15 +01:00
|
|
|
res["sha256Perf"] = CpuBench(ctx, 5, 125*time.Millisecond, false)
|
|
|
|
res["hashPerf"] = CpuBench(ctx, 5, 125*time.Millisecond, true)
|
2014-06-11 20:04:23 +02:00
|
|
|
|
2014-06-12 20:47:46 +02:00
|
|
|
bytes, err := memorySize()
|
|
|
|
if err == nil {
|
|
|
|
res["memorySize"] = bytes / 1024 / 1024
|
|
|
|
}
|
2015-09-06 16:36:09 +02:00
|
|
|
res["numCPU"] = runtime.NumCPU()
|
|
|
|
|
|
|
|
var rescanIntvs []int
|
|
|
|
folderUses := map[string]int{
|
2018-05-13 09:58:00 +02:00
|
|
|
"sendonly": 0,
|
|
|
|
"sendreceive": 0,
|
2018-09-09 13:55:19 +02:00
|
|
|
"receiveonly": 0,
|
2016-02-13 08:18:40 +01:00
|
|
|
"ignorePerms": 0,
|
|
|
|
"ignoreDelete": 0,
|
|
|
|
"autoNormalize": 0,
|
|
|
|
"simpleVersioning": 0,
|
|
|
|
"externalVersioning": 0,
|
|
|
|
"staggeredVersioning": 0,
|
|
|
|
"trashcanVersioning": 0,
|
2015-09-06 16:36:09 +02:00
|
|
|
}
|
2019-03-26 20:53:58 +01:00
|
|
|
for _, cfg := range s.cfg.Folders() {
|
2015-09-06 16:36:09 +02:00
|
|
|
rescanIntvs = append(rescanIntvs, cfg.RescanIntervalS)
|
|
|
|
|
2018-05-13 09:58:00 +02:00
|
|
|
switch cfg.Type {
|
|
|
|
case config.FolderTypeSendOnly:
|
|
|
|
folderUses["sendonly"]++
|
|
|
|
case config.FolderTypeSendReceive:
|
|
|
|
folderUses["sendreceive"]++
|
2018-09-09 13:55:19 +02:00
|
|
|
case config.FolderTypeReceiveOnly:
|
|
|
|
folderUses["receiveonly"]++
|
2015-09-06 16:36:09 +02:00
|
|
|
}
|
|
|
|
if cfg.IgnorePerms {
|
|
|
|
folderUses["ignorePerms"]++
|
|
|
|
}
|
|
|
|
if cfg.IgnoreDelete {
|
|
|
|
folderUses["ignoreDelete"]++
|
|
|
|
}
|
|
|
|
if cfg.AutoNormalize {
|
|
|
|
folderUses["autoNormalize"]++
|
|
|
|
}
|
2016-02-13 08:18:40 +01:00
|
|
|
if cfg.Versioning.Type != "" {
|
|
|
|
folderUses[cfg.Versioning.Type+"Versioning"]++
|
|
|
|
}
|
2015-09-06 16:36:09 +02:00
|
|
|
}
|
|
|
|
sort.Ints(rescanIntvs)
|
|
|
|
res["rescanIntvs"] = rescanIntvs
|
|
|
|
res["folderUses"] = folderUses
|
|
|
|
|
|
|
|
deviceUses := map[string]int{
|
|
|
|
"introducer": 0,
|
|
|
|
"customCertName": 0,
|
|
|
|
"compressAlways": 0,
|
|
|
|
"compressMetadata": 0,
|
|
|
|
"compressNever": 0,
|
|
|
|
"dynamicAddr": 0,
|
|
|
|
"staticAddr": 0,
|
|
|
|
}
|
2019-03-26 20:53:58 +01:00
|
|
|
for _, cfg := range s.cfg.Devices() {
|
2015-09-06 16:36:09 +02:00
|
|
|
if cfg.Introducer {
|
|
|
|
deviceUses["introducer"]++
|
|
|
|
}
|
|
|
|
if cfg.CertName != "" && cfg.CertName != "syncthing" {
|
|
|
|
deviceUses["customCertName"]++
|
|
|
|
}
|
|
|
|
if cfg.Compression == protocol.CompressAlways {
|
|
|
|
deviceUses["compressAlways"]++
|
|
|
|
} else if cfg.Compression == protocol.CompressMetadata {
|
|
|
|
deviceUses["compressMetadata"]++
|
|
|
|
} else if cfg.Compression == protocol.CompressNever {
|
|
|
|
deviceUses["compressNever"]++
|
|
|
|
}
|
|
|
|
for _, addr := range cfg.Addresses {
|
|
|
|
if addr == "dynamic" {
|
|
|
|
deviceUses["dynamicAddr"]++
|
|
|
|
} else {
|
|
|
|
deviceUses["staticAddr"]++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
res["deviceUses"] = deviceUses
|
|
|
|
|
|
|
|
defaultAnnounceServersDNS, defaultAnnounceServersIP, otherAnnounceServers := 0, 0, 0
|
2019-11-26 17:07:25 +01:00
|
|
|
for _, addr := range opts.RawGlobalAnnServers {
|
2015-11-09 15:35:14 +01:00
|
|
|
if addr == "default" || addr == "default-v4" || addr == "default-v6" {
|
2015-09-06 16:36:09 +02:00
|
|
|
defaultAnnounceServersDNS++
|
2015-09-21 10:54:21 +02:00
|
|
|
} else {
|
2015-09-06 16:36:09 +02:00
|
|
|
otherAnnounceServers++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
res["announce"] = map[string]interface{}{
|
2017-01-30 22:33:07 +01:00
|
|
|
"globalEnabled": opts.GlobalAnnEnabled,
|
|
|
|
"localEnabled": opts.LocalAnnEnabled,
|
2015-09-06 16:36:09 +02:00
|
|
|
"defaultServersDNS": defaultAnnounceServersDNS,
|
|
|
|
"defaultServersIP": defaultAnnounceServersIP,
|
|
|
|
"otherServers": otherAnnounceServers,
|
|
|
|
}
|
|
|
|
|
2015-09-06 21:15:46 +02:00
|
|
|
defaultRelayServers, otherRelayServers := 0, 0
|
2019-11-26 17:07:25 +01:00
|
|
|
for _, addr := range s.cfg.Options().ListenAddresses() {
|
2016-05-04 21:38:12 +02:00
|
|
|
switch {
|
|
|
|
case addr == "dynamic+https://relays.syncthing.net/endpoint":
|
2015-09-06 21:15:46 +02:00
|
|
|
defaultRelayServers++
|
2016-05-04 21:38:12 +02:00
|
|
|
case strings.HasPrefix(addr, "relay://") || strings.HasPrefix(addr, "dynamic+http"):
|
2015-09-06 21:15:46 +02:00
|
|
|
otherRelayServers++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
res["relays"] = map[string]interface{}{
|
2016-05-04 21:38:12 +02:00
|
|
|
"enabled": defaultRelayServers+otherAnnounceServers > 0,
|
2015-09-06 21:15:46 +02:00
|
|
|
"defaultServers": defaultRelayServers,
|
|
|
|
"otherServers": otherRelayServers,
|
|
|
|
}
|
|
|
|
|
2017-01-30 22:33:07 +01:00
|
|
|
res["usesRateLimit"] = opts.MaxRecvKbps > 0 || opts.MaxSendKbps > 0
|
2015-09-06 16:36:09 +02:00
|
|
|
|
2019-03-26 20:53:58 +01:00
|
|
|
res["upgradeAllowedManual"] = !(upgrade.DisabledByCompilation || s.noUpgrade)
|
|
|
|
res["upgradeAllowedAuto"] = !(upgrade.DisabledByCompilation || s.noUpgrade) && opts.AutoUpgradeIntervalH > 0
|
|
|
|
res["upgradeAllowedPre"] = !(upgrade.DisabledByCompilation || s.noUpgrade) && opts.AutoUpgradeIntervalH > 0 && opts.UpgradeToPreReleases
|
2014-06-12 20:47:46 +02:00
|
|
|
|
2019-03-26 20:53:58 +01:00
|
|
|
if urVersion >= 3 {
|
|
|
|
res["uptime"] = s.UptimeS()
|
|
|
|
res["natType"] = s.connectionsService.NATType()
|
2017-10-15 09:45:15 +02:00
|
|
|
res["alwaysLocalNets"] = len(opts.AlwaysLocalNets) > 0
|
|
|
|
res["cacheIgnoredFiles"] = opts.CacheIgnoredFiles
|
|
|
|
res["overwriteRemoteDeviceNames"] = opts.OverwriteRemoteDevNames
|
|
|
|
res["progressEmitterEnabled"] = opts.ProgressUpdateIntervalS > -1
|
|
|
|
res["customDefaultFolderPath"] = opts.DefaultFolderPath != "~"
|
|
|
|
res["customTrafficClass"] = opts.TrafficClass != 0
|
|
|
|
res["customTempIndexMinBlocks"] = opts.TempIndexMinBlocks != 10
|
|
|
|
res["temporariesDisabled"] = opts.KeepTemporariesH == 0
|
|
|
|
res["temporariesCustom"] = opts.KeepTemporariesH != 24
|
|
|
|
res["limitBandwidthInLan"] = opts.LimitBandwidthInLan
|
|
|
|
res["customReleaseURL"] = opts.ReleasesURL != "https://upgrades.syncthing.net/meta.json"
|
|
|
|
res["restartOnWakeup"] = opts.RestartOnWakeup
|
|
|
|
|
|
|
|
folderUsesV3 := map[string]int{
|
|
|
|
"scanProgressDisabled": 0,
|
|
|
|
"conflictsDisabled": 0,
|
|
|
|
"conflictsUnlimited": 0,
|
|
|
|
"conflictsOther": 0,
|
|
|
|
"disableSparseFiles": 0,
|
|
|
|
"disableTempIndexes": 0,
|
|
|
|
"alwaysWeakHash": 0,
|
|
|
|
"customWeakHashThreshold": 0,
|
2017-10-20 18:25:20 +02:00
|
|
|
"fsWatcherEnabled": 0,
|
2017-10-15 09:45:15 +02:00
|
|
|
}
|
|
|
|
pullOrder := make(map[string]int)
|
|
|
|
filesystemType := make(map[string]int)
|
2017-10-20 18:25:20 +02:00
|
|
|
var fsWatcherDelays []int
|
2019-03-26 20:53:58 +01:00
|
|
|
for _, cfg := range s.cfg.Folders() {
|
2017-10-15 09:45:15 +02:00
|
|
|
if cfg.ScanProgressIntervalS < 0 {
|
|
|
|
folderUsesV3["scanProgressDisabled"]++
|
|
|
|
}
|
|
|
|
if cfg.MaxConflicts == 0 {
|
|
|
|
folderUsesV3["conflictsDisabled"]++
|
|
|
|
} else if cfg.MaxConflicts < 0 {
|
|
|
|
folderUsesV3["conflictsUnlimited"]++
|
|
|
|
} else {
|
|
|
|
folderUsesV3["conflictsOther"]++
|
|
|
|
}
|
|
|
|
if cfg.DisableSparseFiles {
|
|
|
|
folderUsesV3["disableSparseFiles"]++
|
|
|
|
}
|
|
|
|
if cfg.DisableTempIndexes {
|
|
|
|
folderUsesV3["disableTempIndexes"]++
|
|
|
|
}
|
|
|
|
if cfg.WeakHashThresholdPct < 0 {
|
|
|
|
folderUsesV3["alwaysWeakHash"]++
|
|
|
|
} else if cfg.WeakHashThresholdPct != 25 {
|
|
|
|
folderUsesV3["customWeakHashThreshold"]++
|
|
|
|
}
|
2017-10-20 18:25:20 +02:00
|
|
|
if cfg.FSWatcherEnabled {
|
|
|
|
folderUsesV3["fsWatcherEnabled"]++
|
|
|
|
}
|
2017-10-15 09:45:15 +02:00
|
|
|
pullOrder[cfg.Order.String()]++
|
|
|
|
filesystemType[cfg.FilesystemType.String()]++
|
2017-10-20 18:25:20 +02:00
|
|
|
fsWatcherDelays = append(fsWatcherDelays, cfg.FSWatcherDelayS)
|
2017-10-15 09:45:15 +02:00
|
|
|
}
|
2017-10-20 18:25:20 +02:00
|
|
|
sort.Ints(fsWatcherDelays)
|
2017-10-15 09:45:15 +02:00
|
|
|
folderUsesV3Interface := map[string]interface{}{
|
2017-10-20 18:25:20 +02:00
|
|
|
"pullOrder": pullOrder,
|
|
|
|
"filesystemType": filesystemType,
|
|
|
|
"fsWatcherDelays": fsWatcherDelays,
|
2017-10-15 09:45:15 +02:00
|
|
|
}
|
|
|
|
for key, value := range folderUsesV3 {
|
|
|
|
folderUsesV3Interface[key] = value
|
|
|
|
}
|
|
|
|
res["folderUsesV3"] = folderUsesV3Interface
|
|
|
|
|
2019-03-26 20:53:58 +01:00
|
|
|
guiCfg := s.cfg.GUI()
|
2017-10-15 09:45:15 +02:00
|
|
|
// Anticipate multiple GUI configs in the future, hence store counts.
|
|
|
|
guiStats := map[string]int{
|
|
|
|
"enabled": 0,
|
|
|
|
"useTLS": 0,
|
|
|
|
"useAuth": 0,
|
|
|
|
"insecureAdminAccess": 0,
|
|
|
|
"debugging": 0,
|
|
|
|
"insecureSkipHostCheck": 0,
|
|
|
|
"insecureAllowFrameLoading": 0,
|
|
|
|
"listenLocal": 0,
|
|
|
|
"listenUnspecified": 0,
|
|
|
|
}
|
|
|
|
theme := make(map[string]int)
|
|
|
|
if guiCfg.Enabled {
|
|
|
|
guiStats["enabled"]++
|
|
|
|
if guiCfg.UseTLS() {
|
|
|
|
guiStats["useTLS"]++
|
|
|
|
}
|
|
|
|
if len(guiCfg.User) > 0 && len(guiCfg.Password) > 0 {
|
|
|
|
guiStats["useAuth"]++
|
|
|
|
}
|
|
|
|
if guiCfg.InsecureAdminAccess {
|
|
|
|
guiStats["insecureAdminAccess"]++
|
|
|
|
}
|
|
|
|
if guiCfg.Debugging {
|
|
|
|
guiStats["debugging"]++
|
|
|
|
}
|
|
|
|
if guiCfg.InsecureSkipHostCheck {
|
|
|
|
guiStats["insecureSkipHostCheck"]++
|
|
|
|
}
|
|
|
|
if guiCfg.InsecureAllowFrameLoading {
|
|
|
|
guiStats["insecureAllowFrameLoading"]++
|
|
|
|
}
|
|
|
|
|
|
|
|
addr, err := net.ResolveTCPAddr("tcp", guiCfg.Address())
|
|
|
|
if err == nil {
|
|
|
|
if addr.IP.IsLoopback() {
|
|
|
|
guiStats["listenLocal"]++
|
|
|
|
} else if addr.IP.IsUnspecified() {
|
|
|
|
guiStats["listenUnspecified"]++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
theme[guiCfg.Theme]++
|
|
|
|
}
|
|
|
|
guiStatsInterface := map[string]interface{}{
|
|
|
|
"theme": theme,
|
|
|
|
}
|
|
|
|
for key, value := range guiStats {
|
|
|
|
guiStatsInterface[key] = value
|
|
|
|
}
|
|
|
|
res["guiStats"] = guiStatsInterface
|
2017-10-12 08:16:46 +02:00
|
|
|
}
|
|
|
|
|
2019-03-26 20:53:58 +01:00
|
|
|
for key, value := range s.model.UsageReportingStats(urVersion, preview) {
|
2017-10-12 08:16:46 +02:00
|
|
|
res[key] = value
|
|
|
|
}
|
|
|
|
|
2014-06-11 20:04:23 +02:00
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2019-03-26 20:53:58 +01:00
|
|
|
func (s *Service) UptimeS() int {
|
|
|
|
return int(time.Since(StartTime).Seconds())
|
2015-09-29 20:05:22 +02:00
|
|
|
}
|
|
|
|
|
2020-02-24 21:57:15 +01:00
|
|
|
func (s *Service) sendUsageReport(ctx context.Context) error {
|
|
|
|
d := s.ReportData(ctx)
|
2014-06-11 20:04:23 +02:00
|
|
|
var b bytes.Buffer
|
2019-02-02 10:11:42 +01:00
|
|
|
if err := json.NewEncoder(&b).Encode(d); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-06-12 01:05:00 +02:00
|
|
|
|
2015-11-24 20:46:07 +01:00
|
|
|
client := &http.Client{
|
|
|
|
Transport: &http.Transport{
|
2019-11-26 08:39:51 +01:00
|
|
|
DialContext: dialer.DialContext,
|
|
|
|
Proxy: http.ProxyFromEnvironment,
|
2015-11-24 20:46:07 +01:00
|
|
|
TLSClientConfig: &tls.Config{
|
|
|
|
InsecureSkipVerify: s.cfg.Options().URPostInsecurely,
|
|
|
|
},
|
|
|
|
},
|
2015-09-10 14:08:40 +02:00
|
|
|
}
|
2020-02-24 21:57:15 +01:00
|
|
|
req, err := http.NewRequest("POST", s.cfg.Options().URURL, &b)
|
2020-04-06 09:53:37 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2020-02-24 21:57:15 +01:00
|
|
|
}
|
2020-04-06 09:53:37 +02:00
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
req.Cancel = ctx.Done()
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
resp.Body.Close()
|
|
|
|
return nil
|
2014-06-11 20:04:23 +02:00
|
|
|
}
|
|
|
|
|
2019-11-21 08:41:15 +01:00
|
|
|
func (s *Service) serve(ctx context.Context) {
|
2019-12-04 07:15:00 +01:00
|
|
|
s.cfg.Subscribe(s)
|
|
|
|
defer s.cfg.Unsubscribe(s)
|
|
|
|
|
2017-10-12 08:16:46 +02:00
|
|
|
t := time.NewTimer(time.Duration(s.cfg.Options().URInitialDelayS) * time.Second)
|
2014-06-11 20:04:23 +02:00
|
|
|
for {
|
|
|
|
select {
|
2019-11-21 08:41:15 +01:00
|
|
|
case <-ctx.Done():
|
2015-05-12 09:35:37 +02:00
|
|
|
return
|
2017-10-12 08:16:46 +02:00
|
|
|
case <-s.forceRun:
|
|
|
|
t.Reset(0)
|
2014-06-11 20:04:23 +02:00
|
|
|
case <-t.C:
|
2017-10-12 08:16:46 +02:00
|
|
|
if s.cfg.Options().URAccepted >= 2 {
|
2020-02-24 21:57:15 +01:00
|
|
|
err := s.sendUsageReport(ctx)
|
2017-10-12 08:16:46 +02:00
|
|
|
if err != nil {
|
|
|
|
l.Infoln("Usage report:", err)
|
|
|
|
} else {
|
|
|
|
l.Infof("Sent usage report (version %d)", s.cfg.Options().URAccepted)
|
|
|
|
}
|
2014-06-12 01:05:00 +02:00
|
|
|
}
|
2015-05-12 09:35:37 +02:00
|
|
|
t.Reset(24 * time.Hour) // next report tomorrow
|
2014-06-11 20:04:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-26 20:53:58 +01:00
|
|
|
func (s *Service) VerifyConfiguration(from, to config.Configuration) error {
|
2017-10-12 08:16:46 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-03-26 20:53:58 +01:00
|
|
|
func (s *Service) CommitConfiguration(from, to config.Configuration) bool {
|
2017-10-12 08:16:46 +02:00
|
|
|
if from.Options.URAccepted != to.Options.URAccepted || from.Options.URUniqueID != to.Options.URUniqueID || from.Options.URURL != to.Options.URURL {
|
2018-11-07 11:05:07 +01:00
|
|
|
select {
|
|
|
|
case s.forceRun <- struct{}{}:
|
2019-07-09 11:40:30 +02:00
|
|
|
default:
|
|
|
|
// s.forceRun is one buffered, so even though nothing
|
|
|
|
// was sent, a run will still happen after this point.
|
2018-11-07 11:05:07 +01:00
|
|
|
}
|
2017-10-12 08:16:46 +02:00
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2019-03-26 20:53:58 +01:00
|
|
|
func (*Service) String() string {
|
|
|
|
return "ur.Service"
|
2014-06-11 20:04:23 +02:00
|
|
|
}
|
|
|
|
|
2019-08-25 23:43:28 +02:00
|
|
|
var (
|
|
|
|
blocksResult []protocol.BlockInfo // so the result is not optimized away
|
|
|
|
blocksResultMut sync.Mutex
|
|
|
|
)
|
|
|
|
|
2019-03-26 20:53:58 +01:00
|
|
|
// CpuBench returns CPU performance as a measure of single threaded SHA-256 MiB/s
|
2020-02-24 21:57:15 +01:00
|
|
|
func CpuBench(ctx context.Context, iterations int, duration time.Duration, useWeakHash bool) float64 {
|
2019-08-25 23:43:28 +02:00
|
|
|
blocksResultMut.Lock()
|
|
|
|
defer blocksResultMut.Unlock()
|
|
|
|
|
2018-04-16 20:08:50 +02:00
|
|
|
dataSize := 16 * protocol.MinBlockSize
|
2017-02-06 13:42:39 +01:00
|
|
|
bs := make([]byte, dataSize)
|
2019-02-02 12:16:27 +01:00
|
|
|
rand.Reader.Read(bs)
|
2017-02-06 13:42:39 +01:00
|
|
|
|
2015-10-20 08:27:22 +02:00
|
|
|
var perf float64
|
|
|
|
for i := 0; i < iterations; i++ {
|
2020-02-24 21:57:15 +01:00
|
|
|
if v := cpuBenchOnce(ctx, duration, useWeakHash, bs); v > perf {
|
2015-10-20 08:27:22 +02:00
|
|
|
perf = v
|
|
|
|
}
|
|
|
|
}
|
2017-01-23 22:56:43 +01:00
|
|
|
blocksResult = nil
|
2015-10-20 08:27:22 +02:00
|
|
|
return perf
|
|
|
|
}
|
|
|
|
|
2020-02-24 21:57:15 +01:00
|
|
|
func cpuBenchOnce(ctx context.Context, duration time.Duration, useWeakHash bool, bs []byte) float64 {
|
2014-06-11 20:04:23 +02:00
|
|
|
t0 := time.Now()
|
|
|
|
b := 0
|
2020-02-24 21:57:15 +01:00
|
|
|
var err error
|
2015-10-20 08:27:22 +02:00
|
|
|
for time.Since(t0) < duration {
|
2017-01-23 22:56:43 +01:00
|
|
|
r := bytes.NewReader(bs)
|
2020-02-24 21:57:15 +01:00
|
|
|
blocksResult, err = scanner.Blocks(ctx, r, protocol.MinBlockSize, int64(len(bs)), nil, useWeakHash)
|
|
|
|
if err != nil {
|
|
|
|
return 0 // Context done
|
|
|
|
}
|
2017-02-06 13:42:39 +01:00
|
|
|
b += len(bs)
|
2014-06-11 20:04:23 +02:00
|
|
|
}
|
|
|
|
d := time.Since(t0)
|
|
|
|
return float64(int(float64(b)/d.Seconds()/(1<<20)*100)) / 100
|
|
|
|
}
|