github.com/supabase/cli@v1.168.1/internal/utils/cloudflare/dns.go (about)

     1  package cloudflare
     2  
     3  import (
     4  	"context"
     5  	"net/http"
     6  
     7  	"github.com/go-errors/errors"
     8  	"github.com/google/go-querystring/query"
     9  	"github.com/supabase/cli/pkg/fetcher"
    10  )
    11  
    12  type DNSType uint16
    13  
    14  // Spec: https://www.iana.org/assignments/dns-parameters/dns-parameters.xhtml#dns-parameters-4
    15  // Impl: https://github.com/miekg/dns/blob/master/types.go#L23
    16  const (
    17  	TypeNone       DNSType = 0
    18  	TypeA          DNSType = 1 // IPv4
    19  	TypeNS         DNSType = 2
    20  	TypeMD         DNSType = 3
    21  	TypeMF         DNSType = 4
    22  	TypeCNAME      DNSType = 5
    23  	TypeSOA        DNSType = 6
    24  	TypeMB         DNSType = 7
    25  	TypeMG         DNSType = 8
    26  	TypeMR         DNSType = 9
    27  	TypeNULL       DNSType = 10
    28  	TypePTR        DNSType = 12
    29  	TypeHINFO      DNSType = 13
    30  	TypeMINFO      DNSType = 14
    31  	TypeMX         DNSType = 15
    32  	TypeTXT        DNSType = 16
    33  	TypeRP         DNSType = 17
    34  	TypeAFSDB      DNSType = 18
    35  	TypeX25        DNSType = 19
    36  	TypeISDN       DNSType = 20
    37  	TypeRT         DNSType = 21
    38  	TypeNSAPPTR    DNSType = 23
    39  	TypeSIG        DNSType = 24
    40  	TypeKEY        DNSType = 25
    41  	TypePX         DNSType = 26
    42  	TypeGPOS       DNSType = 27
    43  	TypeAAAA       DNSType = 28 // IPv6
    44  	TypeLOC        DNSType = 29
    45  	TypeNXT        DNSType = 30
    46  	TypeEID        DNSType = 31
    47  	TypeNIMLOC     DNSType = 32
    48  	TypeSRV        DNSType = 33
    49  	TypeATMA       DNSType = 34
    50  	TypeNAPTR      DNSType = 35
    51  	TypeKX         DNSType = 36
    52  	TypeCERT       DNSType = 37
    53  	TypeDNAME      DNSType = 39
    54  	TypeOPT        DNSType = 41 // EDNS
    55  	TypeAPL        DNSType = 42
    56  	TypeDS         DNSType = 43
    57  	TypeSSHFP      DNSType = 44
    58  	TypeIPSECKEY   DNSType = 45
    59  	TypeRRSIG      DNSType = 46
    60  	TypeNSEC       DNSType = 47
    61  	TypeDNSKEY     DNSType = 48
    62  	TypeDHCID      DNSType = 49
    63  	TypeNSEC3      DNSType = 50
    64  	TypeNSEC3PARAM DNSType = 51
    65  	TypeTLSA       DNSType = 52
    66  	TypeSMIMEA     DNSType = 53
    67  	TypeHIP        DNSType = 55
    68  	TypeNINFO      DNSType = 56
    69  	TypeRKEY       DNSType = 57
    70  	TypeTALINK     DNSType = 58
    71  	TypeCDS        DNSType = 59
    72  	TypeCDNSKEY    DNSType = 60
    73  	TypeOPENPGPKEY DNSType = 61
    74  	TypeCSYNC      DNSType = 62
    75  	TypeZONEMD     DNSType = 63
    76  	TypeSVCB       DNSType = 64
    77  	TypeHTTPS      DNSType = 65
    78  	TypeSPF        DNSType = 99
    79  	TypeUINFO      DNSType = 100
    80  	TypeUID        DNSType = 101
    81  	TypeGID        DNSType = 102
    82  	TypeUNSPEC     DNSType = 103
    83  	TypeNID        DNSType = 104
    84  	TypeL32        DNSType = 105
    85  	TypeL64        DNSType = 106
    86  	TypeLP         DNSType = 107
    87  	TypeEUI48      DNSType = 108
    88  	TypeEUI64      DNSType = 109
    89  	TypeURI        DNSType = 256
    90  	TypeCAA        DNSType = 257
    91  	TypeAVC        DNSType = 258
    92  	TypeAMTRELAY   DNSType = 260
    93  
    94  	TypeTKEY DNSType = 249
    95  	TypeTSIG DNSType = 250
    96  
    97  	// valid Question.Qtype only
    98  	TypeIXFR  DNSType = 251
    99  	TypeAXFR  DNSType = 252
   100  	TypeMAILB DNSType = 253
   101  	TypeMAILA DNSType = 254
   102  	TypeANY   DNSType = 255
   103  
   104  	TypeTA       DNSType = 32768
   105  	TypeDLV      DNSType = 32769
   106  	TypeReserved DNSType = 65535
   107  )
   108  
   109  type DNSQuestion struct {
   110  	Name string  `json:"name"`
   111  	Type DNSType `json:"type"`
   112  }
   113  
   114  type DNSAnswer struct {
   115  	Name string  `json:"name"`
   116  	Type DNSType `json:"type"`
   117  	Ttl  uint32  `json:"TTL"`
   118  	Data string  `json:"data"`
   119  }
   120  
   121  // Ref: https://www.iana.org/assignments/dns-parameters/dns-parameters.xhtml#dns-parameters-6
   122  type DNSResponse struct {
   123  	Status     uint16
   124  	TC         bool
   125  	RD         bool
   126  	RA         bool
   127  	AD         bool
   128  	CD         bool
   129  	Question   []DNSQuestion `json:",omitempty"`
   130  	Answer     []DNSAnswer   `json:",omitempty"`
   131  	Authority  []DNSAnswer   `json:",omitempty"`
   132  	Additional []DNSAnswer   `json:",omitempty"`
   133  }
   134  
   135  type DNSParams struct {
   136  	Name string   `url:"name"`
   137  	Type *DNSType `url:"type,omitempty"`
   138  	Do   *bool    `url:"do,omitempty"`
   139  	Cd   *bool    `url:"cd,omitempty"`
   140  }
   141  
   142  // Performs DNS lookup via HTTPS, in case firewall blocks native netgo resolver.
   143  // Ref: https://developers.cloudflare.com/1.1.1.1/encryption/dns-over-https/make-api-requests/dns-json
   144  func (c *CloudflareAPI) DNSQuery(ctx context.Context, params DNSParams) (*DNSResponse, error) {
   145  	values, err := query.Values(params)
   146  	if err != nil {
   147  		return nil, errors.Errorf("failed to encode query params: %w", err)
   148  	}
   149  	resp, err := c.Send(ctx, http.MethodGet, "/dns-query?"+values.Encode(), nil)
   150  	if err != nil {
   151  		return nil, err
   152  	}
   153  	defer resp.Body.Close()
   154  	return fetcher.ParseJSON[DNSResponse](resp.Body)
   155  }