mirror of
https://github.com/v2fly/v2ray-core.git
synced 2024-12-22 01:57:12 -05:00
space with context
This commit is contained in:
parent
5b3e84ede6
commit
46ab9c45cc
18
app/dns.go
18
app/dns.go
@ -8,3 +8,21 @@ type DnsCache interface {
|
||||
Get(domain string) net.IP
|
||||
Add(domain string, ip net.IP)
|
||||
}
|
||||
|
||||
type DnsCacheWithContext interface {
|
||||
Get(context Context, domain string) net.IP
|
||||
Add(contaxt Context, domain string, ip net.IP)
|
||||
}
|
||||
|
||||
type contextedDnsCache struct {
|
||||
context Context
|
||||
dnsCache DnsCacheWithContext
|
||||
}
|
||||
|
||||
func (this *contextedDnsCache) Get(domain string) net.IP {
|
||||
return this.dnsCache.Get(this.context, domain)
|
||||
}
|
||||
|
||||
func (this *contextedDnsCache) Add(domain string, ip net.IP) {
|
||||
this.dnsCache.Add(this.context, domain, ip)
|
||||
}
|
||||
|
@ -4,6 +4,8 @@ import (
|
||||
"net"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/v2ray/v2ray-core/app"
|
||||
)
|
||||
|
||||
type entry struct {
|
||||
@ -38,6 +40,7 @@ type DnsCache struct {
|
||||
func NewCache(config CacheConfig) *DnsCache {
|
||||
cache := &DnsCache{
|
||||
cache: make(map[string]*entry),
|
||||
config: config,
|
||||
}
|
||||
go cache.cleanup()
|
||||
return cache
|
||||
@ -64,7 +67,7 @@ func (this *DnsCache) cleanup() {
|
||||
}
|
||||
}
|
||||
|
||||
func (this *DnsCache) Add(domain string, ip net.IP) {
|
||||
func (this *DnsCache) Add(context app.Context, domain string, ip net.IP) {
|
||||
this.RLock()
|
||||
entry, found := this.cache[domain]
|
||||
this.RUnlock()
|
||||
@ -78,7 +81,7 @@ func (this *DnsCache) Add(domain string, ip net.IP) {
|
||||
}
|
||||
}
|
||||
|
||||
func (this *DnsCache) Get(domain string) net.IP {
|
||||
func (this *DnsCache) Get(context app.Context, domain string) net.IP {
|
||||
this.RLock()
|
||||
entry, found := this.cache[domain]
|
||||
this.RUnlock()
|
||||
|
@ -5,6 +5,7 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/v2ray/v2ray-core/app/dns"
|
||||
apptesting "github.com/v2ray/v2ray-core/app/testing"
|
||||
netassert "github.com/v2ray/v2ray-core/common/net/testing/assert"
|
||||
v2testing "github.com/v2ray/v2ray-core/testing"
|
||||
)
|
||||
@ -14,10 +15,10 @@ func TestDnsAdd(t *testing.T) {
|
||||
|
||||
domain := "v2ray.com"
|
||||
cache := dns.NewCache(nil)
|
||||
ip := cache.Get(domain)
|
||||
ip := cache.Get(&apptesting.Context{}, domain)
|
||||
netassert.IP(ip).IsNil()
|
||||
|
||||
cache.Add(domain, []byte{1, 2, 3, 4})
|
||||
ip = cache.Get(domain)
|
||||
cache.Add(&apptesting.Context{}, domain, []byte{1, 2, 3, 4})
|
||||
ip = cache.Get(&apptesting.Context{}, domain)
|
||||
netassert.IP(ip).Equals(net.IP([]byte{1, 2, 3, 4}))
|
||||
}
|
||||
|
@ -9,3 +9,16 @@ import (
|
||||
type PacketDispatcher interface {
|
||||
DispatchToOutbound(packet v2net.Packet) ray.InboundRay
|
||||
}
|
||||
|
||||
type PacketDispatcherWithContext interface {
|
||||
DispatchToOutbound(context Context, packet v2net.Packet) ray.InboundRay
|
||||
}
|
||||
|
||||
type contextedPacketDispatcher struct {
|
||||
context Context
|
||||
packetDispatcher PacketDispatcherWithContext
|
||||
}
|
||||
|
||||
func (this *contextedPacketDispatcher) DispatchToOutbound(packet v2net.Packet) ray.InboundRay {
|
||||
return this.packetDispatcher.DispatchToOutbound(this.context, packet)
|
||||
}
|
||||
|
58
app/space.go
58
app/space.go
@ -1,22 +1,60 @@
|
||||
package app
|
||||
|
||||
type Context interface {
|
||||
CallerTag() string
|
||||
}
|
||||
|
||||
type contextImpl struct {
|
||||
callerTag string
|
||||
}
|
||||
|
||||
func (this *contextImpl) CallerTag() string {
|
||||
return this.callerTag
|
||||
}
|
||||
|
||||
type SpaceController struct {
|
||||
packetDispatcher PacketDispatcherWithContext
|
||||
dnsCache DnsCacheWithContext
|
||||
}
|
||||
|
||||
func NewSpaceController() *SpaceController {
|
||||
return new(SpaceController)
|
||||
}
|
||||
|
||||
func (this *SpaceController) Bind(object interface{}) {
|
||||
if packetDispatcher, ok := object.(PacketDispatcherWithContext); ok {
|
||||
this.packetDispatcher = packetDispatcher
|
||||
}
|
||||
|
||||
if dnsCache, ok := object.(DnsCacheWithContext); ok {
|
||||
this.dnsCache = dnsCache
|
||||
}
|
||||
}
|
||||
|
||||
func (this *SpaceController) ForContext(tag string) *Space {
|
||||
return newSpace(this, &contextImpl{callerTag: tag})
|
||||
}
|
||||
|
||||
type Space struct {
|
||||
packetDispatcher PacketDispatcher
|
||||
dnsCache DnsCache
|
||||
}
|
||||
|
||||
func NewSpace() *Space {
|
||||
return new(Space)
|
||||
}
|
||||
|
||||
func (this *Space) Bind(object interface{}) {
|
||||
if packetDispatcher, ok := object.(PacketDispatcher); ok {
|
||||
this.packetDispatcher = packetDispatcher
|
||||
func newSpace(controller *SpaceController, context Context) *Space {
|
||||
space := new(Space)
|
||||
if controller.packetDispatcher != nil {
|
||||
space.packetDispatcher = &contextedPacketDispatcher{
|
||||
context: context,
|
||||
packetDispatcher: controller.packetDispatcher,
|
||||
}
|
||||
|
||||
if dnsCache, ok := object.(DnsCache); ok {
|
||||
this.dnsCache = dnsCache
|
||||
}
|
||||
if controller.dnsCache != nil {
|
||||
space.dnsCache = &contextedDnsCache{
|
||||
context: context,
|
||||
dnsCache: controller.dnsCache,
|
||||
}
|
||||
}
|
||||
return space
|
||||
}
|
||||
|
||||
func (this *Space) HasPacketDispatcher() bool {
|
||||
|
9
app/testing/space.go
Normal file
9
app/testing/space.go
Normal file
@ -0,0 +1,9 @@
|
||||
package testing
|
||||
|
||||
type Context struct {
|
||||
CallerTagValue string
|
||||
}
|
||||
|
||||
func (this *Context) CallerTag() string {
|
||||
return this.CallerTagValue
|
||||
}
|
@ -22,7 +22,7 @@ type Point struct {
|
||||
idh []*InboundDetourHandler
|
||||
odh map[string]connhandler.OutboundConnectionHandler
|
||||
router router.Router
|
||||
space *app.Space
|
||||
space *app.SpaceController
|
||||
}
|
||||
|
||||
// NewPoint returns a new Point server based on given configuration.
|
||||
@ -50,7 +50,7 @@ func NewPoint(pConfig PointConfig) (*Point, error) {
|
||||
log.SetLogLevel(logConfig.LogLevel())
|
||||
}
|
||||
|
||||
vpoint.space = app.NewSpace()
|
||||
vpoint.space = app.NewSpaceController()
|
||||
vpoint.space.Bind(vpoint)
|
||||
|
||||
ichFactory := connhandler.GetInboundConnectionHandlerFactory(pConfig.InboundConfig().Protocol())
|
||||
@ -59,7 +59,7 @@ func NewPoint(pConfig PointConfig) (*Point, error) {
|
||||
return nil, BadConfiguration
|
||||
}
|
||||
ichConfig := pConfig.InboundConfig().Settings()
|
||||
ich, err := ichFactory.Create(vpoint.space, ichConfig)
|
||||
ich, err := ichFactory.Create(vpoint.space.ForContext("vpoint-default-inbound"), ichConfig)
|
||||
if err != nil {
|
||||
log.Error("Failed to create inbound connection handler: %v", err)
|
||||
return nil, err
|
||||
@ -72,7 +72,7 @@ func NewPoint(pConfig PointConfig) (*Point, error) {
|
||||
return nil, BadConfiguration
|
||||
}
|
||||
ochConfig := pConfig.OutboundConfig().Settings()
|
||||
och, err := ochFactory.Create(vpoint.space, ochConfig)
|
||||
och, err := ochFactory.Create(vpoint.space.ForContext("vpoint-default-outbound"), ochConfig)
|
||||
if err != nil {
|
||||
log.Error("Failed to create outbound connection handler: %v", err)
|
||||
return nil, err
|
||||
@ -84,7 +84,7 @@ func NewPoint(pConfig PointConfig) (*Point, error) {
|
||||
vpoint.idh = make([]*InboundDetourHandler, len(detours))
|
||||
for idx, detourConfig := range detours {
|
||||
detourHandler := &InboundDetourHandler{
|
||||
space: vpoint.space,
|
||||
space: vpoint.space.ForContext(detourConfig.Tag()),
|
||||
config: detourConfig,
|
||||
}
|
||||
err := detourHandler.Initialize()
|
||||
@ -104,7 +104,7 @@ func NewPoint(pConfig PointConfig) (*Point, error) {
|
||||
log.Error("Unknown detour outbound connection handler factory %s", detourConfig.Protocol())
|
||||
return nil, BadConfiguration
|
||||
}
|
||||
detourHandler, err := detourFactory.Create(vpoint.space, detourConfig.Settings())
|
||||
detourHandler, err := detourFactory.Create(vpoint.space.ForContext(detourConfig.Tag()), detourConfig.Settings())
|
||||
if err != nil {
|
||||
log.Error("Failed to create detour outbound connection handler: %v", err)
|
||||
return nil, err
|
||||
@ -159,7 +159,7 @@ func (this *Point) Start() error {
|
||||
// Dispatches a Packet to an OutboundConnection.
|
||||
// The packet will be passed through the router (if configured), and then sent to an outbound
|
||||
// connection with matching tag.
|
||||
func (this *Point) DispatchToOutbound(packet v2net.Packet) ray.InboundRay {
|
||||
func (this *Point) DispatchToOutbound(context app.Context, packet v2net.Packet) ray.InboundRay {
|
||||
direct := ray.NewRay()
|
||||
dest := packet.Destination()
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user