MCPcopy Create free account
hub / github.com/1Panel-dev/1Panel / FindRecentSuccessLoginNotInWhitelist

Function FindRecentSuccessLoginNotInWhitelist

agent/utils/alert/alert.go:453–490  ·  view source on GitHub ↗
(minutes int, whitelist []string)

Source from the content-addressed store, hash-verified

451}
452
453func FindRecentSuccessLoginNotInWhitelist(minutes int, whitelist []string) ([]string, error) {
454 lines, err := grepSSHLog([]string{"Accepted password", "Accepted publickey"})
455 if err != nil {
456 return nil, err
457 }
458
459 thresholdTime := time.Now().Add(-time.Duration(minutes) * time.Minute)
460 var abnormalLogins []string
461
462 whitelistMap := make(map[string]struct{}, len(whitelist))
463 for _, ip := range whitelist {
464 whitelistMap[ip] = struct{}{}
465 }
466
467 ipRegex := re.GetRegex(re.AlertIPPattern)
468
469 for _, line := range lines {
470 line = strings.TrimSpace(line)
471 if line == "" {
472 continue
473 }
474
475 t, err := parseLogTime(line)
476 if err != nil || t.Before(thresholdTime) {
477 continue
478 }
479
480 match := ipRegex.FindStringSubmatch(line)
481 if len(match) >= 2 {
482 ip := match[1]
483 if _, ok := whitelistMap[ip]; !ok {
484 abnormalLogins = append(abnormalLogins, fmt.Sprintf("%s-%s", ip, t.Format("2006-01-02 15:04:05")))
485 }
486 }
487 }
488
489 return abnormalLogins, nil
490}
491
492func findGrepPath() (string, error) {
493 path, err := exec.LookPath("grep")

Callers

nothing calls this directly

Calls 3

grepSSHLogFunction · 0.85
parseLogTimeFunction · 0.85
FormatMethod · 0.45

Tested by

no test coverage detected