Add ping proxy support
This commit is contained in:
parent
4a56d47035
commit
933bd2b2d5
16 changed files with 542 additions and 19 deletions
45
route_mapping.go
Normal file
45
route_mapping.go
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
package tun
|
||||
|
||||
import (
|
||||
"net/netip"
|
||||
"time"
|
||||
|
||||
"github.com/sagernet/sing/common"
|
||||
"github.com/sagernet/sing/contrab/freelru"
|
||||
"github.com/sagernet/sing/contrab/maphash"
|
||||
)
|
||||
|
||||
type DirectRouteSession struct {
|
||||
// IPVersion uint8
|
||||
// Network uint8
|
||||
Source netip.Addr
|
||||
Destination netip.Addr
|
||||
}
|
||||
|
||||
type RouteMapping struct {
|
||||
status freelru.Cache[DirectRouteSession, DirectRouteDestination]
|
||||
}
|
||||
|
||||
func NewRouteMapping(timeout time.Duration) *RouteMapping {
|
||||
status := common.Must1(freelru.NewSharded[DirectRouteSession, DirectRouteDestination](1024, maphash.NewHasher[DirectRouteSession]().Hash32))
|
||||
status.SetOnEvict(func(session DirectRouteSession, action DirectRouteDestination) {
|
||||
action.Close()
|
||||
})
|
||||
status.SetLifetime(timeout)
|
||||
return &RouteMapping{status}
|
||||
}
|
||||
|
||||
func (m *RouteMapping) Lookup(session DirectRouteSession, constructor func() (DirectRouteDestination, error)) (DirectRouteDestination, error) {
|
||||
var (
|
||||
created DirectRouteDestination
|
||||
err error
|
||||
)
|
||||
action, _, ok := m.status.GetAndRefreshOrAdd(session, func() (DirectRouteDestination, bool) {
|
||||
created, err = constructor()
|
||||
return created, err != nil
|
||||
})
|
||||
if !ok {
|
||||
return created, err
|
||||
}
|
||||
return action, nil
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue