Skip to content

Commit

Permalink
refine app settings in v2ray config
Browse files Browse the repository at this point in the history
  • Loading branch information
DarienRaymond committed Oct 16, 2016
1 parent e33b7df commit e866ff2
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 91 deletions.
16 changes: 16 additions & 0 deletions app/dns/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"v2ray.com/core/app"
"v2ray.com/core/app/dispatcher"
"v2ray.com/core/common/loader"
"v2ray.com/core/common/log"
v2net "v2ray.com/core/common/net"

Expand Down Expand Up @@ -111,3 +112,18 @@ func (this *CacheServer) Get(domain string) []net.IP {
log.Debug("DNS: Returning nil for domain ", domain)
return nil
}

type CacheServerFactory struct{}

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

func (this CacheServerFactory) AppId() app.ID {
return APP_ID
}

func init() {
app.RegisterApplicationFactory(loader.GetType(new(Config)), CacheServerFactory{})
}
16 changes: 16 additions & 0 deletions app/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"v2ray.com/core/app"
"v2ray.com/core/app/dns"
"v2ray.com/core/common/loader"
"v2ray.com/core/common/log"
v2net "v2ray.com/core/common/net"
)
Expand Down Expand Up @@ -107,3 +108,18 @@ func (this *Router) TakeDetour(dest v2net.Destination) (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 (RouterFactory) AppId() app.ID {
return APP_ID
}

func init() {
app.RegisterApplicationFactory(loader.GetType(new(Config)), RouterFactory{})
}
27 changes: 27 additions & 0 deletions app/space.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,19 @@ type Application interface {
}

type ApplicationInitializer func() error
type ApplicationFactory interface {
Create(space Space, config interface{}) (Application, error)
AppId() ID
}

var (
applicationFactoryCache map[string]ApplicationFactory
)

func RegisterApplicationFactory(name string, factory ApplicationFactory) error {
applicationFactoryCache[name] = factory
return nil
}

// A Space contains all apps that may be available in a V2Ray runtime.
// Caller must check the availability of an app by calling HasXXX before getting its instance.
Expand All @@ -36,6 +49,7 @@ type Space interface {
HasApp(ID) bool
GetApp(ID) Application
BindApp(ID, Application)
BindFromConfig(name string, config interface{}) error
}

type spaceImpl struct {
Expand Down Expand Up @@ -80,3 +94,16 @@ func (this *spaceImpl) GetApp(id ID) Application {
func (this *spaceImpl) BindApp(id ID, application Application) {
this.cache[id] = application
}

func (this *spaceImpl) BindFromConfig(name string, config interface{}) error {
factory, found := applicationFactoryCache[name]
if !found {
return errors.New("Space: app not registered: " + name)
}
app, err := factory.Create(this, config)
if err != nil {
return err
}
this.BindApp(factory.AppId(), app)
return nil
}
127 changes: 57 additions & 70 deletions config.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 2 additions & 5 deletions config.proto
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ option go_package = "core";
option java_package = "com.v2ray.core";
option java_outer_classname = "ConfigProto";

import "v2ray.com/core/app/router/config.proto";
import "v2ray.com/core/app/dns/config.proto";
import "v2ray.com/core/common/loader/type.proto";
import "v2ray.com/core/common/net/port.proto";
import "v2ray.com/core/common/net/address.proto";
Expand Down Expand Up @@ -70,7 +68,6 @@ message Config {
repeated InboundConnectionConfig inbound = 1;
repeated OutboundConnectionConfig outbound = 2;
v2ray.core.common.log.Config log = 3;
v2ray.core.app.router.Config router = 4;
v2ray.core.app.dns.Config dns = 5;
v2ray.core.transport.Config transport = 6;
repeated v2ray.core.common.loader.TypedSettings app = 4;
v2ray.core.transport.Config transport = 5;
}
27 changes: 11 additions & 16 deletions v2ray.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ import (
"v2ray.com/core/app"
"v2ray.com/core/app/dispatcher"
dispatchers "v2ray.com/core/app/dispatcher/impl"
"v2ray.com/core/app/dns"
"v2ray.com/core/app/proxyman"
"v2ray.com/core/app/router"
"v2ray.com/core/common"
"v2ray.com/core/common/log"
"v2ray.com/core/proxy"
Expand All @@ -21,8 +19,7 @@ type Point struct {
outboundHandlers []proxy.OutboundHandler
taggedOutboundHandlers map[string]proxy.OutboundHandler

router *router.Router
space app.Space
space app.Space
}

// NewPoint returns a new Point server based on given configuration.
Expand All @@ -38,23 +35,21 @@ func NewPoint(pConfig *Config) (*Point, error) {
return nil, err
}

vpoint.space = app.NewSpace()
space := app.NewSpace()
vpoint.space = space
vpoint.space.BindApp(proxyman.APP_ID_INBOUND_MANAGER, vpoint)

outboundHandlerManager := proxyman.NewDefaultOutboundHandlerManager()
vpoint.space.BindApp(proxyman.APP_ID_OUTBOUND_MANAGER, outboundHandlerManager)

dnsConfig := pConfig.Dns
if dnsConfig != nil {
dnsServer := dns.NewCacheServer(vpoint.space, dnsConfig)
vpoint.space.BindApp(dns.APP_ID, dnsServer)
}

routerConfig := pConfig.Router
if routerConfig != nil {
r := router.NewRouter(routerConfig, vpoint.space)
vpoint.space.BindApp(router.APP_ID, r)
vpoint.router = r
for _, app := range pConfig.App {
settings, err := app.GetInstance()
if err != nil {
return nil, err
}
if err := space.BindFromConfig(app.Type, settings); err != nil {
return nil, err
}
}

vpoint.space.BindApp(dispatcher.APP_ID, dispatchers.NewDefaultDispatcher(vpoint.space))
Expand Down

0 comments on commit e866ff2

Please sign in to comment.