Skip to content

Commit e6a2eab

Browse files
committed
Move ReservedIPNetworkList to common, fix some bugs
1 parent d5d742a commit e6a2eab

File tree

5 files changed

+34
-35
lines changed

5 files changed

+34
-35
lines changed

core/common/common.go

+20-4
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@ package common
77

88
import (
99
"net"
10-
"time"
1110
"strings"
11+
"time"
1212

1313
log "github.com/Sirupsen/logrus"
1414
"github.com/miekg/dns"
1515
)
1616

17+
var ReservedIPNetworkList = getReservedIPNetworkList()
18+
1719
func IsIPMatchList(ip net.IP, ipnl []*net.IPNet, isLog bool) bool {
1820

1921
for _, ip_net := range ipnl {
@@ -43,7 +45,21 @@ func IsAnswerEmpty(m *dns.Msg) bool {
4345
return false
4446
}
4547

46-
func HasSubDomain(s string, sub string) bool{
48+
func HasSubDomain(s string, sub string) bool {
49+
50+
return strings.HasSuffix(sub, "."+s) || s == sub
51+
}
4752

48-
return strings.HasSuffix(sub, "." + s) || s == sub
49-
}
53+
func getReservedIPNetworkList() []*net.IPNet {
54+
55+
ipnl := make([]*net.IPNet, 0)
56+
localCIDR := []string{"127.0.0.0/8", "10.0.0.0/8", "172.16.0.0/12", "192.168.0.0/16", "100.64.0.0/10"}
57+
for _, c := range localCIDR {
58+
_, ip_net, err := net.ParseCIDR(c)
59+
if err != nil {
60+
break
61+
}
62+
ipnl = append(ipnl, ip_net)
63+
}
64+
return ipnl
65+
}

core/outbound/client.go

+6-23
Original file line numberDiff line numberDiff line change
@@ -18,30 +18,27 @@ type Client struct {
1818
ResponseMessage *dns.Msg
1919
QuestionMessage *dns.Msg
2020

21-
DNSUpstream *DNSUpstream
22-
EDNSClientSubnetIP string
23-
InboundIP string
21+
DNSUpstream *DNSUpstream
22+
EDNSClientSubnetIP string
23+
InboundIP string
2424

2525
Hosts *hosts.Hosts
2626
Cache *cache.Cache
2727
}
2828

29-
var ReservedIPNetworkList = getReservedIPNetworkList()
30-
3129
func NewClient(q *dns.Msg, u *DNSUpstream, ip string, h *hosts.Hosts, cache *cache.Cache) *Client {
3230

3331
c := &Client{QuestionMessage: q, DNSUpstream: u, InboundIP: ip, Hosts: h, Cache: cache}
34-
if ReservedIPNetworkList == nil{
35-
c.getEDNSClientSubnetIP()
36-
}
32+
33+
c.getEDNSClientSubnetIP()
3734
return c
3835
}
3936

4037
func (c *Client) getEDNSClientSubnetIP() {
4138

4239
switch c.DNSUpstream.EDNSClientSubnet.Policy {
4340
case "auto":
44-
if !common.IsIPMatchList(net.ParseIP(c.InboundIP), ReservedIPNetworkList, false) {
41+
if !common.IsIPMatchList(net.ParseIP(c.InboundIP), common.ReservedIPNetworkList, false) {
4542
c.EDNSClientSubnetIP = c.InboundIP
4643
} else {
4744
c.EDNSClientSubnetIP = c.DNSUpstream.EDNSClientSubnet.ExternalIP
@@ -224,17 +221,3 @@ func (c *Client) logAnswer(indicator string) {
224221
log.Debug(name + " Answer: " + a.String())
225222
}
226223
}
227-
228-
func getReservedIPNetworkList() []*net.IPNet {
229-
230-
ipnl := make([]*net.IPNet, 0)
231-
localCIDR := []string{"127.0.0.0/8", "10.0.0.0/8", "172.16.0.0/12", "192.168.0.0/16", "100.64.0.0/10"}
232-
for _, c := range localCIDR {
233-
_, ip_net, err := net.ParseCIDR(c)
234-
if err != nil {
235-
break
236-
}
237-
ipnl = append(ipnl, ip_net)
238-
}
239-
return ipnl
240-
}

core/outbound/clientbundle.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func (cb *ClientBundle) ExchangeFromRemote(isCache bool, isLog bool) {
5151
var em *dns.Msg
5252

5353
for i := 0; i < len(cb.ClientList); i++ {
54-
if m := <- ch; m != nil {
54+
if m := <-ch; m != nil {
5555
if common.IsAnswerEmpty(m) {
5656
em = m
5757
break
@@ -96,4 +96,4 @@ func (cb *ClientBundle) CacheResults() {
9696
if cb.Cache != nil {
9797
cb.Cache.InsertMessage(cache.Key(cb.QuestionMessage.Question[0], getEDNSClientSubnetIP(cb.QuestionMessage)), cb.ResponseMessage)
9898
}
99-
}
99+
}

core/outbound/edns.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func isEDNSClientSubnet(o *dns.OPT) *dns.EDNS0_SUBNET {
5353
return nil
5454
}
5555

56-
func getEDNSClientSubnetIP(m *dns.Msg) string{
56+
func getEDNSClientSubnetIP(m *dns.Msg) string {
5757

5858
o := m.IsEdns0()
5959
if o != nil {
@@ -65,4 +65,4 @@ func getEDNSClientSubnetIP(m *dns.Msg) string{
6565
}
6666
}
6767
return ""
68-
}
68+
}

main/main.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ package main
77

88
import (
99
"flag"
10-
"runtime"
11-
"os"
1210
"io"
11+
"os"
12+
"runtime"
1313

1414
log "github.com/Sirupsen/logrus"
1515
"github.com/shawn1m/overture/core"
@@ -40,10 +40,10 @@ func main() {
4040
log.SetLevel(log.InfoLevel)
4141
}
4242

43-
logf, err := os.OpenFile(logPath, os.O_APPEND | os.O_WRONLY | os.O_CREATE, 0640)
43+
logf, err := os.OpenFile(logPath, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0640)
4444
if err != nil {
4545
println("Logfile error: Please check your log file path")
46-
}else{
46+
} else {
4747
log.SetOutput(io.MultiWriter(logf, os.Stdout))
4848
}
4949

0 commit comments

Comments
 (0)