diff --git a/.vscode/launch.json b/.vscode/launch.json index 5c7247b..9dae3d4 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -3,5 +3,8 @@ // Hover to view descriptions of existing attributes. // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 "version": "0.2.0", - "configurations": [] + "configurations": [ + + + ] } \ No newline at end of file diff --git a/README.MD b/README.MD index 0f1c909..90cc582 100644 --- a/README.MD +++ b/README.MD @@ -9,7 +9,7 @@ A simple tool to convert your dns request into http dns request so we can avoid finally,I found that DNSpod have a simple Httpdns API server in China and currently there has no evidence showed which ISP already hijack the http dns request between user's network and DNSpod server, so I developed this tiny tool to make it work. > by version 1.5 now this tool will use standard DOH which will access https://doh.pub/dns-query for results,which considered as a better security performance. -- Current Version: **1.7** +- Current Version: **1.8** - Language:golang ## Usage @@ -28,7 +28,7 @@ A simple tool to convert your dns request into http dns request so we can avoid ![example](/ex.png) **4.** Enjoy your browsing! - +>from version 1.8,you can now load dns records from ``-c `` option. ## Tips @@ -48,6 +48,7 @@ A simple tool to convert your dns request into http dns request so we can avoid >the tool itself by default will listen on ```0.0.0.0```,so simple put main DNS as 127.0.0.1,another to your LAN address.(which like 192.168.1.x 172.0.1.x...) >also by this you can serve the other teminal in your LAN if you like. + ## Thanks - miekg https://github.com/miekg/dns diff --git a/SnowPearDNS.go b/SnowPearDNS.go index f804a29..09b50d0 100644 --- a/SnowPearDNS.go +++ b/SnowPearDNS.go @@ -9,6 +9,10 @@ import ( "math/rand" "net" "net/http" + "os" + "path/filepath" + "runtime" + "strconv" "strings" "time" @@ -19,14 +23,13 @@ import ( var ( //server_url string = "http://119.29.29.29/d?dn=%s" - server_url string = "https://doh.pub/dns-query?name=%s&type=%s" - //server_url string - version string = "1.7" - //cache_time :=60*60*24 - dnsAcache = cache2go.Cache("DNACACHE") - dnsCcache = cache2go.Cache("DNCCACHE") - hostsflag *bool - cache_time = 60 * 60 * 24 * time.Second + server_url string = "https://doh.pub/dns-query?name=%s&type=%s" + version string = Version.String() + dnsAcache = cache2go.Cache("DNACACHE") + dnsCcache = cache2go.Cache("DNCCACHE") + hostsflag *bool + lchostsflag *string + cache_time = 60 * 60 * 24 * time.Second ) type DOH_Answer struct { @@ -106,7 +109,7 @@ func get_a(domain string) []string { r, err := http.Get(url) if err != nil { - fmt.Println(err) + //fmt.Println(err) return []string{} } @@ -115,7 +118,7 @@ func get_a(domain string) []string { buf, err := ioutil.ReadAll(r.Body) //fmt.Println(string(buf)) if err != nil { - fmt.Println(err) + //fmt.Println(err) return []string{} } //here we add res to cache @@ -200,7 +203,7 @@ func get_cname(domain string) []string { r, err := http.Get(url) if err != nil { - fmt.Println(err) + //fmt.Println(err) return []string{} } @@ -208,7 +211,8 @@ func get_cname(domain string) []string { buf, err := ioutil.ReadAll(r.Body) if err != nil { - fmt.Println(err) + //fmt.Println(err) + return []string{} } //here we add res to cache @@ -365,12 +369,93 @@ func add_localhosts() { } } } +func ReadAll(filePth string) ([]byte, error) { + f, err := os.Open(filePth) + if err != nil { + return nil, err + } + return ioutil.ReadAll(f) +} +func FileExist(path string) bool { + _, err := os.Lstat(path) + return !os.IsNotExist(err) +} +func parse_localdnsrecord() (int, bool) { + if *lchostsflag != "" { + // fmt.Println("loading Hosts file...") + hostsMap, err := hostsparser.ParseHosts(ReadAll(*lchostsflag)) + if err != nil { + return 0, false + } + rcdcount := 0 + for k, v := range hostsMap { + rcdcount = rcdcount + 1 + dnsAcache.Add(k+".", 0, v) + } + return rcdcount, true + } else { + // get cwd config file + // path, err := os.Executable() + // if err != nil { + // log.Printf(err) + // } + // dir := filepath.Dir(path) + // fmt.Println(path) // for example /home/user/main + // fmt.Println(dir) // for example /home/user + // ----------- + // func ReadAll(filePth string) ([]byte, error) { + // f, err := os.Open(filePth) + // if err != nil { + // return nil, err + // } + + // return ioutil.ReadAll(f) + // } + path, err := os.Executable() + if err != nil { + log.Fatal(err) + } + dir := filepath.Dir(path) + // dir=dir+"/" + slash := "/" + switch runtime.GOOS { + case "windows": + slash = "\\" + } + confpath := dir + slash + "spdhosts.conf" + if FileExist(confpath) { + fmt.Println("Loading DNS records conf data from " + confpath) + hostsMap, err := hostsparser.ParseHosts(ReadAll(confpath)) + if err != nil { + return 0, false + } + rcdcount := 0 + for k, v := range hostsMap { + rcdcount = rcdcount + 1 + dnsAcache.Add(k+".", 0, v) + } + return rcdcount, true + } else { + return 0, false + } + + } +} func main() { fmt.Println("SnowPearDNS version: ", version) fmt.Println("https://github.com/arryboom/SnowPearDNS") - hostsflag = flag.Bool("hosts", false, "using local hosts file,default false") + hostsflag = flag.Bool("hosts", false, "using local hosts file,default false.(Conflict with -c)") + lchostsflag = flag.String("c", "", "conf file path,default Current Directory spdhosts.conf") flag.Parse() add_localhosts() + if *hostsflag && *lchostsflag != "" { + fmt.Println("-hosts and -c enabled at the same time,ignore -c option") + } else { + ct, sig := parse_localdnsrecord() + if sig { + fmt.Println("Loaded " + strconv.Itoa(ct) + " dns records in conf.") + } + } fmt.Println("Start Dns Server Now...") if !(init_dohip()) { fmt.Println("Failed to init DOH server's DNS resolve,pls check your network connection.") diff --git a/release/snowpear_32 b/release/snowpear_32 index a38b8c4..2091ace 100644 Binary files a/release/snowpear_32 and b/release/snowpear_32 differ diff --git a/release/snowpear_64 b/release/snowpear_64 index 5da0301..be363ce 100644 Binary files a/release/snowpear_64 and b/release/snowpear_64 differ diff --git a/release/snowpeardns_32.exe b/release/snowpeardns_32.exe index 8cc4e5c..0d7ee63 100644 Binary files a/release/snowpeardns_32.exe and b/release/snowpeardns_32.exe differ diff --git a/release/snowpeardns_64.exe b/release/snowpeardns_64.exe index dbf3115..a069f69 100644 Binary files a/release/snowpeardns_64.exe and b/release/snowpeardns_64.exe differ diff --git a/spdhosts.conf.example b/spdhosts.conf.example new file mode 100644 index 0000000..b07d7a5 --- /dev/null +++ b/spdhosts.conf.example @@ -0,0 +1,3 @@ +1.1.1.1 www.google.com + 8.8.8.8 www.bing.com +#above tab or space both supported. \ No newline at end of file diff --git a/version.go b/version.go index c1b8656..31da826 100644 --- a/version.go +++ b/version.go @@ -3,7 +3,7 @@ package main import "fmt" // Version is current version of this library. -var Version = v{1, 7, 0} +var Version = v{1, 8, 0} // v holds the version of this library. type v struct {