Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions app/router/condition.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package router

import (
"context"
"os"
"regexp"
"strings"

Expand Down Expand Up @@ -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
}
8 changes: 8 additions & 0 deletions app/router/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,14 @@ func (rr *RoutingRule) BuildCondition() (Condition, error) {
conds.Add(matcher)
}

if len(rr.ProcessName) > 0 {
refinedNames := make([]string, 0, len(rr.ProcessName))
for _, name := range rr.ProcessName {
refinedNames = append(refinedNames, strings.TrimSuffix(name, ".exe"))
}
conds.Add(&ProcessNameMatcher{refinedNames})
}

if conds.Len() == 0 {
return nil, errors.New("this rule has no effective fields").AtWarning()
}
Expand Down
132 changes: 71 additions & 61 deletions app/router/config.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions app/router/config.proto
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ message RoutingRule {
xray.common.net.PortList local_port_list = 18;

xray.common.net.PortList vless_route_list = 20;
repeated string process_name = 21;
}

message BalancingRule {
Expand Down
Loading