Skip to content

Commit 238f809

Browse files
committed
v1.8
add -c option
1 parent 6fd1540 commit 238f809

9 files changed

+109
-17
lines changed

.vscode/launch.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,8 @@
33
// Hover to view descriptions of existing attributes.
44
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
55
"version": "0.2.0",
6-
"configurations": []
6+
"configurations": [
7+
8+
9+
]
710
}

README.MD

+3-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ A simple tool to convert your dns request into http dns request so we can avoid
99
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.
1010
> 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.
1111
12-
- Current Version: **1.7**
12+
- Current Version: **1.8**
1313
- Language:golang
1414

1515
## Usage
@@ -28,7 +28,7 @@ A simple tool to convert your dns request into http dns request so we can avoid
2828
![example](/ex.png)
2929
**4.** Enjoy your browsing!
3030

31-
31+
>from version 1.8,you can now load dns records from ``-c <your hosts file location>`` option.
3232
3333
## Tips
3434

@@ -48,6 +48,7 @@ A simple tool to convert your dns request into http dns request so we can avoid
4848
>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...)
4949
>also by this you can serve the other teminal in your LAN if you like.
5050
51+
5152
## Thanks
5253
- miekg
5354
https://github.com/miekg/dns

SnowPearDNS.go

+98-13
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ import (
99
"math/rand"
1010
"net"
1111
"net/http"
12+
"os"
13+
"path/filepath"
14+
"runtime"
15+
"strconv"
1216
"strings"
1317
"time"
1418

@@ -19,14 +23,13 @@ import (
1923

2024
var (
2125
//server_url string = "http://119.29.29.29/d?dn=%s"
22-
server_url string = "https://doh.pub/dns-query?name=%s&type=%s"
23-
//server_url string
24-
version string = "1.7"
25-
//cache_time :=60*60*24
26-
dnsAcache = cache2go.Cache("DNACACHE")
27-
dnsCcache = cache2go.Cache("DNCCACHE")
28-
hostsflag *bool
29-
cache_time = 60 * 60 * 24 * time.Second
26+
server_url string = "https://doh.pub/dns-query?name=%s&type=%s"
27+
version string = Version.String()
28+
dnsAcache = cache2go.Cache("DNACACHE")
29+
dnsCcache = cache2go.Cache("DNCCACHE")
30+
hostsflag *bool
31+
lchostsflag *string
32+
cache_time = 60 * 60 * 24 * time.Second
3033
)
3134

3235
type DOH_Answer struct {
@@ -106,7 +109,7 @@ func get_a(domain string) []string {
106109
r, err := http.Get(url)
107110

108111
if err != nil {
109-
fmt.Println(err)
112+
//fmt.Println(err)
110113
return []string{}
111114
}
112115

@@ -115,7 +118,7 @@ func get_a(domain string) []string {
115118
buf, err := ioutil.ReadAll(r.Body)
116119
//fmt.Println(string(buf))
117120
if err != nil {
118-
fmt.Println(err)
121+
//fmt.Println(err)
119122
return []string{}
120123
}
121124
//here we add res to cache
@@ -200,15 +203,16 @@ func get_cname(domain string) []string {
200203
r, err := http.Get(url)
201204

202205
if err != nil {
203-
fmt.Println(err)
206+
//fmt.Println(err)
204207
return []string{}
205208
}
206209

207210
defer r.Body.Close()
208211

209212
buf, err := ioutil.ReadAll(r.Body)
210213
if err != nil {
211-
fmt.Println(err)
214+
//fmt.Println(err)
215+
212216
return []string{}
213217
}
214218
//here we add res to cache
@@ -365,12 +369,93 @@ func add_localhosts() {
365369
}
366370
}
367371
}
372+
func ReadAll(filePth string) ([]byte, error) {
373+
f, err := os.Open(filePth)
374+
if err != nil {
375+
return nil, err
376+
}
377+
return ioutil.ReadAll(f)
378+
}
379+
func FileExist(path string) bool {
380+
_, err := os.Lstat(path)
381+
return !os.IsNotExist(err)
382+
}
383+
func parse_localdnsrecord() (int, bool) {
384+
if *lchostsflag != "" {
385+
// fmt.Println("loading Hosts file...")
386+
hostsMap, err := hostsparser.ParseHosts(ReadAll(*lchostsflag))
387+
if err != nil {
388+
return 0, false
389+
}
390+
rcdcount := 0
391+
for k, v := range hostsMap {
392+
rcdcount = rcdcount + 1
393+
dnsAcache.Add(k+".", 0, v)
394+
}
395+
return rcdcount, true
396+
} else {
397+
// get cwd config file
398+
// path, err := os.Executable()
399+
// if err != nil {
400+
// log.Printf(err)
401+
// }
402+
// dir := filepath.Dir(path)
403+
// fmt.Println(path) // for example /home/user/main
404+
// fmt.Println(dir) // for example /home/user
405+
// -----------
406+
// func ReadAll(filePth string) ([]byte, error) {
407+
// f, err := os.Open(filePth)
408+
// if err != nil {
409+
// return nil, err
410+
// }
411+
412+
// return ioutil.ReadAll(f)
413+
// }
414+
path, err := os.Executable()
415+
if err != nil {
416+
log.Fatal(err)
417+
}
418+
dir := filepath.Dir(path)
419+
// dir=dir+"/"
420+
slash := "/"
421+
switch runtime.GOOS {
422+
case "windows":
423+
slash = "\\"
424+
}
425+
confpath := dir + slash + "spdhosts.conf"
426+
if FileExist(confpath) {
427+
fmt.Println("Loading DNS records conf data from " + confpath)
428+
hostsMap, err := hostsparser.ParseHosts(ReadAll(confpath))
429+
if err != nil {
430+
return 0, false
431+
}
432+
rcdcount := 0
433+
for k, v := range hostsMap {
434+
rcdcount = rcdcount + 1
435+
dnsAcache.Add(k+".", 0, v)
436+
}
437+
return rcdcount, true
438+
} else {
439+
return 0, false
440+
}
441+
442+
}
443+
}
368444
func main() {
369445
fmt.Println("SnowPearDNS version: ", version)
370446
fmt.Println("https://github.com/arryboom/SnowPearDNS")
371-
hostsflag = flag.Bool("hosts", false, "using local hosts file,default false")
447+
hostsflag = flag.Bool("hosts", false, "using local hosts file,default false.(Conflict with -c)")
448+
lchostsflag = flag.String("c", "", "conf file path,default Current Directory spdhosts.conf")
372449
flag.Parse()
373450
add_localhosts()
451+
if *hostsflag && *lchostsflag != "" {
452+
fmt.Println("-hosts and -c enabled at the same time,ignore -c option")
453+
} else {
454+
ct, sig := parse_localdnsrecord()
455+
if sig {
456+
fmt.Println("Loaded " + strconv.Itoa(ct) + " dns records in conf.")
457+
}
458+
}
374459
fmt.Println("Start Dns Server Now...")
375460
if !(init_dohip()) {
376461
fmt.Println("Failed to init DOH server's DNS resolve,pls check your network connection.")

release/snowpear_32

5.66 KB
Binary file not shown.

release/snowpear_64

9.57 KB
Binary file not shown.

release/snowpeardns_32.exe

8 KB
Binary file not shown.

release/snowpeardns_64.exe

10 KB
Binary file not shown.

spdhosts.conf.example

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
1.1.1.1 www.google.com
2+
8.8.8.8 www.bing.com
3+
#above tab or space both supported.

version.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package main
33
import "fmt"
44

55
// Version is current version of this library.
6-
var Version = v{1, 7, 0}
6+
var Version = v{1, 8, 0}
77

88
// v holds the version of this library.
99
type v struct {

0 commit comments

Comments
 (0)