This commit is contained in:
YouROK
2021-05-17 14:55:57 +03:00
parent 29f13fd482
commit e578628886
40 changed files with 1319 additions and 656 deletions

View File

@@ -0,0 +1,115 @@
package blocker
import (
"bufio"
"bytes"
"errors"
"io/ioutil"
"net"
"net/http"
"path/filepath"
"strings"
"server/log"
"server/settings"
"github.com/gin-gonic/gin"
)
func Blocker() gin.HandlerFunc {
emptyFN := func(c *gin.Context) {
c.Next()
}
name := filepath.Join(settings.Path, "bip.txt")
buf, _ := ioutil.ReadFile(name)
blackIpList := scanBuf(buf)
name = filepath.Join(settings.Path, "wip.txt")
buf, _ = ioutil.ReadFile(name)
whiteIpList := scanBuf(buf)
if blackIpList.NumRanges() == 0 && whiteIpList.NumRanges() == 0 {
return emptyFN
}
return func(c *gin.Context) {
arr := strings.Split(c.Request.RemoteAddr, ":")
if len(arr) > 0 {
ip := net.ParseIP(arr[0])
minifyIP(&ip)
if whiteIpList.NumRanges() > 0 {
if _, ok := whiteIpList.Lookup(ip); !ok {
log.WebLogln("Block ip, not in white list", ip.String())
c.String(http.StatusTeapot, "Banned")
c.Abort()
return
}
}
if blackIpList.NumRanges() > 0 {
if r, ok := blackIpList.Lookup(ip); ok {
log.WebLogln("Block ip, in black list:", ip.String(), "in range", r.Description, ":", r.First, "-", r.Last)
c.String(http.StatusTeapot, "Banned")
c.Abort()
return
}
}
}
c.Next()
}
}
func scanBuf(buf []byte) Ranger {
if len(buf) == 0 {
return New(nil)
}
var ranges []Range
scanner := bufio.NewScanner(strings.NewReader(string(buf)))
for scanner.Scan() {
r, ok, err := parseLine(scanner.Bytes())
if err != nil {
log.TLogln("Error scan ip list:", err)
return New(nil)
}
if ok {
ranges = append(ranges, r)
}
}
err := scanner.Err()
if err != nil {
log.TLogln("Error scan ip list:", err)
}
if len(ranges) > 0 {
return New(ranges)
}
return New(nil)
}
func parseLine(l []byte) (r Range, ok bool, err error) {
l = bytes.TrimSpace(l)
if len(l) == 0 || bytes.HasPrefix(l, []byte("#")) {
return
}
colon := bytes.LastIndexAny(l, ":")
hyphen := bytes.IndexByte(l[colon+1:], '-')
hyphen += colon + 1
if colon >= 0 {
r.Description = string(l[:colon])
}
if hyphen-(colon+1) >= 0 {
r.First = net.ParseIP(string(l[colon+1 : hyphen]))
minifyIP(&r.First)
r.Last = net.ParseIP(string(l[hyphen+1:]))
minifyIP(&r.Last)
} else {
r.First = net.ParseIP(string(l[colon+1:]))
minifyIP(&r.First)
r.Last = r.First
}
if r.First == nil || r.Last == nil || len(r.First) != len(r.Last) {
err = errors.New("bad IP range")
return
}
ok = true
return
}

View File

@@ -0,0 +1,87 @@
package blocker
import (
"bytes"
"fmt"
"net"
)
type Ranger interface {
Lookup(net.IP) (r Range, ok bool)
NumRanges() int
}
type IPList struct {
ranges []Range
}
type Range struct {
First, Last net.IP
Description string
}
func (r Range) String() string {
return fmt.Sprintf("%s-%s: %s", r.First, r.Last, r.Description)
}
// Create a new IP list. The given ranges must already sorted by the lower
// bound IP in each range. Behaviour is undefined for lists of overlapping
// ranges.
func New(initSorted []Range) *IPList {
return &IPList{
ranges: initSorted,
}
}
func (ipl *IPList) NumRanges() int {
if ipl == nil {
return 0
}
return len(ipl.ranges)
}
// Return the range the given IP is in. ok if false if no range is found.
func (ipl *IPList) Lookup(ip net.IP) (r Range, ok bool) {
if ipl == nil {
return
}
v4 := ip.To4()
if v4 != nil {
r, ok = ipl.lookup(v4)
if ok {
return
}
}
v6 := ip.To16()
if v6 != nil {
return ipl.lookup(v6)
}
if v4 == nil && v6 == nil {
r = Range{
Description: "bad IP",
}
ok = true
}
return
}
// Return the range the given IP is in. Returns nil if no range is found.
func (ipl *IPList) lookup(ip net.IP) (Range, bool) {
var rng Range
var ok = false
for _, r := range ipl.ranges {
ok = bytes.Compare(r.First, ip) <= 0 && bytes.Compare(ip, r.Last) <= 0
if ok {
rng = r
break
}
}
return rng, ok
}
func minifyIP(ip *net.IP) {
v4 := ip.To4()
if v4 != nil {
*ip = append(make([]byte, 0, 4), v4...)
}
}