Skip to content

Commit

Permalink
fix: panic due to goroutine setting returned error (daeuniverse#748)
Browse files Browse the repository at this point in the history
  • Loading branch information
mzz2017 authored Feb 19, 2025
1 parent c20e0e1 commit e7c2497
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 84 deletions.
38 changes: 0 additions & 38 deletions common/netutils/context_dialer.go

This file was deleted.

19 changes: 19 additions & 0 deletions common/netutils/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,25 @@ func ResolveNS(ctx context.Context, d netproxy.Dialer, dns netip.AddrPort, host
return records, nil
}

func ResolveSOA(ctx context.Context, d netproxy.Dialer, dns netip.AddrPort, host string, network string) (records []string, err error) {
typ := dnsmessage.TypeSOA
resources, err := resolve(ctx, d, dns, host, typ, network)
if err != nil {
return nil, err
}
for _, ans := range resources {
if ans.Header().Rrtype != typ {
continue
}
ns, ok := ans.(*dnsmessage.SOA)
if !ok {
return nil, ErrBadDnsAns
}
records = append(records, ns.Ns)
}
return records, nil
}

func resolve(ctx context.Context, d netproxy.Dialer, dns netip.AddrPort, host string, typ uint16, network string) (ans []dnsmessage.RR, err error) {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
Expand Down
45 changes: 15 additions & 30 deletions common/netutils/ip46.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ package netutils
import (
"context"
"errors"
"fmt"
"net/netip"
"sync"

Expand All @@ -22,24 +21,22 @@ type Ip46 struct {
Ip6 netip.Addr
}

func ResolveIp46(ctx context.Context, dialer netproxy.Dialer, dns netip.AddrPort, host string, network string, race bool) (ipv46 *Ip46, err error) {
func ResolveIp46(ctx context.Context, dialer netproxy.Dialer, dns netip.AddrPort, host string, network string, race bool) (ipv46 *Ip46, err4, err6 error) {
var log *logrus.Logger
if _log := ctx.Value("logger"); _log != nil {
log = _log.(*logrus.Logger)
defer func() {
if err == nil {
log.Tracef("ResolveIp46 %v using %v: A(%v) AAAA(%v)", host, systemDns, ipv46.Ip4, ipv46.Ip6)
} else {
log.Tracef("ResolveIp46 %v using %v: %v", host, systemDns, err)
}
log.WithField("err4", err4).
WithField("err6", err6).
Tracef("ResolveIp46 %v using %v: A(%v) AAAA(%v)", host, systemDns, ipv46.Ip4, ipv46.Ip6)
}()
}
var wg sync.WaitGroup
wg.Add(2)
var err4, err6 error
var addrs4, addrs6 []netip.Addr
ctx4, cancel4 := context.WithCancel(ctx)
ctx6, cancel6 := context.WithCancel(ctx)
var _err4, _err6 error
go func() {
defer func() {
wg.Done()
Expand All @@ -50,13 +47,10 @@ func ResolveIp46(ctx context.Context, dialer netproxy.Dialer, dns netip.AddrPort
}()
var e error
addrs4, e = ResolveNetip(ctx4, dialer, dns, host, dnsmessage.TypeA, network)
if err != nil && !errors.Is(e, context.Canceled) {
err4 = e
if e != nil && !errors.Is(e, context.Canceled) {
_err4 = e
return
}
if len(addrs4) == 0 {
addrs4 = []netip.Addr{{}}
}
}()
go func() {
defer func() {
Expand All @@ -68,27 +62,18 @@ func ResolveIp46(ctx context.Context, dialer netproxy.Dialer, dns netip.AddrPort
}()
var e error
addrs6, e = ResolveNetip(ctx6, dialer, dns, host, dnsmessage.TypeAAAA, network)
if err != nil && !errors.Is(e, context.Canceled) {
if e != nil && !errors.Is(e, context.Canceled) {
err6 = e
return
}
if len(addrs6) == 0 {
addrs6 = []netip.Addr{{}}
}
}()
wg.Wait()
if err4 != nil || err6 != nil {
if err4 != nil && err6 != nil {
return nil, fmt.Errorf("%w: %v", err4, err6)
}
if err4 != nil {
return nil, err4
} else {
return nil, err6
}
ipv46 = &Ip46{}
if len(addrs4) != 0 {
ipv46.Ip4 = addrs4[0]
}
if len(addrs6) != 0 {
ipv46.Ip6 = addrs6[0]
}
return &Ip46{
Ip4: addrs4[0],
Ip6: addrs6[0],
}, nil
return ipv46, _err4, _err6
}
7 changes: 4 additions & 3 deletions common/netutils/ip46_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ import (
func TestResolveIp46(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
ip46, err := ResolveIp46(ctx, direct.SymmetricDirect, netip.MustParseAddrPort("223.5.5.5:53"), "www.apple.com", "udp", false)
if err != nil {
t.Fatal(err)
ip46, err4, err6 := ResolveIp46(ctx, direct.SymmetricDirect, netip.MustParseAddrPort("223.5.5.5:53"), "ipv6.google.com", "udp", false)
if err4 != nil || err6 != nil {
t.Fatal(err4, err6)
}
if !ip46.Ip4.IsValid() && !ip46.Ip6.IsValid() {
t.Fatal("No record")
}
t.Log(ip46)
}
5 changes: 1 addition & 4 deletions component/dns/upstream.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,7 @@ func NewUpstream(ctx context.Context, upstream *url.URL, resolverNetwork string)
}
}()

ip46, err := netutils.ResolveIp46(ctx, direct.SymmetricDirect, systemDns, hostname, resolverNetwork, false)
if err != nil {
return nil, fmt.Errorf("failed to resolve dns_upstream: %w", err)
}
ip46, _, _ := netutils.ResolveIp46(ctx, direct.SymmetricDirect, systemDns, hostname, resolverNetwork, false)
if !ip46.Ip4.IsValid() && !ip46.Ip6.IsValid() {
return nil, fmt.Errorf("dns_upstream %v has no record", upstream.String())
}
Expand Down
12 changes: 6 additions & 6 deletions component/outbound/dialer/connectivity_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,9 @@ func ParseTcpCheckOption(ctx context.Context, rawURL []string, method string, re
if len(rawURL) > 1 {
ip46 = parseIp46FromList(rawURL[1:])
} else {
ip46, err = netutils.ResolveIp46(ctx, direct.SymmetricDirect, systemDns, u.Hostname(), resolverNetwork, false)
if err != nil {
return nil, err
ip46, _, _ = netutils.ResolveIp46(ctx, direct.SymmetricDirect, systemDns, u.Hostname(), resolverNetwork, false)
if !ip46.Ip4.IsValid() && !ip46.Ip6.IsValid() {
return nil, fmt.Errorf("ResolveIp46: no valid ip for %v", u.Hostname())
}
}
return &TcpCheckOption{
Expand Down Expand Up @@ -205,9 +205,9 @@ func ParseCheckDnsOption(ctx context.Context, dnsHostPort []string, resolverNetw
if len(dnsHostPort) > 1 {
ip46 = parseIp46FromList(dnsHostPort[1:])
} else {
ip46, err = netutils.ResolveIp46(ctx, direct.SymmetricDirect, systemDns, host, resolverNetwork, false)
if err != nil {
return nil, err
ip46, _, _ = netutils.ResolveIp46(ctx, direct.SymmetricDirect, systemDns, host, resolverNetwork, false)
if !ip46.Ip4.IsValid() && !ip46.Ip6.IsValid() {
return nil, fmt.Errorf("ResolveIp46: no valid ip for %v", host)
}
}
return &CheckDnsOption{
Expand Down
2 changes: 1 addition & 1 deletion component/sniffing/sniffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func (s *Sniffer) SniffTcp() (d string, err error) {
if s.stream {
go func() {
// Read once.
_, err = s.buf.ReadFromOnce(s.r)
_, err := s.buf.ReadFromOnce(s.r)
if err != nil {
s.dataError = err
}
Expand Down
2 changes: 1 addition & 1 deletion control/control_plane.go
Original file line number Diff line number Diff line change
Expand Up @@ -662,7 +662,7 @@ func (c *ControlPlane) ChooseDialTarget(outbound consts.OutboundIndex, dst netip
// TODO: use DNS controller and re-route by control plane.
systemDns, err := netutils.SystemDns()
if err == nil {
if ip46, err := netutils.ResolveIp46(ctx, direct.SymmetricDirect, systemDns, domain, common.MagicNetwork("udp", c.soMarkFromDae, c.mptcp), true); err == nil && (ip46.Ip4.IsValid() || ip46.Ip6.IsValid()) {
if ip46, _, _ := netutils.ResolveIp46(ctx, direct.SymmetricDirect, systemDns, domain, common.MagicNetwork("udp", c.soMarkFromDae, c.mptcp), true); ip46.Ip4.IsValid() || ip46.Ip6.IsValid() {
// Has A/AAAA records. It is a real domain.
dialMode = consts.DialMode_Domain
// Add it to real-domain set.
Expand Down
2 changes: 1 addition & 1 deletion control/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ func (d *DoUDP) ForwardDNS(ctx context.Context, data []byte) (*dnsmessage.Msg, e
go func() {
// Send DNS request every seconds.
for {
_, err = conn.Write(data)
_, _ = conn.Write(data)
// if err != nil {
// if c.log.IsLevelEnabled(logrus.DebugLevel) {
// c.log.WithFields(logrus.Fields{
Expand Down

0 comments on commit e7c2497

Please sign in to comment.