1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-22 10:08:15 -05:00

space with context

This commit is contained in:
v2ray 2015-12-10 23:55:39 +01:00
parent 5b3e84ede6
commit 46ab9c45cc
7 changed files with 105 additions and 23 deletions

View File

@ -8,3 +8,21 @@ type DnsCache interface {
Get(domain string) net.IP Get(domain string) net.IP
Add(domain string, ip 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)
}

View File

@ -4,6 +4,8 @@ import (
"net" "net"
"sync" "sync"
"time" "time"
"github.com/v2ray/v2ray-core/app"
) )
type entry struct { type entry struct {
@ -37,7 +39,8 @@ type DnsCache struct {
func NewCache(config CacheConfig) *DnsCache { func NewCache(config CacheConfig) *DnsCache {
cache := &DnsCache{ cache := &DnsCache{
cache: make(map[string]*entry), cache: make(map[string]*entry),
config: config,
} }
go cache.cleanup() go cache.cleanup()
return cache 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() this.RLock()
entry, found := this.cache[domain] entry, found := this.cache[domain]
this.RUnlock() 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() this.RLock()
entry, found := this.cache[domain] entry, found := this.cache[domain]
this.RUnlock() this.RUnlock()

View File

@ -5,6 +5,7 @@ import (
"testing" "testing"
"github.com/v2ray/v2ray-core/app/dns" "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" netassert "github.com/v2ray/v2ray-core/common/net/testing/assert"
v2testing "github.com/v2ray/v2ray-core/testing" v2testing "github.com/v2ray/v2ray-core/testing"
) )
@ -14,10 +15,10 @@ func TestDnsAdd(t *testing.T) {
domain := "v2ray.com" domain := "v2ray.com"
cache := dns.NewCache(nil) cache := dns.NewCache(nil)
ip := cache.Get(domain) ip := cache.Get(&apptesting.Context{}, domain)
netassert.IP(ip).IsNil() netassert.IP(ip).IsNil()
cache.Add(domain, []byte{1, 2, 3, 4}) cache.Add(&apptesting.Context{}, domain, []byte{1, 2, 3, 4})
ip = cache.Get(domain) ip = cache.Get(&apptesting.Context{}, domain)
netassert.IP(ip).Equals(net.IP([]byte{1, 2, 3, 4})) netassert.IP(ip).Equals(net.IP([]byte{1, 2, 3, 4}))
} }

View File

@ -9,3 +9,16 @@ import (
type PacketDispatcher interface { type PacketDispatcher interface {
DispatchToOutbound(packet v2net.Packet) ray.InboundRay 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)
}

View File

@ -1,22 +1,60 @@
package app 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 { type Space struct {
packetDispatcher PacketDispatcher packetDispatcher PacketDispatcher
dnsCache DnsCache dnsCache DnsCache
} }
func NewSpace() *Space { func newSpace(controller *SpaceController, context Context) *Space {
return new(Space) space := new(Space)
} if controller.packetDispatcher != nil {
space.packetDispatcher = &contextedPacketDispatcher{
func (this *Space) Bind(object interface{}) { context: context,
if packetDispatcher, ok := object.(PacketDispatcher); ok { packetDispatcher: controller.packetDispatcher,
this.packetDispatcher = packetDispatcher }
} }
if controller.dnsCache != nil {
if dnsCache, ok := object.(DnsCache); ok { space.dnsCache = &contextedDnsCache{
this.dnsCache = dnsCache context: context,
dnsCache: controller.dnsCache,
}
} }
return space
} }
func (this *Space) HasPacketDispatcher() bool { func (this *Space) HasPacketDispatcher() bool {

9
app/testing/space.go Normal file
View File

@ -0,0 +1,9 @@
package testing
type Context struct {
CallerTagValue string
}
func (this *Context) CallerTag() string {
return this.CallerTagValue
}

View File

@ -22,7 +22,7 @@ type Point struct {
idh []*InboundDetourHandler idh []*InboundDetourHandler
odh map[string]connhandler.OutboundConnectionHandler odh map[string]connhandler.OutboundConnectionHandler
router router.Router router router.Router
space *app.Space space *app.SpaceController
} }
// NewPoint returns a new Point server based on given configuration. // NewPoint returns a new Point server based on given configuration.
@ -50,7 +50,7 @@ func NewPoint(pConfig PointConfig) (*Point, error) {
log.SetLogLevel(logConfig.LogLevel()) log.SetLogLevel(logConfig.LogLevel())
} }
vpoint.space = app.NewSpace() vpoint.space = app.NewSpaceController()
vpoint.space.Bind(vpoint) vpoint.space.Bind(vpoint)
ichFactory := connhandler.GetInboundConnectionHandlerFactory(pConfig.InboundConfig().Protocol()) ichFactory := connhandler.GetInboundConnectionHandlerFactory(pConfig.InboundConfig().Protocol())
@ -59,7 +59,7 @@ func NewPoint(pConfig PointConfig) (*Point, error) {
return nil, BadConfiguration return nil, BadConfiguration
} }
ichConfig := pConfig.InboundConfig().Settings() 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 { if err != nil {
log.Error("Failed to create inbound connection handler: %v", err) log.Error("Failed to create inbound connection handler: %v", err)
return nil, err return nil, err
@ -72,7 +72,7 @@ func NewPoint(pConfig PointConfig) (*Point, error) {
return nil, BadConfiguration return nil, BadConfiguration
} }
ochConfig := pConfig.OutboundConfig().Settings() 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 { if err != nil {
log.Error("Failed to create outbound connection handler: %v", err) log.Error("Failed to create outbound connection handler: %v", err)
return nil, err return nil, err
@ -84,7 +84,7 @@ func NewPoint(pConfig PointConfig) (*Point, error) {
vpoint.idh = make([]*InboundDetourHandler, len(detours)) vpoint.idh = make([]*InboundDetourHandler, len(detours))
for idx, detourConfig := range detours { for idx, detourConfig := range detours {
detourHandler := &InboundDetourHandler{ detourHandler := &InboundDetourHandler{
space: vpoint.space, space: vpoint.space.ForContext(detourConfig.Tag()),
config: detourConfig, config: detourConfig,
} }
err := detourHandler.Initialize() err := detourHandler.Initialize()
@ -104,7 +104,7 @@ func NewPoint(pConfig PointConfig) (*Point, error) {
log.Error("Unknown detour outbound connection handler factory %s", detourConfig.Protocol()) log.Error("Unknown detour outbound connection handler factory %s", detourConfig.Protocol())
return nil, BadConfiguration 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 { if err != nil {
log.Error("Failed to create detour outbound connection handler: %v", err) log.Error("Failed to create detour outbound connection handler: %v", err)
return nil, err return nil, err
@ -159,7 +159,7 @@ func (this *Point) Start() error {
// Dispatches a Packet to an OutboundConnection. // Dispatches a Packet to an OutboundConnection.
// The packet will be passed through the router (if configured), and then sent to an outbound // The packet will be passed through the router (if configured), and then sent to an outbound
// connection with matching tag. // 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() direct := ray.NewRay()
dest := packet.Destination() dest := packet.Destination()