You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

net_linux.go 778B

1234567891011121314151617181920212223242526272829303132
  1. //go:build linux
  2. // +build linux
  3. package utils
  4. import (
  5. "fmt"
  6. "net"
  7. "syscall"
  8. )
  9. // Output a description of a connection that can identify it to other systems
  10. // administration tools.
  11. func DescribeConn(c net.Conn) (description string) {
  12. description = "<error>"
  13. switch conn := c.(type) {
  14. case *net.UnixConn:
  15. f, err := conn.File()
  16. if err != nil {
  17. return
  18. }
  19. defer f.Close()
  20. ucred, err := syscall.GetsockoptUcred(int(f.Fd()), syscall.SOL_SOCKET, syscall.SO_PEERCRED)
  21. if err != nil {
  22. return
  23. }
  24. return fmt.Sprintf("%s <-> %s [pid=%d, uid=%d]", conn.LocalAddr().String(), conn.RemoteAddr().String(), ucred.Pid, ucred.Uid)
  25. default:
  26. // *net.TCPConn or *tls.Conn
  27. return fmt.Sprintf("%s <-> %s", conn.LocalAddr().String(), conn.RemoteAddr().String())
  28. }
  29. }