Skip to content

Commit

Permalink
improve address:port handling and update ip list
Browse files Browse the repository at this point in the history
  • Loading branch information
domosekai committed Sep 17, 2019
1 parent 8ce3887 commit 4e5fcf0
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 17 deletions.
23 changes: 22 additions & 1 deletion cnipv4.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3930,6 +3930,27 @@
103.142.0.0/23
103.142.28.0/23
103.142.58.0/23
103.142.82.0/23
103.142.96.0/23
103.142.102.0/23
103.142.122.0/23
103.142.126.0/24
103.142.128.0/23
103.142.140.0/23
103.142.154.0/23
103.142.156.0/23
103.142.172.0/23
103.142.180.0/23
103.142.186.0/23
103.142.190.0/23
103.142.220.0/23
103.142.230.0/24
103.142.234.0/23
103.142.238.0/23
103.142.248.0/23
103.143.16.0/23
103.143.18.0/23
103.143.31.0/24
103.192.0.0/22
103.192.4.0/22
103.192.8.0/22
Expand Down Expand Up @@ -6216,6 +6237,7 @@
183.182.0.0/19
183.184.0.0/13
183.192.0.0/10
185.203.36.0/22
188.131.128.0/17
192.51.188.0/24
192.55.46.0/24
Expand Down Expand Up @@ -7960,7 +7982,6 @@
203.212.80.0/20
203.215.232.0/21
203.217.164.0/22
203.222.192.0/20
203.223.0.0/20
203.223.16.0/21
204.52.191.0/24
Expand Down
20 changes: 20 additions & 0 deletions cnipv6.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@
2001:df1:c80::/48
2001:df1:2080::/48
2001:df1:3d00::/48
2001:df1:4180::/48
2001:df1:4580::/48
2001:df1:5280::/48
2001:df1:5b80::/48
2001:df1:6180::/48
2001:df1:6b80::/48
2001:df1:8b00::/48
2001:df1:a100::/48
2001:df1:c900::/48
Expand Down Expand Up @@ -1258,21 +1264,35 @@
2405:2c80::/32
2405:2d80::/32
2405:2e80::/32
2405:2ec0::/32
2405:2f40::/32
2405:2f80::/32
2405:3140::/32
2405:31c0::/32
2405:37c0::/32
2405:3880::/32
2405:3980::/32
2405:39c0::/32
2405:3a80::/32
2405:3ac0::/32
2405:3b00::/32
2405:3b80::/32
2405:3bc0::/32
2405:3c40::/32
2405:3c80::/32
2405:3d80::/32
2405:3e80::/32
2405:3f40::/32
2405:3f80::/32
2405:4080::/32
2405:4140::/32
2405:4180::/32
2405:41c0::/32
2405:4280::/32
2405:4380::/32
2405:4480::/32
2405:44c0::/32
2405:4540::/32
2405:4580::/32
2405:4680::/32
2405:4780::/32
Expand Down
39 changes: 23 additions & 16 deletions shdns.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,21 +86,25 @@ var (
errlog = log.New(os.Stderr, "", log.Ldate|log.Ltime|log.Lmicroseconds)
)

func parseudpaddr(str string) (*net.UDPAddr, error) {
_, _, err := net.SplitHostPort(str)
if err == nil {
return net.ResolveUDPAddr("udp", str)
}
if _, _, err := net.SplitHostPort(str + ":53"); err == nil {
return net.ResolveUDPAddr("udp", str+":53")
}
if _, _, err := net.SplitHostPort("[" + str + "]:53"); err == nil {
return net.ResolveUDPAddr("udp", "["+str+"]:53")
}
return nil, err
}

func parseservers(str string, stype servertype) {
serverstr := strings.Split(str, ",")
for _, s := range serverstr {
switch c := strings.Count(s, ":"); {
case c == 0:
s += ":53"
case c > 1:
if !strings.Contains(s, "]") {
s = "[" + s + "]:53"
} else if s[len(s)-1] == ']' {
s += ":53"
}
}
if addr, err := net.ResolveUDPAddr("udp", s); err != nil {
errlog.Fatalf("Invalid nameserver %s: %v", s, err)
if addr, err := parseudpaddr(s); err != nil {
errlog.Fatalf("Invalid nameserver: %s", s)
} else {
if addr.Zone != "" {
if zoneid, err := strconv.Atoi(addr.Zone); err == nil {
Expand All @@ -115,7 +119,7 @@ func parseservers(str string, stype servertype) {
}
if _, exist := lookupserver(addr); !exist {
servers = append(servers, nameserver{udpaddr: addr, stype: stype})
logger.Printf("Using nameserver %s", s)
logger.Printf("Using nameserver %s", addr)
} else {
errlog.Fatalf("Nameserver exists: %s", s)
}
Expand Down Expand Up @@ -570,13 +574,16 @@ func main() {
} else {
*minwait = 0
}
loc, _ := net.ResolveUDPAddr("udp", *localnet)
inconn, err := net.ListenUDP("udp", loc)
addr, err := parseudpaddr(*localnet)
if err != nil {
errlog.Fatalf("Invalid binding address: %s", *localnet)
}
inconn, err := net.ListenUDP("udp", addr)
if err != nil {
errlog.Fatalln(err)
}
defer inconn.Close()
logger.Printf("Listening on UDP %s", *localnet)
logger.Printf("Listening on UDP %s", addr)
for {
payload := make([]byte, 1500)
if n, addr, err := inconn.ReadFromUDP(payload); err != nil {
Expand Down

0 comments on commit 4e5fcf0

Please sign in to comment.