2022-09-02 16:29:50 +00:00
|
|
|
package packet
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"net/netip"
|
|
|
|
"sync"
|
|
|
|
"sync/atomic"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
ErrFunnelNotFound = errors.New("funnel not found")
|
|
|
|
)
|
|
|
|
|
|
|
|
// Funnel is an abstraction to pipe from 1 src to 1 or more destinations
|
|
|
|
type Funnel interface {
|
2022-10-13 10:01:25 +00:00
|
|
|
// Updates the last time traffic went through this funnel
|
|
|
|
UpdateLastActive()
|
|
|
|
// LastActive returns the last time there is traffic through this funnel
|
2022-09-02 16:29:50 +00:00
|
|
|
LastActive() time.Time
|
|
|
|
// Close closes the funnel. Further call to SendToDst or ReturnToSrc should return an error
|
|
|
|
Close() error
|
|
|
|
// Equal compares if 2 funnels are equivalent
|
|
|
|
Equal(other Funnel) bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// FunnelUniPipe is a unidirectional pipe for sending raw packets
|
|
|
|
type FunnelUniPipe interface {
|
|
|
|
// SendPacket sends a packet to/from the funnel. It must not modify the packet,
|
|
|
|
// and after return it must not read the packet
|
|
|
|
SendPacket(dst netip.Addr, pk RawPacket) error
|
|
|
|
Close() error
|
|
|
|
}
|
|
|
|
|
2022-10-13 10:01:25 +00:00
|
|
|
type ActivityTracker struct {
|
2022-09-02 16:29:50 +00:00
|
|
|
// last active unix time. Unit is seconds
|
|
|
|
lastActive int64
|
|
|
|
}
|
|
|
|
|
2022-10-13 10:01:25 +00:00
|
|
|
func NewActivityTracker() *ActivityTracker {
|
|
|
|
return &ActivityTracker{
|
2022-09-02 16:29:50 +00:00
|
|
|
lastActive: time.Now().Unix(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-13 10:01:25 +00:00
|
|
|
func (at *ActivityTracker) UpdateLastActive() {
|
|
|
|
atomic.StoreInt64(&at.lastActive, time.Now().Unix())
|
2022-09-02 16:29:50 +00:00
|
|
|
}
|
|
|
|
|
2022-10-13 10:01:25 +00:00
|
|
|
func (at *ActivityTracker) LastActive() time.Time {
|
|
|
|
lastActive := atomic.LoadInt64(&at.lastActive)
|
2022-09-02 16:29:50 +00:00
|
|
|
return time.Unix(lastActive, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
// FunnelID represents a key type that can be used by FunnelTracker
|
|
|
|
type FunnelID interface {
|
|
|
|
// Type returns the name of the type that implements the FunnelID
|
|
|
|
Type() string
|
|
|
|
fmt.Stringer
|
|
|
|
}
|
|
|
|
|
|
|
|
// FunnelTracker tracks funnel from the perspective of eyeball to origin
|
|
|
|
type FunnelTracker struct {
|
|
|
|
lock sync.RWMutex
|
|
|
|
funnels map[FunnelID]Funnel
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewFunnelTracker() *FunnelTracker {
|
|
|
|
return &FunnelTracker{
|
|
|
|
funnels: make(map[FunnelID]Funnel),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ft *FunnelTracker) ScheduleCleanup(ctx context.Context, idleTimeout time.Duration) {
|
|
|
|
checkIdleTicker := time.NewTicker(idleTimeout)
|
|
|
|
defer checkIdleTicker.Stop()
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
case <-checkIdleTicker.C:
|
|
|
|
ft.cleanup(idleTimeout)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ft *FunnelTracker) cleanup(idleTimeout time.Duration) {
|
|
|
|
ft.lock.Lock()
|
|
|
|
defer ft.lock.Unlock()
|
|
|
|
|
|
|
|
now := time.Now()
|
|
|
|
for id, funnel := range ft.funnels {
|
|
|
|
lastActive := funnel.LastActive()
|
|
|
|
if now.After(lastActive.Add(idleTimeout)) {
|
|
|
|
funnel.Close()
|
|
|
|
delete(ft.funnels, id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ft *FunnelTracker) Get(id FunnelID) (Funnel, bool) {
|
|
|
|
ft.lock.RLock()
|
|
|
|
defer ft.lock.RUnlock()
|
|
|
|
funnel, ok := ft.funnels[id]
|
|
|
|
return funnel, ok
|
|
|
|
}
|
|
|
|
|
|
|
|
// Registers a funnel. It replaces the current funnel.
|
2022-09-09 15:48:42 +00:00
|
|
|
func (ft *FunnelTracker) GetOrRegister(id FunnelID, newFunnelFunc func() (Funnel, error)) (funnel Funnel, new bool, err error) {
|
2022-09-02 16:29:50 +00:00
|
|
|
ft.lock.Lock()
|
|
|
|
defer ft.lock.Unlock()
|
|
|
|
currentFunnel, exists := ft.funnels[id]
|
2022-09-09 15:48:42 +00:00
|
|
|
if exists {
|
|
|
|
return currentFunnel, false, nil
|
2022-09-02 16:29:50 +00:00
|
|
|
}
|
2022-09-09 15:48:42 +00:00
|
|
|
newFunnel, err := newFunnelFunc()
|
|
|
|
if err != nil {
|
|
|
|
return nil, false, err
|
2022-09-02 16:29:50 +00:00
|
|
|
}
|
2022-09-09 15:48:42 +00:00
|
|
|
ft.funnels[id] = newFunnel
|
|
|
|
return newFunnel, true, nil
|
2022-09-02 16:29:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Unregisters a funnel if the funnel equals to the current funnel
|
|
|
|
func (ft *FunnelTracker) Unregister(id FunnelID, funnel Funnel) (deleted bool) {
|
|
|
|
ft.lock.Lock()
|
|
|
|
defer ft.lock.Unlock()
|
|
|
|
currentFunnel, exists := ft.funnels[id]
|
|
|
|
if !exists {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if currentFunnel.Equal(funnel) {
|
|
|
|
currentFunnel.Close()
|
|
|
|
delete(ft.funnels, id)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|