Skip to content

Commit

Permalink
context'ize apps
Browse files Browse the repository at this point in the history
  • Loading branch information
DarienRaymond committed Jan 13, 2017
1 parent 148e483 commit 17504d2
Show file tree
Hide file tree
Showing 15 changed files with 168 additions and 149 deletions.
3 changes: 1 addition & 2 deletions app/dispatcher/dispatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package dispatcher

import (
"v2ray.com/core/app"
"v2ray.com/core/common/serial"
"v2ray.com/core/proxy"
"v2ray.com/core/transport/ray"
)
Expand All @@ -13,7 +12,7 @@ type PacketDispatcher interface {
}

func FromSpace(space app.Space) PacketDispatcher {
if app := space.(app.AppGetter).GetApp(serial.GetMessageType((*Config)(nil))); app != nil {
if app := space.GetApplication((*PacketDispatcher)(nil)); app != nil {
return app.(PacketDispatcher)
}
return nil
Expand Down
23 changes: 14 additions & 9 deletions app/dispatcher/impl/default.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package impl

import (
"context"
"time"

"v2ray.com/core/app"
Expand All @@ -21,7 +22,11 @@ type DefaultDispatcher struct {
router *router.Router
}

func NewDefaultDispatcher(space app.Space) *DefaultDispatcher {
func NewDefaultDispatcher(ctx context.Context, config *dispatcher.Config) (*DefaultDispatcher, error) {
space := app.SpaceFromContext(ctx)
if space == nil {
return nil, errors.New("DefaultDispatcher: No space in context.")
}
d := &DefaultDispatcher{}
space.OnInitialize(func() error {
d.ohm = proxyman.OutboundHandlerManagerFromSpace(space)
Expand All @@ -31,7 +36,11 @@ func NewDefaultDispatcher(space app.Space) *DefaultDispatcher {
d.router = router.FromSpace(space)
return nil
})
return d
return d, nil
}

func (DefaultDispatcher) Interface() interface{} {
return (*dispatcher.PacketDispatcher)(nil)
}

func (v *DefaultDispatcher) DispatchToOutbound(session *proxy.SessionInfo) ray.InboundRay {
Expand Down Expand Up @@ -79,14 +88,10 @@ func (v *DefaultDispatcher) waitAndDispatch(wait func() error, destination v2net
dispatcher.Dispatch(destination, link)
}

type DefaultDispatcherFactory struct{}

func (v DefaultDispatcherFactory) Create(space app.Space, config interface{}) (app.Application, error) {
return NewDefaultDispatcher(space), nil
}

func init() {
common.Must(app.RegisterApplicationFactory((*dispatcher.Config)(nil), DefaultDispatcherFactory{}))
common.Must(common.RegisterConfig((*dispatcher.Config)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
return NewDefaultDispatcher(ctx, config.(*dispatcher.Config))
}))
}

type waitDataInspector struct {
Expand Down
3 changes: 1 addition & 2 deletions app/dns/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"net"

"v2ray.com/core/app"
"v2ray.com/core/common/serial"
)

// A DnsCache is an internal cache of DNS resolutions.
Expand All @@ -13,7 +12,7 @@ type Server interface {
}

func FromSpace(space app.Space) Server {
app := space.(app.AppGetter).GetApp(serial.GetMessageType((*Config)(nil)))
app := space.GetApplication((*Server)(nil))
if app == nil {
return nil
}
Expand Down
27 changes: 15 additions & 12 deletions app/dns/server/server.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
package server

import (
"context"
"net"
"sync"
"time"

dnsmsg "github.com/miekg/dns"
"v2ray.com/core/app"
"v2ray.com/core/app/dispatcher"
"v2ray.com/core/app/dns"
"v2ray.com/core/common"
"v2ray.com/core/common/errors"
"v2ray.com/core/common/log"
v2net "v2ray.com/core/common/net"

dnsmsg "github.com/miekg/dns"
)

const (
Expand All @@ -32,7 +32,11 @@ type CacheServer struct {
servers []NameServer
}

func NewCacheServer(space app.Space, config *dns.Config) *CacheServer {
func NewCacheServer(ctx context.Context, config *dns.Config) (*CacheServer, error) {
space := app.SpaceFromContext(ctx)
if space == nil {
return nil, errors.New("DNSCacheServer: No space in context.")
}
server := &CacheServer{
records: make(map[string]*DomainRecord),
servers: make([]NameServer, len(config.NameServers)),
Expand Down Expand Up @@ -62,7 +66,11 @@ func NewCacheServer(space app.Space, config *dns.Config) *CacheServer {
}
return nil
})
return server
return server, nil
}

func (CacheServer) Interface() interface{} {
return (*dns.Server)(nil)
}

// Private: Visible for testing.
Expand Down Expand Up @@ -109,13 +117,8 @@ func (v *CacheServer) Get(domain string) []net.IP {
return nil
}

type CacheServerFactory struct{}

func (v CacheServerFactory) Create(space app.Space, config interface{}) (app.Application, error) {
server := NewCacheServer(space, config.(*dns.Config))
return server, nil
}

func init() {
common.Must(app.RegisterApplicationFactory((*dns.Config)(nil), CacheServerFactory{}))
common.Must(common.RegisterConfig((*dns.Config)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
return NewCacheServer(ctx, config.(*dns.Config))
}))
}
27 changes: 16 additions & 11 deletions app/proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ import (
"net"
"time"

"context"

"v2ray.com/core/app"
"v2ray.com/core/app/proxyman"
"v2ray.com/core/common"
"v2ray.com/core/common/buf"
"v2ray.com/core/common/errors"
"v2ray.com/core/common/log"
v2net "v2ray.com/core/common/net"
"v2ray.com/core/common/serial"
"v2ray.com/core/transport/internet"
"v2ray.com/core/transport/ray"
)
Expand All @@ -21,7 +22,11 @@ type OutboundProxy struct {
outboundManager proxyman.OutboundHandlerManager
}

func NewOutboundProxy(space app.Space) *OutboundProxy {
func NewOutboundProxy(ctx context.Context, config *Config) (*OutboundProxy, error) {
space := app.SpaceFromContext(ctx)
if space == nil {
return nil, errors.New("OutboundProxy: No space in context.")
}
proxy := new(OutboundProxy)
space.OnInitialize(func() error {
proxy.outboundManager = proxyman.OutboundHandlerManagerFromSpace(space)
Expand All @@ -30,7 +35,11 @@ func NewOutboundProxy(space app.Space) *OutboundProxy {
}
return nil
})
return proxy
return proxy, nil
}

func (OutboundProxy) Interface() interface{} {
return (*OutboundProxy)(nil)
}

func (v *OutboundProxy) RegisterDialer() {
Expand Down Expand Up @@ -132,20 +141,16 @@ func (v *Connection) SetReusable(bool) {

}

type OutboundProxyFactory struct{}

func (OutboundProxyFactory) Create(space app.Space, config interface{}) (app.Application, error) {
return NewOutboundProxy(space), nil
}

func OutboundProxyFromSpace(space app.Space) *OutboundProxy {
app := space.(app.AppGetter).GetApp(serial.GetMessageType((*Config)(nil)))
app := space.GetApplication((*OutboundProxy)(nil))
if app == nil {
return nil
}
return app.(*OutboundProxy)
}

func init() {
common.Must(app.RegisterApplicationFactory((*Config)(nil), OutboundProxyFactory{}))
common.Must(common.RegisterConfig((*Config)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
return NewOutboundProxy(ctx, config.(*Config))
}))
}
5 changes: 2 additions & 3 deletions app/proxy/proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func TestProxyDial(t *testing.T) {

space := app.NewSpace()
ctx := app.ContextWithSpace(context.Background(), space)
assert.Error(space.AddApp(new(proxyman.OutboundConfig)))
assert.Error(app.AddApplicationToSpace(ctx, new(proxyman.OutboundConfig))).IsNil()
outboundManager := proxyman.OutboundHandlerManagerFromSpace(space)
freedom, err := freedom.New(proxy.ContextWithOutboundMeta(ctx, &proxy.OutboundHandlerMeta{
Tag: "tag",
Expand All @@ -34,9 +34,8 @@ func TestProxyDial(t *testing.T) {
assert.Error(err).IsNil()
common.Must(outboundManager.SetHandler("tag", freedom))

assert.Error(space.AddApp(new(Config))).IsNil()
assert.Error(app.AddApplicationToSpace(ctx, new(Config))).IsNil()
proxy := OutboundProxyFromSpace(space)

assert.Error(space.Initialize()).IsNil()

xor := func(b []byte) []byte {
Expand Down
21 changes: 11 additions & 10 deletions app/proxyman/outbound/outbound.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ package outbound
import (
"sync"

"v2ray.com/core/app"
"context"

"v2ray.com/core/app/proxyman"
"v2ray.com/core/common"
"v2ray.com/core/proxy"
Expand All @@ -15,10 +16,14 @@ type DefaultOutboundHandlerManager struct {
taggedHandler map[string]proxy.OutboundHandler
}

func New() *DefaultOutboundHandlerManager {
func New(ctx context.Context, config *proxyman.OutboundConfig) (*DefaultOutboundHandlerManager, error) {
return &DefaultOutboundHandlerManager{
taggedHandler: make(map[string]proxy.OutboundHandler),
}
}, nil
}

func (DefaultOutboundHandlerManager) Interface() interface{} {
return (*proxyman.OutboundHandlerManager)(nil)
}

func (v *DefaultOutboundHandlerManager) GetDefaultHandler() proxy.OutboundHandler {
Expand Down Expand Up @@ -54,12 +59,8 @@ func (v *DefaultOutboundHandlerManager) SetHandler(tag string, handler proxy.Out
return nil
}

type OutboundHandlerManagerFactory struct{}

func (v OutboundHandlerManagerFactory) Create(space app.Space, config interface{}) (app.Application, error) {
return New(), nil
}

func init() {
common.Must(app.RegisterApplicationFactory((*proxyman.OutboundConfig)(nil), OutboundHandlerManagerFactory{}))
common.Must(common.RegisterConfig((*proxyman.OutboundConfig)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
return New(ctx, config.(*proxyman.OutboundConfig))
}))
}
5 changes: 2 additions & 3 deletions app/proxyman/proxyman.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package proxyman

import (
"v2ray.com/core/app"
"v2ray.com/core/common/serial"
"v2ray.com/core/proxy"
)

Expand All @@ -19,15 +18,15 @@ type OutboundHandlerManager interface {
}

func InboundHandlerManagerFromSpace(space app.Space) InboundHandlerManager {
app := space.(app.AppGetter).GetApp(serial.GetMessageType((*InboundConfig)(nil)))
app := space.GetApplication((*InboundHandlerManager)(nil))
if app == nil {
return nil
}
return app.(InboundHandlerManager)
}

func OutboundHandlerManagerFromSpace(space app.Space) OutboundHandlerManager {
app := space.(app.AppGetter).GetApp(serial.GetMessageType((*OutboundConfig)(nil)))
app := space.GetApplication((*OutboundHandlerManager)(nil))
if app == nil {
return nil
}
Expand Down
26 changes: 16 additions & 10 deletions app/router/router.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package router

import (
"context"

"v2ray.com/core/app"
"v2ray.com/core/app/dns"
"v2ray.com/core/common"
"v2ray.com/core/common/errors"
"v2ray.com/core/common/log"
v2net "v2ray.com/core/common/net"
"v2ray.com/core/common/serial"
"v2ray.com/core/proxy"
)

Expand All @@ -23,7 +24,11 @@ type Router struct {
dnsServer dns.Server
}

func NewRouter(config *Config, space app.Space) *Router {
func NewRouter(ctx context.Context, config *Config) (*Router, error) {
space := app.SpaceFromContext(ctx)
if space == nil {
return nil, errors.New("Router: No space in context.")
}
r := &Router{
domainStrategy: config.DomainStrategy,
//cache: NewRoutingTable(),
Expand All @@ -46,7 +51,7 @@ func NewRouter(config *Config, space app.Space) *Router {
}
return nil
})
return r
return r, nil
}

// Private: Visible for testing.
Expand Down Expand Up @@ -106,21 +111,22 @@ func (v *Router) TakeDetour(session *proxy.SessionInfo) (string, error) {
//return tag, err
}

type RouterFactory struct{}

func (RouterFactory) Create(space app.Space, config interface{}) (app.Application, error) {
router := NewRouter(config.(*Config), space)
return router, nil
func (Router) Interface() interface{} {
return (*Router)(nil)
}

type RouterFactory struct{}

func FromSpace(space app.Space) *Router {
app := space.(app.AppGetter).GetApp(serial.GetMessageType((*Config)(nil)))
app := space.GetApplication((*Router)(nil))
if app == nil {
return nil
}
return app.(*Router)
}

func init() {
common.Must(app.RegisterApplicationFactory((*Config)(nil), RouterFactory{}))
common.Must(common.RegisterConfig((*Config)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
return NewRouter(ctx, config.(*Config))
}))
}
10 changes: 6 additions & 4 deletions app/router/router_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package router_test

import (
"context"
"testing"

"v2ray.com/core/app"
Expand Down Expand Up @@ -31,10 +32,11 @@ func TestSimpleRouter(t *testing.T) {
}

space := app.NewSpace()
assert.Error(space.AddApp(new(dns.Config))).IsNil()
assert.Error(space.AddApp(new(dispatcher.Config))).IsNil()
assert.Error(space.AddApp(new(proxyman.OutboundConfig))).IsNil()
assert.Error(space.AddApp(config)).IsNil()
ctx := app.ContextWithSpace(context.Background(), space)
assert.Error(app.AddApplicationToSpace(ctx, new(dns.Config))).IsNil()
assert.Error(app.AddApplicationToSpace(ctx, new(dispatcher.Config))).IsNil()
assert.Error(app.AddApplicationToSpace(ctx, new(proxyman.OutboundConfig))).IsNil()
assert.Error(app.AddApplicationToSpace(ctx, config)).IsNil()
assert.Error(space.Initialize()).IsNil()

r := FromSpace(space)
Expand Down
Loading

0 comments on commit 17504d2

Please sign in to comment.