Skip to content

Routing: Add localOS that directly matches runtime.GOOS - #6553

Merged
RPRX merged 1 commit into
XTLS:mainfrom
fanymagnet:routing-local-os
Aug 12, 2026
Merged

RPRX merged 1 commit into
XTLS:mainfrom
fanymagnet:routing-local-os

Conversation

@fanymagnet

Copy link
Copy Markdown
Contributor

Adds localOS to routing rules: the rule only applies when Xray itself runs on
one of the listed operating systems. Values are runtime.GOOS (android, ios,
windows, darwin, linux, ...), compared case-insensitively. Since the OS
never changes at runtime, the result is resolved once when the rule is built, so
there is no cost on the hot path.

Why: clients commonly fetch a single config from a URL, and the same config
is shared across Android, iOS, Windows and desktop. Some rules are inherently
platform-specific — process values are package names on Android, and a tun
inbound only exists on mobile — so without a guard they either silently never
match or, in the case of a catch-all, do the wrong thing everywhere else.

In the example below the second rule blocks everything from tun-in that is not
an allowed app. Both rules are scoped to Android, so on Windows or macOS the
same shared config simply falls through them instead of blocking all traffic.

{
  "inbounds": [
    {
      "protocol": "tun",
      "tag": "tun-in"
    }
  ],
  "outbounds": [
    {
      "protocol": "freedom",
      "tag": "direct-out"
    },
    {
      "protocol": "loopback",
      "settings": {
        "inboundTag": "allowed-apps-in"
      },
      "tag": "allowed-apps-out"
    },
    {
      "protocol": "blackhole",
      "tag": "block-out"
    }
  ],
  "routing": {
    "rules": [
      {
        "inboundTag": ["tun-in"],
        "process": [
          "com.google.android.youtube",
          "com.android.chrome"
        ],
        "localOS": ["android"],
        "outboundTag": "allowed-apps-out"
      },
      {
        "inboundTag": ["tun-in"],
        "localOS": ["android"],
        "outboundTag": "block-out"
      }
    ]
  }
}

@patterniha

patterniha commented Jul 29, 2026 •

Copy link
Copy Markdown
Collaborator

This is exactly what I need for "Serverless-for-Iran" (because fragment behaves differently on different OSes), I was thinking of adding it. Thanks.

@vo0ov

vo0ov commented Jul 30, 2026

Copy link
Copy Markdown

Great idea! This would be a really useful addition for shared configs across different platforms. Hope it gets merged.

Matches the operating system Xray itself is running on (`runtime.GOOS`,
compared case-insensitively). Resolved once when the rule is built.

Lets a single config shared across platforms carry platform-specific
rules, such as Android `process` package names or `tun` inbound rules,
without them misfiring on other operating systems.
@RPRX

RPRX commented Aug 12, 2026

Copy link
Copy Markdown
Member

感觉 GOOS 在匹配 darwin/ios 上有点混乱比如 #6524 ,不过似乎只影响开发者,且无关这个 PR 毕竟单值肯定取最精确的

@RPRX

RPRX commented Aug 12, 2026

Copy link
Copy Markdown
Member

以后真有需要的话可以水一个 PR 加上 localARCH 等别的东西

@RPRX

RPRX commented Aug 12, 2026 •

Copy link
Copy Markdown
Member

@Fangliding 写文档时要说明 localOS 既然是数组就应该写最精确的,比如 darwin 不含 ios,要同时匹配上两个的话需要都写

@RPRX RPRX changed the title Routing: Add localOS rule condition Routing: Add localOS that directly matches runtime.GOOS Aug 12, 2026
@RPRX
RPRX merged commit a12801c into XTLS:main Aug 12, 2026
40 checks passed
@Fangliding

Copy link
Copy Markdown
Member

神秘goos逻辑

@RPRX

RPRX commented Aug 12, 2026 •

Copy link
Copy Markdown
Member

可能是因为 iOS 不像 Android 上还有可能跑裸 bin 所以 go build 那里没分配单独的 iOS,但又有 gomobile,总之有点混乱

不过 // go:build 那里写 linux 似乎也能作用到 android 来着?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants