diff --git a/common/environment/app.go b/common/environment/app.go new file mode 100644 index 000000000..2b786febd --- /dev/null +++ b/common/environment/app.go @@ -0,0 +1,22 @@ +package environment + +import ( + "github.com/v2fly/v2ray-core/v4/features/extension" +) + +type AppEnvironmentCapabilitySet interface { + BaseEnvironmentCapabilitySet + SystemNetworkCapabilitySet + InstanceNetworkCapabilitySet + FileSystemCapabilitySet + + PersistentStorage() extension.ScopedPersistentStorage + TransientStorage() extension.ScopedTransientStorage +} + +type AppEnvironment interface { + AppEnvironmentCapabilitySet + + NarrowScope(key []byte) (AppEnvironment, error) + doNotImpl() +} diff --git a/common/environment/base.go b/common/environment/base.go new file mode 100644 index 000000000..7c69e3d6b --- /dev/null +++ b/common/environment/base.go @@ -0,0 +1,41 @@ +package environment + +import ( + "github.com/v2fly/v2ray-core/v4/common/log" + "github.com/v2fly/v2ray-core/v4/common/platform/filesystem" + "github.com/v2fly/v2ray-core/v4/transport/internet" + "github.com/v2fly/v2ray-core/v4/transport/internet/tagged" +) + +type BaseEnvironmentCapabilitySet interface { + FeaturesLookupCapabilitySet + LogCapabilitySet +} + +type BaseEnvironment interface { + BaseEnvironmentCapabilitySet + doNotImpl() +} + +type SystemNetworkCapabilitySet interface { + Dialer() internet.SystemDialer + Listener() internet.SystemListener +} + +type InstanceNetworkCapabilitySet interface { + OutboundDialer() tagged.DialFunc +} + +type FeaturesLookupCapabilitySet interface { + RequireFeatures(callback interface{}) error +} + +type LogCapabilitySet interface { + RecordLog(msg log.Message) +} + +type FileSystemCapabilitySet interface { + OpenFileForReadSeek() filesystem.FileSeekerFunc + OpenFileForRead() filesystem.FileReaderFunc + OpenFileForWrite() filesystem.FileWriterFunc +} diff --git a/common/environment/connection.go b/common/environment/connection.go new file mode 100644 index 000000000..d0bddb5ef --- /dev/null +++ b/common/environment/connection.go @@ -0,0 +1,17 @@ +package environment + +import "github.com/v2fly/v2ray-core/v4/common/log" + +type ConnectionCapabilitySet interface { + ConnectionLogCapabilitySet +} + +type ConnectionEnvironment interface { + ConnectionCapabilitySet + + doNotImpl() +} + +type ConnectionLogCapabilitySet interface { + RecordConnectionLog(msg log.Message) +} diff --git a/common/environment/proxy.go b/common/environment/proxy.go new file mode 100644 index 000000000..483cea332 --- /dev/null +++ b/common/environment/proxy.go @@ -0,0 +1,19 @@ +package environment + +import "github.com/v2fly/v2ray-core/v4/features/extension" + +type ProxyEnvironmentCapabilitySet interface { + BaseEnvironmentCapabilitySet + InstanceNetworkCapabilitySet + + TransientStorage() extension.ScopedTransientStorage +} + +type ProxyEnvironment interface { + ProxyEnvironmentCapabilitySet + + NarrowScope(key []byte) (ProxyEnvironment, error) + NarrowScopeToTransport(key []byte) (TransportEnvironment, error) + + doNotImpl() +} diff --git a/common/environment/transport.go b/common/environment/transport.go new file mode 100644 index 000000000..180d9ff47 --- /dev/null +++ b/common/environment/transport.go @@ -0,0 +1,18 @@ +package environment + +import "github.com/v2fly/v2ray-core/v4/features/extension" + +type TransportEnvironmentCapacitySet interface { + BaseEnvironmentCapabilitySet + SystemNetworkCapabilitySet + InstanceNetworkCapabilitySet + + TransientStorage() extension.ScopedTransientStorage +} + +type TransportEnvironment interface { + TransportEnvironmentCapacitySet + + NarrowScope(key []byte) (TransportEnvironment, error) + doNotImpl() +} diff --git a/features/extension/storage.go b/features/extension/storage.go new file mode 100644 index 000000000..4435930ce --- /dev/null +++ b/features/extension/storage.go @@ -0,0 +1,35 @@ +package extension + +import ( + "context" + "github.com/v2fly/v2ray-core/v4/features" +) + +type PersistentStorageEngine interface { + features.Feature + + PersistentStorageEngine() + Put(ctx context.Context, key []byte, value []byte) error + Get(ctx context.Context, key []byte) ([]byte, error) + List(ctx context.Context, keyPrefix []byte) ([][]byte, error) +} + +type ScopedPersistentStorage interface { + ScopedPersistentStorageEngine() + + Put(ctx context.Context, key []byte, value []byte) error + Get(ctx context.Context, key []byte) ([]byte, error) + List(ctx context.Context, keyPrefix []byte) ([][]byte, error) + + ClearIfCharacteristicMismatch(ctx context.Context, characteristic []byte) error + NarrowScope(ctx context.Context, key []byte) (ScopedPersistentStorage, error) +} + +type ScopedTransientStorage interface { + ScopedTransientStorage() + Put(ctx context.Context, key []byte, value interface{}) error + Get(ctx context.Context, key []byte) (interface{}, error) + List(ctx context.Context, keyPrefix []byte) ([][]byte, error) + Clear(ctx context.Context) + NarrowScope(ctx context.Context, key []byte) (ScopedPersistentStorage, error) +} diff --git a/infra/conf/cfgcommon/session.go b/infra/conf/cfgcommon/session.go index 283fe31f1..2fcf73882 100644 --- a/infra/conf/cfgcommon/session.go +++ b/infra/conf/cfgcommon/session.go @@ -2,7 +2,6 @@ package cfgcommon import ( "context" - "github.com/v2fly/v2ray-core/v4/common" "github.com/v2fly/v2ray-core/v4/infra/conf/geodata" ) @@ -24,10 +23,20 @@ func (c *configureLoadingEnvironment) GetGeoLoader() geodata.Loader { return c.geoLoader } -type ConfigureLoadingEnvironment interface { +func (c *configureLoadingEnvironment) doNotImpl() {} + +type ConfigureLoadingEnvironmentCapabilitySet interface { GetGeoLoader() geodata.Loader } +type ConfigureLoadingEnvironment interface { + ConfigureLoadingEnvironmentCapabilitySet + doNotImpl() + + // TODO environment.BaseEnvironmentCapabilitySet + // TODO environment.FileSystemCapabilitySet +} + func NewConfigureLoadingContext(ctx context.Context) context.Context { environment := &configureLoadingEnvironment{} return context.WithValue(ctx, confContextKey, environment) diff --git a/transport/internet/system_listener.go b/transport/internet/system_listener.go index a13a36768..435cce421 100644 --- a/transport/internet/system_listener.go +++ b/transport/internet/system_listener.go @@ -102,3 +102,8 @@ func RegisterListenerController(controller func(network, address string, fd uint effectiveListener.controllers = append(effectiveListener.controllers, controller) return nil } + +type SystemListener interface { + Listen(ctx context.Context, addr net.Addr, sockopt *SocketConfig) (net.Listener, error) + ListenPacket(ctx context.Context, addr net.Addr, sockopt *SocketConfig) (net.PacketConn, error) +}