star/dns/cmd.go
2024-04-14 00:25:30 +08:00

80 lines
1.9 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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",
Aliases: []string{"dig"},
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)
}
localdns := GetDNSServers()
for _, v := range localdns {
fmt.Println("本地DNS:", v.String())
}
if dnsServer == "" {
switch itype {
case 0, 1:
if len(localdns) != 0 {
dnsServer = localdns[0].String()
} else {
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)
fmt.Println(data.Str)
}
},
}