2022-08-17 15:46:49 +00:00
|
|
|
package packet
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2022-08-22 16:41:51 +00:00
|
|
|
"fmt"
|
2022-08-17 15:46:49 +00:00
|
|
|
"net/netip"
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
ErrFlowNotFound = errors.New("flow not found")
|
|
|
|
)
|
|
|
|
|
2022-08-18 15:03:47 +00:00
|
|
|
// FlowID represents a key type that can be used by FlowTracker
|
|
|
|
type FlowID interface {
|
2022-08-22 16:41:51 +00:00
|
|
|
// Type returns the name of the type that implements the FlowID
|
|
|
|
Type() string
|
|
|
|
fmt.Stringer
|
2022-08-17 15:46:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Flow struct {
|
|
|
|
Src netip.Addr
|
|
|
|
Dst netip.Addr
|
|
|
|
Responder FlowResponder
|
|
|
|
}
|
|
|
|
|
|
|
|
func isSameFlow(f1, f2 *Flow) bool {
|
|
|
|
if f1 == nil || f2 == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return *f1 == *f2
|
|
|
|
}
|
|
|
|
|
|
|
|
// FlowResponder sends response packets to the flow
|
|
|
|
type FlowResponder interface {
|
|
|
|
// SendPacket returns a packet to the flow. It must not modify the packet,
|
|
|
|
// and after return it must not read the packet
|
|
|
|
SendPacket(pk RawPacket) error
|
|
|
|
}
|
|
|
|
|
2022-08-18 15:03:47 +00:00
|
|
|
// FlowTracker tracks flow from the perspective of eyeball to origin
|
|
|
|
type FlowTracker struct {
|
2022-08-17 15:46:49 +00:00
|
|
|
lock sync.RWMutex
|
2022-08-18 15:03:47 +00:00
|
|
|
flows map[FlowID]*Flow
|
2022-08-17 15:46:49 +00:00
|
|
|
}
|
|
|
|
|
2022-08-18 15:03:47 +00:00
|
|
|
func NewFlowTracker() *FlowTracker {
|
|
|
|
return &FlowTracker{
|
|
|
|
flows: make(map[FlowID]*Flow),
|
2022-08-17 15:46:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-18 15:03:47 +00:00
|
|
|
func (sft *FlowTracker) Get(id FlowID) (*Flow, bool) {
|
2022-08-17 15:46:49 +00:00
|
|
|
sft.lock.RLock()
|
|
|
|
defer sft.lock.RUnlock()
|
|
|
|
flow, ok := sft.flows[id]
|
|
|
|
return flow, ok
|
|
|
|
}
|
|
|
|
|
|
|
|
// Registers a flow. If shouldReplace = true, replace the current flow
|
2022-08-18 15:03:47 +00:00
|
|
|
func (sft *FlowTracker) Register(id FlowID, flow *Flow, shouldReplace bool) (replaced bool) {
|
2022-08-17 15:46:49 +00:00
|
|
|
sft.lock.Lock()
|
|
|
|
defer sft.lock.Unlock()
|
|
|
|
currentFlow, ok := sft.flows[id]
|
|
|
|
if !ok {
|
|
|
|
sft.flows[id] = flow
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-08-26 16:52:06 +00:00
|
|
|
if shouldReplace && !isSameFlow(currentFlow, flow) {
|
2022-08-17 15:46:49 +00:00
|
|
|
sft.flows[id] = flow
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unregisters a flow. If force = true, delete it even if it maps to a different flow
|
2022-08-18 15:03:47 +00:00
|
|
|
func (sft *FlowTracker) Unregister(id FlowID, flow *Flow, force bool) (forceDeleted bool) {
|
2022-08-17 15:46:49 +00:00
|
|
|
sft.lock.Lock()
|
|
|
|
defer sft.lock.Unlock()
|
|
|
|
currentFlow, ok := sft.flows[id]
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if isSameFlow(currentFlow, flow) {
|
|
|
|
delete(sft.flows, id)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if force {
|
|
|
|
delete(sft.flows, id)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|