Skip to content

Commit

Permalink
all: cleanup unused code, part 1 (tailscale#10661)
Browse files Browse the repository at this point in the history
Run `staticcheck` with `U1000` to find unused code. This cleans up about
a half of it. I'll do the other half separately to keep PRs manageable.

Updates #cleanup

Signed-off-by: Andrew Lytvynov <awly@tailscale.com>
  • Loading branch information
awly committed Dec 20, 2023
1 parent 3c333f6 commit 1302bd1
Show file tree
Hide file tree
Showing 26 changed files with 81 additions and 274 deletions.
23 changes: 0 additions & 23 deletions net/packet/packet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,18 +74,6 @@ var icmp4ReplyBuffer = []byte{
0x72, 0x65, 0x70, 0x6c, 0x79, 0x5f, 0x70, 0x61, 0x79, 0x6c, 0x6f, 0x61, 0x64,
}

var icmp4ReplyDecode = Parsed{
b: icmp4ReplyBuffer,
subofs: 20,
dataofs: 24,
length: len(icmp4ReplyBuffer),

IPVersion: 4,
IPProto: ICMPv4,
Src: mustIPPort("1.2.3.4:0"),
Dst: mustIPPort("5.6.7.8:0"),
}

// ICMPv6 Router Solicitation
var icmp6PacketBuffer = []byte{
0x60, 0x00, 0x00, 0x00, 0x00, 0x08, 0x3a, 0xff,
Expand Down Expand Up @@ -257,17 +245,6 @@ var udp4ReplyBuffer = []byte{
0x72, 0x65, 0x70, 0x6c, 0x79, 0x5f, 0x70, 0x61, 0x79, 0x6c, 0x6f, 0x61, 0x64,
}

var udp4ReplyDecode = Parsed{
b: udp4ReplyBuffer,
subofs: 20,
dataofs: 28,
length: len(udp4ReplyBuffer),

IPProto: UDP,
Src: mustIPPort("1.2.3.4:567"),
Dst: mustIPPort("5.6.7.8:123"),
}

// First TCP fragment of a packet with leading 24 bytes of 'a's
var tcp4MediumFragmentBuffer = []byte{
// IP header up to checksum
Expand Down
2 changes: 0 additions & 2 deletions net/portmapper/igd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,7 @@ type TestIGDOptions struct {
type igdCounters struct {
numUPnPDiscoRecv int32
numUPnPOtherUDPRecv int32
numUPnPHTTPRecv int32
numPMPRecv int32
numPMPDiscoRecv int32
numPCPRecv int32
numPCPDiscoRecv int32
numPCPMapRecv int32
Expand Down
4 changes: 2 additions & 2 deletions net/proxymux/mux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,8 @@ func mkWorld(t *testing.T) (ret *world) {
}
go httpProxy.Serve(ret.httpListener)

socksProxy := socks5.Server{}
go socksProxy.Serve(ret.socksListener)
ret.socksProxy = &socks5.Server{}
go ret.socksProxy.Serve(ret.socksListener)

ret.httpClient = &http.Client{
Transport: &http.Transport{
Expand Down
1 change: 1 addition & 0 deletions net/routetable/routetable.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (

var (
defaultRouteIPv4 = RouteDestination{Prefix: netip.PrefixFrom(netip.IPv4Unspecified(), 0)}
//lint:ignore U1000 used in routetable_bsd_test.go
defaultRouteIPv6 = RouteDestination{Prefix: netip.PrefixFrom(netip.IPv6Unspecified(), 0)}
)

Expand Down
1 change: 0 additions & 1 deletion net/tsaddr/tsaddr.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ func CGNATRange() netip.Prefix {

var (
cgnatRange oncePrefix
ulaRange oncePrefix
tsUlaRange oncePrefix
tsViaRange oncePrefix
ula4To6Range oncePrefix
Expand Down
14 changes: 13 additions & 1 deletion net/tsdial/peerapi_macios_ext.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func peerDialControlFuncNetworkExtension(d *Dialer) func(network, address string
defer d.mu.Unlock()

index := -1
if x, ok := d.interfaceIndexLocked(d.tunName); ok {
if x, ok := interfaceIndexLocked(d); ok {
index = x
}
var lc net.ListenConfig
Expand All @@ -38,3 +38,15 @@ func peerDialControlFuncNetworkExtension(d *Dialer) func(network, address string
return lc.Control(network, address, c)
}
}

func interfaceIndexLocked(d *Dialer) (index int, ok bool) {
if d.netMon == nil {
return 0, false
}
st := d.netMon.InterfaceState()
iface, ok := st.Interface[d.tunName]
if !ok {
return 0, false
}
return iface.Index, true
}
12 changes: 0 additions & 12 deletions net/tsdial/tsdial.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,18 +196,6 @@ func (d *Dialer) closeSysConn(id int) {
go c.Close() // ignore the error
}

func (d *Dialer) interfaceIndexLocked(ifName string) (index int, ok bool) {
if d.netMon == nil {
return 0, false
}
st := d.netMon.InterfaceState()
iface, ok := st.Interface[ifName]
if !ok {
return 0, false
}
return iface.Index, true
}

// peerDialControlFunc is non-nil on platforms that require a way to
// bind to dial out to other peers.
var peerDialControlFunc func(*Dialer) func(network, address string, c syscall.RawConn) error
Expand Down
1 change: 0 additions & 1 deletion net/tstun/wrap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,6 @@ func TestWriteAndInject(t *testing.T) {
chtun, tun := newChannelTUN(t.Logf, false)
defer tun.Close()

const size = 2 // all payloads have this size
written := []string{"w0", "w1"}
injected := []string{"i0", "i1"}

Expand Down
21 changes: 0 additions & 21 deletions release/dist/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ package cli

import (
"context"
"crypto"
"crypto/x509"
"encoding/binary"
"encoding/pem"
"errors"
"flag"
"fmt"
Expand Down Expand Up @@ -213,24 +210,6 @@ func runBuild(ctx context.Context, filters []string, targets []dist.Target) erro
return nil
}

func parseSigningKey(path string) (crypto.Signer, error) {
if path == "" {
return nil, nil
}
raw, err := os.ReadFile(path)
if err != nil {
return nil, err
}
b, rest := pem.Decode(raw)
if b == nil {
return nil, fmt.Errorf("failed to decode PEM data in %q", path)
}
if len(rest) > 0 {
return nil, fmt.Errorf("trailing data in %q, please check that the key file was not corrupted", path)
}
return x509.ParseECPrivateKey(b.Bytes)
}

var genKeyArgs struct {
root bool
signing bool
Expand Down
26 changes: 12 additions & 14 deletions ssh/tailssh/tailssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -1905,20 +1905,18 @@ func envEq(a, b string) bool {
}

var (
metricActiveSessions = clientmetric.NewGauge("ssh_active_sessions")
metricIncomingConnections = clientmetric.NewCounter("ssh_incoming_connections")
metricPublicKeyConnections = clientmetric.NewCounter("ssh_publickey_connections") // total
metricPublicKeyAccepts = clientmetric.NewCounter("ssh_publickey_accepts") // accepted subset of ssh_publickey_connections
metricTerminalAccept = clientmetric.NewCounter("ssh_terminalaction_accept")
metricTerminalReject = clientmetric.NewCounter("ssh_terminalaction_reject")
metricTerminalInterrupt = clientmetric.NewCounter("ssh_terminalaction_interrupt")
metricTerminalMalformed = clientmetric.NewCounter("ssh_terminalaction_malformed")
metricTerminalFetchError = clientmetric.NewCounter("ssh_terminalaction_fetch_error")
metricHolds = clientmetric.NewCounter("ssh_holds")
metricPolicyChangeKick = clientmetric.NewCounter("ssh_policy_change_kick")
metricSFTP = clientmetric.NewCounter("ssh_sftp_sessions")
metricLocalPortForward = clientmetric.NewCounter("ssh_local_port_forward_requests")
metricRemotePortForward = clientmetric.NewCounter("ssh_remote_port_forward_requests")
metricActiveSessions = clientmetric.NewGauge("ssh_active_sessions")
metricIncomingConnections = clientmetric.NewCounter("ssh_incoming_connections")
metricPublicKeyAccepts = clientmetric.NewCounter("ssh_publickey_accepts") // accepted subset of ssh_publickey_connections
metricTerminalAccept = clientmetric.NewCounter("ssh_terminalaction_accept")
metricTerminalReject = clientmetric.NewCounter("ssh_terminalaction_reject")
metricTerminalMalformed = clientmetric.NewCounter("ssh_terminalaction_malformed")
metricTerminalFetchError = clientmetric.NewCounter("ssh_terminalaction_fetch_error")
metricHolds = clientmetric.NewCounter("ssh_holds")
metricPolicyChangeKick = clientmetric.NewCounter("ssh_policy_change_kick")
metricSFTP = clientmetric.NewCounter("ssh_sftp_sessions")
metricLocalPortForward = clientmetric.NewCounter("ssh_local_port_forward_requests")
metricRemotePortForward = clientmetric.NewCounter("ssh_remote_port_forward_requests")
)

// userVisibleError is a wrapper around an error that implements
Expand Down
25 changes: 0 additions & 25 deletions tailcfg/tailcfg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package tailcfg_test

import (
"encoding"
"encoding/json"
"net/netip"
"os"
Expand Down Expand Up @@ -639,30 +638,6 @@ func TestNetInfoFields(t *testing.T) {
}
}

type keyIn interface {
String() string
MarshalText() ([]byte, error)
}

func testKey(t *testing.T, prefix string, in keyIn, out encoding.TextUnmarshaler) {
got, err := in.MarshalText()
if err != nil {
t.Fatal(err)
}
if err := out.UnmarshalText(got); err != nil {
t.Fatal(err)
}
if s := in.String(); string(got) != s {
t.Errorf("MarshalText = %q != String %q", got, s)
}
if !strings.HasPrefix(string(got), prefix) {
t.Errorf("%q didn't start with prefix %q", got, prefix)
}
if reflect.ValueOf(out).Elem().Interface() != in {
t.Errorf("mismatch after unmarshal")
}
}

func TestCloneUser(t *testing.T) {
tests := []struct {
name string
Expand Down
18 changes: 0 additions & 18 deletions tka/scenario_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ package tka

import (
"crypto/ed25519"
"fmt"
"sort"
"strings"
"testing"
)

Expand Down Expand Up @@ -100,22 +98,6 @@ func (s *scenarioTest) mkNodeWithForks(name string, signWithDefault bool, chains
return n
}

func aumsToNames(n *scenarioNode, aums []AUM) string {
out := make([]string, 0, len(aums))
outer:
for _, a := range aums {
for name, candidate := range n.AUMs {
if candidate.Hash() == a.Hash() {
out = append(out, name)
continue outer
}
}
out = append(out, fmt.Sprintf("%x", a.Hash()))
}

return strings.Join(out, ",")
}

func (s *scenarioTest) syncBetween(n1, n2 *scenarioNode) error {
o1, err := n1.A.SyncOffer(n1.storage)
if err != nil {
Expand Down
25 changes: 0 additions & 25 deletions tool/gocross/goroot.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package main
import (
"errors"
"fmt"
"io"
"io/fs"
"os"
"path/filepath"
Expand Down Expand Up @@ -42,30 +41,6 @@ func makeGoroot(toolchainRoot, outPath string) error {
return nil
}

func copyFile(src, dst string) error {
s, err := os.Open(src)
if err != nil {
return fmt.Errorf("opening %q: %v", src, err)
}
defer s.Close()

d, err := os.OpenFile(dst, os.O_RDWR|os.O_CREATE, 0755)
if err != nil {
return fmt.Errorf("opening %q: %v", dst, err)
}

if _, err := io.Copy(d, s); err != nil {
d.Close()
return fmt.Errorf("copying %q to %q: %v", src, dst, err)
}

if err := d.Close(); err != nil {
return fmt.Errorf("closing %q: %v", dst, err)
}

return nil
}

// linkFarm symlinks every entry in srcDir into outDir, unless that
// directory entry already exists.
func linkFarm(srcDir, outDir string) error {
Expand Down
2 changes: 1 addition & 1 deletion tstest/archtest/archtest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
func TestAlignedAtomicInt64(t *testing.T) {
type T struct {
A atomicbitops.Int64
x int32
_ int32
B atomicbitops.Int64
}

Expand Down
7 changes: 0 additions & 7 deletions tstest/integration/vms/derive_bindhost_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package vms

import (
"io"
"net/netip"
"runtime"
"testing"
Expand Down Expand Up @@ -43,9 +42,3 @@ func TestDeriveBindhost(t *testing.T) {
}
t.Log(deriveBindhost(t))
}

type nopWriteCloser struct {
io.Writer
}

func (nwc nopWriteCloser) Close() error { return nil }
2 changes: 0 additions & 2 deletions tstest/integration/vms/vms_steps_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ import (
"golang.org/x/crypto/ssh"
)

const timeout = 15 * time.Second

func retry(t *testing.T, fn func() error) {
t.Helper()
const tries = 3
Expand Down
23 changes: 0 additions & 23 deletions tstest/natlab/natlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -442,16 +442,6 @@ func (m *Machine) forwardPacket(p *Packet, iif *Interface) {
oif.net.write(p)
}

func unspecOf(ip netip.Addr) netip.Addr {
if ip.Is4() {
return v4unspec
}
if ip.Is6() {
return v6unspec
}
panic(fmt.Sprintf("bogus IP %#v", ip))
}

// Attach adds an interface to a machine.
//
// The first interface added to a Machine becomes that machine's
Expand Down Expand Up @@ -572,19 +562,6 @@ func (m *Machine) interfaceForIP(ip netip.Addr) (*Interface, error) {
return nil, fmt.Errorf("no route found to %v", ip)
}

func (m *Machine) hasv6() bool {
m.mu.Lock()
defer m.mu.Unlock()
for _, f := range m.interfaces {
for _, ip := range f.ips {
if ip.Is6() {
return true
}
}
}
return false
}

func (m *Machine) pickEphemPort() (port uint16, err error) {
m.mu.Lock()
defer m.mu.Unlock()
Expand Down

0 comments on commit 1302bd1

Please sign in to comment.