mirror of
https://github.com/tailscale/tailscale.git
synced 2026-02-09 17:52:57 +01:00
This file was never truly necessary and has never actually been used in the history of Tailscale's open source releases. A Brief History of AUTHORS files --- The AUTHORS file was a pattern developed at Google, originally for Chromium, then adopted by Go and a bunch of other projects. The problem was that Chromium originally had a copyright line only recognizing Google as the copyright holder. Because Google (and most open source projects) do not require copyright assignemnt for contributions, each contributor maintains their copyright. Some large corporate contributors then tried to add their own name to the copyright line in the LICENSE file or in file headers. This quickly becomes unwieldy, and puts a tremendous burden on anyone building on top of Chromium, since the license requires that they keep all copyright lines intact. The compromise was to create an AUTHORS file that would list all of the copyright holders. The LICENSE file and source file headers would then include that list by reference, listing the copyright holder as "The Chromium Authors". This also become cumbersome to simply keep the file up to date with a high rate of new contributors. Plus it's not always obvious who the copyright holder is. Sometimes it is the individual making the contribution, but many times it may be their employer. There is no way for the proejct maintainer to know. Eventually, Google changed their policy to no longer recommend trying to keep the AUTHORS file up to date proactively, and instead to only add to it when requested: https://opensource.google/docs/releasing/authors. They are also clear that: > Adding contributors to the AUTHORS file is entirely within the > project's discretion and has no implications for copyright ownership. It was primarily added to appease a small number of large contributors that insisted that they be recognized as copyright holders (which was entirely their right to do). But it's not truly necessary, and not even the most accurate way of identifying contributors and/or copyright holders. In practice, we've never added anyone to our AUTHORS file. It only lists Tailscale, so it's not really serving any purpose. It also causes confusion because Tailscalars put the "Tailscale Inc & AUTHORS" header in other open source repos which don't actually have an AUTHORS file, so it's ambiguous what that means. Instead, we just acknowledge that the contributors to Tailscale (whoever they are) are copyright holders for their individual contributions. We also have the benefit of using the DCO (developercertificate.org) which provides some additional certification of their right to make the contribution. The source file changes were purely mechanical with: git ls-files | xargs sed -i -e 's/\(Tailscale Inc &\) AUTHORS/\1 contributors/g' Updates #cleanup Change-Id: Ia101a4a3005adb9118051b3416f5a64a4a45987d Signed-off-by: Will Norris <will@tailscale.com>
219 lines
6.3 KiB
Go
219 lines
6.3 KiB
Go
// Copyright (c) Tailscale Inc & contributors
|
||
// SPDX-License-Identifier: BSD-3-Clause
|
||
|
||
//go:build !ts_omit_netlog && !ts_omit_logtail
|
||
|
||
package netlog
|
||
|
||
import (
|
||
"cmp"
|
||
"net/netip"
|
||
"slices"
|
||
"strings"
|
||
"time"
|
||
"unicode/utf8"
|
||
|
||
"tailscale.com/tailcfg"
|
||
"tailscale.com/types/bools"
|
||
"tailscale.com/types/netlogtype"
|
||
"tailscale.com/util/set"
|
||
)
|
||
|
||
// maxLogSize is the maximum number of bytes for a log message.
|
||
const maxLogSize = 256 << 10
|
||
|
||
// record is the in-memory representation of a [netlogtype.Message].
|
||
// It uses maps to efficiently look-up addresses and connections.
|
||
// In contrast, [netlogtype.Message] is designed to be JSON serializable,
|
||
// where complex keys types are not well support in JSON objects.
|
||
type record struct {
|
||
selfNode nodeUser
|
||
|
||
start time.Time
|
||
end time.Time
|
||
|
||
seenNodes map[netip.Addr]nodeUser
|
||
|
||
virtConns map[netlogtype.Connection]countsType
|
||
physConns map[netlogtype.Connection]netlogtype.Counts
|
||
}
|
||
|
||
// nodeUser is a node with additional user profile information.
|
||
type nodeUser struct {
|
||
tailcfg.NodeView
|
||
user tailcfg.UserProfileView // UserProfileView for NodeView.User
|
||
}
|
||
|
||
// countsType is a counts with classification information about the connection.
|
||
type countsType struct {
|
||
netlogtype.Counts
|
||
connType connType
|
||
}
|
||
|
||
type connType uint8
|
||
|
||
const (
|
||
unknownTraffic connType = iota
|
||
virtualTraffic
|
||
subnetTraffic
|
||
exitTraffic
|
||
)
|
||
|
||
// toMessage converts a [record] into a [netlogtype.Message].
|
||
func (r record) toMessage(excludeNodeInfo, anonymizeExitTraffic bool) netlogtype.Message {
|
||
if !r.selfNode.Valid() {
|
||
return netlogtype.Message{}
|
||
}
|
||
|
||
m := netlogtype.Message{
|
||
NodeID: r.selfNode.StableID(),
|
||
Start: r.start.UTC(),
|
||
End: r.end.UTC(),
|
||
}
|
||
|
||
// Convert node fields.
|
||
if !excludeNodeInfo {
|
||
m.SrcNode = r.selfNode.toNode()
|
||
seenIDs := set.Of(r.selfNode.ID())
|
||
for _, node := range r.seenNodes {
|
||
if _, ok := seenIDs[node.ID()]; !ok && node.Valid() {
|
||
m.DstNodes = append(m.DstNodes, node.toNode())
|
||
seenIDs.Add(node.ID())
|
||
}
|
||
}
|
||
slices.SortFunc(m.DstNodes, func(x, y netlogtype.Node) int {
|
||
return cmp.Compare(x.NodeID, y.NodeID)
|
||
})
|
||
}
|
||
|
||
// Converter traffic fields.
|
||
anonymizedExitTraffic := make(map[netlogtype.Connection]netlogtype.Counts)
|
||
for conn, cnts := range r.virtConns {
|
||
switch cnts.connType {
|
||
case virtualTraffic:
|
||
m.VirtualTraffic = append(m.VirtualTraffic, netlogtype.ConnectionCounts{Connection: conn, Counts: cnts.Counts})
|
||
case subnetTraffic:
|
||
m.SubnetTraffic = append(m.SubnetTraffic, netlogtype.ConnectionCounts{Connection: conn, Counts: cnts.Counts})
|
||
default:
|
||
if anonymizeExitTraffic {
|
||
conn = netlogtype.Connection{ // scrub the IP protocol type
|
||
Src: netip.AddrPortFrom(conn.Src.Addr(), 0), // scrub the port number
|
||
Dst: netip.AddrPortFrom(conn.Dst.Addr(), 0), // scrub the port number
|
||
}
|
||
if !r.seenNodes[conn.Src.Addr()].Valid() {
|
||
conn.Src = netip.AddrPort{} // not a Tailscale node, so scrub the address
|
||
}
|
||
if !r.seenNodes[conn.Dst.Addr()].Valid() {
|
||
conn.Dst = netip.AddrPort{} // not a Tailscale node, so scrub the address
|
||
}
|
||
anonymizedExitTraffic[conn] = anonymizedExitTraffic[conn].Add(cnts.Counts)
|
||
continue
|
||
}
|
||
m.ExitTraffic = append(m.ExitTraffic, netlogtype.ConnectionCounts{Connection: conn, Counts: cnts.Counts})
|
||
}
|
||
}
|
||
for conn, cnts := range anonymizedExitTraffic {
|
||
m.ExitTraffic = append(m.ExitTraffic, netlogtype.ConnectionCounts{Connection: conn, Counts: cnts})
|
||
}
|
||
for conn, cnts := range r.physConns {
|
||
m.PhysicalTraffic = append(m.PhysicalTraffic, netlogtype.ConnectionCounts{Connection: conn, Counts: cnts})
|
||
}
|
||
|
||
// Sort the connections for deterministic results.
|
||
slices.SortFunc(m.VirtualTraffic, compareConnCnts)
|
||
slices.SortFunc(m.SubnetTraffic, compareConnCnts)
|
||
slices.SortFunc(m.ExitTraffic, compareConnCnts)
|
||
slices.SortFunc(m.PhysicalTraffic, compareConnCnts)
|
||
|
||
return m
|
||
}
|
||
|
||
func compareConnCnts(x, y netlogtype.ConnectionCounts) int {
|
||
return cmp.Or(
|
||
netip.AddrPort.Compare(x.Src, y.Src),
|
||
netip.AddrPort.Compare(x.Dst, y.Dst),
|
||
cmp.Compare(x.Proto, y.Proto))
|
||
}
|
||
|
||
// jsonLen computes an upper-bound on the size of the JSON representation.
|
||
func (nu nodeUser) jsonLen() (n int) {
|
||
if !nu.Valid() {
|
||
return len(`{"nodeId":""}`)
|
||
}
|
||
n += len(`{}`)
|
||
n += len(`"nodeId":`) + jsonQuotedLen(string(nu.StableID())) + len(`,`)
|
||
if len(nu.Name()) > 0 {
|
||
n += len(`"name":`) + jsonQuotedLen(nu.Name()) + len(`,`)
|
||
}
|
||
if nu.Addresses().Len() > 0 {
|
||
n += len(`"addresses":[]`)
|
||
for _, addr := range nu.Addresses().All() {
|
||
n += bools.IfElse(addr.Addr().Is4(), len(`"255.255.255.255"`), len(`"ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"`)) + len(",")
|
||
}
|
||
}
|
||
if nu.Hostinfo().Valid() && len(nu.Hostinfo().OS()) > 0 {
|
||
n += len(`"os":`) + jsonQuotedLen(nu.Hostinfo().OS()) + len(`,`)
|
||
}
|
||
if nu.Tags().Len() > 0 {
|
||
n += len(`"tags":[]`)
|
||
for _, tag := range nu.Tags().All() {
|
||
n += jsonQuotedLen(tag) + len(",")
|
||
}
|
||
} else if nu.user.Valid() && nu.user.ID() == nu.User() && len(nu.user.LoginName()) > 0 {
|
||
n += len(`"user":`) + jsonQuotedLen(nu.user.LoginName()) + len(",")
|
||
}
|
||
return n
|
||
}
|
||
|
||
// toNode converts the [nodeUser] into a [netlogtype.Node].
|
||
func (nu nodeUser) toNode() netlogtype.Node {
|
||
if !nu.Valid() {
|
||
return netlogtype.Node{}
|
||
}
|
||
n := netlogtype.Node{
|
||
NodeID: nu.StableID(),
|
||
Name: strings.TrimSuffix(nu.Name(), "."),
|
||
}
|
||
var ipv4, ipv6 netip.Addr
|
||
for _, addr := range nu.Addresses().All() {
|
||
switch {
|
||
case addr.IsSingleIP() && addr.Addr().Is4():
|
||
ipv4 = addr.Addr()
|
||
case addr.IsSingleIP() && addr.Addr().Is6():
|
||
ipv6 = addr.Addr()
|
||
}
|
||
}
|
||
n.Addresses = []netip.Addr{ipv4, ipv6}
|
||
n.Addresses = slices.DeleteFunc(n.Addresses, func(a netip.Addr) bool { return !a.IsValid() })
|
||
if nu.Hostinfo().Valid() {
|
||
n.OS = nu.Hostinfo().OS()
|
||
}
|
||
if nu.Tags().Len() > 0 {
|
||
n.Tags = nu.Tags().AsSlice()
|
||
slices.Sort(n.Tags)
|
||
n.Tags = slices.Compact(n.Tags)
|
||
} else if nu.user.Valid() && nu.user.ID() == nu.User() {
|
||
n.User = nu.user.LoginName()
|
||
}
|
||
return n
|
||
}
|
||
|
||
// jsonQuotedLen computes the length of the JSON serialization of s
|
||
// according to [jsontext.AppendQuote].
|
||
func jsonQuotedLen(s string) int {
|
||
n := len(`"`) + len(s) + len(`"`)
|
||
for i, r := range s {
|
||
switch {
|
||
case r == '\b', r == '\t', r == '\n', r == '\f', r == '\r', r == '"', r == '\\':
|
||
n += len(`\X`) - 1
|
||
case r < ' ':
|
||
n += len(`\uXXXX`) - 1
|
||
case r == utf8.RuneError:
|
||
if _, m := utf8.DecodeRuneInString(s[i:]); m == 1 { // exactly an invalid byte
|
||
n += len("<22>") - 1
|
||
}
|
||
}
|
||
}
|
||
return n
|
||
}
|