From f4afca81ada6416ad4b1899210d1845b2b33f5de Mon Sep 17 00:00:00 2001 From: Randall Winkhart Date: Thu, 3 Apr 2025 23:19:49 -0400 Subject: [PATCH] Strip unneeded functionality --- peercred.go | 27 +++++++++------------------ peercred_darwin.go | 14 -------------- peercred_freebsd.go | 14 -------------- peercred_linux.go | 14 -------------- peercred_solaris.go | 14 -------------- peercred_unix_test.go | 4 ++-- 6 files changed, 11 insertions(+), 76 deletions(-) diff --git a/peercred.go b/peercred.go index 5f474e6..e3bd2d9 100644 --- a/peercred.go +++ b/peercred.go @@ -7,9 +7,7 @@ package peercred import ( - "errors" "net" - "runtime" ) // Creds are the peer credentials. @@ -23,25 +21,18 @@ func (c *Creds) PID() (pid int, ok bool) { } // UserID returns the userid (or Windows SID) that owns the other side -// of the connection, if known. (ok is false if not known) +// of the connection. // The returned string is suitable to passing to os/user.LookupId. -func (c *Creds) UserID() (uid string, ok bool) { - return c.uid, c.uid != "" +func (c *Creds) UserID() (uid string) { + return c.uid } -var osGet func(net.Conn) (*Creds, error) - -var ( - ErrNotImplemented = errors.New("not implemented on " + runtime.GOOS) - ErrUnsupportedConnType = errors.New("unsupported connection type") -) +func osGet(c net.Conn) (*Creds, error) { + return getUnix(c.(*net.UnixConn)) +} // Get returns the peer credentials for c. -// -// For unsupported system, the error is ErrNotImplemented. -func Get(c net.Conn) (*Creds, error) { - if osGet == nil { - return nil, ErrNotImplemented - } - return osGet(c) +func Get(c net.Conn) *Creds { + creds, _ := osGet(c) + return creds } diff --git a/peercred_darwin.go b/peercred_darwin.go index 4844c39..b9fb9cb 100644 --- a/peercred_darwin.go +++ b/peercred_darwin.go @@ -12,20 +12,6 @@ import ( "golang.org/x/sys/unix" ) -func init() { - osGet = getDarwin -} - -func getDarwin(c net.Conn) (*Creds, error) { - switch c := c.(type) { - case *net.UnixConn: - return getUnix(c) - case *net.TCPConn: - // TODO: use /proc tcp info for localhost connections like Windows? - } - return nil, ErrUnsupportedConnType -} - func getUnix(c *net.UnixConn) (*Creds, error) { raw, err := c.SyscallConn() if err != nil { diff --git a/peercred_freebsd.go b/peercred_freebsd.go index 6606fbe..dd41c3b 100644 --- a/peercred_freebsd.go +++ b/peercred_freebsd.go @@ -12,20 +12,6 @@ import ( "golang.org/x/sys/unix" ) -func init() { - osGet = getFreeBSD -} - -func getFreeBSD(c net.Conn) (*Creds, error) { - switch c := c.(type) { - case *net.UnixConn: - return getUnix(c) - case *net.TCPConn: - // TODO: use sysctl net.inet.tcp.pcblist for localhost connections like Windows? - } - return nil, ErrUnsupportedConnType -} - func getUnix(c *net.UnixConn) (*Creds, error) { raw, err := c.SyscallConn() if err != nil { diff --git a/peercred_linux.go b/peercred_linux.go index c1319ad..617012c 100644 --- a/peercred_linux.go +++ b/peercred_linux.go @@ -12,20 +12,6 @@ import ( "golang.org/x/sys/unix" ) -func init() { - osGet = getLinux -} - -func getLinux(c net.Conn) (*Creds, error) { - switch c := c.(type) { - case *net.UnixConn: - return getUnix(c) - case *net.TCPConn: - // TODO: use /proc tcp info for localhost connections like Windows? - } - return nil, ErrUnsupportedConnType -} - func getUnix(c *net.UnixConn) (*Creds, error) { raw, err := c.SyscallConn() if err != nil { diff --git a/peercred_solaris.go b/peercred_solaris.go index aa84dda..2236970 100644 --- a/peercred_solaris.go +++ b/peercred_solaris.go @@ -12,20 +12,6 @@ import ( "golang.org/x/sys/unix" ) -func init() { - osGet = getSolaris -} - -func getSolaris(c net.Conn) (*Creds, error) { - switch c := c.(type) { - case *net.UnixConn: - return getUnix(c) - case *net.TCPConn: - // TODO: Need ideas - } - return nil, ErrUnsupportedConnType -} - func getUnix(c *net.UnixConn) (*Creds, error) { raw, err := c.SyscallConn() if err != nil { diff --git a/peercred_unix_test.go b/peercred_unix_test.go index c183c11..fe46073 100644 --- a/peercred_unix_test.go +++ b/peercred_unix_test.go @@ -46,12 +46,12 @@ func TestUnixSock(t *testing.T) { } defer c.Close() - creds, err := Get(c) + creds := Get(c) if err != nil { t.Fatalf("Get: %v", err) } - uid, ok := creds.UserID() + uid := creds.UserID() if !ok { t.Errorf("no UID") }