95 lines
2.2 KiB
Go
95 lines
2.2 KiB
Go
package types
|
|
|
|
import (
|
|
"encoding/binary"
|
|
"fmt"
|
|
"io"
|
|
"net"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
"gopkg.in/natefinch/lumberjack.v2"
|
|
)
|
|
|
|
func IP2Int(ip net.IP) uint32 {
|
|
if len(ip) == 16 {
|
|
return binary.BigEndian.Uint32(ip[12:16])
|
|
}
|
|
return binary.BigEndian.Uint32(ip)
|
|
}
|
|
|
|
func Int2ip(nn uint32) net.IP {
|
|
ip := make(net.IP, 4)
|
|
binary.BigEndian.PutUint32(ip, nn)
|
|
return ip
|
|
}
|
|
|
|
//Stolen from : https://github.com/llimllib/ipaddress/
|
|
// Return the final address of a net range. Convert to IPv4 if possible,
|
|
// otherwise return an ipv6
|
|
func LastAddress(n *net.IPNet) net.IP {
|
|
ip := n.IP.To4()
|
|
if ip == nil {
|
|
ip = n.IP
|
|
return net.IP{
|
|
ip[0] | ^n.Mask[0], ip[1] | ^n.Mask[1], ip[2] | ^n.Mask[2],
|
|
ip[3] | ^n.Mask[3], ip[4] | ^n.Mask[4], ip[5] | ^n.Mask[5],
|
|
ip[6] | ^n.Mask[6], ip[7] | ^n.Mask[7], ip[8] | ^n.Mask[8],
|
|
ip[9] | ^n.Mask[9], ip[10] | ^n.Mask[10], ip[11] | ^n.Mask[11],
|
|
ip[12] | ^n.Mask[12], ip[13] | ^n.Mask[13], ip[14] | ^n.Mask[14],
|
|
ip[15] | ^n.Mask[15]}
|
|
}
|
|
|
|
return net.IPv4(
|
|
ip[0]|^n.Mask[0],
|
|
ip[1]|^n.Mask[1],
|
|
ip[2]|^n.Mask[2],
|
|
ip[3]|^n.Mask[3])
|
|
}
|
|
|
|
var logFormatter log.Formatter
|
|
var logOutput io.Writer
|
|
var logLevel log.Level
|
|
var logReportCaller bool
|
|
|
|
func SetDefaultLoggerConfig(cfgMode string, cfgFolder string, cfgLevel log.Level) error {
|
|
|
|
/*Configure logs*/
|
|
if cfgMode == "file" {
|
|
logOutput = &lumberjack.Logger{
|
|
Filename: cfgFolder + "/crowdsec.log",
|
|
MaxSize: 500, //megabytes
|
|
MaxBackups: 3,
|
|
MaxAge: 28, //days
|
|
Compress: true, //disabled by default
|
|
}
|
|
log.SetOutput(logOutput)
|
|
} else if cfgMode != "stdout" {
|
|
return fmt.Errorf("log mode '%s' unknown", cfgMode)
|
|
}
|
|
logLevel = cfgLevel
|
|
log.SetLevel(logLevel)
|
|
if logLevel >= log.InfoLevel {
|
|
logFormatter = &log.TextFormatter{TimestampFormat: "02-01-2006 15:04:05", FullTimestamp: true}
|
|
log.SetFormatter(logFormatter)
|
|
}
|
|
if logLevel >= log.DebugLevel {
|
|
logReportCaller = true
|
|
log.SetReportCaller(true)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func ConfigureLogger(clog *log.Logger) error {
|
|
/*Configure logs*/
|
|
if logOutput != nil {
|
|
clog.SetOutput(logOutput)
|
|
}
|
|
if logReportCaller {
|
|
clog.SetReportCaller(true)
|
|
}
|
|
if logFormatter != nil {
|
|
clog.SetFormatter(logFormatter)
|
|
}
|
|
clog.SetLevel(logLevel)
|
|
return nil
|
|
}
|