github.com/kelleygo/clashcore@v1.0.2/dns/rcode.go (about)

     1  package dns
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	D "github.com/miekg/dns"
     8  )
     9  
    10  func newRCodeClient(addr string) rcodeClient {
    11  	var rcode int
    12  	switch addr {
    13  	case "success":
    14  		rcode = D.RcodeSuccess
    15  	case "format_error":
    16  		rcode = D.RcodeFormatError
    17  	case "server_failure":
    18  		rcode = D.RcodeServerFailure
    19  	case "name_error":
    20  		rcode = D.RcodeNameError
    21  	case "not_implemented":
    22  		rcode = D.RcodeNotImplemented
    23  	case "refused":
    24  		rcode = D.RcodeRefused
    25  	default:
    26  		panic(fmt.Errorf("unsupported RCode type: %s", addr))
    27  	}
    28  
    29  	return rcodeClient{
    30  		rcode: rcode,
    31  		addr:  "rcode://" + addr,
    32  	}
    33  }
    34  
    35  type rcodeClient struct {
    36  	rcode int
    37  	addr  string
    38  }
    39  
    40  var _ dnsClient = rcodeClient{}
    41  
    42  func (r rcodeClient) ExchangeContext(ctx context.Context, m *D.Msg) (*D.Msg, error) {
    43  	m.Response = true
    44  	m.Rcode = r.rcode
    45  	return m, nil
    46  }
    47  
    48  func (r rcodeClient) Address() string {
    49  	return r.addr
    50  }