run.go 899 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package cmd
  2. import (
  3. "fmt"
  4. "github.com/spf13/cobra"
  5. "rinne.dev/doh-resolver/app/func/hosts"
  6. "rinne.dev/doh-resolver/app/func/resolver"
  7. "rinne.dev/doh-resolver/app/func/writer"
  8. "rinne.dev/doh-resolver/pkg/config"
  9. )
  10. // Run 运行 DoH 查询
  11. var Run = &cobra.Command{
  12. Use: "run",
  13. Short: "Run DoH query and save to file",
  14. Run: run,
  15. Args: cobra.NoArgs,
  16. }
  17. func run(cmd *cobra.Command, args []string) {
  18. // 获取需要查询的域名
  19. domains := config.GetStringArray("domains")
  20. // DoH 查询
  21. resData := make(map[string]string)
  22. for _, domain := range domains {
  23. ans, err := resolver.Resolve(domain)
  24. if err != nil {
  25. fmt.Println(err)
  26. return
  27. }
  28. resData[domain] = ans
  29. }
  30. // 生成 hosts 文件
  31. hostsFile := hosts.GenerateHostsFile(resData)
  32. // 写入文件
  33. err := writer.WriteFile(config.GetString("output"), hostsFile)
  34. if err != nil {
  35. fmt.Println(err)
  36. return
  37. }
  38. }