2021-02-09 22:40:23 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package metainfo
|
|
|
|
|
|
|
|
import (
|
2021-12-15 09:45:00 +00:00
|
|
|
"fmt"
|
2022-02-23 14:59:00 +00:00
|
|
|
"sort"
|
2021-02-09 22:40:23 +00:00
|
|
|
"strings"
|
|
|
|
|
2021-12-15 09:45:00 +00:00
|
|
|
"github.com/blang/semver"
|
2021-02-09 22:40:23 +00:00
|
|
|
"github.com/spacemonkeygo/monkit/v3"
|
2021-12-15 09:45:00 +00:00
|
|
|
"go.uber.org/zap"
|
2021-02-09 22:40:23 +00:00
|
|
|
|
|
|
|
"storj.io/common/useragent"
|
|
|
|
)
|
|
|
|
|
|
|
|
const uplinkProduct = "uplink"
|
|
|
|
|
2022-05-13 15:15:42 +01:00
|
|
|
type transfer string
|
|
|
|
|
|
|
|
const upload = transfer("upload")
|
|
|
|
const download = transfer("download")
|
|
|
|
|
2022-01-13 11:07:15 +00:00
|
|
|
var knownUserAgents = []string{
|
2022-02-03 14:29:15 +00:00
|
|
|
"rclone", "gateway-st", "gateway-mt", "linksharing", "uplink-cli", "transfer-sh", "filezilla", "duplicati",
|
2022-05-17 12:09:22 +01:00
|
|
|
"comet", "orbiter", "uplink-php", "nextcloud", "aws-cli", "ipfs-go-ds-storj",
|
2022-01-13 11:07:15 +00:00
|
|
|
}
|
|
|
|
|
2021-02-09 22:40:23 +00:00
|
|
|
type versionOccurrence struct {
|
2021-12-15 09:45:00 +00:00
|
|
|
Product string
|
2021-02-09 22:40:23 +00:00
|
|
|
Version string
|
|
|
|
Method string
|
|
|
|
}
|
|
|
|
|
|
|
|
type versionCollector struct {
|
2021-12-15 09:45:00 +00:00
|
|
|
log *zap.Logger
|
2021-02-09 22:40:23 +00:00
|
|
|
}
|
|
|
|
|
2021-12-15 09:45:00 +00:00
|
|
|
func newVersionCollector(log *zap.Logger) *versionCollector {
|
2021-02-09 22:40:23 +00:00
|
|
|
return &versionCollector{
|
2021-12-15 09:45:00 +00:00
|
|
|
log: log,
|
2021-02-09 22:40:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-21 15:48:13 +01:00
|
|
|
func (vc *versionCollector) collect(useragentRaw []byte, method string) {
|
2021-12-15 09:45:00 +00:00
|
|
|
if len(useragentRaw) == 0 {
|
2022-04-21 15:48:13 +01:00
|
|
|
return
|
2021-12-15 09:45:00 +00:00
|
|
|
}
|
2021-02-09 22:40:23 +00:00
|
|
|
|
2021-12-15 09:45:00 +00:00
|
|
|
entries, err := useragent.ParseEntries(useragentRaw)
|
|
|
|
if err != nil {
|
2022-04-21 15:48:13 +01:00
|
|
|
vc.log.Warn("unable to collect uplink version", zap.Error(err))
|
|
|
|
mon.Meter("user_agents", monkit.NewSeriesTag("user_agent", "unparseable")).Mark(1)
|
|
|
|
return
|
2021-12-15 09:45:00 +00:00
|
|
|
}
|
|
|
|
|
2022-02-23 14:59:00 +00:00
|
|
|
// foundProducts tracks potentially multiple noteworthy products names from the user-agent
|
|
|
|
var foundProducts []string
|
2021-12-15 09:45:00 +00:00
|
|
|
for _, entry := range entries {
|
2022-02-23 14:59:00 +00:00
|
|
|
product := strings.ToLower(entry.Product)
|
|
|
|
if product == uplinkProduct {
|
|
|
|
vo := versionOccurrence{Product: product, Version: entry.Version, Method: method}
|
2021-12-15 09:45:00 +00:00
|
|
|
vc.sendUplinkMetric(vo)
|
2022-02-23 14:59:00 +00:00
|
|
|
} else if contains(knownUserAgents, product) && !contains(foundProducts, product) {
|
|
|
|
foundProducts = append(foundProducts, product)
|
2021-02-09 22:40:23 +00:00
|
|
|
}
|
|
|
|
}
|
2022-02-23 14:59:00 +00:00
|
|
|
|
|
|
|
if len(foundProducts) > 0 {
|
|
|
|
sort.Strings(foundProducts)
|
|
|
|
// concatenate all known products for this metric, EG "gateway-mt + rclone"
|
|
|
|
mon.Meter("user_agents", monkit.NewSeriesTag("user_agent", strings.Join(foundProducts, " + "))).Mark(1)
|
|
|
|
} else { // lets keep also general value for user agents with no known product
|
2022-02-04 21:19:01 +00:00
|
|
|
mon.Meter("user_agents", monkit.NewSeriesTag("user_agent", "other")).Mark(1)
|
|
|
|
}
|
2021-12-15 09:45:00 +00:00
|
|
|
}
|
2021-02-09 22:40:23 +00:00
|
|
|
|
2021-12-15 09:45:00 +00:00
|
|
|
func (vc *versionCollector) sendUplinkMetric(vo versionOccurrence) {
|
|
|
|
if vo.Version == "" {
|
|
|
|
vo.Version = "unknown"
|
|
|
|
} else {
|
2022-01-13 11:07:15 +00:00
|
|
|
// use only minor to avoid using too many resources and
|
2021-12-15 09:45:00 +00:00
|
|
|
// minimize risk of abusing by sending lots of different versions
|
|
|
|
semVer, err := semver.ParseTolerant(vo.Version)
|
|
|
|
if err != nil {
|
|
|
|
vc.log.Warn("invalid uplink library user agent version", zap.String("version", vo.Version), zap.Error(err))
|
|
|
|
return
|
|
|
|
}
|
2022-01-13 11:07:15 +00:00
|
|
|
|
|
|
|
// keep number of possible versions very limited
|
|
|
|
if semVer.Major != 1 || semVer.Minor > 30 {
|
|
|
|
vc.log.Warn("invalid uplink library user agent version", zap.String("version", vo.Version), zap.Error(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
vo.Version = fmt.Sprintf("v%d.%d", 1, semVer.Minor)
|
2021-02-09 22:40:23 +00:00
|
|
|
}
|
|
|
|
|
2021-12-15 09:45:00 +00:00
|
|
|
mon.Meter("uplink_versions", monkit.NewSeriesTag("version", vo.Version), monkit.NewSeriesTag("method", vo.Method)).Mark(1)
|
2021-02-09 22:40:23 +00:00
|
|
|
}
|
2022-01-13 11:07:15 +00:00
|
|
|
|
2022-05-13 15:15:42 +01:00
|
|
|
func (vc *versionCollector) collectTransferStats(useragentRaw []byte, transfer transfer, transferSize int) {
|
|
|
|
entries, err := useragent.ParseEntries(useragentRaw)
|
|
|
|
if err != nil {
|
|
|
|
vc.log.Warn("unable to collect transfer statistics", zap.Error(err))
|
|
|
|
mon.Meter("user_agents_transfer_stats", monkit.NewSeriesTag("user_agent", "unparseable"), monkit.NewSeriesTag("type", string(transfer))).Mark(transferSize)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// foundProducts tracks potentially multiple noteworthy products names from the user-agent
|
|
|
|
var foundProducts []string
|
|
|
|
for _, entry := range entries {
|
|
|
|
product := strings.ToLower(entry.Product)
|
|
|
|
if contains(knownUserAgents, product) && !contains(foundProducts, product) {
|
|
|
|
foundProducts = append(foundProducts, product)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(foundProducts) > 0 {
|
|
|
|
sort.Strings(foundProducts)
|
|
|
|
// concatenate all known products for this metric, EG "gateway-mt + rclone"
|
|
|
|
mon.Meter("user_agents_transfer_stats", monkit.NewSeriesTag("user_agent", strings.Join(foundProducts, " + ")), monkit.NewSeriesTag("type", string(transfer))).Mark(transferSize)
|
|
|
|
} else { // lets keep also general value for user agents with no known product
|
|
|
|
mon.Meter("user_agents_transfer_stats", monkit.NewSeriesTag("user_agent", "other"), monkit.NewSeriesTag("type", string(transfer))).Mark(transferSize)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-23 14:59:00 +00:00
|
|
|
// contains returns true if the given string is contained in the given slice.
|
|
|
|
func contains(slice []string, testValue string) bool {
|
|
|
|
for _, sliceValue := range slice {
|
|
|
|
if sliceValue == testValue {
|
2022-01-13 11:07:15 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|