Add DNS mode
This commit is contained in:
parent
3c7d6ad82f
commit
47bdde06c3
4 changed files with 103 additions and 58 deletions
|
|
@ -16,7 +16,6 @@ import (
|
|||
"github.com/sagernet/sing-tun/internal/winipcfg"
|
||||
"github.com/sagernet/sing-tun/internal/winsys"
|
||||
"github.com/sagernet/sing-tun/internal/wintun"
|
||||
"github.com/sagernet/sing/common"
|
||||
E "github.com/sagernet/sing/common/exceptions"
|
||||
"github.com/sagernet/sing/common/windnsapi"
|
||||
|
||||
|
|
@ -81,16 +80,14 @@ func (t *NativeTun) configure() error {
|
|||
if err != nil {
|
||||
return E.Cause(err, "set ipv4 address")
|
||||
}
|
||||
if t.options.AutoRoute && !t.options.EXP_DisableDNSHijack {
|
||||
dnsServers := common.Filter(t.options.DNSServers, netip.Addr.Is4)
|
||||
if len(dnsServers) == 0 && HasNextAddress(t.options.Inet4Address[0], 1) {
|
||||
dnsServers = []netip.Addr{t.options.Inet4Address[0].Addr().Next()}
|
||||
if t.options.AutoRoute && t.options.DNSModeOrDefault() != DNSModeDisabled {
|
||||
dnsServers, err := t.options.Inet4DNSAddress()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(dnsServers) > 0 {
|
||||
err = luid.SetDNS(winipcfg.AddressFamily(windows.AF_INET), dnsServers, nil)
|
||||
if err != nil {
|
||||
return E.Cause(err, "set ipv4 dns")
|
||||
}
|
||||
err = luid.SetDNS(winipcfg.AddressFamily(windows.AF_INET), dnsServers, nil)
|
||||
if err != nil {
|
||||
return E.Cause(err, "set ipv4 dns")
|
||||
}
|
||||
} else {
|
||||
err = luid.SetDNS(winipcfg.AddressFamily(windows.AF_INET), nil, nil)
|
||||
|
|
@ -104,16 +101,14 @@ func (t *NativeTun) configure() error {
|
|||
if err != nil {
|
||||
return E.Cause(err, "set ipv6 address")
|
||||
}
|
||||
if t.options.AutoRoute && !t.options.EXP_DisableDNSHijack {
|
||||
dnsServers := common.Filter(t.options.DNSServers, netip.Addr.Is6)
|
||||
if len(dnsServers) == 0 && HasNextAddress(t.options.Inet6Address[0], 1) {
|
||||
dnsServers = []netip.Addr{t.options.Inet6Address[0].Addr().Next()}
|
||||
if t.options.AutoRoute && t.options.DNSModeOrDefault() != DNSModeDisabled {
|
||||
dnsServers, err := t.options.Inet6DNSAddress()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(dnsServers) > 0 {
|
||||
err = luid.SetDNS(winipcfg.AddressFamily(windows.AF_INET6), dnsServers, nil)
|
||||
if err != nil {
|
||||
return E.Cause(err, "set ipv6 dns")
|
||||
}
|
||||
err = luid.SetDNS(winipcfg.AddressFamily(windows.AF_INET6), dnsServers, nil)
|
||||
if err != nil {
|
||||
return E.Cause(err, "set ipv6 dns")
|
||||
}
|
||||
} else {
|
||||
err = luid.SetDNS(winipcfg.AddressFamily(windows.AF_INET6), nil, nil)
|
||||
|
|
@ -334,7 +329,7 @@ func (t *NativeTun) Start() error {
|
|||
}
|
||||
}
|
||||
|
||||
if !t.options.EXP_DisableDNSHijack {
|
||||
if t.options.DNSModeOrDefault() == DNSModeHijack {
|
||||
blockDNSCondition := make([]winsys.FWPM_FILTER_CONDITION0, 1)
|
||||
blockDNSCondition[0].FieldKey = winsys.FWPM_CONDITION_IP_REMOTE_PORT
|
||||
blockDNSCondition[0].MatchType = winsys.FWP_MATCH_EQUAL
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue