fix range block list

This commit is contained in:
YouROK
2021-04-28 16:06:05 +03:00
parent b96930d186
commit f6f1ddc952
2 changed files with 97 additions and 22 deletions

View File

@@ -13,15 +13,9 @@ import (
"server/log" "server/log"
"server/settings" "server/settings"
"github.com/anacrolix/torrent/iplist"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
type IPB struct {
Ip net.IP
Description string
}
func Blocker() gin.HandlerFunc { func Blocker() gin.HandlerFunc {
emptyFN := func(c *gin.Context) { emptyFN := func(c *gin.Context) {
c.Next() c.Next()
@@ -54,7 +48,7 @@ func Blocker() gin.HandlerFunc {
} }
if blackIpList.NumRanges() > 0 { if blackIpList.NumRanges() > 0 {
if r, ok := blackIpList.Lookup(ip); ok { if r, ok := blackIpList.Lookup(ip); ok {
log.WebLogln("Block ip, in black list:", ip.String(), "in range", r) log.WebLogln("Block ip, in black list:", ip.String(), "in range", r.Description, ":", r.First, "-", r.Last)
c.String(http.StatusTeapot, "Banned") c.String(http.StatusTeapot, "Banned")
c.Abort() c.Abort()
return return
@@ -65,17 +59,17 @@ func Blocker() gin.HandlerFunc {
} }
} }
func scanBuf(buf []byte) []IPB { func scanBuf(buf []byte) Ranger {
if len(buf) == 0 { if len(buf) == 0 {
return nil return New(nil)
} }
var ranges []IPB var ranges []Range
scanner := bufio.NewScanner(strings.NewReader(string(buf))) scanner := bufio.NewScanner(strings.NewReader(string(buf)))
for scanner.Scan() { for scanner.Scan() {
r, ok, err := parseLine(scanner.Bytes()) r, ok, err := parseLine(scanner.Bytes())
if err != nil { if err != nil {
log.TLogln("Error scan ip list:", err) log.TLogln("Error scan ip list:", err)
return nil return New(nil)
} }
if ok { if ok {
ranges = append(ranges, r) ranges = append(ranges, r)
@@ -86,18 +80,19 @@ func scanBuf(buf []byte) []IPB {
log.TLogln("Error scan ip list:", err) log.TLogln("Error scan ip list:", err)
} }
if len(ranges) > 0 { if len(ranges) > 0 {
return iplist.New(ranges) return New(ranges)
} }
return iplist.New(nil) return New(nil)
} }
func parseLine(l []byte) (r IPB, ok bool, err error) { func parseLine(l []byte) (r Range, ok bool, err error) {
l = bytes.TrimSpace(l) l = bytes.TrimSpace(l)
if len(l) == 0 || bytes.HasPrefix(l, []byte("#")) { if len(l) == 0 || bytes.HasPrefix(l, []byte("#")) {
return return
} }
colon := bytes.LastIndexAny(l, ":") colon := bytes.LastIndexAny(l, ":")
hyphen := bytes.IndexByte(l[colon+1:], '-')
hyphen += colon + 1
if colon >= 0 { if colon >= 0 {
r.Description = string(l[:colon]) r.Description = string(l[:colon])
} }
@@ -118,10 +113,3 @@ func parseLine(l []byte) (r IPB, ok bool, err error) {
ok = true ok = true
return return
} }
func minifyIP(ip *net.IP) {
v4 := ip.To4()
if v4 != nil {
*ip = append(make([]byte, 0, 4), v4...)
}
}

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...)
}
}