utils.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. package types
  2. import (
  3. "bytes"
  4. "encoding/binary"
  5. "encoding/gob"
  6. "fmt"
  7. "net"
  8. log "github.com/sirupsen/logrus"
  9. "gopkg.in/natefinch/lumberjack.v2"
  10. )
  11. func IP2Int(ip net.IP) uint32 {
  12. if len(ip) == 16 {
  13. return binary.BigEndian.Uint32(ip[12:16])
  14. }
  15. return binary.BigEndian.Uint32(ip)
  16. }
  17. func Int2ip(nn uint32) net.IP {
  18. ip := make(net.IP, 4)
  19. binary.BigEndian.PutUint32(ip, nn)
  20. return ip
  21. }
  22. //Stolen from : https://github.com/llimllib/ipaddress/
  23. // Return the final address of a net range. Convert to IPv4 if possible,
  24. // otherwise return an ipv6
  25. func LastAddress(n *net.IPNet) net.IP {
  26. ip := n.IP.To4()
  27. if ip == nil {
  28. ip = n.IP
  29. return net.IP{
  30. ip[0] | ^n.Mask[0], ip[1] | ^n.Mask[1], ip[2] | ^n.Mask[2],
  31. ip[3] | ^n.Mask[3], ip[4] | ^n.Mask[4], ip[5] | ^n.Mask[5],
  32. ip[6] | ^n.Mask[6], ip[7] | ^n.Mask[7], ip[8] | ^n.Mask[8],
  33. ip[9] | ^n.Mask[9], ip[10] | ^n.Mask[10], ip[11] | ^n.Mask[11],
  34. ip[12] | ^n.Mask[12], ip[13] | ^n.Mask[13], ip[14] | ^n.Mask[14],
  35. ip[15] | ^n.Mask[15]}
  36. }
  37. return net.IPv4(
  38. ip[0]|^n.Mask[0],
  39. ip[1]|^n.Mask[1],
  40. ip[2]|^n.Mask[2],
  41. ip[3]|^n.Mask[3])
  42. }
  43. var logFormatter log.Formatter
  44. var LogOutput *lumberjack.Logger //io.Writer
  45. var logLevel log.Level
  46. var logReportCaller bool
  47. func SetDefaultLoggerConfig(cfgMode string, cfgFolder string, cfgLevel log.Level) error {
  48. /*Configure logs*/
  49. if cfgMode == "file" {
  50. LogOutput = &lumberjack.Logger{
  51. Filename: cfgFolder + "/crowdsec.log",
  52. MaxSize: 500, //megabytes
  53. MaxBackups: 3,
  54. MaxAge: 28, //days
  55. Compress: true, //disabled by default
  56. }
  57. log.SetOutput(LogOutput)
  58. } else if cfgMode != "stdout" {
  59. return fmt.Errorf("log mode '%s' unknown", cfgMode)
  60. }
  61. logLevel = cfgLevel
  62. log.SetLevel(logLevel)
  63. if logLevel >= log.InfoLevel {
  64. logFormatter = &log.TextFormatter{TimestampFormat: "02-01-2006 15:04:05", FullTimestamp: true}
  65. log.SetFormatter(logFormatter)
  66. }
  67. if logLevel >= log.DebugLevel {
  68. logReportCaller = true
  69. log.SetReportCaller(true)
  70. }
  71. return nil
  72. }
  73. func ConfigureLogger(clog *log.Logger) error {
  74. /*Configure logs*/
  75. if LogOutput != nil {
  76. clog.SetOutput(LogOutput)
  77. }
  78. if logReportCaller {
  79. clog.SetReportCaller(true)
  80. }
  81. if logFormatter != nil {
  82. clog.SetFormatter(logFormatter)
  83. }
  84. clog.SetLevel(logLevel)
  85. return nil
  86. }
  87. func Clone(a, b interface{}) error {
  88. buff := new(bytes.Buffer)
  89. enc := gob.NewEncoder(buff)
  90. dec := gob.NewDecoder(buff)
  91. if err := enc.Encode(a); err != nil {
  92. return fmt.Errorf("failed cloning %T", a)
  93. }
  94. if err := dec.Decode(b); err != nil {
  95. return fmt.Errorf("failed cloning %T", b)
  96. }
  97. return nil
  98. }