peerinfo

package module
v0.0.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 15, 2023 License: MIT Imports: 13 Imported by: 0

README

GoDoc

peerinfo

Grab information about the process of a connected TCP socket.

This allows doing stuff we typically do with unix sockets with programs that do not allow unix sockets.

It's all qemu's fault by the way, see https://bugs.launchpad.net/bugs/1903470

Usage

Calling peerinfo.Find will return details on the process owning the given socket address on the local machine. This is done by reading files in /proc so this library is linux only.

import "github.com/KarpelesLab/peerinfo"

func xxx(c net.Conn) {
	// remote and local are reversed as we want to remote side of the socket
	ci, err := peerinfo.Find(c.RemoteAddr(), c.LocalAddr())
	
	// check err, etc...
}

With Unix sockets?

Go notoriously lacks a way to get the peer's information on a unix socket. As it turns out, an issue was opened in 2010 about this (get SO_PEERCRED information from an open socket), but was hijacked into sending FDs and credentials over unix sockets and the original issue was cast aside for the issue to be finally closed in 2017 without a proper resolution.

Because of this, this package will also provide a method to obtain peer credentials based on a net.UnixConn. Maybe one day Go will have a proper API to perform this.

You can either directly query info with peerinfo.UnixPeer() if you know it will be a unix conn, or let peerinfo detect the type of connection.

import "github.com/KarpelesLab/peerinfo"

func xxx(c net.Conn) {
	// just pass the connection and it'll be automatic
	ci, err := peerinfo.FindPeer(c)
	if err != nil {
		...
	}

	// can use ci.Pid, ci.GetUid(), ci.GetGid()

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrUnsupportedConnectionType = errors.New("unsupported connection type")
)

Functions

This section is empty.

Types

type Process

type Process struct {
	Pid int
	Uid int
	Gid int
	// contains filtered or unexported fields
}

Process identifies a specific running process on the machine

func Find

func Find(loc, rem net.Addr) (*Process, error)

func FindPeer added in v0.0.3

func FindPeer(n net.Conn) (*Process, error)

func UnixPeer added in v0.0.3

func UnixPeer(c *net.UnixConn) (*Process, error)

func (*Process) Cmdline

func (c *Process) Cmdline() ([]string, error)

Cmdline will return an array of arguments used to run this executable

func (*Process) Exe

func (c *Process) Exe() (string, error)

Exe will return the path of the running executable

func (*Process) GetGid added in v0.0.3

func (p *Process) GetGid() int

func (*Process) GetUid added in v0.0.3

func (p *Process) GetUid() int

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL