Browse Source

Add environment support in root level instance

Shelikhoo 3 years ago
parent
commit
0ae6c7119e
1 changed files with 12 additions and 3 deletions
  1. 12 3
      v2ray.go

+ 12 - 3
v2ray.go

@@ -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
 		}
 		}