2023-02-08 19:15:24 +07:00
|
|
|
/*
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
2024-01-04 16:28:16 +07:00
|
|
|
* Copyright (c) 2022-2024, daeuniverse Organization <dae@v2raya.org>
|
2023-02-08 19:15:24 +07:00
|
|
|
*/
|
|
|
|
|
|
|
|
package netutils
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-02-12 14:39:00 +07:00
|
|
|
"encoding/binary"
|
2023-02-08 19:15:24 +07:00
|
|
|
"fmt"
|
2023-02-12 14:39:00 +07:00
|
|
|
"io"
|
2023-02-09 12:56:21 +07:00
|
|
|
"math"
|
2023-02-08 19:15:24 +07:00
|
|
|
"net/netip"
|
2023-02-09 10:40:34 +07:00
|
|
|
"sync"
|
|
|
|
"time"
|
2023-04-23 12:27:29 +07:00
|
|
|
|
2023-07-11 00:25:05 +07:00
|
|
|
"github.com/daeuniverse/dae/common/consts"
|
2024-04-24 01:22:50 +07:00
|
|
|
"github.com/daeuniverse/outbound/netproxy"
|
|
|
|
"github.com/daeuniverse/outbound/pkg/fastrand"
|
|
|
|
"github.com/daeuniverse/outbound/pool"
|
2023-07-09 15:02:17 +07:00
|
|
|
dnsmessage "github.com/miekg/dns"
|
2023-02-08 19:15:24 +07:00
|
|
|
)
|
|
|
|
|
2023-02-09 10:40:34 +07:00
|
|
|
var (
|
2023-02-09 18:22:37 +07:00
|
|
|
systemDnsMu sync.Mutex
|
|
|
|
systemDns netip.AddrPort
|
|
|
|
systemDnsNextUpdateAfter time.Time
|
2023-03-13 14:52:44 +07:00
|
|
|
|
2023-11-15 13:32:57 +07:00
|
|
|
ErrBadDnsAns = fmt.Errorf("bad dns answer")
|
2023-04-07 19:38:06 +07:00
|
|
|
|
|
|
|
BootstrapDns = netip.MustParseAddrPort("208.67.222.222:5353")
|
2023-02-09 10:40:34 +07:00
|
|
|
)
|
|
|
|
|
2023-02-09 18:22:37 +07:00
|
|
|
func TryUpdateSystemDns() (err error) {
|
|
|
|
systemDnsMu.Lock()
|
|
|
|
err = tryUpdateSystemDns()
|
|
|
|
systemDnsMu.Unlock()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-04-02 16:43:50 +07:00
|
|
|
// TryUpdateSystemDnsElapse will update system DNS if duration has elapsed since the last TryUpdateSystemDns1s call.
|
|
|
|
func TryUpdateSystemDnsElapse(k time.Duration) (err error) {
|
2023-02-09 18:22:37 +07:00
|
|
|
systemDnsMu.Lock()
|
|
|
|
defer systemDnsMu.Unlock()
|
2023-04-02 16:43:50 +07:00
|
|
|
return tryUpdateSystemDnsElapse(k)
|
|
|
|
}
|
|
|
|
func tryUpdateSystemDnsElapse(k time.Duration) (err error) {
|
2023-02-09 18:22:37 +07:00
|
|
|
if time.Now().Before(systemDnsNextUpdateAfter) {
|
|
|
|
return fmt.Errorf("update too quickly")
|
|
|
|
}
|
|
|
|
err = tryUpdateSystemDns()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-04-02 16:43:50 +07:00
|
|
|
systemDnsNextUpdateAfter = time.Now().Add(k)
|
2023-02-09 18:22:37 +07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func tryUpdateSystemDns() (err error) {
|
|
|
|
dnsConf := dnsReadConfig("/etc/resolv.conf")
|
2023-04-07 19:38:06 +07:00
|
|
|
systemDns = netip.AddrPort{}
|
|
|
|
for _, s := range dnsConf.servers {
|
|
|
|
ipPort := netip.MustParseAddrPort(s)
|
|
|
|
if !ipPort.Addr().IsLoopback() {
|
|
|
|
systemDns = ipPort
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !systemDns.IsValid() {
|
|
|
|
systemDns = BootstrapDns
|
|
|
|
}
|
2023-02-09 18:22:37 +07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-02-09 10:40:34 +07:00
|
|
|
func SystemDns() (dns netip.AddrPort, err error) {
|
|
|
|
systemDnsMu.Lock()
|
|
|
|
defer systemDnsMu.Unlock()
|
|
|
|
if !systemDns.IsValid() {
|
2023-02-09 18:22:37 +07:00
|
|
|
if err = tryUpdateSystemDns(); err != nil {
|
2023-02-09 10:40:34 +07:00
|
|
|
return netip.AddrPort{}, err
|
|
|
|
}
|
|
|
|
}
|
2023-04-02 16:43:50 +07:00
|
|
|
// To avoid environment changing.
|
|
|
|
_ = tryUpdateSystemDnsElapse(5 * time.Second)
|
2023-02-09 10:40:34 +07:00
|
|
|
return systemDns, nil
|
|
|
|
}
|
|
|
|
|
2023-07-09 15:02:17 +07:00
|
|
|
func ResolveNetip(ctx context.Context, d netproxy.Dialer, dns netip.AddrPort, host string, typ uint16, network string) (addrs []netip.Addr, err error) {
|
2023-06-04 10:38:05 +07:00
|
|
|
resources, err := resolve(ctx, d, dns, host, typ, network)
|
2023-03-13 14:52:44 +07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for _, ans := range resources {
|
2023-07-09 15:02:17 +07:00
|
|
|
if ans.Header().Rrtype != typ {
|
2023-03-13 14:52:44 +07:00
|
|
|
continue
|
|
|
|
}
|
2023-07-09 15:02:17 +07:00
|
|
|
var (
|
|
|
|
ip netip.Addr
|
|
|
|
okk bool
|
|
|
|
)
|
2023-03-13 14:52:44 +07:00
|
|
|
switch typ {
|
|
|
|
case dnsmessage.TypeA:
|
2023-07-09 15:02:17 +07:00
|
|
|
a, ok := ans.(*dnsmessage.A)
|
2023-03-13 14:52:44 +07:00
|
|
|
if !ok {
|
2023-11-15 13:32:57 +07:00
|
|
|
return nil, ErrBadDnsAns
|
2023-03-13 14:52:44 +07:00
|
|
|
}
|
2023-07-09 15:02:17 +07:00
|
|
|
ip, okk = netip.AddrFromSlice(a.A)
|
2023-03-13 14:52:44 +07:00
|
|
|
case dnsmessage.TypeAAAA:
|
2023-07-09 15:02:17 +07:00
|
|
|
a, ok := ans.(*dnsmessage.AAAA)
|
2023-03-13 14:52:44 +07:00
|
|
|
if !ok {
|
2023-11-15 13:32:57 +07:00
|
|
|
return nil, ErrBadDnsAns
|
2023-03-13 14:52:44 +07:00
|
|
|
}
|
2023-07-09 15:02:17 +07:00
|
|
|
ip, okk = netip.AddrFromSlice(a.AAAA)
|
2023-03-13 14:52:44 +07:00
|
|
|
}
|
2023-07-09 15:02:17 +07:00
|
|
|
if !okk {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
addrs = append(addrs, ip)
|
2023-03-13 14:52:44 +07:00
|
|
|
}
|
|
|
|
return addrs, nil
|
|
|
|
}
|
|
|
|
|
2023-06-04 10:38:05 +07:00
|
|
|
func ResolveNS(ctx context.Context, d netproxy.Dialer, dns netip.AddrPort, host string, network string) (records []string, err error) {
|
2023-03-13 14:52:44 +07:00
|
|
|
typ := dnsmessage.TypeNS
|
2023-06-04 10:38:05 +07:00
|
|
|
resources, err := resolve(ctx, d, dns, host, typ, network)
|
2023-03-13 14:52:44 +07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for _, ans := range resources {
|
2023-07-09 15:02:17 +07:00
|
|
|
if ans.Header().Rrtype != typ {
|
2023-03-13 14:52:44 +07:00
|
|
|
continue
|
|
|
|
}
|
2023-07-09 15:02:17 +07:00
|
|
|
ns, ok := ans.(*dnsmessage.NS)
|
2023-03-13 14:52:44 +07:00
|
|
|
if !ok {
|
2023-11-15 13:32:57 +07:00
|
|
|
return nil, ErrBadDnsAns
|
2023-02-08 19:15:24 +07:00
|
|
|
}
|
2023-07-09 15:02:17 +07:00
|
|
|
records = append(records, ns.Ns)
|
2023-02-08 19:15:24 +07:00
|
|
|
}
|
2023-03-13 14:52:44 +07:00
|
|
|
return records, nil
|
|
|
|
}
|
|
|
|
|
2023-07-09 15:02:17 +07:00
|
|
|
func resolve(ctx context.Context, d netproxy.Dialer, dns netip.AddrPort, host string, typ uint16, network string) (ans []dnsmessage.RR, err error) {
|
2023-03-13 14:52:44 +07:00
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
|
|
|
defer cancel()
|
2023-07-09 15:02:17 +07:00
|
|
|
fqdn := dnsmessage.CanonicalName(host)
|
2023-02-08 19:15:24 +07:00
|
|
|
switch typ {
|
|
|
|
case dnsmessage.TypeA, dnsmessage.TypeAAAA:
|
2023-03-13 14:52:44 +07:00
|
|
|
if addr, err := netip.ParseAddr(host); err == nil {
|
|
|
|
if (addr.Is4() || addr.Is4In6()) && typ == dnsmessage.TypeA {
|
2023-07-09 15:02:17 +07:00
|
|
|
return []dnsmessage.RR{
|
|
|
|
&dnsmessage.A{
|
|
|
|
Hdr: dnsmessage.RR_Header{
|
|
|
|
Name: dnsmessage.CanonicalName(fqdn),
|
|
|
|
Class: dnsmessage.ClassINET,
|
|
|
|
Ttl: 0,
|
|
|
|
Rrtype: typ,
|
2023-03-13 14:52:44 +07:00
|
|
|
},
|
2023-07-09 15:02:17 +07:00
|
|
|
A: addr.AsSlice(),
|
2023-03-13 14:52:44 +07:00
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
} else if addr.Is6() && typ == dnsmessage.TypeAAAA {
|
2023-07-09 15:02:17 +07:00
|
|
|
return []dnsmessage.RR{
|
|
|
|
&dnsmessage.AAAA{
|
|
|
|
Hdr: dnsmessage.RR_Header{
|
|
|
|
Name: dnsmessage.CanonicalName(fqdn),
|
|
|
|
Class: dnsmessage.ClassINET,
|
|
|
|
Ttl: 0,
|
|
|
|
Rrtype: typ,
|
2023-03-13 14:52:44 +07:00
|
|
|
},
|
2023-07-09 15:02:17 +07:00
|
|
|
AAAA: addr.AsSlice(),
|
2023-03-13 14:52:44 +07:00
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
2023-03-13 14:55:12 +07:00
|
|
|
// MUST No record.
|
|
|
|
return nil, nil
|
2023-03-13 14:52:44 +07:00
|
|
|
}
|
2023-02-08 19:15:24 +07:00
|
|
|
default:
|
|
|
|
}
|
|
|
|
// Build DNS req.
|
2023-07-09 15:02:17 +07:00
|
|
|
builder := dnsmessage.Msg{
|
|
|
|
MsgHdr: dnsmessage.MsgHdr{
|
|
|
|
Id: uint16(fastrand.Intn(math.MaxUint16 + 1)),
|
|
|
|
Response: false,
|
|
|
|
Opcode: 0,
|
|
|
|
Truncated: false,
|
|
|
|
RecursionDesired: true,
|
|
|
|
Authoritative: false,
|
|
|
|
},
|
2023-02-08 19:15:24 +07:00
|
|
|
}
|
2023-07-09 15:02:17 +07:00
|
|
|
builder.SetQuestion(fqdn, typ)
|
|
|
|
b, err := builder.Pack()
|
2023-02-08 19:15:24 +07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-06-04 10:38:05 +07:00
|
|
|
magicNetwork, err := netproxy.ParseMagicNetwork(network)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if magicNetwork.Network == "tcp" {
|
2023-02-20 17:06:54 +07:00
|
|
|
// Put DNS request length
|
2023-02-12 14:39:00 +07:00
|
|
|
buf := pool.Get(2 + len(b))
|
|
|
|
defer pool.Put(buf)
|
|
|
|
binary.BigEndian.PutUint16(buf, uint16(len(b)))
|
|
|
|
copy(buf[2:], b)
|
|
|
|
b = buf
|
|
|
|
}
|
2023-02-08 19:15:24 +07:00
|
|
|
|
|
|
|
// Dial and write.
|
2024-09-26 21:40:29 +07:00
|
|
|
c, err := d.DialContext(ctx, network, dns.String())
|
2023-02-08 19:15:24 +07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer c.Close()
|
|
|
|
_, err = c.Write(b)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-02-09 10:40:34 +07:00
|
|
|
ch := make(chan error, 2)
|
2023-06-04 10:38:05 +07:00
|
|
|
if magicNetwork.Network == "udp" {
|
2023-02-12 14:39:00 +07:00
|
|
|
go func() {
|
|
|
|
// Resend every 3 seconds for UDP.
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
time.Sleep(3 * time.Second)
|
|
|
|
}
|
|
|
|
_, err := c.Write(b)
|
|
|
|
if err != nil {
|
|
|
|
ch <- err
|
|
|
|
return
|
|
|
|
}
|
2023-02-09 10:40:34 +07:00
|
|
|
}
|
2023-02-12 14:39:00 +07:00
|
|
|
}()
|
|
|
|
}
|
|
|
|
go func() {
|
2023-07-11 00:25:05 +07:00
|
|
|
buf := pool.GetFullCap(consts.EthernetMtu)
|
|
|
|
defer buf.Put()
|
2023-06-04 10:38:05 +07:00
|
|
|
if magicNetwork.Network == "tcp" {
|
2023-02-20 17:06:54 +07:00
|
|
|
// Read DNS response length
|
2023-02-12 14:39:00 +07:00
|
|
|
_, err := io.ReadFull(c, buf[:2])
|
2023-02-09 10:40:34 +07:00
|
|
|
if err != nil {
|
|
|
|
ch <- err
|
|
|
|
return
|
|
|
|
}
|
2023-02-12 14:39:00 +07:00
|
|
|
n := binary.BigEndian.Uint16(buf)
|
2023-07-11 00:25:05 +07:00
|
|
|
if int(n) > cap(buf) {
|
2023-02-12 14:39:00 +07:00
|
|
|
ch <- fmt.Errorf("too big dns resp")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
buf = buf[:n]
|
2023-02-09 10:40:34 +07:00
|
|
|
}
|
2023-02-08 19:15:24 +07:00
|
|
|
n, err := c.Read(buf)
|
|
|
|
if err != nil {
|
|
|
|
ch <- err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Resolve DNS response and extract A/AAAA record.
|
2023-07-09 15:02:17 +07:00
|
|
|
var msg dnsmessage.Msg
|
2023-02-08 19:15:24 +07:00
|
|
|
if err = msg.Unpack(buf[:n]); err != nil {
|
|
|
|
ch <- err
|
|
|
|
return
|
|
|
|
}
|
2023-07-09 15:02:17 +07:00
|
|
|
ans = msg.Answer
|
2023-02-08 19:15:24 +07:00
|
|
|
ch <- nil
|
|
|
|
}()
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return nil, fmt.Errorf("timeout")
|
|
|
|
case err = <-ch:
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-03-13 14:52:44 +07:00
|
|
|
return ans, nil
|
2023-02-08 19:15:24 +07:00
|
|
|
}
|
|
|
|
}
|