Skip to content

Commit

Permalink
v1.8
Browse files Browse the repository at this point in the history
add -c option
  • Loading branch information
Arryboom committed Jan 3, 2022
1 parent 6fd1540 commit 238f809
Show file tree
Hide file tree
Showing 9 changed files with 109 additions and 17 deletions.
5 changes: 4 additions & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [


]
}
5 changes: 3 additions & 2 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 <your hosts file location>`` option.
## Tips

Expand All @@ -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
Expand Down
111 changes: 98 additions & 13 deletions SnowPearDNS.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ import (
"math/rand"
"net"
"net/http"
"os"
"path/filepath"
"runtime"
"strconv"
"strings"
"time"

Expand All @@ -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 {
Expand Down Expand Up @@ -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{}
}

Expand All @@ -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
Expand Down Expand Up @@ -200,15 +203,16 @@ func get_cname(domain string) []string {
r, err := http.Get(url)

if err != nil {
fmt.Println(err)
//fmt.Println(err)
return []string{}
}

defer r.Body.Close()

buf, err := ioutil.ReadAll(r.Body)
if err != nil {
fmt.Println(err)
//fmt.Println(err)

return []string{}
}
//here we add res to cache
Expand Down Expand Up @@ -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.")
Expand Down
Binary file modified release/snowpear_32
Binary file not shown.
Binary file modified release/snowpear_64
Binary file not shown.
Binary file modified release/snowpeardns_32.exe
Binary file not shown.
Binary file modified release/snowpeardns_64.exe
Binary file not shown.
3 changes: 3 additions & 0 deletions spdhosts.conf.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
1.1.1.1 www.google.com
8.8.8.8 www.bing.com
#above tab or space both supported.
2 changes: 1 addition & 1 deletion version.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 238f809

Please sign in to comment.