package isolation import ( "errors" "path/filepath" "strconv" "strings" "time" ) // Pure-FTPd's documented stats format: // epoch session username ip U|D bytes duration filename (last field unquoted). func parseFTPStats(line, username, month string, location *time.Location) (uint64, bool) { fields := strings.Fields(line) if len(fields) < 8 || fields[2] != username || (fields[4] != "U" && fields[4] != "D") { return 0, false } epoch, err := strconv.ParseInt(fields[0], 10, 64) if err != nil || epoch <= 0 || time.Unix(epoch, 0).In(location).Format("2006-01") != month { return 0, false } bytes, err := strconv.ParseUint(fields[5], 10, 64) return bytes, err == nil } func (a Agent) measureFTPBandwidth(username string, now time.Time) (uint64, error) { if !managedWebHostPattern.MatchString(username) { return 0, errors.New("invalid FTP usage identity") } trafficStateMu.Lock() defer trafficStateMu.Unlock() statePath := a.UsageStatePath if statePath == "" { statePath = "/var/lib/sea-vhost-agent/traffic.json" } if !filepath.IsAbs(statePath) { return 0, errors.New("invalid traffic state path") } state, err := loadTrafficState(statePath) if err != nil { return 0, err } key := "ftp:" + username cursor := state.Domains[key] month := now.Format("2006-01") if cursor.Month != month { cursor.Month = month cursor.Bytes = 0 } cursor, err = measureTransferLog(a.ftpTransferLogPath(), cursor, month, func(line string) (uint64, bool) { return parseFTPStats(line, username, month, now.Location()) }) if err != nil { return 0, err } state.Domains[key] = cursor if err = saveTrafficState(statePath, state); err != nil { return 0, err } return cursor.Bytes, nil }