Strip unneeded functionality

This commit is contained in:
2025-04-03 23:19:49 -04:00
parent 35a0c7bd7e
commit f4afca81ad
6 changed files with 11 additions and 76 deletions
+9 -18
View File
@@ -7,9 +7,7 @@
package peercred package peercred
import ( import (
"errors"
"net" "net"
"runtime"
) )
// Creds are the peer credentials. // 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 // 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. // The returned string is suitable to passing to os/user.LookupId.
func (c *Creds) UserID() (uid string, ok bool) { func (c *Creds) UserID() (uid string) {
return c.uid, c.uid != "" return c.uid
} }
var osGet func(net.Conn) (*Creds, error) func osGet(c net.Conn) (*Creds, error) {
return getUnix(c.(*net.UnixConn))
var ( }
ErrNotImplemented = errors.New("not implemented on " + runtime.GOOS)
ErrUnsupportedConnType = errors.New("unsupported connection type")
)
// Get returns the peer credentials for c. // Get returns the peer credentials for c.
// func Get(c net.Conn) *Creds {
// For unsupported system, the error is ErrNotImplemented. creds, _ := osGet(c)
func Get(c net.Conn) (*Creds, error) { return creds
if osGet == nil {
return nil, ErrNotImplemented
}
return osGet(c)
} }
-14
View File
@@ -12,20 +12,6 @@ import (
"golang.org/x/sys/unix" "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) { func getUnix(c *net.UnixConn) (*Creds, error) {
raw, err := c.SyscallConn() raw, err := c.SyscallConn()
if err != nil { if err != nil {
-14
View File
@@ -12,20 +12,6 @@ import (
"golang.org/x/sys/unix" "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) { func getUnix(c *net.UnixConn) (*Creds, error) {
raw, err := c.SyscallConn() raw, err := c.SyscallConn()
if err != nil { if err != nil {
-14
View File
@@ -12,20 +12,6 @@ import (
"golang.org/x/sys/unix" "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) { func getUnix(c *net.UnixConn) (*Creds, error) {
raw, err := c.SyscallConn() raw, err := c.SyscallConn()
if err != nil { if err != nil {
-14
View File
@@ -12,20 +12,6 @@ import (
"golang.org/x/sys/unix" "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) { func getUnix(c *net.UnixConn) (*Creds, error) {
raw, err := c.SyscallConn() raw, err := c.SyscallConn()
if err != nil { if err != nil {
+2 -2
View File
@@ -46,12 +46,12 @@ func TestUnixSock(t *testing.T) {
} }
defer c.Close() defer c.Close()
creds, err := Get(c) creds := Get(c)
if err != nil { if err != nil {
t.Fatalf("Get: %v", err) t.Fatalf("Get: %v", err)
} }
uid, ok := creds.UserID() uid := creds.UserID()
if !ok { if !ok {
t.Errorf("no UID") t.Errorf("no UID")
} }