|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + "io/ioutil" |
| 7 | + "net" |
| 8 | + "os" |
| 9 | + "strings" |
| 10 | + "sync" |
| 11 | + "time" |
| 12 | + "unicode" |
| 13 | + |
| 14 | + "github.com/hashicorp/go-immutable-radix" |
| 15 | + "github.com/jedisct1/dlog" |
| 16 | + "github.com/miekg/dns" |
| 17 | +) |
| 18 | + |
| 19 | +type PluginBlockIP struct { |
| 20 | + sync.Mutex |
| 21 | + blockedPrefixes *iradix.Tree |
| 22 | + blockedIPs map[string]interface{} |
| 23 | + outFd *os.File |
| 24 | + format string |
| 25 | +} |
| 26 | + |
| 27 | +func (plugin *PluginBlockIP) Name() string { |
| 28 | + return "block_ip" |
| 29 | +} |
| 30 | + |
| 31 | +func (plugin *PluginBlockIP) Description() string { |
| 32 | + return "Block responses containing specific IP addresses" |
| 33 | +} |
| 34 | + |
| 35 | +func (plugin *PluginBlockIP) Init(proxy *Proxy) error { |
| 36 | + dlog.Noticef("Loading the set of IP blocking rules from [%s]", proxy.blockIPFile) |
| 37 | + bin, err := ioutil.ReadFile(proxy.blockIPFile) |
| 38 | + if err != nil { |
| 39 | + return err |
| 40 | + } |
| 41 | + plugin.blockedPrefixes = iradix.New() |
| 42 | + plugin.blockedIPs = make(map[string]interface{}) |
| 43 | + for lineNo, line := range strings.Split(string(bin), "\n") { |
| 44 | + line = strings.TrimFunc(line, unicode.IsSpace) |
| 45 | + if len(line) == 0 || strings.HasPrefix(line, "#") { |
| 46 | + continue |
| 47 | + } |
| 48 | + ip := net.ParseIP(line) |
| 49 | + trailingStar := strings.HasSuffix(line, "*") |
| 50 | + if len(line) < 2 || (ip != nil && trailingStar) { |
| 51 | + dlog.Errorf("Suspicious IP blocking rule [%s] at line %d", line, lineNo) |
| 52 | + continue |
| 53 | + } |
| 54 | + if trailingStar { |
| 55 | + line = line[:len(line)-1] |
| 56 | + } |
| 57 | + if strings.HasSuffix(line, ":") || strings.HasSuffix(line, ".") { |
| 58 | + line = line[:len(line)-1] |
| 59 | + } |
| 60 | + if len(line) == 0 { |
| 61 | + dlog.Errorf("Empty IP blocking rule at line %d", lineNo) |
| 62 | + continue |
| 63 | + } |
| 64 | + if strings.Contains(line, "*") { |
| 65 | + dlog.Errorf("Invalid rule: [%s] - wildcards can only be used as a suffix at line %d", line, lineNo) |
| 66 | + continue |
| 67 | + } |
| 68 | + line = strings.ToLower(line) |
| 69 | + if trailingStar { |
| 70 | + plugin.blockedPrefixes, _, _ = plugin.blockedPrefixes.Insert([]byte(line), 0) |
| 71 | + } else { |
| 72 | + plugin.blockedIPs[line] = true |
| 73 | + } |
| 74 | + } |
| 75 | + if len(proxy.blockIPLogFile) == 0 { |
| 76 | + return nil |
| 77 | + } |
| 78 | + plugin.Lock() |
| 79 | + defer plugin.Unlock() |
| 80 | + outFd, err := os.OpenFile(proxy.blockIPLogFile, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0644) |
| 81 | + if err != nil { |
| 82 | + return err |
| 83 | + } |
| 84 | + plugin.outFd = outFd |
| 85 | + plugin.format = proxy.blockIPFormat |
| 86 | + |
| 87 | + return nil |
| 88 | +} |
| 89 | + |
| 90 | +func (plugin *PluginBlockIP) Drop() error { |
| 91 | + return nil |
| 92 | +} |
| 93 | + |
| 94 | +func (plugin *PluginBlockIP) Reload() error { |
| 95 | + return nil |
| 96 | +} |
| 97 | + |
| 98 | +func (plugin *PluginBlockIP) Eval(pluginsState *PluginsState, msg *dns.Msg) error { |
| 99 | + answers := msg.Answer |
| 100 | + if len(answers) == 0 { |
| 101 | + return nil |
| 102 | + } |
| 103 | + reject, reason, ipStr := false, "", "" |
| 104 | + for _, answer := range answers { |
| 105 | + header := answer.Header() |
| 106 | + Rrtype := header.Rrtype |
| 107 | + if header.Class != dns.ClassINET || (Rrtype != dns.TypeA && Rrtype != dns.TypeAAAA) { |
| 108 | + continue |
| 109 | + } |
| 110 | + if Rrtype == dns.TypeA { |
| 111 | + ipStr = answer.(*dns.A).A.String() |
| 112 | + } else if Rrtype == dns.TypeAAAA { |
| 113 | + ipStr = answer.(*dns.AAAA).AAAA.String() // IPv4-mapped IPv6 addresses are converted to IPv4 |
| 114 | + } |
| 115 | + if _, found := plugin.blockedIPs[ipStr]; found { |
| 116 | + reject, reason = true, ipStr |
| 117 | + break |
| 118 | + } |
| 119 | + match, _, found := plugin.blockedPrefixes.Root().LongestPrefix([]byte(ipStr)) |
| 120 | + if found { |
| 121 | + if len(match) == len(ipStr) || (ipStr[len(match)] == '.' || ipStr[len(match)] == ':') { |
| 122 | + reject, reason = true, string(match)+"*" |
| 123 | + break |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + if reject { |
| 128 | + pluginsState.action = PluginsActionReject |
| 129 | + if plugin.outFd != nil { |
| 130 | + questions := msg.Question |
| 131 | + if len(questions) != 1 { |
| 132 | + return nil |
| 133 | + } |
| 134 | + qName := strings.ToLower(StripTrailingDot(questions[0].Name)) |
| 135 | + if len(qName) < 2 { |
| 136 | + return nil |
| 137 | + } |
| 138 | + var clientIPStr string |
| 139 | + if pluginsState.clientProto == "udp" { |
| 140 | + clientIPStr = (*pluginsState.clientAddr).(*net.UDPAddr).IP.String() |
| 141 | + } else { |
| 142 | + clientIPStr = (*pluginsState.clientAddr).(*net.TCPAddr).IP.String() |
| 143 | + } |
| 144 | + var line string |
| 145 | + if plugin.format == "tsv" { |
| 146 | + now := time.Now() |
| 147 | + year, month, day := now.Date() |
| 148 | + hour, minute, second := now.Clock() |
| 149 | + tsStr := fmt.Sprintf("[%d-%02d-%02d %02d:%02d:%02d]", year, int(month), day, hour, minute, second) |
| 150 | + line = fmt.Sprintf("%s\t%s\t%s\t%s\t%s\n", tsStr, clientIPStr, StringQuote(qName), StringQuote(ipStr), StringQuote(reason)) |
| 151 | + } else if plugin.format == "ltsv" { |
| 152 | + line = fmt.Sprintf("time:%d\thost:%s\tqname:%s\tip:%s\tmessage:%s\n", time.Now().Unix(), clientIPStr, StringQuote(qName), StringQuote(ipStr), StringQuote(reason)) |
| 153 | + } else { |
| 154 | + dlog.Fatalf("Unexpected log format: [%s]", plugin.format) |
| 155 | + } |
| 156 | + plugin.Lock() |
| 157 | + if plugin.outFd == nil { |
| 158 | + return errors.New("Log file not initialized") |
| 159 | + } |
| 160 | + plugin.outFd.WriteString(line) |
| 161 | + defer plugin.Unlock() |
| 162 | + } |
| 163 | + } |
| 164 | + return nil |
| 165 | +} |
0 commit comments