Skip to content

Commit

Permalink
Move ReservedIPNetworkList to common, fix some bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
shawn1m committed May 13, 2017
1 parent d5d742a commit e6a2eab
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 35 deletions.
24 changes: 20 additions & 4 deletions core/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ package common

import (
"net"
"time"
"strings"
"time"

log "github.com/Sirupsen/logrus"
"github.com/miekg/dns"
)

var ReservedIPNetworkList = getReservedIPNetworkList()

func IsIPMatchList(ip net.IP, ipnl []*net.IPNet, isLog bool) bool {

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

func HasSubDomain(s string, sub string) bool{
func HasSubDomain(s string, sub string) bool {

return strings.HasSuffix(sub, "."+s) || s == sub
}

return strings.HasSuffix(sub, "." + s) || s == sub
}
func getReservedIPNetworkList() []*net.IPNet {

ipnl := make([]*net.IPNet, 0)
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"}
for _, c := range localCIDR {
_, ip_net, err := net.ParseCIDR(c)
if err != nil {
break
}
ipnl = append(ipnl, ip_net)
}
return ipnl
}
29 changes: 6 additions & 23 deletions core/outbound/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,30 +18,27 @@ type Client struct {
ResponseMessage *dns.Msg
QuestionMessage *dns.Msg

DNSUpstream *DNSUpstream
EDNSClientSubnetIP string
InboundIP string
DNSUpstream *DNSUpstream
EDNSClientSubnetIP string
InboundIP string

Hosts *hosts.Hosts
Cache *cache.Cache
}

var ReservedIPNetworkList = getReservedIPNetworkList()

func NewClient(q *dns.Msg, u *DNSUpstream, ip string, h *hosts.Hosts, cache *cache.Cache) *Client {

c := &Client{QuestionMessage: q, DNSUpstream: u, InboundIP: ip, Hosts: h, Cache: cache}
if ReservedIPNetworkList == nil{
c.getEDNSClientSubnetIP()
}

c.getEDNSClientSubnetIP()
return c
}

func (c *Client) getEDNSClientSubnetIP() {

switch c.DNSUpstream.EDNSClientSubnet.Policy {
case "auto":
if !common.IsIPMatchList(net.ParseIP(c.InboundIP), ReservedIPNetworkList, false) {
if !common.IsIPMatchList(net.ParseIP(c.InboundIP), common.ReservedIPNetworkList, false) {
c.EDNSClientSubnetIP = c.InboundIP
} else {
c.EDNSClientSubnetIP = c.DNSUpstream.EDNSClientSubnet.ExternalIP
Expand Down Expand Up @@ -224,17 +221,3 @@ func (c *Client) logAnswer(indicator string) {
log.Debug(name + " Answer: " + a.String())
}
}

func getReservedIPNetworkList() []*net.IPNet {

ipnl := make([]*net.IPNet, 0)
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"}
for _, c := range localCIDR {
_, ip_net, err := net.ParseCIDR(c)
if err != nil {
break
}
ipnl = append(ipnl, ip_net)
}
return ipnl
}
4 changes: 2 additions & 2 deletions core/outbound/clientbundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (cb *ClientBundle) ExchangeFromRemote(isCache bool, isLog bool) {
var em *dns.Msg

for i := 0; i < len(cb.ClientList); i++ {
if m := <- ch; m != nil {
if m := <-ch; m != nil {
if common.IsAnswerEmpty(m) {
em = m
break
Expand Down Expand Up @@ -96,4 +96,4 @@ func (cb *ClientBundle) CacheResults() {
if cb.Cache != nil {
cb.Cache.InsertMessage(cache.Key(cb.QuestionMessage.Question[0], getEDNSClientSubnetIP(cb.QuestionMessage)), cb.ResponseMessage)
}
}
}
4 changes: 2 additions & 2 deletions core/outbound/edns.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func isEDNSClientSubnet(o *dns.OPT) *dns.EDNS0_SUBNET {
return nil
}

func getEDNSClientSubnetIP(m *dns.Msg) string{
func getEDNSClientSubnetIP(m *dns.Msg) string {

o := m.IsEdns0()
if o != nil {
Expand All @@ -65,4 +65,4 @@ func getEDNSClientSubnetIP(m *dns.Msg) string{
}
}
return ""
}
}
8 changes: 4 additions & 4 deletions main/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ package main

import (
"flag"
"runtime"
"os"
"io"
"os"
"runtime"

log "github.com/Sirupsen/logrus"
"github.com/shawn1m/overture/core"
Expand Down Expand Up @@ -40,10 +40,10 @@ func main() {
log.SetLevel(log.InfoLevel)
}

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

Expand Down

0 comments on commit e6a2eab

Please sign in to comment.