From 3344c3b89bd105231db7d2409a5b1564ed4130e9 Mon Sep 17 00:00:00 2001 From: Will Norris Date: Wed, 31 Aug 2022 16:08:21 -0700 Subject: [PATCH] tsnet: add Server method to listener Allow callers to verify that a net.Listener is a tsnet.listener by type asserting against this Server method, as well as providing access to the underlying Server. This is initially being added to support the caddy integration in caddyserver/caddy#5002. Signed-off-by: Will Norris --- tsnet/tsnet.go | 3 +++ tsnet/tsnet_test.go | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+) create mode 100644 tsnet/tsnet_test.go diff --git a/tsnet/tsnet.go b/tsnet/tsnet.go index ffc57deed..9b5e26cbf 100644 --- a/tsnet/tsnet.go +++ b/tsnet/tsnet.go @@ -541,6 +541,9 @@ func (ln *listener) Close() error { return nil } +// Server returns the tsnet Server associated with the listener. +func (ln *listener) Server() *Server { return ln.s } + type addr struct{ ln *listener } func (a addr) Network() string { return a.ln.key.network } diff --git a/tsnet/tsnet_test.go b/tsnet/tsnet_test.go new file mode 100644 index 000000000..59f7ce206 --- /dev/null +++ b/tsnet/tsnet_test.go @@ -0,0 +1,18 @@ +// Copyright (c) 2022 Tailscale Inc & AUTHORS All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package tsnet + +import "testing" + +// TestListener_Server ensures that the listener type always keeps the Server +// method, which is used by some external applications to identify a tsnet.Listener +// from other net.Listeners, as well as access the underlying Server. +func TestListener_Server(t *testing.T) { + s := &Server{} + ln := listener{s: s} + if ln.Server() != s { + t.Errorf("listener.Server() returned %v, want %v", ln.Server(), s) + } +}