mirror of
https://github.com/SunBK201/UA3F.git
synced 2025-12-16 16:57:08 +00:00
feat: reuse dump records
This commit is contained in:
parent
9de776f21b
commit
241cc6a743
@ -17,8 +17,10 @@ type ConnectionRecordList struct {
|
|||||||
recordRemoveChan chan *ConnectionRecord
|
recordRemoveChan chan *ConnectionRecord
|
||||||
records map[string]*ConnectionRecord
|
records map[string]*ConnectionRecord
|
||||||
mu sync.RWMutex
|
mu sync.RWMutex
|
||||||
dumpFile string
|
|
||||||
dumpWriter *bufio.Writer
|
dumpRecords []*ConnectionRecord
|
||||||
|
dumpFile string
|
||||||
|
dumpWriter *bufio.Writer
|
||||||
}
|
}
|
||||||
|
|
||||||
type ConnectionRecord struct {
|
type ConnectionRecord struct {
|
||||||
@ -34,6 +36,7 @@ func NewConnectionRecordList(dumpFile string) *ConnectionRecordList {
|
|||||||
recordRemoveChan: make(chan *ConnectionRecord, 100),
|
recordRemoveChan: make(chan *ConnectionRecord, 100),
|
||||||
records: make(map[string]*ConnectionRecord, 500),
|
records: make(map[string]*ConnectionRecord, 500),
|
||||||
mu: sync.RWMutex{},
|
mu: sync.RWMutex{},
|
||||||
|
dumpRecords: make([]*ConnectionRecord, 0, 500),
|
||||||
dumpFile: dumpFile,
|
dumpFile: dumpFile,
|
||||||
dumpWriter: bufio.NewWriter(nil),
|
dumpWriter: bufio.NewWriter(nil),
|
||||||
}
|
}
|
||||||
@ -98,16 +101,16 @@ func (l *ConnectionRecordList) Dump() {
|
|||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
l.dumpRecords = l.dumpRecords[:0]
|
||||||
l.mu.RLock()
|
l.mu.RLock()
|
||||||
records := make([]*ConnectionRecord, 0, len(l.records))
|
|
||||||
for _, r := range l.records {
|
for _, r := range l.records {
|
||||||
records = append(records, r)
|
l.dumpRecords = append(l.dumpRecords, r)
|
||||||
}
|
}
|
||||||
l.mu.RUnlock()
|
l.mu.RUnlock()
|
||||||
|
|
||||||
// Sort by start time (newest first)
|
// Sort by start time (newest first)
|
||||||
sort.SliceStable(records, func(i, j int) bool {
|
sort.SliceStable(l.dumpRecords, func(i, j int) bool {
|
||||||
return records[i].StartTime.After(records[j].StartTime)
|
return l.dumpRecords[i].StartTime.After(l.dumpRecords[j].StartTime)
|
||||||
})
|
})
|
||||||
|
|
||||||
l.dumpWriter.Reset(f)
|
l.dumpWriter.Reset(f)
|
||||||
@ -118,7 +121,7 @@ func (l *ConnectionRecordList) Dump() {
|
|||||||
}()
|
}()
|
||||||
|
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
for _, record := range records {
|
for _, record := range l.dumpRecords {
|
||||||
duration := now.Sub(record.StartTime)
|
duration := now.Sub(record.StartTime)
|
||||||
_, err := fmt.Fprintf(l.dumpWriter, "%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()))
|
||||||
|
|||||||
@ -15,8 +15,10 @@ type PassThroughRecordList struct {
|
|||||||
recordAddChan chan *PassThroughRecord
|
recordAddChan chan *PassThroughRecord
|
||||||
records map[string]*PassThroughRecord
|
records map[string]*PassThroughRecord
|
||||||
mu sync.RWMutex
|
mu sync.RWMutex
|
||||||
dumpFile string
|
|
||||||
dumpWriter *bufio.Writer
|
dumpRecords []*PassThroughRecord
|
||||||
|
dumpFile string
|
||||||
|
dumpWriter *bufio.Writer
|
||||||
}
|
}
|
||||||
|
|
||||||
type PassThroughRecord struct {
|
type PassThroughRecord struct {
|
||||||
@ -31,6 +33,7 @@ func NewPassThroughRecordList(dumpFile string) *PassThroughRecordList {
|
|||||||
recordAddChan: make(chan *PassThroughRecord, 100),
|
recordAddChan: make(chan *PassThroughRecord, 100),
|
||||||
records: make(map[string]*PassThroughRecord, 100),
|
records: make(map[string]*PassThroughRecord, 100),
|
||||||
mu: sync.RWMutex{},
|
mu: sync.RWMutex{},
|
||||||
|
dumpRecords: make([]*PassThroughRecord, 0, 100),
|
||||||
dumpFile: dumpFile,
|
dumpFile: dumpFile,
|
||||||
dumpWriter: bufio.NewWriter(nil),
|
dumpWriter: bufio.NewWriter(nil),
|
||||||
}
|
}
|
||||||
@ -86,15 +89,15 @@ func (l *PassThroughRecordList) Dump() {
|
|||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
l.dumpRecords = l.dumpRecords[:0]
|
||||||
l.mu.RLock()
|
l.mu.RLock()
|
||||||
records := make([]*PassThroughRecord, 0, len(l.records))
|
|
||||||
for _, r := range l.records {
|
for _, r := range l.records {
|
||||||
records = append(records, r)
|
l.dumpRecords = append(l.dumpRecords, r)
|
||||||
}
|
}
|
||||||
l.mu.RUnlock()
|
l.mu.RUnlock()
|
||||||
|
|
||||||
sort.SliceStable(records, func(i, j int) bool {
|
sort.SliceStable(l.dumpRecords, func(i, j int) bool {
|
||||||
return records[i].Count > records[j].Count
|
return l.dumpRecords[i].Count > l.dumpRecords[j].Count
|
||||||
})
|
})
|
||||||
|
|
||||||
l.dumpWriter.Reset(f)
|
l.dumpWriter.Reset(f)
|
||||||
@ -104,7 +107,7 @@ func (l *PassThroughRecordList) Dump() {
|
|||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
for _, record := range records {
|
for _, record := range l.dumpRecords {
|
||||||
_, err := fmt.Fprintf(l.dumpWriter, "%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 {
|
||||||
|
|||||||
@ -14,8 +14,10 @@ type RewriteRecordList struct {
|
|||||||
recordAddChan chan *RewriteRecord
|
recordAddChan chan *RewriteRecord
|
||||||
records map[string]*RewriteRecord
|
records map[string]*RewriteRecord
|
||||||
mu sync.RWMutex
|
mu sync.RWMutex
|
||||||
dumpFile string
|
|
||||||
dumpWriter *bufio.Writer
|
dumpRecords []*RewriteRecord
|
||||||
|
dumpFile string
|
||||||
|
dumpWriter *bufio.Writer
|
||||||
}
|
}
|
||||||
|
|
||||||
type RewriteRecord struct {
|
type RewriteRecord struct {
|
||||||
@ -28,8 +30,9 @@ type RewriteRecord struct {
|
|||||||
func NewRewriteRecordList(dumpFile string) *RewriteRecordList {
|
func NewRewriteRecordList(dumpFile string) *RewriteRecordList {
|
||||||
return &RewriteRecordList{
|
return &RewriteRecordList{
|
||||||
recordAddChan: make(chan *RewriteRecord, 100),
|
recordAddChan: make(chan *RewriteRecord, 100),
|
||||||
records: make(map[string]*RewriteRecord, 100),
|
records: make(map[string]*RewriteRecord, 300),
|
||||||
mu: sync.RWMutex{},
|
mu: sync.RWMutex{},
|
||||||
|
dumpRecords: make([]*RewriteRecord, 0, 300),
|
||||||
dumpFile: dumpFile,
|
dumpFile: dumpFile,
|
||||||
dumpWriter: bufio.NewWriter(nil),
|
dumpWriter: bufio.NewWriter(nil),
|
||||||
}
|
}
|
||||||
@ -81,15 +84,15 @@ func (l *RewriteRecordList) Dump() {
|
|||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
l.dumpRecords = l.dumpRecords[:0]
|
||||||
l.mu.RLock()
|
l.mu.RLock()
|
||||||
records := make([]*RewriteRecord, 0, len(l.records))
|
|
||||||
for _, record := range l.records {
|
for _, record := range l.records {
|
||||||
records = append(records, record)
|
l.dumpRecords = append(l.dumpRecords, record)
|
||||||
}
|
}
|
||||||
l.mu.RUnlock()
|
l.mu.RUnlock()
|
||||||
|
|
||||||
sort.SliceStable(records, func(i, j int) bool {
|
sort.SliceStable(l.dumpRecords, func(i, j int) bool {
|
||||||
return records[i].Count > records[j].Count
|
return l.dumpRecords[i].Count > l.dumpRecords[j].Count
|
||||||
})
|
})
|
||||||
|
|
||||||
l.dumpWriter.Reset(f)
|
l.dumpWriter.Reset(f)
|
||||||
@ -99,7 +102,7 @@ func (l *RewriteRecordList) Dump() {
|
|||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
for _, record := range records {
|
for _, record := range l.dumpRecords {
|
||||||
_, err := fmt.Fprintf(l.dumpWriter, "%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 {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user