Skip to content

Commit cff16c6

Browse files
authored
feat(rule): implement process name matching for routing rules (#956)
* match process name * up * clippy * f * up * up
1 parent 4479974 commit cff16c6

File tree

5 files changed

+144
-11
lines changed

5 files changed

+144
-11
lines changed

.github/workflows/ci.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,19 +148,19 @@ jobs:
148148
rustflags: "-Ctarget-feature=+crt-static --cfg tokio_unstable"
149149
no-test: true
150150
# MacOSX
151-
- os: macos-14
151+
- os: macos-latest
152152
target: x86_64-apple-darwin
153153
extra-args: -F "plus"
154-
- os: macos-14
154+
- os: macos-latest
155155
target: aarch64-apple-darwin
156156
extra-args: -F "plus"
157157
# MacOSX static-crt
158-
- os: macos-14
158+
- os: macos-latest
159159
target: x86_64-apple-darwin
160160
release-name: x86_64-apple-darwin-static-crt
161161
extra-args: -F "plus"
162162
rustflags: "-Ctarget-feature=+crt-static --cfg tokio_unstable"
163-
- os: macos-14
163+
- os: macos-latest
164164
target: aarch64-apple-darwin
165165
release-name: aarch64-apple-darwin-static-crt
166166
tool: cargo

.github/workflows/coverage.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ jobs:
1717
runs-on: ${{ matrix.os }}
1818
strategy:
1919
matrix:
20-
os: ["macos-14", "windows-latest", "ubuntu-latest"]
20+
os: ["macos-latest", "windows-latest", "ubuntu-latest"]
2121
steps:
2222
- uses: actions/checkout@v5
2323
with:

Cargo.lock

Lines changed: 106 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

clash-lib/Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ anyhow = "1"
7777
# logging
7878
time = { version = "=0.3", features = ["macros", "local-offset"] }
7979
tracing-subscriber = { version = "0.3", features = ["env-filter", "local-time"] }
80-
tracing-appender = { version = "0.2"}
80+
tracing-appender = { version = "0.2" }
8181
tracing-log = "0.2"
8282

8383
tracing-chrome = { version = "0.7", optional = true }
@@ -211,6 +211,8 @@ http-body-util = "0.1"
211211
[build-dependencies]
212212
prost-build = "0.14"
213213

214+
[target.'cfg(any(target_os="linux", target_os="macos"))'.dependencies]
215+
sock2proc = { git = "https://github.com/Watfaq/sock2proc.git", rev = "1097e6b" }
214216
[target.'cfg(unix)'.dependencies]
215217
hyperlocal = { version = "0.9", features = ["client"] }
216218

clash-lib/src/app/router/rules/process.rs

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,36 @@ impl std::fmt::Display for Process {
1414
}
1515

1616
impl RuleMatcher for Process {
17-
fn apply(&self, _sess: &crate::session::Session) -> bool {
18-
// TODO: implement this
19-
false
17+
fn apply(&self, sess: &crate::session::Session) -> bool {
18+
#[cfg(any(target_os = "linux", target_os = "macos"))]
19+
{
20+
use crate::session::Network;
21+
use tracing::debug;
22+
23+
sock2proc::find_process_name(
24+
Some(sess.source),
25+
sess.destination.clone().try_into_socket_addr(),
26+
match sess.network {
27+
Network::Tcp => sock2proc::NetworkProtocol::TCP,
28+
Network::Udp => sock2proc::NetworkProtocol::UDP,
29+
},
30+
)
31+
.is_some_and(|proc| {
32+
debug!("Matching process name: {} with {}", proc, self.name);
33+
if self.name_only {
34+
proc == self.name
35+
} else {
36+
proc.contains(&self.name)
37+
}
38+
})
39+
}
40+
#[cfg(not(any(target_os = "linux", target_os = "macos")))]
41+
{
42+
use tracing::info;
43+
44+
info!("PROCESS-NAME not supported on Windows yet: {}", &sess);
45+
false
46+
}
2047
}
2148

2249
fn target(&self) -> &str {

0 commit comments

Comments
 (0)