Revert "Stop using DHCP on iOS and tvOS"

This commit is contained in:
世界
2025-10-13 13:31:44 +08:00
parent 5bc0dfa9dd
commit a930356b04
4 changed files with 36 additions and 33 deletions

View File

@@ -46,7 +46,7 @@ var (
sharedFlags []string sharedFlags []string
debugFlags []string debugFlags []string
sharedTags []string sharedTags []string
macOSTags []string darwinTags []string
memcTags []string memcTags []string
notMemcTags []string notMemcTags []string
debugTags []string debugTags []string
@@ -63,7 +63,7 @@ func init() {
debugFlags = append(debugFlags, "-ldflags", "-X github.com/sagernet/sing-box/constant.Version="+currentTag+" -checklinkname=0") debugFlags = append(debugFlags, "-ldflags", "-X github.com/sagernet/sing-box/constant.Version="+currentTag+" -checklinkname=0")
sharedTags = append(sharedTags, "with_gvisor", "with_quic", "with_wireguard", "with_utls", "with_clash_api", "with_conntrack", "badlinkname", "tfogo_checklinkname0") sharedTags = append(sharedTags, "with_gvisor", "with_quic", "with_wireguard", "with_utls", "with_clash_api", "with_conntrack", "badlinkname", "tfogo_checklinkname0")
macOSTags = append(macOSTags, "with_dhcp") darwinTags = append(darwinTags, "with_dhcp")
memcTags = append(memcTags, "with_tailscale") memcTags = append(memcTags, "with_tailscale")
notMemcTags = append(notMemcTags, "with_low_memory") notMemcTags = append(notMemcTags, "with_low_memory")
debugTags = append(debugTags, "debug") debugTags = append(debugTags, "debug")
@@ -158,9 +158,7 @@ func buildApple() {
"-tags-not-macos=with_low_memory", "-tags-not-macos=with_low_memory",
} }
if !withTailscale { if !withTailscale {
args = append(args, "-tags-macos="+strings.Join(append(macOSTags, memcTags...), ",")) args = append(args, "-tags-macos="+strings.Join(memcTags, ","))
} else {
args = append(args, "-tags-macos="+strings.Join(macOSTags, ","))
} }
if !debugEnabled { if !debugEnabled {
@@ -169,7 +167,7 @@ func buildApple() {
args = append(args, debugFlags...) args = append(args, debugFlags...)
} }
tags := sharedTags tags := append(sharedTags, darwinTags...)
if withTailscale { if withTailscale {
tags = append(tags, memcTags...) tags = append(tags, memcTags...)
} }

View File

@@ -4,5 +4,5 @@ import "time"
const ( const (
DHCPTTL = time.Hour DHCPTTL = time.Hour
DHCPTimeout = time.Minute DHCPTimeout = 5 * time.Second
) )

View File

@@ -49,6 +49,7 @@ type Transport struct {
interfaceCallback *list.Element[tun.DefaultInterfaceUpdateCallback] interfaceCallback *list.Element[tun.DefaultInterfaceUpdateCallback]
transportLock sync.RWMutex transportLock sync.RWMutex
updatedAt time.Time updatedAt time.Time
lastError error
servers []M.Socksaddr servers []M.Socksaddr
search []string search []string
ndots int ndots int
@@ -92,7 +93,7 @@ func (t *Transport) Start(stage adapter.StartStage) error {
t.interfaceCallback = t.networkManager.InterfaceMonitor().RegisterCallback(t.interfaceUpdated) t.interfaceCallback = t.networkManager.InterfaceMonitor().RegisterCallback(t.interfaceUpdated)
} }
go func() { go func() {
_, err := t.Fetch() _, err := t.fetch()
if err != nil { if err != nil {
t.logger.Error(E.Cause(err, "fetch DNS servers")) t.logger.Error(E.Cause(err, "fetch DNS servers"))
} }
@@ -108,7 +109,7 @@ func (t *Transport) Close() error {
} }
func (t *Transport) Exchange(ctx context.Context, message *mDNS.Msg) (*mDNS.Msg, error) { func (t *Transport) Exchange(ctx context.Context, message *mDNS.Msg) (*mDNS.Msg, error) {
servers, err := t.Fetch() servers, err := t.fetch()
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -128,11 +129,20 @@ func (t *Transport) Exchange0(ctx context.Context, message *mDNS.Msg, servers []
} }
} }
func (t *Transport) Fetch() ([]M.Socksaddr, error) { func (t *Transport) Fetch() []M.Socksaddr {
servers, _ := t.fetch()
return servers
}
func (t *Transport) fetch() ([]M.Socksaddr, error) {
t.transportLock.RLock() t.transportLock.RLock()
updatedAt := t.updatedAt updatedAt := t.updatedAt
lastError := t.lastError
servers := t.servers servers := t.servers
t.transportLock.RUnlock() t.transportLock.RUnlock()
if lastError != nil {
return nil, lastError
}
if time.Since(updatedAt) < C.DHCPTTL { if time.Since(updatedAt) < C.DHCPTTL {
return servers, nil return servers, nil
} }
@@ -143,7 +153,7 @@ func (t *Transport) Fetch() ([]M.Socksaddr, error) {
} }
err := t.updateServers() err := t.updateServers()
if err != nil { if err != nil {
return nil, err return servers, err
} }
return t.servers, nil return t.servers, nil
} }
@@ -173,12 +183,15 @@ func (t *Transport) updateServers() error {
fetchCtx, cancel := context.WithTimeout(t.ctx, C.DHCPTimeout) fetchCtx, cancel := context.WithTimeout(t.ctx, C.DHCPTimeout)
err = t.fetchServers0(fetchCtx, iface) err = t.fetchServers0(fetchCtx, iface)
cancel() cancel()
t.updatedAt = time.Now()
if err != nil { if err != nil {
t.lastError = err
return err return err
} else if len(t.servers) == 0 { } else if len(t.servers) == 0 {
return E.New("dhcp: empty DNS servers response") t.lastError = E.New("dhcp: empty DNS servers response")
return t.lastError
} else { } else {
t.updatedAt = time.Now() t.lastError = nil
return nil return nil
} }
} }

