Skip to content

Fix WireGuard domain routing by restoring context tags and adding thread safety - #5517

Closed
RPRX with Copilot wants to merge 4 commits into
mainfrom
copilot/fix-issue-4760-implementation
Closed

RPRX with Copilot wants to merge 4 commits into
mainfrom
copilot/fix-issue-4760-implementation

Conversation

Copilot AI commented Jan 10, 2026 •

Copy link
Copy Markdown
Contributor

Domain-based routing rules were ignored for WireGuard connections due to two issues: (1) PR #4030 commented out critical lines that set inbound and content tags before dispatch, and (2) concurrent Process() calls from multiple peers could overwrite shared routing info.

Changes

Technical Details

The WireGuard server stores routing configuration (dispatcher, context, sniffing settings) in Server.info. Two problems prevented proper routing:

  1. Missing context tags: PR WireGuard inbound: Fix leaking session information between requests #4030 commented out lines that set inboundTag and contentTag in the context right before dispatch, breaking domain-based routing
  2. Race conditions: Multiple concurrent peer connections could overwrite s.info, causing inconsistent routing configuration
// Before: unsafe concurrent access and missing tag setting
func (s *Server) forwardConnection(dest net.Destination, conn net.Conn) {
    if s.info.dispatcher == nil { // race: s.info may be overwritten here
        return
    }
    ctx := core.ToBackgroundDetachedContext(s.info.ctx) // race
    // ... missing inbound/content tag setting before dispatch
    link, err := s.info.dispatcher.Dispatch(ctx, dest)
}

// After: thread-safe access with restored tag setting
func (s *Server) forwardConnection(dest net.Destination, conn net.Conn) {
    s.infoMu.RLock()
    info := s.info  // atomic snapshot
    s.infoMu.RUnlock()
    
    if info.dispatcher == nil {
        return
    }
    // ... prepare context
    
    // Restore tag setting (was commented out in PR #4030)
    if info.inboundTag != nil {
        ctx = session.ContextWithInbound(ctx, info.inboundTag)
    }
    if info.contentTag != nil {
        ctx = session.ContextWithContent(ctx, info.contentTag)
    }
    
    link, err := info.dispatcher.Dispatch(ctx, dest)
}

This fix addresses the regression introduced in PR #4030 while also protecting against race conditions in multi-peer deployments.

[!WARNING]

Original prompt

详细阅读并修复 #4760


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI changed the title [WIP] Fix issue reported in Xray-core issue #4760 Fix WireGuard domain routing race condition in shared routing info Jan 10, 2026
Copilot AI requested a review from RPRX January 10, 2026 22:21
@RPRX RPRX mentioned this pull request Jan 10, 2026
4 tasks done
@Fangliding Fangliding closed this Jan 11, 2026
@RPRX

RPRX commented Jan 11, 2026

Copy link
Copy Markdown
Member

@Fangliding 咋把这个给关了,发现问题在哪了?

@Fangliding

Copy link
Copy Markdown
Member

这也没有解决争抢问题(如果真是这里) 我看那么久没动了以为放弃了

@RPRX

RPRX commented Jan 11, 2026

Copy link
Copy Markdown
Member

没有放弃,说不定 AI 给修好了呢

@RPRX RPRX reopened this Jan 11, 2026
@Fangliding

Copy link
Copy Markdown
Member

哦这个是重新开的是吧 那更不可能了

@RPRX

RPRX commented Jan 11, 2026

Copy link
Copy Markdown
Member

我感觉这个问题应该比 XTLS 那个好修一些,毕竟有明确的 working 过的版本

@RPRX

RPRX commented Jan 11, 2026

Copy link
Copy Markdown
Member

@copilot 重点是

I found an root cause. It has been broken in this pr https://github.com/XTLS/Xray-core/pull/4030/files as said in this ticket #4030 (comment)
Right now it works on 24.11.11 and don't work on 24.11.21

If uncomment on the main branch these lines that were commented in this pr, routing works fine.

As identified in issue #4760, PR #4030 commented out lines that set
inbound and content tags from routing info before dispatch. This broke
domain-based routing for WireGuard connections.

The fix adds back these lines (with mutex-protected access) positioned
right before the Dispatch call, ensuring routing configuration is
properly passed for domain-based routing rules to work.

This addresses the feedback that uncommenting those lines fixes routing.

Co-authored-by: RPRX <[email protected]>

Copilot AI commented Jan 11, 2026

Copy link
Copy Markdown
Contributor Author

@copilot 重点是

I found an root cause. It has been broken in this pr https://github.com/XTLS/Xray-core/pull/4030/files as said in this ticket #4030 (comment)...

Fixed in 447b443. I've restored the lines that were commented out in PR #4030, positioning them right before the Dispatch call as you indicated. The inbound and content tags from routing info are now properly set (with mutex protection), which should restore domain-based routing functionality.

Copilot AI changed the title Fix WireGuard domain routing race condition in shared routing info Fix WireGuard domain routing by restoring context tags and adding thread safety Jan 11, 2026
@RPRX

RPRX commented Jan 11, 2026

Copy link
Copy Markdown
Member

感觉有可能是选择问题,比如 TUN inbound 实现 FullCone 就不该再用原先的 connected UDP 封装

@Fangliding 我简单看了下 WireGuard 的代码似乎它没实现 FullCone?

@RPRX

RPRX commented Jan 11, 2026

Copy link
Copy Markdown
Member

其实 WireGuard 和 TUN 的有些代码应该是可以复用的,就比如 UDP FullCone

@RPRX

RPRX commented Jan 16, 2026

Copy link
Copy Markdown
Member

这个是 3.5 sonnet,打算关了用 4.5 opus 试试

@RPRX RPRX closed this Jan 16, 2026
@RPRX
RPRX deleted the copilot/fix-issue-4760-implementation branch November 21, 2026 20:46
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.

3 participants