hide space implementations from interfaces

pull/62/head
Darien Raymond 2015-12-11 11:01:20 +00:00
parent 46ab9c45cc
commit dd81fc6f6a
23 changed files with 162 additions and 119 deletions

View File

@ -0,0 +1,31 @@
package controller
import (
"github.com/v2ray/v2ray-core/app"
"github.com/v2ray/v2ray-core/app/internal"
)
// A SpaceController is supposed to be used by a shell to create Spaces. It should not be used
// directly by proxies.
type SpaceController struct {
packetDispatcher internal.PacketDispatcherWithContext
dnsCache internal.DnsCacheWithContext
}
func New() *SpaceController {
return new(SpaceController)
}
func (this *SpaceController) Bind(object interface{}) {
if packetDispatcher, ok := object.(internal.PacketDispatcherWithContext); ok {
this.packetDispatcher = packetDispatcher
}
if dnsCache, ok := object.(internal.DnsCacheWithContext); ok {
this.dnsCache = dnsCache
}
}
func (this *SpaceController) ForContext(tag string) app.Space {
return internal.NewSpace(tag, this.packetDispatcher, this.dnsCache)
}

View File

@ -4,25 +4,8 @@ import (
"net" "net"
) )
// A DnsCache is an internal cache of DNS resolutions.
type DnsCache interface { 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)
}

9
app/internal/context.go Normal file
View File

@ -0,0 +1,9 @@
package internal
type contextImpl struct {
callerTag string
}
func (this *contextImpl) CallerTag() string {
return this.callerTag
}

25
app/internal/dns.go Normal file
View File

