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

80 lines
1.9 KiB
Go

9 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{
8 months ago
Use: "dns",
Aliases: []string{"dig"},
Short: "dns查询服务",
Long: "DNS查询服务支持UDP,TCP,DoT,DoH",
9 months ago
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)
}
9 months ago
localdns := GetDNSServers()
for _, v := range localdns {
fmt.Println("本地DNS:", v.String())
}
9 months ago
if dnsServer == "" {
switch itype {
case 0, 1:
9 months ago
if len(localdns) != 0 {
dnsServer = localdns[0].String()
} else {
dnsServer = "223.5.5.5:53"
}
9 months ago
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)
9 months ago
fmt.Println("查询DNS:", dnsServer)
9 months ago
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)
9 months ago
fmt.Println(data.Str)
9 months ago
}
},
}