2019-07-12 21:32:49 +02:00
|
|
|
package ipManager
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/asaskevich/govalidator"
|
2019-07-15 22:16:02 +02:00
|
|
|
"github.com/lbryio/ytsync/util"
|
2019-07-12 21:32:49 +02:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
|
|
|
"github.com/lbryio/lbry.go/extras/errors"
|
|
|
|
"github.com/lbryio/lbry.go/extras/stop"
|
|
|
|
|
|
|
|
"net"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2019-07-15 22:16:02 +02:00
|
|
|
const IPCooldownPeriod = 25 * time.Second
|
2019-07-12 21:32:49 +02:00
|
|
|
const unbanTimeout = 3 * time.Hour
|
|
|
|
|
|
|
|
var ipv6Pool []string
|
|
|
|
var ipv4Pool []string
|
|
|
|
var throttledIPs map[string]bool
|
2019-07-12 23:54:48 +02:00
|
|
|
var ipInUse map[string]bool
|
2019-07-12 21:32:49 +02:00
|
|
|
var ipLastUsed map[string]time.Time
|
|
|
|
var ipMutex sync.Mutex
|
|
|
|
var stopper = stop.New()
|
|
|
|
|
|
|
|
func GetNextIP(ipv6 bool) (string, error) {
|
|
|
|
ipMutex.Lock()
|
|
|
|
defer ipMutex.Unlock()
|
|
|
|
if len(ipv4Pool) < 1 || len(ipv6Pool) < 1 {
|
2019-07-12 22:58:34 +02:00
|
|
|
throttledIPs = make(map[string]bool)
|
2019-07-12 23:54:48 +02:00
|
|
|
ipInUse = make(map[string]bool)
|
2019-07-12 22:58:34 +02:00
|
|
|
ipLastUsed = make(map[string]time.Time)
|
2019-07-12 21:32:49 +02:00
|
|
|
addrs, err := net.InterfaceAddrs()
|
|
|
|
if err != nil {
|
|
|
|
return "", errors.Err(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, address := range addrs {
|
|
|
|
if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
|
|
|
|
if ipnet.IP.To16() != nil && govalidator.IsIPv6(ipnet.IP.String()) {
|
|
|
|
ipv6Pool = append(ipv6Pool, ipnet.IP.String())
|
2019-07-12 23:54:48 +02:00
|
|
|
ipLastUsed[ipnet.IP.String()] = time.Now().Add(-IPCooldownPeriod)
|
2019-07-12 21:32:49 +02:00
|
|
|
} else if ipnet.IP.To4() != nil && govalidator.IsIPv4(ipnet.IP.String()) {
|
|
|
|
ipv4Pool = append(ipv4Pool, ipnet.IP.String())
|
2019-07-12 23:54:48 +02:00
|
|
|
ipLastUsed[ipnet.IP.String()] = time.Now().Add(-IPCooldownPeriod)
|
2019-07-12 21:32:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
nextIP := ""
|
|
|
|
if ipv6 {
|
|
|
|
nextIP = getLeastUsedIP(ipv6Pool)
|
|
|
|
} else {
|
|
|
|
nextIP = getLeastUsedIP(ipv4Pool)
|
|
|
|
}
|
|
|
|
if nextIP == "" {
|
|
|
|
return "throttled", errors.Err("all IPs are throttled")
|
|
|
|
}
|
|
|
|
lastUse := ipLastUsed[nextIP]
|
|
|
|
if time.Since(lastUse) < IPCooldownPeriod {
|
2019-07-12 23:54:48 +02:00
|
|
|
log.Debugf("The IP %s is too hot, waiting for %.1f seconds before continuing", nextIP, (IPCooldownPeriod - time.Since(lastUse)).Seconds())
|
2019-07-12 21:32:49 +02:00
|
|
|
time.Sleep(IPCooldownPeriod - time.Since(lastUse))
|
|
|
|
}
|
|
|
|
|
2019-07-12 23:54:48 +02:00
|
|
|
ipInUse[nextIP] = true
|
2019-07-12 21:32:49 +02:00
|
|
|
return nextIP, nil
|
|
|
|
}
|
|
|
|
|
2019-07-13 11:33:40 +02:00
|
|
|
func ReleaseIP(ip string) {
|
2019-07-12 23:54:48 +02:00
|
|
|
ipMutex.Lock()
|
|
|
|
defer ipMutex.Unlock()
|
|
|
|
ipLastUsed[ip] = time.Now()
|
|
|
|
ipInUse[ip] = false
|
|
|
|
}
|
2019-07-13 11:33:40 +02:00
|
|
|
|
2019-07-12 21:32:49 +02:00
|
|
|
func getLeastUsedIP(ipPool []string) string {
|
|
|
|
nextIP := ""
|
|
|
|
veryLastUse := time.Now()
|
|
|
|
for _, ip := range ipPool {
|
2019-07-12 23:54:48 +02:00
|
|
|
isThrottled := throttledIPs[ip]
|
2019-07-12 21:32:49 +02:00
|
|
|
if isThrottled {
|
|
|
|
continue
|
|
|
|
}
|
2019-07-12 23:54:48 +02:00
|
|
|
inUse := ipInUse[ip]
|
|
|
|
if inUse {
|
|
|
|
continue
|
|
|
|
}
|
2019-07-12 21:32:49 +02:00
|
|
|
lastUse := ipLastUsed[ip]
|
|
|
|
if lastUse.Before(veryLastUse) {
|
|
|
|
nextIP = ip
|
|
|
|
veryLastUse = lastUse
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nextIP
|
|
|
|
}
|
|
|
|
|
|
|
|
func SetIpThrottled(ip string, stopGrp *stop.Group) {
|
|
|
|
ipMutex.Lock()
|
2019-07-12 23:54:48 +02:00
|
|
|
isThrottled := throttledIPs[ip]
|
2019-07-12 21:32:49 +02:00
|
|
|
if isThrottled {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
throttledIPs[ip] = true
|
2019-07-15 22:16:02 +02:00
|
|
|
ipMutex.Unlock()
|
|
|
|
util.SendErrorToSlack("%s set to throttled", ip)
|
2019-07-12 21:32:49 +02:00
|
|
|
|
|
|
|
stopper.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer stopper.Done()
|
|
|
|
unbanTimer := time.NewTimer(unbanTimeout)
|
2019-07-12 22:58:34 +02:00
|
|
|
select {
|
|
|
|
case <-unbanTimer.C:
|
2019-07-15 22:16:02 +02:00
|
|
|
ipMutex.Lock()
|
2019-07-12 22:58:34 +02:00
|
|
|
throttledIPs[ip] = false
|
2019-07-15 22:16:02 +02:00
|
|
|
ipMutex.Unlock()
|
|
|
|
util.SendInfoToSlack("%s set back to not throttled", ip)
|
2019-07-12 22:58:34 +02:00
|
|
|
case <-stopGrp.Ch():
|
|
|
|
unbanTimer.Stop()
|
2019-07-12 21:32:49 +02:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|