Skip to content

Commit

Permalink
Add support for auto redirect
Browse files Browse the repository at this point in the history
  • Loading branch information
nekohasekai committed May 29, 2024
1 parent 3014a63 commit d83242c
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 12 deletions.
9 changes: 5 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ require (
github.com/go-chi/render v1.0.3
github.com/miekg/dns v1.1.59
github.com/sagernet/bbolt v0.0.0-20231014093535-ea5cb2fe9f0a
github.com/sagernet/sing v0.4.0-beta.19
github.com/sagernet/sing-box v1.9.0-rc.19
github.com/sagernet/sing v0.5.0-alpha.7
github.com/sagernet/sing-box v1.10.0-alpha.2
github.com/sagernet/sing-dns v0.2.0-beta.18
github.com/spf13/cobra v1.8.0
golang.org/x/mod v0.17.0
Expand Down Expand Up @@ -59,14 +59,15 @@ require (
github.com/quic-go/qtls-go1-20 v0.4.1 // indirect
github.com/sagernet/cloudflare-tls v0.0.0-20231208171750-a4483c1b7cd1 // indirect
github.com/sagernet/gvisor v0.0.0-20240428053021-e691de28565f // indirect
github.com/sagernet/netlink v0.0.0-20220905062125-8043b4a9aa97 // indirect
github.com/sagernet/netlink v0.0.0-20240523065131-45e60152f9ba // indirect
github.com/sagernet/quic-go v0.43.1-beta.1 // indirect
github.com/sagernet/reality v0.0.0-20230406110435-ee17307e7691 // indirect
github.com/sagernet/sing-mux v0.2.0 // indirect
github.com/sagernet/sing-quic v0.2.0-beta.5 // indirect
github.com/sagernet/sing-shadowsocks v0.2.6 // indirect
github.com/sagernet/sing-shadowsocks2 v0.2.0 // indirect
github.com/sagernet/sing-shadowtls v0.1.4 // indirect
github.com/sagernet/sing-tun v0.3.0-beta.2 // indirect
github.com/sagernet/sing-tun v0.3.0-beta.6 // indirect
github.com/sagernet/sing-vmess v0.1.8 // indirect
github.com/sagernet/smux v0.0.0-20231208180855-7041f6ea79e7 // indirect
github.com/sagernet/tfo-go v0.0.0-20231209031829-7b5343ac1dc6 // indirect
Expand Down
22 changes: 14 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -113,29 +113,35 @@ github.com/sagernet/cloudflare-tls v0.0.0-20231208171750-a4483c1b7cd1 h1:YbmpqPQ
github.com/sagernet/cloudflare-tls v0.0.0-20231208171750-a4483c1b7cd1/go.mod h1:J2yAxTFPDjrDPhuAi9aWFz2L3ox9it4qAluBBbN0H5k=
github.com/sagernet/gvisor v0.0.0-20240428053021-e691de28565f h1:NkhuupzH5ch7b/Y/6ZHJWrnNLoiNnSJaow6DPb8VW2I=
github.com/sagernet/gvisor v0.0.0-20240428053021-e691de28565f/go.mod h1:KXmw+ouSJNOsuRpg4wgwwCQuunrGz4yoAqQjsLjc6N0=
github.com/sagernet/netlink v0.0.0-20220905062125-8043b4a9aa97 h1:iL5gZI3uFp0X6EslacyapiRz7LLSJyr4RajF/BhMVyE=
github.com/sagernet/netlink v0.0.0-20220905062125-8043b4a9aa97/go.mod h1:xLnfdiJbSp8rNqYEdIW/6eDO4mVoogml14Bh2hSiFpM=
github.com/sagernet/netlink v0.0.0-20240523065131-45e60152f9ba h1:EY5AS7CCtfmARNv2zXUOrsEMPFDGYxaw65JzA2p51Vk=
github.com/sagernet/netlink v0.0.0-20240523065131-45e60152f9ba/go.mod h1:xLnfdiJbSp8rNqYEdIW/6eDO4mVoogml14Bh2hSiFpM=
github.com/sagernet/quic-go v0.43.1-beta.1 h1:alizUjpvWYcz08dBCQsULOd+1xu0o7UtlyYf6SLbRNg=
github.com/sagernet/quic-go v0.43.1-beta.1/go.mod h1:BkrQYeop7Jx3hN3TW8/76CXcdhYiNPyYEBL/BVJ1ifc=
github.com/sagernet/reality v0.0.0-20230406110435-ee17307e7691 h1:5Th31OC6yj8byLGkEnIYp6grlXfo1QYUfiYFGjewIdc=
github.com/sagernet/reality v0.0.0-20230406110435-ee17307e7691/go.mod h1:B8lp4WkQ1PwNnrVMM6KyuFR20pU8jYBD+A4EhJovEXU=
github.com/sagernet/sing v0.2.18/go.mod h1:OL6k2F0vHmEzXz2KW19qQzu172FDgSbUSODylighuVo=
github.com/sagernet/sing v0.4.0-beta.19 h1:Kzh2f6eaxt09S+bEL8F4Bgy03qkNf+4Tt8g7/5lxp/o=
github.com/sagernet/sing v0.4.0-beta.19/go.mod h1:PFQKbElc2Pke7faBLv8oEba5ehtKO21Ho+TkYemTI3Y=
github.com/sagernet/sing-box v1.9.0-rc.19 h1:pFVSepj0brtWLbyoakLjD1vJkfQCDLmb8eH3QpiJdt8=
github.com/sagernet/sing-box v1.9.0-rc.19/go.mod h1:irDVISS/9jCcf+Js5sscJ44Okbjx5IiKZzk7IFO4K6g=
github.com/sagernet/sing v0.5.0-alpha.5 h1:Y1uv06KfUVQWo4Bw2tFVoGVatpzYJnfbgeZxrhrnl5s=
github.com/sagernet/sing v0.5.0-alpha.5/go.mod h1:Xh4KO9nGdvm4K/LVg9Xn9jSxJdqe9KcXbAzNC1S2qfw=
github.com/sagernet/sing v0.5.0-alpha.7 h1:yxjiH0vQAotu87JNJ9B0BGO0OJqsFjug84xcYdwyDm4=
github.com/sagernet/sing v0.5.0-alpha.7/go.mod h1:Xh4KO9nGdvm4K/LVg9Xn9jSxJdqe9KcXbAzNC1S2qfw=
github.com/sagernet/sing-box v1.10.0-alpha.1 h1:ahDvE9e7sqcy9+9Tmp0Ba02DVkQRUXRfnAhXuDfGGCQ=
github.com/sagernet/sing-box v1.10.0-alpha.1/go.mod h1:skqvO7cGD3pmRNomE6mT++yIJ2sKURY7QPHDlu0FyuY=
github.com/sagernet/sing-box v1.10.0-alpha.2 h1:lt+nivIaqhltiWw5uA89XK8/CXxgl1aCrHOD4nXQ89c=
github.com/sagernet/sing-box v1.10.0-alpha.2/go.mod h1:KYAOtxT/fb98V3n842Z4H9JGPgrvUEg2XBUvRoXEuH8=
github.com/sagernet/sing-dns v0.2.0-beta.18 h1:6vzXZThRdA7YUzBOpSbUT48XRumtl/KIpIHFSOP0za8=
github.com/sagernet/sing-dns v0.2.0-beta.18/go.mod h1:k/dmFcQpg6+m08gC1yQBy+13+QkuLqpKr4bIreq4U24=
github.com/sagernet/sing-mux v0.2.0 h1:4C+vd8HztJCWNYfufvgL49xaOoOHXty2+EAjnzN3IYo=
github.com/sagernet/sing-mux v0.2.0/go.mod h1:khzr9AOPocLa+g53dBplwNDz4gdsyx/YM3swtAhlkHQ=
github.com/sagernet/sing-quic v0.2.0-beta.5 h1:ceKFLd1iS5AtM+pScKmcDp5k7R6WgYIe8vl6nB0aVsE=
github.com/sagernet/sing-quic v0.2.0-beta.5/go.mod h1:lfad61lScAZhAxZ0DHZWvEIcAaT38O6zPTR4vLsHeP0=
github.com/sagernet/sing-shadowsocks v0.2.6 h1:xr7ylAS/q1cQYS8oxKKajhuQcchd5VJJ4K4UZrrpp0s=
github.com/sagernet/sing-shadowsocks v0.2.6/go.mod h1:j2YZBIpWIuElPFL/5sJAj470bcn/3QQ5lxZUNKLDNAM=
github.com/sagernet/sing-shadowsocks2 v0.2.0 h1:wpZNs6wKnR7mh1wV9OHwOyUr21VkS3wKFHi+8XwgADg=
github.com/sagernet/sing-shadowsocks2 v0.2.0/go.mod h1:RnXS0lExcDAovvDeniJ4IKa2IuChrdipolPYWBv9hWQ=
github.com/sagernet/sing-shadowtls v0.1.4 h1:aTgBSJEgnumzFenPvc+kbD9/W0PywzWevnVpEx6Tw3k=
github.com/sagernet/sing-shadowtls v0.1.4/go.mod h1:F8NBgsY5YN2beQavdgdm1DPlhaKQlaL6lpDdcBglGK4=
github.com/sagernet/sing-tun v0.3.0-beta.2 h1:sfeHWnBTKGpFUjXpT+O/JEwFP8oVAo3M0Xx94ghesjU=
github.com/sagernet/sing-tun v0.3.0-beta.2/go.mod h1:xPaOkQhngPMILx+/9DMLCFl4vSxUU2tMnCPSlf05HLo=
github.com/sagernet/sing-tun v0.3.0-beta.6 h1:L11kMrM7UfUW0pzQiU66Fffh4o86KZc1SFGbkYi8Ma8=
github.com/sagernet/sing-tun v0.3.0-beta.6/go.mod h1:DxLIyhjWU/HwGYoX0vNGg2c5QgTQIakphU1MuERR5tQ=
github.com/sagernet/sing-vmess v0.1.8 h1:XVWad1RpTy9b5tPxdm5MCU8cGfrTGdR8qCq6HV2aCNc=
github.com/sagernet/sing-vmess v0.1.8/go.mod h1:vhx32UNzTDUkNwOyIjcZQohre1CaytquC5mPplId8uA=
github.com/sagernet/smux v0.0.0-20231208180855-7041f6ea79e7 h1:DImB4lELfQhplLTxeq2z31Fpv8CQqqrUwTbrIRumZqQ=
Expand Down
1 change: 1 addition & 0 deletions option/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type Template struct {

// Inbound
Inbounds []option.Inbound `json:"inbounds,omitempty"`
AutoRedirect bool `json:"auto_redirect,omitempty"`
DisableTUN bool `json:"disable_tun,omitempty"`
DisableSystemProxy bool `json:"disable_system_proxy,omitempty"`
CustomTUN *TypedMessage[option.TunInboundOptions] `json:"custom_tun,omitempty"`
Expand Down
26 changes: 26 additions & 0 deletions template/filter/filter_1100.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package filter

import (
"github.com/sagernet/serenity/common/metadata"
"github.com/sagernet/serenity/common/semver"
C "github.com/sagernet/sing-box/constant"
"github.com/sagernet/sing-box/option"
)

func init() {
filters = append(filters, filter1100)
}

func filter1100(metadata metadata.Metadata, options *option.Options) {
if metadata.Version == nil || metadata.Version.GreaterThanOrEqual(semver.ParseVersion("1.10.0-alpha.2")) {
return
}
newInbounds := make([]option.Inbound, 0, len(options.Inbounds))
for _, inbound := range options.Inbounds {
if inbound.Type == C.TypeTun && inbound.TunOptions.AutoRedirect {
inbound.TunOptions.AutoRedirect = false
}
newInbounds = append(newInbounds, inbound)
}
options.Inbounds = newInbounds
}
7 changes: 7 additions & 0 deletions template/render_inbounds.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"net/netip"

M "github.com/sagernet/serenity/common/metadata"
"github.com/sagernet/serenity/common/semver"
C "github.com/sagernet/sing-box/constant"
"github.com/sagernet/sing-box/option"
"github.com/sagernet/sing-dns"
Expand All @@ -25,6 +26,9 @@ func (t *Template) renderInbounds(metadata M.Metadata, options *option.Options)
domainStrategy = option.DomainStrategy(dns.DomainStrategyPreferIPv4)
}
}
autoRedirect := t.AutoRedirect &&
!metadata.Platform.IsApple() &&
(metadata.Version == nil || metadata.Version.GreaterThanOrEqual(semver.ParseVersion("1.10.0-alpha.2")))
disableTun := t.DisableTUN && !metadata.Platform.TunOnly()
if !disableTun {
options.Route.AutoDetectInterface = true
Expand All @@ -44,6 +48,9 @@ func (t *Template) renderInbounds(metadata M.Metadata, options *option.Options)
},
},
}
if autoRedirect {
tunInbound.TunOptions.AutoRedirect = true
}
if t.EnableFakeIP {
tunInbound.TunOptions.InboundOptions.DomainStrategy = domainStrategy
}
Expand Down

0 comments on commit d83242c

Please sign in to comment.