|
| 1 | +package domain |
| 2 | + |
| 3 | +import ( |
| 4 | + "bufio" |
| 5 | + "compress/gzip" |
| 6 | + logger "github.com/sirupsen/logrus" |
| 7 | + "io" |
| 8 | + "os" |
| 9 | + "path/filepath" |
| 10 | + "regexp" |
| 11 | + "security-gateway/pkg/config" |
| 12 | + "strconv" |
| 13 | + "strings" |
| 14 | + "time" |
| 15 | +) |
| 16 | + |
| 17 | +type AccessLog struct { |
| 18 | + CustomIp string `json:"customIp,omitempty"` |
| 19 | + Domain string `json:"domain,omitempty"` |
| 20 | + MaskingLevel int `json:"maskingLevel,omitempty"` |
| 21 | + Path string `json:"path,omitempty"` |
| 22 | + Port uint16 `json:"port,omitempty"` |
| 23 | + TargetUrl string `json:"targetUrl,omitempty"` |
| 24 | + Username string `json:"username,omitempty"` |
| 25 | + StartTime int64 `json:"startTime,omitempty"` |
| 26 | + EndTime int64 `json:"endTime,omitempty"` |
| 27 | +} |
| 28 | + |
| 29 | +var TraceLogPattern = regexp.MustCompile(`time="?([^"]+)"?\s+level=info\s+msg="requesting record"\s+customIp="?([^"]+)"?\s+domain="?([^"]*)"?\s+maskingLevel=(\d?)\s+path="?([^"]*)"?\s+port=(\d+)\s+target="?([^"]*)"?\s+username="?([^"]*)"?`) |
| 30 | + |
| 31 | +func CountDefaultFileLogs(startTime, endTime time.Time, condition *AccessLog) (count int, earliestTime, latestTime time.Time, err error) { |
| 32 | + loggerDir := config.GetString("logger.dir") |
| 33 | + if loggerDir == "" { |
| 34 | + loggerDir = "logs" |
| 35 | + } |
| 36 | + p, _ := filepath.Abs(loggerDir) |
| 37 | + |
| 38 | + lfPath := filepath.Join(p, "info.log") |
| 39 | + f, err := os.Open(lfPath) |
| 40 | + if err != nil { |
| 41 | + return 0, time.Time{}, time.Time{}, err |
| 42 | + } |
| 43 | + defer func(f *os.File) { |
| 44 | + _ = f.Close() |
| 45 | + }(f) |
| 46 | + |
| 47 | + return CountLogsFromFile(f, startTime, endTime, condition) |
| 48 | +} |
| 49 | + |
| 50 | +func CountLogsFromFile(f *os.File, startTime, endTime time.Time, condition *AccessLog) (count int, earliestTime, latestTime time.Time, err error) { |
| 51 | + return CountLogsFromReader(f, startTime, endTime, condition) |
| 52 | +} |
| 53 | + |
| 54 | +func CountLogsFromGzFileName(gzFileName string, startTime, endTime time.Time, condition *AccessLog) (count int, earliestTime, latestTime time.Time, err error) { |
| 55 | + gzFileName = filepath.Base(gzFileName) |
| 56 | + var gz *os.File |
| 57 | + gz, err = os.Open(gzFileName) |
| 58 | + if err != nil { |
| 59 | + return |
| 60 | + } |
| 61 | + defer func(gz *os.File) { |
| 62 | + _ = gz.Close() |
| 63 | + }(gz) |
| 64 | + |
| 65 | + return CountLogsFromGzFile(gz, startTime, endTime, condition) |
| 66 | +} |
| 67 | +func CountLogsFromGzFile(gzFile *os.File, startTime, endTime time.Time, condition *AccessLog) (count int, earliestTime, latestTime time.Time, err error) { |
| 68 | + gz, err := gzip.NewReader(gzFile) |
| 69 | + if err != nil { |
| 70 | + return 0, time.Time{}, time.Time{}, err |
| 71 | + } |
| 72 | + defer func(gz *gzip.Reader) { |
| 73 | + err = gz.Close() |
| 74 | + if err != nil { |
| 75 | + logger.Error(err) |
| 76 | + } |
| 77 | + }(gz) |
| 78 | + |
| 79 | + return CountLogsFromReader(gz, startTime, endTime, condition) |
| 80 | +} |
| 81 | + |
| 82 | +func CountLogsFromReader(r io.Reader, startTime, endTime time.Time, condition *AccessLog) (count int, earliestTime, latestTime time.Time, err error) { |
| 83 | + count = 0 |
| 84 | + // 记录最早的时间和最晚的时间 |
| 85 | + scanner := bufio.NewScanner(r) |
| 86 | + for scanner.Scan() { |
| 87 | + line := scanner.Text() |
| 88 | + matches := TraceLogPattern.FindStringSubmatch(line) |
| 89 | + if len(matches) > 0 { |
| 90 | + logTime, err := time.ParseInLocation("2006-01-02 15:04:05", matches[1], time.Local) |
| 91 | + if err != nil { |
| 92 | + continue |
| 93 | + } |
| 94 | + |
| 95 | + if earliestTime.IsZero() || logTime.Before(earliestTime) { |
| 96 | + earliestTime = logTime |
| 97 | + } |
| 98 | + |
| 99 | + if latestTime.IsZero() || logTime.After(latestTime) { |
| 100 | + latestTime = logTime |
| 101 | + } |
| 102 | + |
| 103 | + if logTime.After(startTime) && logTime.Before(endTime) { |
| 104 | + if condition != nil { |
| 105 | + if condition.CustomIp != "" && condition.CustomIp != matches[2] { |
| 106 | + continue |
| 107 | + } |
| 108 | + if condition.Domain != "" && condition.Domain != matches[3] { |
| 109 | + continue |
| 110 | + } |
| 111 | + if condition.MaskingLevel != 0 { |
| 112 | + if matches[4] == "" || condition.MaskingLevel != int(matches[4][0]-48) { |
| 113 | + continue |
| 114 | + } |
| 115 | + } |
| 116 | + if condition.Path != "" && condition.Path != matches[5] { |
| 117 | + continue |
| 118 | + } |
| 119 | + if condition.Port != 0 && matches[6] != strconv.FormatInt(int64(condition.Port), 10) { |
| 120 | + continue |
| 121 | + } |
| 122 | + if condition.TargetUrl != "" && condition.TargetUrl != matches[7] { |
| 123 | + continue |
| 124 | + } |
| 125 | + if condition.Username != "" && condition.Username != matches[8] { |
| 126 | + continue |
| 127 | + } |
| 128 | + } |
| 129 | + count++ |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + if err = scanner.Err(); err != nil { |
| 135 | + return 0, time.Time{}, time.Time{}, err |
| 136 | + } |
| 137 | + |
| 138 | + return |
| 139 | +} |
| 140 | + |
| 141 | +func ProxyTraceLogPath() string { |
| 142 | + loggerDir := config.GetString("logger.dir") |
| 143 | + if loggerDir == "" { |
| 144 | + loggerDir = "logs" |
| 145 | + } |
| 146 | + p, _ := filepath.Abs(loggerDir) |
| 147 | + p = filepath.Join(p, "proxy_trace") |
| 148 | + return p |
| 149 | +} |
| 150 | + |
| 151 | +func CurrentProxyTraceLogPath() string { |
| 152 | + return filepath.Join(ProxyTraceLogPath(), "info.log") |
| 153 | +} |
| 154 | + |
| 155 | +// NextProxyTraceLogGzPath 获取下个代理跟踪日志文件的路径 |
| 156 | +func NextProxyTraceLogGzPath(preFileName string) string { |
| 157 | + var latestTime time.Time |
| 158 | + |
| 159 | + if !strings.HasSuffix(preFileName, ".log") { |
| 160 | + t, err := time.Parse("2006-01-02T15-04-05.999.log.gz", preFileName[5:len(preFileName)-7]) |
| 161 | + if err != nil { |
| 162 | + logger.Error(err) |
| 163 | + return "" |
| 164 | + } |
| 165 | + latestTime = t |
| 166 | + } |
| 167 | + |
| 168 | + dir := ProxyTraceLogPath() |
| 169 | + files, err := filepath.Glob(filepath.Join(dir, "*.log.gz")) |
| 170 | + if err != nil { |
| 171 | + logger.Error(err) |
| 172 | + return "" |
| 173 | + } |
| 174 | + if len(files) == 0 { |
| 175 | + return "" |
| 176 | + } |
| 177 | + // 找到最新的一个gz文件,文件命名规则:info-2024-07-01T08-58-56.164.log.gz |
| 178 | + var latestFile string |
| 179 | + for _, file := range files { |
| 180 | + fileName := filepath.Base(file) |
| 181 | + if !strings.HasPrefix(fileName, "info-") || !strings.HasSuffix(fileName, ".log.gz") { |
| 182 | + continue |
| 183 | + } |
| 184 | + t, err := time.Parse("2006-01-02T15-04-05.999.log.gz", fileName[5:len(fileName)-7]) |
| 185 | + if err != nil { |
| 186 | + logger.Error(err) |
| 187 | + continue |
| 188 | + } |
| 189 | + if t.After(latestTime) { |
| 190 | + latestTime = t |
| 191 | + latestFile = file |
| 192 | + } |
| 193 | + } |
| 194 | + |
| 195 | + return latestFile |
| 196 | +} |
| 197 | + |
| 198 | +func CountProxyTraceLogs(startTime, endTime time.Time, condition *AccessLog) (count int, err error) { |
| 199 | + fileName := CurrentProxyTraceLogPath() |
| 200 | + var lf *os.File |
| 201 | + lf, err = os.Open(fileName) |
| 202 | + if err != nil { |
| 203 | + return 0, err |
| 204 | + } |
| 205 | + defer func(lf *os.File) { |
| 206 | + _ = lf.Close() |
| 207 | + }(lf) |
| 208 | + |
| 209 | + var earliestTime, latestTime time.Time |
| 210 | + |
| 211 | + count, earliestTime, latestTime, err = CountLogsFromFile(lf, startTime, endTime, condition) |
| 212 | + if err != nil { |
| 213 | + return |
| 214 | + } |
| 215 | + |
| 216 | + for !earliestTime.IsZero() && !latestTime.IsZero() && earliestTime.After(startTime) { |
| 217 | + fileName = NextProxyTraceLogGzPath(filepath.Base(fileName)) |
| 218 | + if fileName == "" { |
| 219 | + break |
| 220 | + } |
| 221 | + gzFileName := filepath.Base(fileName) |
| 222 | + |
| 223 | + var c int |
| 224 | + c, earliestTime, latestTime, err = CountLogsFromGzFileName(gzFileName, startTime, endTime, condition) |
| 225 | + if err != nil { |
| 226 | + return |
| 227 | + } |
| 228 | + count += c |
| 229 | + } |
| 230 | + |
| 231 | + return |
| 232 | + |
| 233 | +} |
0 commit comments