SagerNet / SagerNet/sing-tun

Feature: Add configurable interface and route metrics

Open
#62 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Go
Stars
237
Forks
228
PR merge metrics
No merged PRs in 30d

Description

Problem

Currently, interface and route metrics are hardcoded in the TUN implementation:

  • Windows: Interface metric is set to 0 when AutoRoute is enabled
  • Routes: Always use metric 0 (highest priority)

This creates issues when users need different routing priorities on different devices while using the same configuration file.

Proposed Solution

Add two new fields to the Options struct:

  • InterfaceMetric uint32 - Sets the metric for the TUN interface (Windows only)
  • RouteMetric uint32 - Sets the metric for routes through the TUN interface (Windows and Linux)
Behavior:
  • InterfaceMetric = 0 (default):
    • When AutoRoute=true: metric is set to 0 (highest priority)
    • When AutoRoute=false: Windows automatic metric is used
  • InterfaceMetric > 0: Explicitly specified metric (lower value = higher priority)
  • RouteMetric = 0 (default): Metric 0 (highest priority)
  • RouteMetric > 0: Explicitly specified metric
  • On Linux, RouteMetric maps to the Priority field in netlink routes

Implementation

Changes required in:

  • tun.go: Add InterfaceMetric and RouteMetric fields to Options
  • tun_windows.go: Implement interface metric logic for IPv4/IPv6, update route metric
  • tun_linux.go: Map RouteMetric to Priority field

Backward Compatibility

Fully backward compatible - when both fields are 0 (default), behavior remains unchanged.

Use Case

Users running the same configuration on multiple devices (e.g., desktop and mobile) can now set different routing priorities without maintaining separate config files.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading the Options struct and current metric handling in tun.go, tun_windows.go, and tun_linux.go. Trace the existing IPv4/IPv6 interface and route setup, then verify that nonzero InterfaceMetric and RouteMetric values are applied while both zero values preserve the documented behavior on Windows and Linux.

Written by the indexing model from the issue text.

Assessment

Tech stack
go
Domain
networking
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.