mirror of
https://github.com/SunBK201/UA3F.git
synced 2025-12-16 16:57:08 +00:00
feat: introduce dumpWriter for record
This commit is contained in:
parent
1558b8a28b
commit
f67658ae86
@ -18,6 +18,7 @@ type ConnectionRecordList struct {
|
|||||||
records map[string]*ConnectionRecord
|
records map[string]*ConnectionRecord
|
||||||
mu sync.RWMutex
|
mu sync.RWMutex
|
||||||
dumpFile string
|
dumpFile string
|
||||||
|
dumpWriter *bufio.Writer
|
||||||
}
|
}
|
||||||
|
|
||||||
type ConnectionRecord struct {
|
type ConnectionRecord struct {
|
||||||
@ -34,6 +35,7 @@ func NewConnectionRecordList(dumpFile string) *ConnectionRecordList {
|
|||||||
records: make(map[string]*ConnectionRecord, 500),
|
records: make(map[string]*ConnectionRecord, 500),
|
||||||
mu: sync.RWMutex{},
|
mu: sync.RWMutex{},
|
||||||
dumpFile: dumpFile,
|
dumpFile: dumpFile,
|
||||||
|
dumpWriter: bufio.NewWriter(nil),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -108,9 +110,9 @@ func (l *ConnectionRecordList) Dump() {
|
|||||||
return records[i].StartTime.After(records[j].StartTime)
|
return records[i].StartTime.After(records[j].StartTime)
|
||||||
})
|
})
|
||||||
|
|
||||||
w := bufio.NewWriter(f)
|
l.dumpWriter.Reset(f)
|
||||||
defer func() {
|
defer func() {
|
||||||
if err := w.Flush(); err != nil {
|
if err := l.dumpWriter.Flush(); err != nil {
|
||||||
slog.Error("bufio.Writer.Flush", slog.Any("error", err))
|
slog.Error("bufio.Writer.Flush", slog.Any("error", err))
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
@ -118,7 +120,7 @@ func (l *ConnectionRecordList) Dump() {
|
|||||||
now := time.Now()
|
now := time.Now()
|
||||||
for _, record := range records {
|
for _, record := range records {
|
||||||
duration := now.Sub(record.StartTime)
|
duration := now.Sub(record.StartTime)
|
||||||
_, err := fmt.Fprintf(w, "%s %s %s %d\n",
|
_, err := fmt.Fprintf(l.dumpWriter, "%s %s %s %d\n",
|
||||||
record.Protocol, record.SrcAddr, record.DestAddr, int(duration.Seconds()))
|
record.Protocol, record.SrcAddr, record.DestAddr, int(duration.Seconds()))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
slog.Error("Dump fmt.Fprintf", slog.Any("error", err))
|
slog.Error("Dump fmt.Fprintf", slog.Any("error", err))
|
||||||
|
|||||||
@ -16,6 +16,7 @@ type PassThroughRecordList struct {
|
|||||||
records map[string]*PassThroughRecord
|
records map[string]*PassThroughRecord
|
||||||
mu sync.RWMutex
|
mu sync.RWMutex
|
||||||
dumpFile string
|
dumpFile string
|
||||||
|
dumpWriter *bufio.Writer
|
||||||
}
|
}
|
||||||
|
|
||||||
type PassThroughRecord struct {
|
type PassThroughRecord struct {
|
||||||
@ -31,6 +32,7 @@ func NewPassThroughRecordList(dumpFile string) *PassThroughRecordList {
|
|||||||
records: make(map[string]*PassThroughRecord, 100),
|
records: make(map[string]*PassThroughRecord, 100),
|
||||||
mu: sync.RWMutex{},
|
mu: sync.RWMutex{},
|
||||||
dumpFile: dumpFile,
|
dumpFile: dumpFile,
|
||||||
|
dumpWriter: bufio.NewWriter(nil),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -95,15 +97,15 @@ func (l *PassThroughRecordList) Dump() {
|
|||||||
return records[i].Count > records[j].Count
|
return records[i].Count > records[j].Count
|
||||||
})
|
})
|
||||||
|
|
||||||
w := bufio.NewWriter(f)
|
l.dumpWriter.Reset(f)
|
||||||
defer func() {
|
defer func() {
|
||||||
if err := w.Flush(); err != nil {
|
if err := l.dumpWriter.Flush(); err != nil {
|
||||||
slog.Error("bufio.Writer.Flush", slog.Any("error", err))
|
slog.Error("bufio.Writer.Flush", slog.Any("error", err))
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
for _, record := range records {
|
for _, record := range records {
|
||||||
_, err := fmt.Fprintf(w, "%s %s %d %s\n",
|
_, err := fmt.Fprintf(l.dumpWriter, "%s %s %d %s\n",
|
||||||
record.SrcAddr, record.DestAddr, record.Count, record.UA)
|
record.SrcAddr, record.DestAddr, record.Count, record.UA)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
slog.Error("Dump fmt.Fprintf", slog.Any("error", err))
|
slog.Error("Dump fmt.Fprintf", slog.Any("error", err))
|
||||||
|
|||||||
@ -15,6 +15,7 @@ type RewriteRecordList struct {
|
|||||||
records map[string]*RewriteRecord
|
records map[string]*RewriteRecord
|
||||||
mu sync.RWMutex
|
mu sync.RWMutex
|
||||||
dumpFile string
|
dumpFile string
|
||||||
|
dumpWriter *bufio.Writer
|
||||||
}
|
}
|
||||||
|
|
||||||
type RewriteRecord struct {
|
type RewriteRecord struct {
|
||||||
@ -30,6 +31,7 @@ func NewRewriteRecordList(dumpFile string) *RewriteRecordList {
|
|||||||
records: make(map[string]*RewriteRecord, 100),
|
records: make(map[string]*RewriteRecord, 100),
|
||||||
mu: sync.RWMutex{},
|
mu: sync.RWMutex{},
|
||||||
dumpFile: dumpFile,
|
dumpFile: dumpFile,
|
||||||
|
dumpWriter: bufio.NewWriter(nil),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -90,15 +92,15 @@ func (l *RewriteRecordList) Dump() {
|
|||||||
return records[i].Count > records[j].Count
|
return records[i].Count > records[j].Count
|
||||||
})
|
})
|
||||||
|
|
||||||
w := bufio.NewWriter(f)
|
l.dumpWriter.Reset(f)
|
||||||
defer func() {
|
defer func() {
|
||||||
if err := w.Flush(); err != nil {
|
if err := l.dumpWriter.Flush(); err != nil {
|
||||||
slog.Error("bufio.Writer.Flush", slog.Any("error", err))
|
slog.Error("bufio.Writer.Flush", slog.Any("error", err))
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
for _, record := range records {
|
for _, record := range records {
|
||||||
_, err := fmt.Fprintf(w, "%s %d %sSEQSEQ%s\n",
|
_, err := fmt.Fprintf(l.dumpWriter, "%s %d %sSEQSEQ%s\n",
|
||||||
record.Host, record.Count, record.OriginalUA, record.MockedUA)
|
record.Host, record.Count, record.OriginalUA, record.MockedUA)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
slog.Error("Dump fmt.Fprintf", slog.Any("error", err))
|
slog.Error("Dump fmt.Fprintf", slog.Any("error", err))
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user