38 lines
654 B
Go
38 lines
654 B
Go
package peer
|
|
|
|
import (
|
|
"context"
|
|
"net"
|
|
"net/http"
|
|
"sync"
|
|
|
|
"tailscale.com/client/local"
|
|
"tailscale.com/client/tailscale/apitype"
|
|
)
|
|
|
|
type key struct{}
|
|
|
|
type peer struct {
|
|
mu sync.Mutex
|
|
who *apitype.WhoIsResponse
|
|
}
|
|
|
|
func ConnContext(ctx context.Context, _ net.Conn) context.Context {
|
|
return context.WithValue(ctx, key{}, &peer{})
|
|
}
|
|
|
|
func WhoIs(r *http.Request, lc *local.Client) (*apitype.WhoIsResponse, error) {
|
|
p := r.Context().Value(key{}).(*peer)
|
|
p.mu.Lock()
|
|
defer p.mu.Unlock()
|
|
if p.who != nil {
|
|
return p.who, nil
|
|
}
|
|
who, err := lc.WhoIs(r.Context(), r.RemoteAddr)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
p.who = who
|
|
return who, nil
|
|
}
|