@ -0,0 +1,25 @@
package internal
import (
"net"
"github.com/v2ray/v2ray-core/app"
)
type DnsCacheWithContext interface {
Get(context app.Context, domain string) net.IP
Add(contaxt app.Context, domain string, ip net.IP)
}
type contextedDnsCache struct {
context app.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

@ -0,0 +1,20 @@
package internal
import (
"github.com/v2ray/v2ray-core/app"
v2net "github.com/v2ray/v2ray-core/common/net"
"github.com/v2ray/v2ray-core/transport/ray"
)
type PacketDispatcherWithContext interface {
DispatchToOutbound(context app.Context, packet v2net.Packet) ray.InboundRay
}
type contextedPacketDispatcher struct {
context app.Context
packetDispatcher PacketDispatcherWithContext
}
func (this *contextedPacketDispatcher) DispatchToOutbound(packet v2net.Packet) ray.InboundRay {
return this.packetDispatcher.DispatchToOutbound(this.context, packet)
}

45
app/internal/space.go Normal file
View File

@ -0,0 +1,45 @@
package internal
import (
"github.com/v2ray/v2ray-core/app"
)
type Space struct {
packetDispatcher PacketDispatcherWithContext
dnsCache DnsCacheWithContext
tag string
}
func NewSpace(tag string, packetDispatcher PacketDispatcherWithContext, dnsCache DnsCacheWithContext) *Space {
return &Space{
tag: tag,
packetDispatcher: packetDispatcher,
dnsCache: dnsCache,
}
}
func (this *Space) HasPacketDispatcher() bool {
return this.packetDispatcher != nil
}
func (this *Space) PacketDispatcher() app.PacketDispatcher {
return &contextedPacketDispatcher{
packetDispatcher: this.packetDispatcher,
context: &contextImpl{
callerTag: this.tag,
},
}
}
func (this *Space) HasDnsCache() bool {
return this.dnsCache != nil
}
func (this *Space) DnsCache() app.DnsCache {
return &contextedDnsCache{
dnsCache: this.dnsCache,
context: &contextImpl{
callerTag: this.tag,
},
}
}

View File

@ -9,16 +9,3 @@ 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,74 +1,16 @@
package app package app
// Context of a function call from proxy to app.
type Context interface { type Context interface {
CallerTag() string CallerTag() string
} }
type contextImpl struct { // A Space contains all apps that may be available in a V2Ray runtime.
callerTag string // Caller must check the availability of an app by calling HasXXX before getting its instance.
} type Space interface {
HasPacketDispatcher() bool
PacketDispatcher() PacketDispatcher
func (this *contextImpl) CallerTag() string { HasDnsCache() bool
return this.callerTag DnsCache() DnsCache
}
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(controller *SpaceController, context Context) *Space {
space := new(Space)
if controller.packetDispatcher != nil {
space.packetDispatcher = &contextedPacketDispatcher{
context: context,
packetDispatcher: controller.packetDispatcher,
}
}
if controller.dnsCache != nil {
space.dnsCache = &contextedDnsCache{
context: context,
dnsCache: controller.dnsCache,
}
}
return space
}
func (this *Space) HasPacketDispatcher() bool {
return this.packetDispatcher != nil
}
func (this *Space) PacketDispatcher() PacketDispatcher {
return this.packetDispatcher
}
func (this *Space) HasDnsCache() bool {
return this.dnsCache != nil
}
func (this *Space) DnsCache() DnsCache {
return this.dnsCache
} }

View File

@ -32,7 +32,7 @@ func (this *BlackHole) Dispatch(firstPacket v2net.Packet, ray ray.OutboundRay) e
type BlackHoleFactory struct { type BlackHoleFactory struct {
} }
func (this BlackHoleFactory) Create(space *app.Space, config interface{}) (connhandler.OutboundConnectionHandler, error) { func (this BlackHoleFactory) Create(space app.Space, config interface{}) (connhandler.OutboundConnectionHandler, error) {
return NewBlackHole(), nil return NewBlackHole(), nil
} }

View File

@ -8,7 +8,7 @@ import (
// A InboundConnectionHandlerFactory creates InboundConnectionHandler on demand. // A InboundConnectionHandlerFactory creates InboundConnectionHandler on demand.
type InboundConnectionHandlerFactory interface { type InboundConnectionHandlerFactory interface {
// Create creates a new InboundConnectionHandler with given configuration. // Create creates a new InboundConnectionHandler with given configuration.
Create(space *app.Space, config interface{}) (InboundConnectionHandler, error) Create(space app.Space, config interface{}) (InboundConnectionHandler, error)
} }
// A InboundConnectionHandler handles inbound network connections to V2Ray. // A InboundConnectionHandler handles inbound network connections to V2Ray.

View File

@ -9,7 +9,7 @@ import (
// An OutboundConnectionHandlerFactory creates OutboundConnectionHandler on demand. // An OutboundConnectionHandlerFactory creates OutboundConnectionHandler on demand.
type OutboundConnectionHandlerFactory interface { type OutboundConnectionHandlerFactory interface {
// Create creates a new OutboundConnectionHandler with given config. // Create creates a new OutboundConnectionHandler with given config.
Create(space *app.Space, config interface{}) (OutboundConnectionHandler, error) Create(space app.Space, config interface{}) (OutboundConnectionHandler, error)
} }
// An OutboundConnectionHandler handles outbound network connection for V2Ray. // An OutboundConnectionHandler handles outbound network connection for V2Ray.

View File

@ -16,10 +16,10 @@ type DokodemoDoor struct {
config Config config Config
accepting bool accepting bool
address v2net.Address address v2net.Address
space *app.Space space app.Space
} }
func NewDokodemoDoor(space *app.Space, config Config) *DokodemoDoor { func NewDokodemoDoor(space app.Space, config Config) *DokodemoDoor {
return &DokodemoDoor{ return &DokodemoDoor{
config: config, config: config,
space: space, space: space,

View File

@ -8,7 +8,7 @@ import (
type DokodemoDoorFactory struct { type DokodemoDoorFactory struct {
} }
func (this DokodemoDoorFactory) Create(space *app.Space, rawConfig interface{}) (connhandler.InboundConnectionHandler, error) { func (this DokodemoDoorFactory) Create(space app.Space, rawConfig interface{}) (connhandler.InboundConnectionHandler, error) {
config := rawConfig.(Config) config := rawConfig.(Config)
return NewDokodemoDoor(space, config), nil return NewDokodemoDoor(space, config), nil
} }

View File

@ -11,7 +11,7 @@ import (
) )
type FreedomConnection struct { type FreedomConnection struct {
space *app.Space space app.Space
} }
func (this *FreedomConnection) Dispatch(firstPacket v2net.Packet, ray ray.OutboundRay) error { func (this *FreedomConnection) Dispatch(firstPacket v2net.Packet, ray ray.OutboundRay) error {

View File

@ -8,7 +8,7 @@ import (
type FreedomFactory struct { type FreedomFactory struct {
} }
func (this FreedomFactory) Create(space *app.Space, config interface{}) (connhandler.OutboundConnectionHandler, error) { func (this FreedomFactory) Create(space app.Space, config interface{}) (connhandler.OutboundConnectionHandler, error) {
return &FreedomConnection{space: space}, nil return &FreedomConnection{space: space}, nil
} }

View File

@ -24,11 +24,11 @@ var (
// SocksServer is a SOCKS 5 proxy server // SocksServer is a SOCKS 5 proxy server
type SocksServer struct { type SocksServer struct {
accepting bool accepting bool
space *app.Space space app.Space
config Config config Config
} }
func NewSocksServer(space *app.Space, config Config) *SocksServer { func NewSocksServer(space app.Space, config Config) *SocksServer {
return &SocksServer{ return &SocksServer{
space: space, space: space,
config: config, config: config,

View File

@ -8,7 +8,7 @@ import (
type SocksServerFactory struct { type SocksServerFactory struct {
} }
func (this SocksServerFactory) Create(space *app.Space, rawConfig interface{}) (connhandler.InboundConnectionHandler, error) { func (this SocksServerFactory) Create(space app.Space, rawConfig interface{}) (connhandler.InboundConnectionHandler, error) {
return NewSocksServer(space, rawConfig.(Config)), nil return NewSocksServer(space, rawConfig.(Config)), nil
} }

View File

@ -11,7 +11,7 @@ import (
type InboundConnectionHandler struct { type InboundConnectionHandler struct {
Port v2net.Port Port v2net.Port
space *app.Space space app.Space
ConnInput io.Reader ConnInput io.Reader
ConnOutput io.Writer ConnOutput io.Writer
} }
@ -49,7 +49,7 @@ func (this *InboundConnectionHandler) Communicate(packet v2net.Packet) error {
return nil return nil
} }
func (this *InboundConnectionHandler) Create(space *app.Space, config interface{}) (connhandler.InboundConnectionHandler, error) { func (this *InboundConnectionHandler) Create(space app.Space, config interface{}) (connhandler.InboundConnectionHandler, error) {
this.space = space this.space = space
return this, nil return this, nil
} }

View File

@ -45,6 +45,6 @@ func (this *OutboundConnectionHandler) Dispatch(packet v2net.Packet, ray ray.Out
return nil return nil
} }
func (this *OutboundConnectionHandler) Create(space *app.Space, config interface{}) (connhandler.OutboundConnectionHandler, error) { func (this *OutboundConnectionHandler) Create(space app.Space, config interface{}) (connhandler.OutboundConnectionHandler, error) {
return this, nil return this, nil
} }

View File

@ -20,12 +20,12 @@ import (
// Inbound connection handler that handles messages in VMess format. // Inbound connection handler that handles messages in VMess format.
type VMessInboundHandler struct { type VMessInboundHandler struct {
space *app.Space space app.Space
clients user.UserSet clients user.UserSet
accepting bool accepting bool
} }
func NewVMessInboundHandler(space *app.Space, clients user.UserSet) *VMessInboundHandler { func NewVMessInboundHandler(space app.Space, clients user.UserSet) *VMessInboundHandler {
return &VMessInboundHandler{ return &VMessInboundHandler{
space: space, space: space,
clients: clients, clients: clients,
@ -142,7 +142,7 @@ func handleOutput(request *protocol.VMessRequest, writer io.Writer, output <-cha
type VMessInboundHandlerFactory struct { type VMessInboundHandlerFactory struct {
} }
func (this *VMessInboundHandlerFactory) Create(space *app.Space, rawConfig interface{}) (connhandler.InboundConnectionHandler, error) { func (this *VMessInboundHandlerFactory) Create(space app.Space, rawConfig interface{}) (connhandler.InboundConnectionHandler, error) {
config := rawConfig.(Config) config := rawConfig.(Config)
allowedClients := user.NewTimedUserSet() allowedClients := user.NewTimedUserSet()

View File

@ -19,7 +19,7 @@ import (
type VMessOutboundHandler struct { type VMessOutboundHandler struct {
receiverManager *ReceiverManager receiverManager *ReceiverManager
space *app.Space space app.Space
} }
func (this *VMessOutboundHandler) Dispatch(firstPacket v2net.Packet, ray ray.OutboundRay) error { func (this *VMessOutboundHandler) Dispatch(firstPacket v2net.Packet, ray ray.OutboundRay) error {
@ -175,7 +175,7 @@ func handleResponse(conn net.Conn, request *protocol.VMessRequest, output chan<-
type VMessOutboundHandlerFactory struct { type VMessOutboundHandlerFactory struct {
} }
func (this *VMessOutboundHandlerFactory) Create(space *app.Space, rawConfig interface{}) (connhandler.OutboundConnectionHandler, error) { func (this *VMessOutboundHandlerFactory) Create(space app.Space, rawConfig interface{}) (connhandler.OutboundConnectionHandler, error) {
vOutConfig := rawConfig.(Config) vOutConfig := rawConfig.(Config)
return &VMessOutboundHandler{ return &VMessOutboundHandler{
space: space, space: space,

View File

@ -15,7 +15,7 @@ type InboundConnectionHandlerWithPort struct {
// Handler for inbound detour connections. // Handler for inbound detour connections.
type InboundDetourHandler struct { type InboundDetourHandler struct {
space *app.Space space app.Space
config InboundDetourConfig config InboundDetourConfig
ich []*InboundConnectionHandlerWithPort ich []*InboundConnectionHandlerWithPort
} }

View File

@ -6,6 +6,7 @@ package point
import ( import (
"github.com/v2ray/v2ray-core/app" "github.com/v2ray/v2ray-core/app"
"github.com/v2ray/v2ray-core/app/controller"
"github.com/v2ray/v2ray-core/app/router" "github.com/v2ray/v2ray-core/app/router"
"github.com/v2ray/v2ray-core/common/log" "github.com/v2ray/v2ray-core/common/log"
v2net "github.com/v2ray/v2ray-core/common/net" v2net "github.com/v2ray/v2ray-core/common/net"
@ -22,7 +23,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.SpaceController space *controller.SpaceController
} }
// NewPoint returns a new Point server based on given configuration. // NewPoint returns a new Point server based on given configuration.
@ -50,7 +51,7 @@ func NewPoint(pConfig PointConfig) (*Point, error) {
log.SetLogLevel(logConfig.LogLevel()) log.SetLogLevel(logConfig.LogLevel())
} }
vpoint.space = app.NewSpaceController() vpoint.space = controller.New()
vpoint.space.Bind(vpoint) vpoint.space.Bind(vpoint)
ichFactory := connhandler.GetInboundConnectionHandlerFactory(pConfig.InboundConfig().Protocol()) ichFactory := connhandler.GetInboundConnectionHandlerFactory(pConfig.InboundConfig().Protocol())