Browse Source

refactor environment context

Shelikhoo 4 years ago
parent
commit
141cd30ee3

+ 3 - 3
common/environment/app.go

@@ -1,7 +1,7 @@
 package environment
 package environment
 
 
 import (
 import (
-	"github.com/v2fly/v2ray-core/v4/features/extension"
+	"github.com/v2fly/v2ray-core/v4/features/extension/storage"
 )
 )
 
 
 type AppEnvironmentCapabilitySet interface {
 type AppEnvironmentCapabilitySet interface {
@@ -10,8 +10,8 @@ type AppEnvironmentCapabilitySet interface {
 	InstanceNetworkCapabilitySet
 	InstanceNetworkCapabilitySet
 	FileSystemCapabilitySet
 	FileSystemCapabilitySet
 
 
-	PersistentStorage() extension.ScopedPersistentStorage
-	TransientStorage() extension.ScopedTransientStorage
+	PersistentStorage() storage.ScopedPersistentStorage
+	TransientStorage() storage.ScopedTransientStorage
 }
 }
 
 
 type AppEnvironment interface {
 type AppEnvironment interface {

+ 2 - 4
common/environment/base.go

@@ -1,8 +1,8 @@
 package environment
 package environment
 
 
 import (
 import (
+	"github.com/v2fly/v2ray-core/v4/common/environment/filesystemcap"
 	"github.com/v2fly/v2ray-core/v4/common/log"
 	"github.com/v2fly/v2ray-core/v4/common/log"
-	"github.com/v2fly/v2ray-core/v4/common/platform/filesystem/fsifce"
 	"github.com/v2fly/v2ray-core/v4/transport/internet"
 	"github.com/v2fly/v2ray-core/v4/transport/internet"
 	"github.com/v2fly/v2ray-core/v4/transport/internet/tagged"
 	"github.com/v2fly/v2ray-core/v4/transport/internet/tagged"
 )
 )
@@ -35,7 +35,5 @@ type LogCapabilitySet interface {
 }
 }
 
 
 type FileSystemCapabilitySet interface {
 type FileSystemCapabilitySet interface {
-	OpenFileForReadSeek() fsifce.FileSeekerFunc
-	OpenFileForRead() fsifce.FileReaderFunc
-	OpenFileForWrite() fsifce.FileWriterFunc
+	filesystemcap.FileSystemCapabilitySet
 }
 }

+ 20 - 0
common/environment/envctx/env.go

@@ -0,0 +1,20 @@
+package envctx
+
+import "context"
+
+type environmentContextKey int
+
+const (
+	environmentKey environmentContextKey = iota
+)
+
+func ContextWithEnvironment(ctx context.Context, environment interface{}) context.Context {
+	return context.WithValue(ctx, environmentKey, environment)
+}
+
+func EnvironmentFromContext(ctx context.Context) interface{} {
+	if environment, ok := ctx.Value(environmentKey).(interface{}); ok {
+		return environment
+	}
+	return nil
+}

+ 9 - 0
common/environment/filesystemcap/fscap.go

@@ -0,0 +1,9 @@
+package filesystemcap
+
+import "github.com/v2fly/v2ray-core/v4/common/platform/filesystem/fsifce"
+
+type FileSystemCapabilitySet interface {
+	OpenFileForReadSeek() fsifce.FileSeekerFunc
+	OpenFileForRead() fsifce.FileReaderFunc
+	OpenFileForWrite() fsifce.FileWriterFunc
+}

+ 4 - 2
common/environment/proxy.go

@@ -1,12 +1,14 @@
 package environment
 package environment
 
 
-import "github.com/v2fly/v2ray-core/v4/features/extension"
+import (
+	"github.com/v2fly/v2ray-core/v4/features/extension/storage"
+)
 
 
 type ProxyEnvironmentCapabilitySet interface {
 type ProxyEnvironmentCapabilitySet interface {
 	BaseEnvironmentCapabilitySet
 	BaseEnvironmentCapabilitySet
 	InstanceNetworkCapabilitySet
 	InstanceNetworkCapabilitySet
 
 
-	TransientStorage() extension.ScopedTransientStorage
+	TransientStorage() storage.ScopedTransientStorage
 }
 }
 
 
 type ProxyEnvironment interface {
 type ProxyEnvironment interface {

+ 4 - 2
common/environment/transport.go

@@ -1,13 +1,15 @@
 package environment
 package environment
 
 
-import "github.com/v2fly/v2ray-core/v4/features/extension"
+import (
+	"github.com/v2fly/v2ray-core/v4/features/extension/storage"
+)
 
 
 type TransportEnvironmentCapacitySet interface {
 type TransportEnvironmentCapacitySet interface {
 	BaseEnvironmentCapabilitySet
 	BaseEnvironmentCapabilitySet
 	SystemNetworkCapabilitySet
 	SystemNetworkCapabilitySet
 	InstanceNetworkCapabilitySet
 	InstanceNetworkCapabilitySet
 
 
-	TransientStorage() extension.ScopedTransientStorage
+	TransientStorage() storage.ScopedTransientStorage
 }
 }
 
 
 type TransportEnvironment interface {
 type TransportEnvironment interface {

+ 0 - 12
common/session/context.go

@@ -15,7 +15,6 @@ const (
 	sockoptSessionKey
 	sockoptSessionKey
 	trackedConnectionErrorKey
 	trackedConnectionErrorKey
 	handlerSessionKey
 	handlerSessionKey
-	environmentKey
 )
 )
 
 
 // ContextWithID returns a new context with the given ID.
 // ContextWithID returns a new context with the given ID.
@@ -134,14 +133,3 @@ func SubmitOutboundErrorToOriginator(ctx context.Context, err error) {
 func TrackedConnectionError(ctx context.Context, tracker TrackedRequestErrorFeedback) context.Context {
 func TrackedConnectionError(ctx context.Context, tracker TrackedRequestErrorFeedback) context.Context {
 	return context.WithValue(ctx, trackedConnectionErrorKey, tracker)
 	return context.WithValue(ctx, trackedConnectionErrorKey, tracker)
 }
 }
-
-func ContextWithEnvironment(ctx context.Context, environment interface{}) context.Context {
-	return context.WithValue(ctx, environmentKey, environment)
-}
-
-func EnvironmentFromContext(ctx context.Context) interface{} {
-	if environment, ok := ctx.Value(environmentKey).(interface{}); ok {
-		return environment
-	}
-	return nil
-}