Routing config: Add processName (#5489)

This commit is contained in:
风扇滑翔翼
2026-01-05 09:12:13 +08:00
committed by GitHub
parent e7c72c011f
commit 7265b5ac3f
9 changed files with 614 additions and 77 deletions
+39
View File
@@ -2,6 +2,7 @@ package router
import (
"context"
"os"
"regexp"
"strings"
@@ -305,3 +306,41 @@ func (m *AttributeMatcher) Apply(ctx routing.Context) bool {
}
return m.Match(attributes)
}
type ProcessNameMatcher struct {
names []string
}
func (m *ProcessNameMatcher) Apply(ctx routing.Context) bool {
srcPort := ctx.GetSourcePort().String()
srcIP := ctx.GetSourceIPs()[0].String()
var network string
switch ctx.GetNetwork() {
case net.Network_TCP:
network = "tcp"
case net.Network_UDP:
network = "udp"
default:
return false
}
src, err := net.ParseDestination(strings.Join([]string{network, srcIP, srcPort}, ":"))
if err != nil {
return false
}
pid, name, err := net.FindProcess(src)
if err != nil {
if err != net.ErrNotLocal {
errors.LogError(context.Background(), "Unables to find local process name: ", err)
}
return false
}
for _, n := range m.names {
if name == "/self" && pid == os.Getpid() {
return true
}
if n == name {
return true
}
}
return false
}