2024-08-02 11:47:12 +02:00
|
|
|
//go:build !android && !ios
|
|
|
|
|
2024-04-26 17:20:10 +02:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"archive/zip"
|
|
|
|
"bufio"
|
2024-12-02 18:04:02 +01:00
|
|
|
"bytes"
|
2024-04-26 17:20:10 +02:00
|
|
|
"context"
|
2024-12-02 18:04:02 +01:00
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
2024-04-26 17:20:10 +02:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2024-12-02 18:04:02 +01:00
|
|
|
"io/fs"
|
2024-08-02 11:47:12 +02:00
|
|
|
"net"
|
|
|
|
"net/netip"
|
2024-04-26 17:20:10 +02:00
|
|
|
"os"
|
2024-08-02 11:47:12 +02:00
|
|
|
"sort"
|
2024-04-26 17:20:10 +02:00
|
|
|
"strings"
|
2024-08-02 11:47:12 +02:00
|
|
|
"time"
|
2024-04-26 17:20:10 +02:00
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
|
|
|
"github.com/netbirdio/netbird/client/anonymize"
|
|
|
|
"github.com/netbirdio/netbird/client/internal/peer"
|
2024-08-02 11:47:12 +02:00
|
|
|
"github.com/netbirdio/netbird/client/internal/routemanager/systemops"
|
2024-12-02 18:04:02 +01:00
|
|
|
"github.com/netbirdio/netbird/client/internal/statemanager"
|
2024-04-26 17:20:10 +02:00
|
|
|
"github.com/netbirdio/netbird/client/proto"
|
|
|
|
)
|
|
|
|
|
2024-08-02 11:47:12 +02:00
|
|
|
const readmeContent = `Netbird debug bundle
|
|
|
|
This debug bundle contains the following files:
|
|
|
|
|
|
|
|
status.txt: Anonymized status information of the NetBird client.
|
|
|
|
client.log: Most recent, anonymized log file of the NetBird client.
|
|
|
|
routes.txt: Anonymized system routes, if --system-info flag was provided.
|
|
|
|
interfaces.txt: Anonymized network interface information, if --system-info flag was provided.
|
|
|
|
config.txt: Anonymized configuration information of the NetBird client.
|
2024-12-02 18:04:02 +01:00
|
|
|
state.json: Anonymized client state dump containing netbird states.
|
2024-08-02 11:47:12 +02:00
|
|
|
|
|
|
|
|
|
|
|
Anonymization Process
|
|
|
|
The files in this bundle have been anonymized to protect sensitive information. Here's how the anonymization was applied:
|
|
|
|
|
|
|
|
IP Addresses
|
|
|
|
|
|
|
|
IPv4 addresses are replaced with addresses starting from 192.51.100.0
|
|
|
|
IPv6 addresses are replaced with addresses starting from 100::
|
|
|
|
|
|
|
|
IP addresses from non public ranges and well known addresses are not anonymized (e.g. 8.8.8.8, 100.64.0.0/10, addresses starting with 192.168., 172.16., 10., etc.).
|
|
|
|
Reoccuring IP addresses are replaced with the same anonymized address.
|
|
|
|
|
|
|
|
Note: The anonymized IP addresses in the status file do not match those in the log and routes files. However, the anonymized IP addresses are consistent within the status file and across the routes and log files.
|
|
|
|
|
|
|
|
Domains
|
|
|
|
All domain names (except for the netbird domains) are replaced with randomly generated strings ending in ".domain". Anonymized domains are consistent across all files in the bundle.
|
|
|
|
Reoccuring domain names are replaced with the same anonymized domain.
|
|
|
|
|
2024-12-02 18:04:02 +01:00
|
|
|
State File
|
|
|
|
The state.json file contains anonymized internal state information of the NetBird client, including:
|
|
|
|
- DNS settings and configuration
|
|
|
|
- Firewall rules
|
|
|
|
- Exclusion routes
|
|
|
|
- Route selection
|
|
|
|
- Other internal states that may be present
|
|
|
|
|
|
|
|
The state file follows the same anonymization rules as other files:
|
|
|
|
- IP addresses (both individual and CIDR ranges) are anonymized while preserving their structure
|
|
|
|
- Domain names are consistently anonymized
|
|
|
|
- Technical identifiers and non-sensitive data remain unchanged
|
|
|
|
|
2024-08-02 11:47:12 +02:00
|
|
|
Routes
|
|
|
|
For anonymized routes, the IP addresses are replaced as described above. The prefix length remains unchanged. Note that for prefixes, the anonymized IP might not be a network address, but the prefix length is still correct.
|
2024-12-02 18:04:02 +01:00
|
|
|
|
2024-08-02 11:47:12 +02:00
|
|
|
Network Interfaces
|
|
|
|
The interfaces.txt file contains information about network interfaces, including:
|
|
|
|
- Interface name
|
|
|
|
- Interface index
|
|
|
|
- MTU (Maximum Transmission Unit)
|
|
|
|
- Flags
|
|
|
|
- IP addresses associated with each interface
|
|
|
|
|
|
|
|
The IP addresses in the interfaces file are anonymized using the same process as described above. Interface names, indexes, MTUs, and flags are not anonymized.
|
|
|
|
|
|
|
|
Configuration
|
|
|
|
The config.txt file contains anonymized configuration information of the NetBird client. Sensitive information such as private keys and SSH keys are excluded. The following fields are anonymized:
|
|
|
|
- ManagementURL
|
|
|
|
- AdminURL
|
|
|
|
- NATExternalIPs
|
|
|
|
- CustomDNSAddress
|
|
|
|
|
|
|
|
Other non-sensitive configuration options are included without anonymization.
|
|
|
|
`
|
|
|
|
|
2024-04-26 17:20:10 +02:00
|
|
|
// DebugBundle creates a debug bundle and returns the location.
|
|
|
|
func (s *Server) DebugBundle(_ context.Context, req *proto.DebugBundleRequest) (resp *proto.DebugBundleResponse, err error) {
|
|
|
|
s.mutex.Lock()
|
|
|
|
defer s.mutex.Unlock()
|
|
|
|
|
|
|
|
if s.logFile == "console" {
|
|
|
|
return nil, fmt.Errorf("log file is set to console, cannot create debug bundle")
|
|
|
|
}
|
|
|
|
|
|
|
|
bundlePath, err := os.CreateTemp("", "netbird.debug.*.zip")
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("create zip file: %w", err)
|
|
|
|
}
|
|
|
|
defer func() {
|
2024-08-02 11:47:12 +02:00
|
|
|
if closeErr := bundlePath.Close(); closeErr != nil && err == nil {
|
|
|
|
err = fmt.Errorf("close zip file: %w", closeErr)
|
2024-04-26 17:20:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
2024-08-02 11:47:12 +02:00
|
|
|
if removeErr := os.Remove(bundlePath.Name()); removeErr != nil {
|
|
|
|
log.Errorf("Failed to remove zip file: %v", removeErr)
|
2024-04-26 17:20:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2024-08-02 11:47:12 +02:00
|
|
|
if err := s.createArchive(bundlePath, req); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &proto.DebugBundleResponse{Path: bundlePath.Name()}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) createArchive(bundlePath *os.File, req *proto.DebugBundleRequest) error {
|
2024-04-26 17:20:10 +02:00
|
|
|
archive := zip.NewWriter(bundlePath)
|
2024-08-02 11:47:12 +02:00
|
|
|
if err := s.addReadme(req, archive); err != nil {
|
|
|
|
return fmt.Errorf("add readme: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := s.addStatus(req, archive); err != nil {
|
|
|
|
return fmt.Errorf("add status: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
anonymizer := anonymize.NewAnonymizer(anonymize.DefaultAddresses())
|
|
|
|
status := s.statusRecorder.GetFullStatus()
|
|
|
|
seedFromStatus(anonymizer, &status)
|
|
|
|
|
|
|
|
if err := s.addConfig(req, anonymizer, archive); err != nil {
|
|
|
|
return fmt.Errorf("add config: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if req.GetSystemInfo() {
|
|
|
|
if err := s.addRoutes(req, anonymizer, archive); err != nil {
|
|
|
|
return fmt.Errorf("add routes: %w", err)
|
2024-04-26 17:20:10 +02:00
|
|
|
}
|
|
|
|
|
2024-08-02 11:47:12 +02:00
|
|
|
if err := s.addInterfaces(req, anonymizer, archive); err != nil {
|
|
|
|
return fmt.Errorf("add interfaces: %w", err)
|
2024-04-26 17:20:10 +02:00
|
|
|
}
|
2024-08-02 11:47:12 +02:00
|
|
|
}
|
|
|
|
|
2024-12-02 18:04:02 +01:00
|
|
|
if err := s.addStateFile(req, anonymizer, archive); err != nil {
|
|
|
|
log.Errorf("Failed to add state file to debug bundle: %v", err)
|
|
|
|
}
|
|
|
|
|
2024-08-02 11:47:12 +02:00
|
|
|
if err := s.addLogfile(req, anonymizer, archive); err != nil {
|
|
|
|
return fmt.Errorf("add log file: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := archive.Close(); err != nil {
|
|
|
|
return fmt.Errorf("close archive writer: %w", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) addReadme(req *proto.DebugBundleRequest, archive *zip.Writer) error {
|
|
|
|
if req.GetAnonymize() {
|
|
|
|
readmeReader := strings.NewReader(readmeContent)
|
|
|
|
if err := addFileToZip(archive, readmeReader, "README.txt"); err != nil {
|
|
|
|
return fmt.Errorf("add README file to zip: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) addStatus(req *proto.DebugBundleRequest, archive *zip.Writer) error {
|
|
|
|
if status := req.GetStatus(); status != "" {
|
2024-04-26 17:20:10 +02:00
|
|
|
statusReader := strings.NewReader(status)
|
2024-08-02 11:47:12 +02:00
|
|
|
if err := addFileToZip(archive, statusReader, "status.txt"); err != nil {
|
|
|
|
return fmt.Errorf("add status file to zip: %w", err)
|
2024-04-26 17:20:10 +02:00
|
|
|
}
|
|
|
|
}
|
2024-08-02 11:47:12 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) addConfig(req *proto.DebugBundleRequest, anonymizer *anonymize.Anonymizer, archive *zip.Writer) error {
|
|
|
|
var configContent strings.Builder
|
|
|
|
s.addCommonConfigFields(&configContent)
|
2024-04-26 17:20:10 +02:00
|
|
|
|
2024-08-02 11:47:12 +02:00
|
|
|
if req.GetAnonymize() {
|
|
|
|
if s.config.ManagementURL != nil {
|
|
|
|
configContent.WriteString(fmt.Sprintf("ManagementURL: %s\n", anonymizer.AnonymizeURI(s.config.ManagementURL.String())))
|
|
|
|
}
|
|
|
|
if s.config.AdminURL != nil {
|
|
|
|
configContent.WriteString(fmt.Sprintf("AdminURL: %s\n", anonymizer.AnonymizeURI(s.config.AdminURL.String())))
|
|
|
|
}
|
|
|
|
configContent.WriteString(fmt.Sprintf("NATExternalIPs: %v\n", anonymizeNATExternalIPs(s.config.NATExternalIPs, anonymizer)))
|
|
|
|
if s.config.CustomDNSAddress != "" {
|
|
|
|
configContent.WriteString(fmt.Sprintf("CustomDNSAddress: %s\n", anonymizer.AnonymizeString(s.config.CustomDNSAddress)))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if s.config.ManagementURL != nil {
|
|
|
|
configContent.WriteString(fmt.Sprintf("ManagementURL: %s\n", s.config.ManagementURL.String()))
|
|
|
|
}
|
|
|
|
if s.config.AdminURL != nil {
|
|
|
|
configContent.WriteString(fmt.Sprintf("AdminURL: %s\n", s.config.AdminURL.String()))
|
|
|
|
}
|
|
|
|
configContent.WriteString(fmt.Sprintf("NATExternalIPs: %v\n", s.config.NATExternalIPs))
|
|
|
|
if s.config.CustomDNSAddress != "" {
|
|
|
|
configContent.WriteString(fmt.Sprintf("CustomDNSAddress: %s\n", s.config.CustomDNSAddress))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add config content to zip file
|
|
|
|
configReader := strings.NewReader(configContent.String())
|
|
|
|
if err := addFileToZip(archive, configReader, "config.txt"); err != nil {
|
|
|
|
return fmt.Errorf("add config file to zip: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) addCommonConfigFields(configContent *strings.Builder) {
|
|
|
|
configContent.WriteString("NetBird Client Configuration:\n\n")
|
|
|
|
|
|
|
|
// Add non-sensitive fields
|
|
|
|
configContent.WriteString(fmt.Sprintf("WgIface: %s\n", s.config.WgIface))
|
|
|
|
configContent.WriteString(fmt.Sprintf("WgPort: %d\n", s.config.WgPort))
|
|
|
|
if s.config.NetworkMonitor != nil {
|
|
|
|
configContent.WriteString(fmt.Sprintf("NetworkMonitor: %v\n", *s.config.NetworkMonitor))
|
|
|
|
}
|
|
|
|
configContent.WriteString(fmt.Sprintf("IFaceBlackList: %v\n", s.config.IFaceBlackList))
|
|
|
|
configContent.WriteString(fmt.Sprintf("DisableIPv6Discovery: %v\n", s.config.DisableIPv6Discovery))
|
|
|
|
configContent.WriteString(fmt.Sprintf("RosenpassEnabled: %v\n", s.config.RosenpassEnabled))
|
|
|
|
configContent.WriteString(fmt.Sprintf("RosenpassPermissive: %v\n", s.config.RosenpassPermissive))
|
|
|
|
if s.config.ServerSSHAllowed != nil {
|
|
|
|
configContent.WriteString(fmt.Sprintf("ServerSSHAllowed: %v\n", *s.config.ServerSSHAllowed))
|
|
|
|
}
|
|
|
|
configContent.WriteString(fmt.Sprintf("DisableAutoConnect: %v\n", s.config.DisableAutoConnect))
|
|
|
|
configContent.WriteString(fmt.Sprintf("DNSRouteInterval: %s\n", s.config.DNSRouteInterval))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) addRoutes(req *proto.DebugBundleRequest, anonymizer *anonymize.Anonymizer, archive *zip.Writer) error {
|
|
|
|
if routes, err := systemops.GetRoutesFromTable(); err != nil {
|
|
|
|
log.Errorf("Failed to get routes: %v", err)
|
|
|
|
} else {
|
|
|
|
// TODO: get routes including nexthop
|
|
|
|
routesContent := formatRoutes(routes, req.GetAnonymize(), anonymizer)
|
|
|
|
routesReader := strings.NewReader(routesContent)
|
|
|
|
if err := addFileToZip(archive, routesReader, "routes.txt"); err != nil {
|
|
|
|
return fmt.Errorf("add routes file to zip: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) addInterfaces(req *proto.DebugBundleRequest, anonymizer *anonymize.Anonymizer, archive *zip.Writer) error {
|
|
|
|
interfaces, err := net.Interfaces()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("get interfaces: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
interfacesContent := formatInterfaces(interfaces, req.GetAnonymize(), anonymizer)
|
|
|
|
interfacesReader := strings.NewReader(interfacesContent)
|
|
|
|
if err := addFileToZip(archive, interfacesReader, "interfaces.txt"); err != nil {
|
|
|
|
return fmt.Errorf("add interfaces file to zip: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-12-02 18:04:02 +01:00
|
|
|
func (s *Server) addStateFile(req *proto.DebugBundleRequest, anonymizer *anonymize.Anonymizer, archive *zip.Writer) error {
|
|
|
|
path := statemanager.GetDefaultStatePath()
|
|
|
|
if path == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := os.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, fs.ErrNotExist) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return fmt.Errorf("read state file: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if req.GetAnonymize() {
|
|
|
|
var rawStates map[string]json.RawMessage
|
|
|
|
if err := json.Unmarshal(data, &rawStates); err != nil {
|
|
|
|
return fmt.Errorf("unmarshal states: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := anonymizeStateFile(&rawStates, anonymizer); err != nil {
|
|
|
|
return fmt.Errorf("anonymize state file: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
bs, err := json.MarshalIndent(rawStates, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("marshal states: %w", err)
|
|
|
|
}
|
|
|
|
data = bs
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := addFileToZip(archive, bytes.NewReader(data), "state.json"); err != nil {
|
|
|
|
return fmt.Errorf("add state file to zip: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-08-02 11:47:12 +02:00
|
|
|
func (s *Server) addLogfile(req *proto.DebugBundleRequest, anonymizer *anonymize.Anonymizer, archive *zip.Writer) (err error) {
|
2024-04-26 17:20:10 +02:00
|
|
|
logFile, err := os.Open(s.logFile)
|
|
|
|
if err != nil {
|
2024-08-02 11:47:12 +02:00
|
|
|
return fmt.Errorf("open log file: %w", err)
|
2024-04-26 17:20:10 +02:00
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err := logFile.Close(); err != nil {
|
2024-08-02 11:47:12 +02:00
|
|
|
log.Errorf("Failed to close original log file: %v", err)
|
2024-04-26 17:20:10 +02:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
var logReader io.Reader
|
|
|
|
if req.GetAnonymize() {
|
2024-08-02 11:47:12 +02:00
|
|
|
var writer *io.PipeWriter
|
2024-04-26 17:20:10 +02:00
|
|
|
logReader, writer = io.Pipe()
|
|
|
|
|
2024-12-02 18:04:02 +01:00
|
|
|
go anonymizeLog(logFile, writer, anonymizer)
|
2024-04-26 17:20:10 +02:00
|
|
|
} else {
|
|
|
|
logReader = logFile
|
|
|
|
}
|
2024-08-02 11:47:12 +02:00
|
|
|
if err := addFileToZip(archive, logReader, "client.log"); err != nil {
|
|
|
|
return fmt.Errorf("add log file to zip: %w", err)
|
2024-04-26 17:20:10 +02:00
|
|
|
}
|
|
|
|
|
2024-08-02 11:47:12 +02:00
|
|
|
return nil
|
2024-04-26 17:20:10 +02:00
|
|
|
}
|
|
|
|
|
2024-05-24 13:27:41 +02:00
|
|
|
// GetLogLevel gets the current logging level for the server.
|
|
|
|
func (s *Server) GetLogLevel(_ context.Context, _ *proto.GetLogLevelRequest) (*proto.GetLogLevelResponse, error) {
|
|
|
|
level := ParseLogLevel(log.GetLevel().String())
|
|
|
|
return &proto.GetLogLevelResponse{Level: level}, nil
|
|
|
|
}
|
|
|
|
|
2024-04-26 17:20:10 +02:00
|
|
|
// SetLogLevel sets the logging level for the server.
|
|
|
|
func (s *Server) SetLogLevel(_ context.Context, req *proto.SetLogLevelRequest) (*proto.SetLogLevelResponse, error) {
|
|
|
|
level, err := log.ParseLevel(req.Level.String())
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("invalid log level: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.SetLevel(level)
|
|
|
|
log.Infof("Log level set to %s", level.String())
|
|
|
|
return &proto.SetLogLevelResponse{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func addFileToZip(archive *zip.Writer, reader io.Reader, filename string) error {
|
|
|
|
header := &zip.FileHeader{
|
2024-08-02 11:47:12 +02:00
|
|
|
Name: filename,
|
|
|
|
Method: zip.Deflate,
|
|
|
|
Modified: time.Now(),
|
|
|
|
|
|
|
|
CreatorVersion: 20, // Version 2.0
|
|
|
|
ReaderVersion: 20, // Version 2.0
|
|
|
|
Flags: 0x800, // UTF-8 filename
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the reader is a file, we can get more accurate information
|
|
|
|
if f, ok := reader.(*os.File); ok {
|
|
|
|
if stat, err := f.Stat(); err != nil {
|
|
|
|
log.Tracef("Failed to get file stat for %s: %v", filename, err)
|
|
|
|
} else {
|
|
|
|
header.Modified = stat.ModTime()
|
|
|
|
}
|
2024-04-26 17:20:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
writer, err := archive.CreateHeader(header)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("create zip file header: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := io.Copy(writer, reader); err != nil {
|
|
|
|
return fmt.Errorf("write file to zip: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func seedFromStatus(a *anonymize.Anonymizer, status *peer.FullStatus) {
|
|
|
|
status.ManagementState.URL = a.AnonymizeURI(status.ManagementState.URL)
|
|
|
|
status.SignalState.URL = a.AnonymizeURI(status.SignalState.URL)
|
|
|
|
|
|
|
|
status.LocalPeerState.FQDN = a.AnonymizeDomain(status.LocalPeerState.FQDN)
|
|
|
|
|
|
|
|
for _, peer := range status.Peers {
|
|
|
|
a.AnonymizeDomain(peer.FQDN)
|
2024-08-02 11:47:12 +02:00
|
|
|
for route := range peer.GetRoutes() {
|
|
|
|
a.AnonymizeRoute(route)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for route := range status.LocalPeerState.Routes {
|
|
|
|
a.AnonymizeRoute(route)
|
2024-04-26 17:20:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, nsGroup := range status.NSGroupStates {
|
|
|
|
for _, domain := range nsGroup.Domains {
|
|
|
|
a.AnonymizeDomain(domain)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, relay := range status.Relays {
|
2024-09-08 12:06:14 +02:00
|
|
|
if relay.URI != "" {
|
|
|
|
a.AnonymizeURI(relay.URI)
|
2024-04-26 17:20:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-08-02 11:47:12 +02:00
|
|
|
|
|
|
|
func formatRoutes(routes []netip.Prefix, anonymize bool, anonymizer *anonymize.Anonymizer) string {
|
|
|
|
var ipv4Routes, ipv6Routes []netip.Prefix
|
|
|
|
|
|
|
|
// Separate IPv4 and IPv6 routes
|
|
|
|
for _, route := range routes {
|
|
|
|
if route.Addr().Is4() {
|
|
|
|
ipv4Routes = append(ipv4Routes, route)
|
|
|
|
} else {
|
|
|
|
ipv6Routes = append(ipv6Routes, route)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sort IPv4 and IPv6 routes separately
|
|
|
|
sort.Slice(ipv4Routes, func(i, j int) bool {
|
|
|
|
return ipv4Routes[i].Bits() > ipv4Routes[j].Bits()
|
|
|
|
})
|
|
|
|
sort.Slice(ipv6Routes, func(i, j int) bool {
|
|
|
|
return ipv6Routes[i].Bits() > ipv6Routes[j].Bits()
|
|
|
|
})
|
|
|
|
|
|
|
|
var builder strings.Builder
|
|
|
|
|
|
|
|
// Format IPv4 routes
|
|
|
|
builder.WriteString("IPv4 Routes:\n")
|
|
|
|
for _, route := range ipv4Routes {
|
|
|
|
formatRoute(&builder, route, anonymize, anonymizer)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Format IPv6 routes
|
|
|
|
builder.WriteString("\nIPv6 Routes:\n")
|
|
|
|
for _, route := range ipv6Routes {
|
|
|
|
formatRoute(&builder, route, anonymize, anonymizer)
|
|
|
|
}
|
|
|
|
|
|
|
|
return builder.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func formatRoute(builder *strings.Builder, route netip.Prefix, anonymize bool, anonymizer *anonymize.Anonymizer) {
|
|
|
|
if anonymize {
|
|
|
|
anonymizedIP := anonymizer.AnonymizeIP(route.Addr())
|
|
|
|
builder.WriteString(fmt.Sprintf("%s/%d\n", anonymizedIP, route.Bits()))
|
|
|
|
} else {
|
|
|
|
builder.WriteString(fmt.Sprintf("%s\n", route))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func formatInterfaces(interfaces []net.Interface, anonymize bool, anonymizer *anonymize.Anonymizer) string {
|
|
|
|
sort.Slice(interfaces, func(i, j int) bool {
|
|
|
|
return interfaces[i].Name < interfaces[j].Name
|
|
|
|
})
|
|
|
|
|
|
|
|
var builder strings.Builder
|
|
|
|
builder.WriteString("Network Interfaces:\n")
|
|
|
|
|
|
|
|
for _, iface := range interfaces {
|
|
|
|
builder.WriteString(fmt.Sprintf("\nInterface: %s\n", iface.Name))
|
|
|
|
builder.WriteString(fmt.Sprintf(" Index: %d\n", iface.Index))
|
|
|
|
builder.WriteString(fmt.Sprintf(" MTU: %d\n", iface.MTU))
|
|
|
|
builder.WriteString(fmt.Sprintf(" Flags: %v\n", iface.Flags))
|
|
|
|
|
|
|
|
addrs, err := iface.Addrs()
|
|
|
|
if err != nil {
|
|
|
|
builder.WriteString(fmt.Sprintf(" Addresses: Error retrieving addresses: %v\n", err))
|
|
|
|
} else {
|
|
|
|
builder.WriteString(" Addresses:\n")
|
|
|
|
for _, addr := range addrs {
|
|
|
|
prefix, err := netip.ParsePrefix(addr.String())
|
|
|
|
if err != nil {
|
|
|
|
builder.WriteString(fmt.Sprintf(" Error parsing address: %v\n", err))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
ip := prefix.Addr()
|
|
|
|
if anonymize {
|
|
|
|
ip = anonymizer.AnonymizeIP(ip)
|
|
|
|
}
|
|
|
|
builder.WriteString(fmt.Sprintf(" %s/%d\n", ip, prefix.Bits()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return builder.String()
|
|
|
|
}
|
|
|
|
|
2024-12-02 18:04:02 +01:00
|
|
|
func anonymizeLog(reader io.Reader, writer *io.PipeWriter, anonymizer *anonymize.Anonymizer) {
|
|
|
|
defer func() {
|
|
|
|
// always nil
|
|
|
|
_ = writer.Close()
|
|
|
|
}()
|
|
|
|
|
|
|
|
scanner := bufio.NewScanner(reader)
|
|
|
|
for scanner.Scan() {
|
|
|
|
line := anonymizer.AnonymizeString(scanner.Text())
|
|
|
|
if _, err := writer.Write([]byte(line + "\n")); err != nil {
|
|
|
|
writer.CloseWithError(fmt.Errorf("anonymize write: %w", err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err := scanner.Err(); err != nil {
|
|
|
|
writer.CloseWithError(fmt.Errorf("anonymize scan: %w", err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-02 11:47:12 +02:00
|
|
|
func anonymizeNATExternalIPs(ips []string, anonymizer *anonymize.Anonymizer) []string {
|
|
|
|
anonymizedIPs := make([]string, len(ips))
|
|
|
|
for i, ip := range ips {
|
|
|
|
parts := strings.SplitN(ip, "/", 2)
|
|
|
|
|
|
|
|
ip1, err := netip.ParseAddr(parts[0])
|
|
|
|
if err != nil {
|
|
|
|
anonymizedIPs[i] = ip
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
ip1anon := anonymizer.AnonymizeIP(ip1)
|
|
|
|
|
|
|
|
if len(parts) == 2 {
|
|
|
|
ip2, err := netip.ParseAddr(parts[1])
|
|
|
|
if err != nil {
|
|
|
|
anonymizedIPs[i] = fmt.Sprintf("%s/%s", ip1anon, parts[1])
|
|
|
|
} else {
|
|
|
|
ip2anon := anonymizer.AnonymizeIP(ip2)
|
|
|
|
anonymizedIPs[i] = fmt.Sprintf("%s/%s", ip1anon, ip2anon)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
anonymizedIPs[i] = ip1anon.String()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return anonymizedIPs
|
|
|
|
}
|
2024-12-02 18:04:02 +01:00
|
|
|
|
|
|
|
func anonymizeStateFile(rawStates *map[string]json.RawMessage, anonymizer *anonymize.Anonymizer) error {
|
|
|
|
for name, rawState := range *rawStates {
|
|
|
|
if string(rawState) == "null" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
var state map[string]any
|
|
|
|
if err := json.Unmarshal(rawState, &state); err != nil {
|
|
|
|
return fmt.Errorf("unmarshal state %s: %w", name, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
state = anonymizeValue(state, anonymizer).(map[string]any)
|
|
|
|
|
|
|
|
bs, err := json.Marshal(state)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("marshal state %s: %w", name, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
(*rawStates)[name] = bs
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func anonymizeValue(value any, anonymizer *anonymize.Anonymizer) any {
|
|
|
|
switch v := value.(type) {
|
|
|
|
case string:
|
|
|
|
return anonymizeString(v, anonymizer)
|
|
|
|
case map[string]any:
|
|
|
|
return anonymizeMap(v, anonymizer)
|
|
|
|
case []any:
|
|
|
|
return anonymizeSlice(v, anonymizer)
|
|
|
|
}
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
|
|
|
|
func anonymizeString(v string, anonymizer *anonymize.Anonymizer) string {
|
|
|
|
if prefix, err := netip.ParsePrefix(v); err == nil {
|
|
|
|
anonIP := anonymizer.AnonymizeIP(prefix.Addr())
|
|
|
|
return fmt.Sprintf("%s/%d", anonIP, prefix.Bits())
|
|
|
|
}
|
|
|
|
if ip, err := netip.ParseAddr(v); err == nil {
|
|
|
|
return anonymizer.AnonymizeIP(ip).String()
|
|
|
|
}
|
|
|
|
return anonymizer.AnonymizeString(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
func anonymizeMap(v map[string]any, anonymizer *anonymize.Anonymizer) map[string]any {
|
|
|
|
result := make(map[string]any, len(v))
|
|
|
|
for key, val := range v {
|
|
|
|
newKey := anonymizeMapKey(key, anonymizer)
|
|
|
|
result[newKey] = anonymizeValue(val, anonymizer)
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
func anonymizeMapKey(key string, anonymizer *anonymize.Anonymizer) string {
|
|
|
|
if prefix, err := netip.ParsePrefix(key); err == nil {
|
|
|
|
anonIP := anonymizer.AnonymizeIP(prefix.Addr())
|
|
|
|
return fmt.Sprintf("%s/%d", anonIP, prefix.Bits())
|
|
|
|
}
|
|
|
|
if ip, err := netip.ParseAddr(key); err == nil {
|
|
|
|
return anonymizer.AnonymizeIP(ip).String()
|
|
|
|
}
|
|
|
|
return key
|
|
|
|
}
|
|
|
|
|
|
|
|
func anonymizeSlice(v []any, anonymizer *anonymize.Anonymizer) []any {
|
|
|
|
for i, val := range v {
|
|
|
|
v[i] = anonymizeValue(val, anonymizer)
|
|
|
|
}
|
|
|
|
return v
|
|
|
|
}
|