Skip to content

Commit

Permalink
Added RawNetMapper interface
Browse files Browse the repository at this point in the history
Useful when dealing with different cap versions

Signed-off-by: Thomas Kosiewski <[email protected]>
  • Loading branch information
ThomasK33 committed Jul 17, 2024
1 parent 90defb6 commit a8d944f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ linters:
- structcheck
- varcheck
- mnd
- funlen

- exhaustruct
- nlreturn
Expand Down
31 changes: 28 additions & 3 deletions handlers/netmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type NetMapper interface {
// periodically send keep alive messages to the tailscale client via the
// long poll NetMap request.
KeepAliveInterval() time.Duration

// NetMap handles the netmap polling request from a tailscale client. It
// returns a channel of netmap responses and a channel of errors.
//
Expand All @@ -43,6 +44,19 @@ type NetMapper interface {
NetMap(ctx context.Context, req tailcfg.MapRequest, peerPublicKey key.MachinePublic) (chan tailcfg.MapResponse, chan error)
}

type RawNetMapper interface {
// RawNetMap handles the netmap polling requests from a tailscale client,
// but isn't limited by the response that it can send to the client.
//
// This becomes particularly useful when working with clients with different
// capability versions and potentially fields that don't exist in
// newer versions.
//
// The behaviour of RawNetMap is the same as of NetMap, with the difference
// that if this interface is satisfied, the NetMap function is never called.
RawNetMap(ctx context.Context, req tailcfg.MapRequest, peerPublicKey key.MachinePublic) (chan any, chan error)
}

func NetMapHandler(coordinator NetMapper, peerPublicKey key.MachinePublic) http.HandlerFunc {

Check failure on line 60 in handlers/netmap.go

View workflow job for this annotation

GitHub Actions / lint

calculated cyclomatic complexity for function NetMapHandler is 12, max is 10 (cyclop)
return func(w http.ResponseWriter, r *http.Request) {
ctx, cancel := context.WithCancelCause(r.Context())
Expand All @@ -56,13 +70,23 @@ func NetMapHandler(coordinator NetMapper, peerPublicKey key.MachinePublic) http.
return
}

resChan, errChan := coordinator.NetMap(ctx, req, peerPublicKey)
var (
resChan chan tailcfg.MapResponse
rawChan chan any
errChan chan error
)

if mapper, ok := coordinator.(RawNetMapper); ok {
rawChan, errChan = mapper.RawNetMap(ctx, req, peerPublicKey)
} else {
resChan, errChan = coordinator.NetMap(ctx, req, peerPublicKey)
}

keepAliveTicker := time.NewTicker(coordinator.KeepAliveInterval())
defer keepAliveTicker.Stop()

var (
res tailcfg.MapResponse
res any
more bool
)

Expand All @@ -71,6 +95,7 @@ func NetMapHandler(coordinator NetMapper, peerPublicKey key.MachinePublic) http.
case _, more = <-ctx.Done():
case err, more = <-errChan:
case res, more = <-resChan:
case res, more = <-rawChan:
case _, more = <-keepAliveTicker.C:
now := time.Now()
res = tailcfg.MapResponse{
Expand Down Expand Up @@ -99,7 +124,7 @@ func NetMapHandler(coordinator NetMapper, peerPublicKey key.MachinePublic) http.
}
}

func writeResponse(w http.ResponseWriter, compress string, res tailcfg.MapResponse) error {
func writeResponse(w http.ResponseWriter, compress string, res any) error {
var payload []byte

marshalled, err := json.Marshal(res)
Expand Down

0 comments on commit a8d944f

Please sign in to comment.