| 1 |
// Copyright (c) 2014-2019 Ludovic Fauvet |
| 2 |
// Licensed under the MIT license |
| 3 |
|
| 4 |
package mirrors |
| 5 |
|
| 6 |
import ( |
| 7 |
"encoding/json" |
| 8 |
"fmt" |
| 9 |
"time" |
| 10 |
|
| 11 |
"github.com/etix/mirrorbits/core" |
| 12 |
"github.com/etix/mirrorbits/database" |
| 13 |
"github.com/gomodule/redigo/redis" |
| 14 |
"github.com/op/go-logging" |
| 15 |
) |
| 16 |
|
| 17 |
var ( |
| 18 |
log = logging.MustGetLogger("main") |
| 19 |
) |
| 20 |
|
| 21 |
type LogType uint |
| 22 |
|
| 23 |
const ( |
| 24 |
_ LogType = iota |
| 25 |
LOGTYPE_ERROR |
| 26 |
LOGTYPE_ADDED |
| 27 |
LOGTYPE_EDITED |
| 28 |
LOGTYPE_ENABLED |
| 29 |
LOGTYPE_DISABLED |
| 30 |
LOGTYPE_STATECHANGED |
| 31 |
LOGTYPE_SCANSTARTED |
| 32 |
LOGTYPE_SCANCOMPLETED |
| 33 |
) |
| 34 |
|
| 35 |
func typeToInstance(typ LogType) LogAction { |
| 36 |
switch LogType(typ) { |
| 37 |
case LOGTYPE_ERROR: |
| 38 |
return &LogError{} |
| 39 |
case LOGTYPE_ADDED: |
| 40 |
return &LogAdded{} |
| 41 |
case LOGTYPE_EDITED: |
| 42 |
return &LogEdited{} |
| 43 |
case LOGTYPE_ENABLED: |
| 44 |
return &LogEnabled{} |
| 45 |
case LOGTYPE_DISABLED: |
| 46 |
return &LogDisabled{} |
| 47 |
case LOGTYPE_STATECHANGED: |
| 48 |
return &LogStateChanged{} |
| 49 |
case LOGTYPE_SCANSTARTED: |
| 50 |
return &LogScanStarted{} |
| 51 |
case LOGTYPE_SCANCOMPLETED: |
| 52 |
return &LogScanCompleted{} |
| 53 |
default: |
| 54 |
} |
| 55 |
return nil |
| 56 |
} |
| 57 |
|
| 58 |
type LogAction interface { |
| 59 |
GetType() LogType |
| 60 |
GetMirrorID() int |
| 61 |
GetTimestamp() time.Time |
| 62 |
GetOutput() string |
| 63 |
} |
| 64 |
|
| 65 |
type LogCommonAction struct { |
| 66 |
Type LogType |
| 67 |
MirrorID int |
| 68 |
Timestamp time.Time |
| 69 |
} |
| 70 |
|
| 71 |
func (l LogCommonAction) GetType() LogType { |
| 72 |
return l.Type |
| 73 |
} |
| 74 |
|
| 75 |
func (l LogCommonAction) GetMirrorID() int { |
| 76 |
return l.MirrorID |
| 77 |
} |
| 78 |
|
| 79 |
func (l LogCommonAction) GetTimestamp() time.Time { |
| 80 |
return l.Timestamp |
| 81 |
} |
| 82 |
|
| 83 |
type LogError struct { |
| 84 |
LogCommonAction |
| 85 |
Err string |
| 86 |
} |
| 87 |
|
| 88 |
func (l *LogError) GetOutput() string { |
| 89 |
return fmt.Sprintf("Error: %s", l.Err) |
| 90 |
} |
| 91 |
|
| 92 |
func NewLogError(id int, err error) LogAction { |
| 93 |
return &LogError{ |
| 94 |
LogCommonAction: LogCommonAction{ |
| 95 |
Type: LOGTYPE_ERROR, |
| 96 |
MirrorID: id, |
| 97 |
Timestamp: time.Now(), |
| 98 |
}, |
| 99 |
Err: err.Error(), |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
type LogAdded struct { |
| 104 |
LogCommonAction |
| 105 |
} |
| 106 |
|
| 107 |
func (l *LogAdded) GetOutput() string { |
| 108 |
return "Mirror added" |
| 109 |
} |
| 110 |
|
| 111 |
func NewLogAdded(id int) LogAction { |
| 112 |
return &LogAdded{ |
| 113 |
LogCommonAction: LogCommonAction{ |
| 114 |
Type: LOGTYPE_ADDED, |
| 115 |
MirrorID: id, |
| 116 |
Timestamp: time.Now(), |
| 117 |
}, |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
type LogEdited struct { |
| 122 |
LogCommonAction |
| 123 |
} |
| 124 |
|
| 125 |
func (l *LogEdited) GetOutput() string { |
| 126 |
return "Mirror edited" |
| 127 |
} |
| 128 |
|
| 129 |
func NewLogEdited(id int) LogAction { |
| 130 |
return &LogEdited{ |
| 131 |
LogCommonAction: LogCommonAction{ |
| 132 |
Type: LOGTYPE_EDITED, |
| 133 |
MirrorID: id, |
| 134 |
Timestamp: time.Now(), |
| 135 |
}, |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
type LogEnabled struct { |
| 140 |
LogCommonAction |
| 141 |
} |
| 142 |
|
| 143 |
func (l *LogEnabled) GetOutput() string { |
| 144 |
return "Mirror enabled" |
| 145 |
} |
| 146 |
|
| 147 |
func NewLogEnabled(id int) LogAction { |
| 148 |
return &LogEnabled{ |
| 149 |
LogCommonAction: LogCommonAction{ |
| 150 |
Type: LOGTYPE_ENABLED, |
| 151 |
MirrorID: id, |
| 152 |
Timestamp: time.Now(), |
| 153 |
}, |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
type LogDisabled struct { |
| 158 |
LogCommonAction |
| 159 |
} |
| 160 |
|
| 161 |
func (l *LogDisabled) GetOutput() string { |
| 162 |
return "Mirror disabled" |
| 163 |
} |
| 164 |
|
| 165 |
func NewLogDisabled(id int) LogAction { |
| 166 |
return &LogDisabled{ |
| 167 |
LogCommonAction: LogCommonAction{ |
| 168 |
Type: LOGTYPE_DISABLED, |
| 169 |
MirrorID: id, |
| 170 |
Timestamp: time.Now(), |
| 171 |
}, |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
type LogStateChanged struct { |
| 176 |
LogCommonAction |
| 177 |
Proto Protocol |
| 178 |
Up bool |
| 179 |
Reason string |
| 180 |
} |
| 181 |
|
| 182 |
func (l *LogStateChanged) GetOutput() string { |
| 183 |
var mirror string |
| 184 |
|
| 185 |
switch l.Proto { |
| 186 |
case HTTP: |
| 187 |
mirror = "HTTP mirror" |
| 188 |
case HTTPS: |
| 189 |
mirror = "HTTPS mirror" |
| 190 |
default: |
| 191 |
mirror = "Mirror" |
| 192 |
} |
| 193 |
|
| 194 |
if l.Up == false { |
| 195 |
if len(l.Reason) == 0 { |
| 196 |
return mirror + " is down" |
| 197 |
} |
| 198 |
return mirror + " is down: " + l.Reason |
| 199 |
} |
| 200 |
|
| 201 |
return mirror + " is up" |
| 202 |
} |
| 203 |
|
| 204 |
func NewLogStateChanged(id int, proto Protocol, up bool, reason string) LogAction { |
| 205 |
return &LogStateChanged{ |
| 206 |
LogCommonAction: LogCommonAction{ |
| 207 |
Type: LOGTYPE_STATECHANGED, |
| 208 |
MirrorID: id, |
| 209 |
Timestamp: time.Now(), |
| 210 |
}, |
| 211 |
Proto: proto, |
| 212 |
Up: up, |
| 213 |
Reason: reason, |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
type LogScanStarted struct { |
| 218 |
LogCommonAction |
| 219 |
Typ core.ScannerType |
| 220 |
} |
| 221 |
|
| 222 |
func (l *LogScanStarted) GetOutput() string { |
| 223 |
switch l.Typ { |
| 224 |
case core.RSYNC: |
| 225 |
return "RSYNC scan started" |
| 226 |
case core.FTP: |
| 227 |
return "FTP scan started" |
| 228 |
default: |
| 229 |
return "Scan started using a unknown protocol" |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
func NewLogScanStarted(id int, typ core.ScannerType) LogAction { |
| 234 |
return &LogScanStarted{ |
| 235 |
LogCommonAction: LogCommonAction{ |
| 236 |
Type: LOGTYPE_SCANSTARTED, |
| 237 |
MirrorID: id, |
| 238 |
Timestamp: time.Now(), |
| 239 |
}, |
| 240 |
Typ: typ, |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
type LogScanCompleted struct { |
| 245 |
LogCommonAction |
| 246 |
FilesIndexed int64 |
| 247 |
KnownIndexed int64 |
| 248 |
Removed int64 |
| 249 |
TZOffset int64 |
| 250 |
} |
| 251 |
|
| 252 |
func (l *LogScanCompleted) GetOutput() string { |
| 253 |
output := fmt.Sprintf("Scan completed: %d files (%d known), %d removed", l.FilesIndexed, l.KnownIndexed, l.Removed) |
| 254 |
if l.TZOffset != 0 { |
| 255 |
offset, _ := time.ParseDuration(fmt.Sprintf("%dms", l.TZOffset)) |
| 256 |
output += fmt.Sprintf(" (corrected timezone offset: %s)", offset) |
| 257 |
} |
| 258 |
return output |
| 259 |
} |
| 260 |
|
| 261 |
func NewLogScanCompleted(id int, files, known, removed, tzoffset int64) LogAction { |
| 262 |
return &LogScanCompleted{ |
| 263 |
LogCommonAction: LogCommonAction{ |
| 264 |
Type: LOGTYPE_SCANCOMPLETED, |
| 265 |
MirrorID: id, |
| 266 |
Timestamp: time.Now(), |
| 267 |
}, |
| 268 |
FilesIndexed: files, |
| 269 |
KnownIndexed: known, |
| 270 |
Removed: removed, |
| 271 |
TZOffset: tzoffset, |
| 272 |
} |
| 273 |
} |
| 274 |
|
| 275 |
func PushLog(r *database.Redis, logAction LogAction) error { |
| 276 |
conn := r.Get() |
| 277 |
defer conn.Close() |
| 278 |
|
| 279 |
key := fmt.Sprintf("MIRRORLOGS_%d", logAction.GetMirrorID()) |
| 280 |
value, err := json.Marshal(logAction) |
| 281 |
if err != nil { |
| 282 |
return err |
| 283 |
} |
| 284 |
|
| 285 |
_, err = conn.Do("RPUSH", key, value) |
| 286 |
return err |
| 287 |
} |
| 288 |
|
| 289 |
func ReadLogs(r *database.Redis, mirrorid, max int) ([]string, error) { |
| 290 |
conn := r.Get() |
| 291 |
defer conn.Close() |
| 292 |
|
| 293 |
if max <= 0 { |
| 294 |
// Get the latest 500 events by default |
| 295 |
max = 500 |
| 296 |
} |
| 297 |
|
| 298 |
key := fmt.Sprintf("MIRRORLOGS_%d", mirrorid) |
| 299 |
lines, err := redis.Strings(conn.Do("LRANGE", key, max*-1, -1)) |
| 300 |
if err != nil { |
| 301 |
return nil, err |
| 302 |
} |
| 303 |
|
| 304 |
outputs := make([]string, 0, len(lines)) |
| 305 |
|
| 306 |
for _, line := range lines { |
| 307 |
var objmap map[string]any |
| 308 |
err = json.Unmarshal([]byte(line), &objmap) |
| 309 |
if err != nil { |
| 310 |
log.Warningf("Unable to parse mirror log line: %s", err) |
| 311 |
continue |
| 312 |
} |
| 313 |
|
| 314 |
typf, ok := objmap["Type"].(float64) |
| 315 |
if !ok { |
| 316 |
log.Warning("Unable to parse mirror log line") |
| 317 |
continue |
| 318 |
} |
| 319 |
|
| 320 |
// Truncate the received float64 back to int |
| 321 |
typ := int(typf) |
| 322 |
|
| 323 |
action := typeToInstance(LogType(typ)) |
| 324 |
if action == nil { |
| 325 |
log.Warning("Unknown mirror log action") |
| 326 |
continue |
| 327 |
} |
| 328 |
|
| 329 |
err = json.Unmarshal([]byte(line), action) |
| 330 |
if err != nil { |
| 331 |
log.Warningf("Unable to unmarshal mirror log line: %s", err) |
| 332 |
continue |
| 333 |
} |
| 334 |
|
| 335 |
line := fmt.Sprintf("%s: %s", action.GetTimestamp().Format("2006-01-02 15:04:05 MST"), action.GetOutput()) |
| 336 |
outputs = append(outputs, line) |
| 337 |
} |
| 338 |
|
| 339 |
return outputs, nil |
| 340 |
} |