Add DNS mode
This commit is contained in:
parent
3c7d6ad82f
commit
47bdde06c3
4 changed files with 103 additions and 58 deletions
38
tun_linux.go
38
tun_linux.go
|
|
@ -354,7 +354,12 @@ func (t *NativeTun) start() error {
|
|||
return E.Cause(err, "set rules")
|
||||
}
|
||||
|
||||
t.setSearchDomainForSystemdResolved()
|
||||
if t.options.DNSMode != DNSModeDisabled {
|
||||
err = t.setSearchDomainForSystemdResolved()
|
||||
if err != nil {
|
||||
return E.Cause(err, "set search domain")
|
||||
}
|
||||
}
|
||||
|
||||
if t.options.AutoRoute && runtime.GOOS == "android" {
|
||||
t.interfaceCallback = t.options.InterfaceMonitor.RegisterCallback(t.routeUpdate)
|
||||
|
|
@ -369,7 +374,9 @@ func (t *NativeTun) Close() error {
|
|||
if t.options.EXP_ExternalConfiguration {
|
||||
return common.Close(common.PtrOrNil(t.tunFile))
|
||||
}
|
||||
t.unsetSearchDomainForSystemdResolved()
|
||||
if t.options.DNSMode != DNSModeDisabled {
|
||||
t.unsetSearchDomainForSystemdResolved()
|
||||
}
|
||||
t.unsetAddresses()
|
||||
return E.Errors(t.unsetRoute(), t.unsetRules(), common.Close(common.PtrOrNil(t.tunFile)))
|
||||
}
|
||||
|
|
@ -1190,37 +1197,24 @@ func (t *NativeTun) routeUpdate(_ *control.Interface, flags int) {
|
|||
}
|
||||
}
|
||||
|
||||
func (t *NativeTun) setSearchDomainForSystemdResolved() {
|
||||
if t.options.EXP_DisableDNSHijack {
|
||||
return
|
||||
}
|
||||
func (t *NativeTun) setSearchDomainForSystemdResolved() error {
|
||||
ctlPath, err := exec.LookPath("resolvectl")
|
||||
if err != nil {
|
||||
return
|
||||
return nil
|
||||
}
|
||||
dnsServer := t.options.DNSServers
|
||||
if len(dnsServer) == 0 {
|
||||
if len(t.options.Inet4Address) > 0 && HasNextAddress(t.options.Inet4Address[0], 1) {
|
||||
dnsServer = append(dnsServer, t.options.Inet4Address[0].Addr().Next())
|
||||
}
|
||||
if len(t.options.Inet6Address) > 0 && HasNextAddress(t.options.Inet6Address[0], 1) {
|
||||
dnsServer = append(dnsServer, t.options.Inet6Address[0].Addr().Next())
|
||||
}
|
||||
}
|
||||
if len(dnsServer) == 0 {
|
||||
return
|
||||
dnsAddress, err := t.options.DNSServerAddress()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
go func() {
|
||||
_ = shell.Exec(ctlPath, "domain", t.options.Name, "~.").Run()
|
||||
_ = shell.Exec(ctlPath, "default-route", t.options.Name, "true").Run()
|
||||
_ = shell.Exec(ctlPath, append([]string{"dns", t.options.Name}, common.Map(dnsServer, netip.Addr.String)...)...).Run()
|
||||
_ = shell.Exec(ctlPath, append([]string{"dns", t.options.Name}, common.Map(dnsAddress, netip.Addr.String)...)...).Run()
|
||||
}()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t *NativeTun) unsetSearchDomainForSystemdResolved() {
|
||||
if t.options.EXP_DisableDNSHijack {
|
||||
return
|
||||
}
|
||||
ctlPath, err := exec.LookPath("resolvectl")
|
||||
if err != nil {
|
||||
return
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue