You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
star/dns/cmd.go

73 lines
1.7 KiB
Go

8 months ago
package dns
import (
"b612.me/starlog"
"fmt"
"github.com/spf13/cobra"
"os"
"strings"
)
var dnsServer, serverType, queryType string
func init() {
Cmd.Flags().StringVarP(&dnsServer, "server", "s", "", "dns服务器地址")
Cmd.Flags().StringVarP(&serverType, "type", "t", "udp", "dns服务器类型,支持udp,tcp,dot,doh")
Cmd.Flags().StringVarP(&queryType, "query-type", "q", "A", "dns查询类型支持 A,CNAME,AAAA,MX,NS,SOA,SRV,PTR,ANY,CAA,TLSA,DS,DNSKEY,NSEC,NSEC3,NSEC3PARAM,RRSIG,SPF,SSHFP,TKEY,TSIG,URI")
}
var Cmd = &cobra.Command{
Use: "dns",
Short: "dns查询服务",
Long: "DNS查询服务支持UDP,TCP,DoT,DoH",
Run: func(cmd *cobra.Command, args []string) {
itype := 0
switch strings.ToLower(serverType) {
case "udp", "":
itype = 0
case "tcp":
itype = 1
case "dot":
itype = 2
case "doh":
itype = 3
default:
starlog.Errorln("unsupport server type:", serverType)
os.Exit(1)
}
if dnsServer == "" {
switch itype {
case 0, 1:
dnsServer = "223.5.5.5:53"
case 2:
dnsServer = "dns.b612.me:853"
case 3:
dnsServer = "https://dns.b612.me/dns-query"
}
}
if (itype == 0 || itype == 1) && !strings.Contains(dnsServer, ":") {
dnsServer = dnsServer + ":53"
}
if len(args) == 0 {
starlog.Errorln("请输入查询目标")
return
}
queryType = strings.ToUpper(queryType)
fmt.Println("dns服务器:", dnsServer)
fmt.Println("查询类型:", queryType)
for _, target := range args {
data, err := QueryDns(target, queryType, itype, dnsServer)
if err != nil {
starlog.Errorln(err)
os.Exit(2)
}
fmt.Println("\n")
fmt.Println("Query:", target)
for _, v := range data {
fmt.Printf("%s\tRTT=%v\n", v.Res, v.Rtt)
}
}
},
}