View File

@@ -43,7 +43,7 @@ type Transport struct {
type dhcpTransport interface { type dhcpTransport interface {
adapter.DNSTransport adapter.DNSTransport
Fetch() ([]M.Socksaddr, error) Fetch() []M.Socksaddr
Exchange0(ctx context.Context, message *mDNS.Msg, servers []M.Socksaddr) (*mDNS.Msg, error) Exchange0(ctx context.Context, message *mDNS.Msg, servers []M.Socksaddr) (*mDNS.Msg, error)
} }
@@ -74,14 +74,12 @@ func (t *Transport) Start(stage adapter.StartStage) error {
break break
} }
} }
if !C.IsIos { if t.fallback {
if t.fallback { t.dhcpTransport = newDHCPTransport(t.TransportAdapter, log.ContextWithOverrideLevel(t.ctx, log.LevelDebug), t.dialer, t.logger)
t.dhcpTransport = newDHCPTransport(t.TransportAdapter, log.ContextWithOverrideLevel(t.ctx, log.LevelDebug), t.dialer, t.logger) if t.dhcpTransport != nil {
if t.dhcpTransport != nil { err := t.dhcpTransport.Start(stage)
err := t.dhcpTransport.Start(stage) if err != nil {
if err != nil { return err
return err
}
} }
} }
} }
@@ -105,12 +103,10 @@ func (t *Transport) Exchange(ctx context.Context, message *mDNS.Msg) (*mDNS.Msg,
if !t.fallback { if !t.fallback {
return t.exchange(ctx, message, question.Name) return t.exchange(ctx, message, question.Name)
} }
if !C.IsIos { if t.dhcpTransport != nil {
if t.dhcpTransport != nil { dhcpTransports := t.dhcpTransport.Fetch()
dhcpTransports, _ := t.dhcpTransport.Fetch() if len(dhcpTransports) > 0 {
if len(dhcpTransports) > 0 { return t.dhcpTransport.Exchange0(ctx, message, dhcpTransports)
return t.dhcpTransport.Exchange0(ctx, message, dhcpTransports)
}
} }
} }
if t.preferGo { if t.preferGo {
@@ -134,9 +130,5 @@ func (t *Transport) Exchange(ctx context.Context, message *mDNS.Msg) (*mDNS.Msg,
} }
return dns.FixedResponse(message.Id, question, addresses, C.DefaultDNSTTL), nil return dns.FixedResponse(message.Id, question, addresses, C.DefaultDNSTTL), nil
} }
if C.IsIos { return nil, E.New("only A and AAAA queries are supported on Apple platforms when using TUN and DHCP unavailable.")
return nil, E.New("only A and AAAA queries are supported on iOS and tvOS when using NetworkExtension.")
} else {
return nil, E.New("only A and AAAA queries are supported on macOS when using NetworkExtension and DHCP unavailable.")
}
} }