-
Notifications
You must be signed in to change notification settings - Fork 0
Add novpn build variants for tunnel and split tunnel #527
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| //go:build !novpn | ||
|
|
||
| package vpn | ||
|
|
||
| import ( | ||
| "slices" | ||
| "testing" | ||
|
|
||
| O "github.com/sagernet/sing-box/option" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestBuildOptions_RejectsQUICAfterDirectRules(t *testing.T) { | ||
| cfg := testConfig(t) | ||
| opts, err := buildOptions(BoxOptions{ | ||
| BasePath: t.TempDir(), | ||
| Options: cfg.Options, | ||
| SmartRouting: cfg.SmartRouting, | ||
| AdBlock: cfg.AdBlock, | ||
| }) | ||
| require.NoError(t, err) | ||
|
|
||
| isQUICReject := func(r O.Rule) bool { | ||
| opts := r.DefaultOptions | ||
| return opts.RuleAction.Action == "reject" && | ||
| slices.Contains(opts.RawDefaultRule.Network, "udp") && | ||
| slices.Contains(opts.RawDefaultRule.Port, uint16(443)) | ||
| } | ||
| isSplitTunnel := func(r O.Rule) bool { | ||
| return slices.Contains(r.DefaultOptions.RawDefaultRule.RuleSet, splitTunnelTag) | ||
| } | ||
| isSelector := func(r O.Rule) bool { | ||
| mode := r.DefaultOptions.RawDefaultRule.ClashMode | ||
| return mode == AutoSelectTag || mode == ManualSelectTag | ||
| } | ||
|
|
||
| quicIdx, splitIdx, selectorIdx := -1, -1, -1 | ||
| for i, r := range opts.Route.Rules { | ||
| switch { | ||
| case isQUICReject(r): | ||
| quicIdx = i | ||
| case isSplitTunnel(r): | ||
| splitIdx = i | ||
| case isSelector(r) && selectorIdx == -1: | ||
| selectorIdx = i | ||
| } | ||
| } | ||
| require.NotEqual(t, -1, quicIdx, "expected UDP/443 reject rule in built options") | ||
| require.NotEqual(t, -1, splitIdx, "expected split-tunnel rule in built options") | ||
| require.NotEqual(t, -1, selectorIdx, "expected at least one selector mode rule in built options") | ||
| assert.Greater(t, quicIdx, splitIdx, "QUIC reject must come after split-tunnel rule so split-direct domains keep QUIC") | ||
| assert.Less(t, quicIdx, selectorIdx, "QUIC reject must come before selector rules so proxied QUIC is rejected") | ||
| } | ||
|
|
||
| func TestBaseOpts_TunIPv6Address(t *testing.T) { | ||
| opts := baseOpts(t.TempDir()) | ||
| require.NotEmpty(t, opts.Inbounds, "expected inbounds in baseOpts output") | ||
|
|
||
| var tunOpts *O.TunInboundOptions | ||
| for _, in := range opts.Inbounds { | ||
| if in.Type == "tun" { | ||
| var ok bool | ||
| tunOpts, ok = in.Options.(*O.TunInboundOptions) | ||
| require.True(t, ok, "expected *TunInboundOptions for tun inbound") | ||
| break | ||
| } | ||
| } | ||
| require.NotNil(t, tunOpts, "expected a tun inbound") | ||
| require.NotEmpty(t, tunOpts.Address, "expected at least the v4 TUN address") | ||
| assert.Equal(t, "10.10.1.1/30", tunOpts.Address[0].String(), "first TUN address should be the v4 prefix") | ||
|
|
||
| if hasGlobalIPv6() { | ||
| require.Len(t, tunOpts.Address, 2, "expected v4 + v6 ULA on TUN when system has global v6") | ||
| assert.Equal(t, "fdfe:dcba:9876::1/126", tunOpts.Address[1].String(), | ||
| "v6 ULA should be appended after the v4 address") | ||
| } else { | ||
| require.Len(t, tunOpts.Address, 1, "expected v4-only TUN when system has no global v6") | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| //go:build !novpn | ||
|
|
||
| package vpn | ||
|
|
||
| import ( | ||
| "log/slog" | ||
| "net/netip" | ||
|
|
||
| C "github.com/sagernet/sing-box/constant" | ||
| O "github.com/sagernet/sing-box/option" | ||
| "github.com/sagernet/sing/common/json/badoption" | ||
|
|
||
| "github.com/getlantern/radiance/bypass" | ||
| "github.com/getlantern/radiance/common" | ||
| ) | ||
|
|
||
| // baseInbounds returns the inbounds for a tunnel build: the TUN device plus the | ||
| // loopback bypass proxy used for kindling connections. The TUN inbound is at | ||
| // index 0 so applyPlatformTunnelOptions can adjust it per platform. | ||
| func baseInbounds() []O.Inbound { | ||
| loopbackAddr := badoption.Addr(netip.MustParseAddr("127.0.0.1")) | ||
|
|
||
| // ULA-only interfaces don't indicate real public v6, so gate the TUN v6 address | ||
| // on genuine global v6 connectivity. | ||
| tunAddress := []netip.Prefix{ | ||
| netip.MustParsePrefix("10.10.1.1/30"), | ||
| } | ||
| if hasGlobalIPv6() { | ||
| tunAddress = append(tunAddress, netip.MustParsePrefix("fdfe:dcba:9876::1/126")) | ||
| slog.Info("vpn: TUN with IPv6 ULA (system has global v6)") | ||
| } else { | ||
| slog.Info("vpn: TUN IPv4-only (no global v6 detected)") | ||
| } | ||
|
|
||
| return []O.Inbound{ | ||
| { | ||
| Type: "tun", | ||
| Tag: "tun-in", | ||
| Options: &O.TunInboundOptions{ | ||
| InterfaceName: "utun225", | ||
| Address: tunAddress, | ||
| AutoRoute: true, | ||
| StrictRoute: true, | ||
| EndpointIndependentNat: true, // needed for QUIC migration and hole-punching | ||
| Stack: "system", // fallback to gvisor on older Android kernels in applyPlatformTunnelOptions | ||
| }, | ||
| }, | ||
| { | ||
| Type: C.TypeMixed, | ||
| Tag: bypass.BypassInboundTag, | ||
| Options: &O.HTTPMixedInboundOptions{ | ||
| ListenOptions: O.ListenOptions{ | ||
| Listen: &loopbackAddr, | ||
| ListenPort: bypass.ProxyPort, | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| // applyPlatformTunnelOptions indexes opts.Inbounds[0] as the TUN device, per the | ||
| // invariant established by baseInbounds. Safe only in tunnel mode. | ||
| func applyPlatformTunnelOptions(opts *O.Options) { | ||
| switch common.Platform { | ||
| case "android": | ||
| opts.Route.OverrideAndroidVPN = true | ||
| kv := kernelVersion() | ||
| slog.Debug("detected kernel version", "kernel", kv) | ||
| if kv == "" { | ||
| slog.Warn("kernel version unknown, keeping default TUN stack") | ||
| } else if kernelBelow(kv, minAndroidSystemStackKernel) { | ||
| opts.Inbounds[0].Options.(*O.TunInboundOptions).Stack = "gvisor" | ||
| slog.Info("kernel below 5.10, using gvisor TUN stack", "kernel", kv) | ||
| } | ||
| slog.Debug("Android platform detected, OverrideAndroidVPN set to true") | ||
| case "ios": | ||
| opts.Inbounds[0].Options.(*O.TunInboundOptions).Stack = "" | ||
| slog.Debug("iOS platform detected, using default TUN stack with no override") | ||
| case "linux": | ||
| opts.Inbounds[0].Options.(*O.TunInboundOptions).AutoRedirect = true | ||
| slog.Debug("Linux platform detected, AutoRedirect set to true") | ||
| } | ||
| } | ||
|
reflog marked this conversation as resolved.
Outdated
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.