sing-box/common/process/searcher.go

45 lines
1 KiB
Go
Raw Permalink Normal View History

package process
import (
"context"
"net/netip"
2023-04-11 06:39:57 +00:00
"os/user"
"github.com/sagernet/sing-box/log"
"github.com/sagernet/sing-tun"
E "github.com/sagernet/sing/common/exceptions"
2023-04-11 06:39:57 +00:00
F "github.com/sagernet/sing/common/format"
)
type Searcher interface {
2022-08-20 02:38:12 +00:00
FindProcessInfo(ctx context.Context, network string, source netip.AddrPort, destination netip.AddrPort) (*Info, error)
}
var ErrNotFound = E.New("process not found")
type Config struct {
Logger log.ContextLogger
PackageManager tun.PackageManager
}
type Info struct {
ProcessPath string
PackageName string
User string
UserId int32
}
2022-08-20 02:38:12 +00:00
func FindProcessInfo(searcher Searcher, ctx context.Context, network string, source netip.AddrPort, destination netip.AddrPort) (*Info, error) {
2023-04-11 06:39:57 +00:00
info, err := searcher.FindProcessInfo(ctx, network, source, destination)
if err != nil {
return nil, err
}
if info.UserId != -1 {
osUser, _ := user.LookupId(F.ToString(info.UserId))
if osUser != nil {
info.User = osUser.Username
}
}
return info, nil
2022-08-20 02:38:12 +00:00
}