mirror of
https://github.com/rwinkhart/peercred-mini.git
synced 2026-09-03 23:57:30 -04:00
Add darwin support
Updates golang/go#27613 Updates tailscale/tailscale#1347 Updates tailscale/tailscale#1348
This commit is contained in:
@@ -2,4 +2,4 @@ module inet.af/peercred
|
|||||||
|
|
||||||
go 1.14
|
go 1.14
|
||||||
|
|
||||||
require golang.org/x/sys v0.0.0-20210216163648-f7da38b97c65
|
require golang.org/x/sys v0.0.0-20210216224549-f992740a1bac
|
||||||
|
|||||||
@@ -1,2 +1,4 @@
|
|||||||
golang.org/x/sys v0.0.0-20210216163648-f7da38b97c65 h1:pTMjDVnP5eVRRlWO76rEWJ8JoC6Lf1CmyjPZXRiy2Sw=
|
golang.org/x/sys v0.0.0-20210216163648-f7da38b97c65 h1:pTMjDVnP5eVRRlWO76rEWJ8JoC6Lf1CmyjPZXRiy2Sw=
|
||||||
golang.org/x/sys v0.0.0-20210216163648-f7da38b97c65/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
golang.org/x/sys v0.0.0-20210216163648-f7da38b97c65/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
|
golang.org/x/sys v0.0.0-20210216224549-f992740a1bac h1:9glrpwtNjBYgRpb67AZJKHfzj1stG/8BL5H7In2oTC4=
|
||||||
|
golang.org/x/sys v0.0.0-20210216224549-f992740a1bac/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
|
|||||||
@@ -0,0 +1,62 @@
|
|||||||
|
// Copyright (c) 2021 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 peercred
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
|
"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 {
|
||||||
|
return nil, fmt.Errorf("SyscallConn: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
var cred *unix.Xucred
|
||||||
|
var pid int
|
||||||
|
cerr := raw.Control(func(fd uintptr) {
|
||||||
|
cred, err = unix.GetsockoptXucred(int(fd),
|
||||||
|
unix.SOL_LOCAL,
|
||||||
|
unix.LOCAL_PEERCRED)
|
||||||
|
if err != nil {
|
||||||
|
err = fmt.Errorf("unix.GetsockoptXucred: %w", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
pid, err = unix.GetsockoptInt(int(fd),
|
||||||
|
unix.SOL_LOCAL,
|
||||||
|
unix.LOCAL_PEERPID)
|
||||||
|
if err != nil {
|
||||||
|
err = fmt.Errorf("unix.GetsockoptInt: %w", err)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
if cerr != nil {
|
||||||
|
return nil, fmt.Errorf("raw.Control: %w", err)
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &Creds{
|
||||||
|
pid: pid,
|
||||||
|
uid: strconv.FormatUint(uint64(cred.Uid), 10),
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
+10
-3
@@ -3,7 +3,7 @@
|
|||||||
// license that can be found in the LICENSE file.
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
// +build go1.15
|
// +build go1.15
|
||||||
// +build linux
|
// +build linux darwin
|
||||||
|
|
||||||
package peercred // import "inet.af/peercred"
|
package peercred // import "inet.af/peercred"
|
||||||
|
|
||||||
@@ -15,7 +15,7 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestUnix(t *testing.T) {
|
func TestUnixSock(t *testing.T) {
|
||||||
d := t.TempDir()
|
d := t.TempDir()
|
||||||
path := filepath.Join(d, "foo.sock")
|
path := filepath.Join(d, "foo.sock")
|
||||||
sock, err := net.Listen("unix", path)
|
sock, err := net.Listen("unix", path)
|
||||||
@@ -24,14 +24,21 @@ func TestUnix(t *testing.T) {
|
|||||||
}
|
}
|
||||||
defer sock.Close()
|
defer sock.Close()
|
||||||
|
|
||||||
|
clientConnCh := make(chan net.Conn, 1)
|
||||||
go func() {
|
go func() {
|
||||||
|
defer close(clientConnCh)
|
||||||
c, err := net.Dial("unix", path)
|
c, err := net.Dial("unix", path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
c.Close()
|
clientConnCh <- c
|
||||||
}()
|
}()
|
||||||
|
clientConn, ok := <-clientConnCh
|
||||||
|
if !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer clientConn.Close()
|
||||||
|
|
||||||
c, err := sock.Accept()
|
c, err := sock.Accept()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user