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.
Problem
Currently, interface and route metrics are hardcoded in the TUN implementation:
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
Optionsstruct: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:
AutoRoute=true: metric is set to 0 (highest priority)AutoRoute=false: Windows automatic metric is usedRouteMetricmaps to thePriorityfield in netlink routesImplementation
Changes required in:
tun.go: AddInterfaceMetricandRouteMetricfields toOptionstun_windows.go: Implement interface metric logic for IPv4/IPv6, update route metrictun_linux.go: MapRouteMetrictoPriorityfieldBackward 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.