Skip to content

Commit

Permalink
save mapresponses
Browse files Browse the repository at this point in the history
Signed-off-by: Kristoffer Dalby <[email protected]>
  • Loading branch information
kradalby committed Jul 17, 2023
1 parent 761aabb commit c1e93d9
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions hscontrol/mapper/mapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import (
"encoding/binary"
"encoding/json"
"fmt"
"io/fs"
"net/url"
"os"
"path"
"sort"
"strings"
"sync"
Expand All @@ -18,6 +21,7 @@ import (
"github.com/klauspost/compress/zstd"
"github.com/rs/zerolog/log"
"github.com/samber/lo"
"tailscale.com/envknob"
"tailscale.com/smallzstd"
"tailscale.com/tailcfg"
"tailscale.com/types/dnstype"
Expand All @@ -29,6 +33,8 @@ const (
reservedResponseHeaderSize = 4
)

var debugDumpMapResponsePath = envknob.String("HEADSCALE_DEBUG_DUMP_MAPRESPONSE_PATH")

type Mapper struct {
db *db.HSDatabase

Expand Down Expand Up @@ -413,6 +419,22 @@ func (m Mapper) marshalMapResponse(
Msg("Cannot marshal map response")
}

if debugDumpMapResponsePath != "" {
perms := fs.FileMode(0o755)
mPath := path.Join(debugDumpMapResponsePath, machine.Hostname)
err := os.MkdirAll(mPath, perms)
if err != nil {
panic(err)
}

now := time.Now().Unix()

err = os.WriteFile(path.Join(mPath, fmt.Sprintf("%d.json", now)), jsonBody, perms)
if err != nil {
panic(err)
}
}

var respBody []byte
if compression == util.ZstdCompression {
respBody = zstdEncode(jsonBody)
Expand Down

0 comments on commit c1e93d9

Please sign in to comment.