Files
sing-box-extended-mirror/common/process/searcher.go
T

40 lines
1.0 KiB
Go
Raw Normal View History

package process
import (
"context"
"net/netip"
2023-04-11 14:39:57 +08:00
"os/user"
"github.com/sagernet/sing-box/adapter"
"github.com/sagernet/sing-box/log"
"github.com/sagernet/sing-tun"
E "github.com/sagernet/sing/common/exceptions"
2023-04-11 14:39:57 +08:00
F "github.com/sagernet/sing/common/format"
)
type Searcher interface {
FindProcessInfo(ctx context.Context, network string, source netip.AddrPort, destination netip.AddrPort) (*adapter.ConnectionOwner, error)
2026-03-23 15:54:29 +08:00
Close() error
}
var ErrNotFound = E.New("process not found")
type Config struct {
Logger log.ContextLogger
PackageManager tun.PackageManager
}
func FindProcessInfo(searcher Searcher, ctx context.Context, network string, source netip.AddrPort, destination netip.AddrPort) (*adapter.ConnectionOwner, error) {
2023-04-11 14:39:57 +08:00
info, err := searcher.FindProcessInfo(ctx, network, source, destination)
if err != nil {
return nil, err
}
2026-03-23 16:49:01 +08:00
if info.UserId != -1 && info.UserName == "" {
2023-04-11 14:39:57 +08:00
osUser, _ := user.LookupId(F.ToString(info.UserId))
if osUser != nil {
info.UserName = osUser.Username
2023-04-11 14:39:57 +08:00
}
}
return info, nil
2022-08-20 10:38:12 +08:00
}