1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-03 01:38:24 -04:00

Add environment support in root level instance

This commit is contained in:
Shelikhoo 2022-02-10 23:20:39 +00:00 committed by Xiaokang Wang (Shelikhoo)
parent 7e35509381
commit 0ae6c7119e

View File

@ -6,6 +6,8 @@ import (
"sync" "sync"
"github.com/v2fly/v2ray-core/v5/common" "github.com/v2fly/v2ray-core/v5/common"
"github.com/v2fly/v2ray-core/v5/common/environment"
"github.com/v2fly/v2ray-core/v5/common/environment/transientstorageimpl"
"github.com/v2fly/v2ray-core/v5/common/serial" "github.com/v2fly/v2ray-core/v5/common/serial"
"github.com/v2fly/v2ray-core/v5/features" "github.com/v2fly/v2ray-core/v5/features"
"github.com/v2fly/v2ray-core/v5/features/dns" "github.com/v2fly/v2ray-core/v5/features/dns"
@ -90,13 +92,15 @@ type Instance struct {
features []features.Feature features []features.Feature
featureResolutions []resolution featureResolutions []resolution
running bool running bool
env environment.RootEnvironment
ctx context.Context ctx context.Context
} }
func AddInboundHandler(server *Instance, config *InboundHandlerConfig) error { func AddInboundHandler(server *Instance, config *InboundHandlerConfig) error {
inboundManager := server.GetFeature(inbound.ManagerType()).(inbound.Manager) inboundManager := server.GetFeature(inbound.ManagerType()).(inbound.Manager)
rawHandler, err := CreateObject(server, config) proxyEnv := server.env.ProxyEnvironment("i" + config.Tag)
rawHandler, err := CreateObjectWithEnvironment(server, config, proxyEnv)
if err != nil { if err != nil {
return err return err
} }
@ -122,7 +126,8 @@ func addInboundHandlers(server *Instance, configs []*InboundHandlerConfig) error
func AddOutboundHandler(server *Instance, config *OutboundHandlerConfig) error { func AddOutboundHandler(server *Instance, config *OutboundHandlerConfig) error {
outboundManager := server.GetFeature(outbound.ManagerType()).(outbound.Manager) outboundManager := server.GetFeature(outbound.ManagerType()).(outbound.Manager)
rawHandler, err := CreateObject(server, config) proxyEnv := server.env.ProxyEnvironment("o" + config.Tag)
rawHandler, err := CreateObjectWithEnvironment(server, config, proxyEnv)
if err != nil { if err != nil {
return err return err
} }
@ -186,12 +191,16 @@ func initInstanceWithConfig(config *Config, server *Instance) (bool, error) {
return true, err return true, err
} }
server.env = environment.NewRootEnvImpl(server.ctx, transientstorageimpl.NewScopedTransientStorageImpl())
for _, appSettings := range config.App { for _, appSettings := range config.App {
settings, err := serial.GetInstanceOf(appSettings) settings, err := serial.GetInstanceOf(appSettings)
if err != nil { if err != nil {
return true, err return true, err
} }
obj, err := CreateObject(server, settings) key := appSettings.TypeUrl
appEnv := server.env.AppEnvironment(key)
obj, err := CreateObjectWithEnvironment(server, settings, appEnv)
if err != nil { if err != nil {
return true, err return true, err
} }