the project structure modified and new features added

This commit is contained in:
MDK
2023-09-26 14:12:04 +08:00
parent 4fcf28804c
commit 4c63d78e4e
14 changed files with 174 additions and 47 deletions

View File

@@ -27,6 +27,15 @@ type DomainInfo struct {
NSList map[string][]string
}
type SVCBRecord struct {
Target string `json:"target"`
Data map[string]string `json:"value"`
}
type SVCBResponse struct {
Records []SVCBRecord `json:"records"`
}
func (e *WrongAnswerError) Error() string {
return fmt.Sprintf("Wrong Answer: %s", e.Message)
}
@@ -39,11 +48,7 @@ func QuestionMaker(domain string, qclass uint16, qtype uint16) *dns.Question {
// build a dns query message
func QueryMaker(query QueryStruct) *dns.Msg {
msg := new(dns.Msg)
if query.Id < 0 {
msg.Id = dns.Id()
} else {
msg.Id = query.Id
}
msg.Id = query.Id
msg.RecursionDesired = query.RD
var query_name string
@@ -155,3 +160,58 @@ func SendVersionQuery(ip string) (*dns.Msg, error) {
return res, err
}
func SendSVCBQuery(ip string, domain string) (*dns.Msg, error) {
addr := ip + ":53"
query := QueryStruct{Id: dns.Id(), Qname: dns.Fqdn(domain), RD: true, Qtype: dns.TypeSVCB}
m := QueryMaker(query)
res, err := dns.Exchange(m, addr)
return res, err
}
func toSVCBKEY(key dns.SVCBKey) string {
switch key {
case dns.SVCB_MANDATORY:
return "mandatory"
case dns.SVCB_ALPN:
return "alpn"
case dns.SVCB_NO_DEFAULT_ALPN:
return "no_default_alpn"
case dns.SVCB_PORT:
return "port"
case dns.SVCB_IPV4HINT:
return "ipv4_hint"
case dns.SVCB_ECHCONFIG:
return "ech_config"
case dns.SVCB_IPV6HINT:
return "ipv6_hint"
case dns.SVCB_DOHPATH:
return "doh_path"
default:
return "unknown"
}
}
func ParseSVCBResponse(msg *dns.Msg) (SVCBResponse, error) {
response := SVCBResponse{Records: make([]SVCBRecord, 0)}
if len(msg.Answer) > 0 {
for _, rr := range msg.Answer {
if svcb, ok := rr.(*dns.SVCB); ok {
record := SVCBRecord{Data: make(map[string]string)}
record.Target = svcb.Target
for _, kv := range svcb.Value {
key := toSVCBKEY(kv.Key())
value := kv.String()
record.Data[key] = value
}
response.Records = append(response.Records, record)
}
}
}
if len(response.Records) > 0 {
return response, nil
}
return response, &WrongAnswerError{Message: "no valid SVCB records"}
}