rootcap_impl.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. package environment
  2. import (
  3. "context"
  4. "github.com/v2fly/v2ray-core/v5/common/platform/filesystem/fsifce"
  5. "github.com/v2fly/v2ray-core/v5/features/extension/storage"
  6. "github.com/v2fly/v2ray-core/v5/transport/internet"
  7. "github.com/v2fly/v2ray-core/v5/transport/internet/tagged"
  8. )
  9. func NewRootEnvImpl(ctx context.Context, transientStorage storage.ScopedTransientStorage,
  10. systemDialer internet.SystemDialer, systemListener internet.SystemListener,
  11. filesystem FileSystemCapabilitySet, persistStorage storage.ScopedPersistentStorage,
  12. ) RootEnvironment {
  13. return &rootEnvImpl{
  14. transientStorage: transientStorage,
  15. systemListener: systemListener,
  16. systemDialer: systemDialer,
  17. filesystem: filesystem,
  18. persistStorage: persistStorage,
  19. ctx: ctx,
  20. }
  21. }
  22. type rootEnvImpl struct {
  23. persistStorage storage.ScopedPersistentStorage
  24. transientStorage storage.ScopedTransientStorage
  25. systemDialer internet.SystemDialer
  26. systemListener internet.SystemListener
  27. filesystem FileSystemCapabilitySet
  28. ctx context.Context
  29. }
  30. func (r *rootEnvImpl) doNotImpl() {
  31. panic("placeholder doNotImpl")
  32. }
  33. func (r *rootEnvImpl) AppEnvironment(tag string) AppEnvironment {
  34. transientStorage, err := r.transientStorage.NarrowScope(r.ctx, tag)
  35. if err != nil {
  36. return nil
  37. }
  38. persistStorage, err := r.persistStorage.NarrowScope(r.ctx, []byte(tag))
  39. if err != nil {
  40. return nil
  41. }
  42. return &appEnvImpl{
  43. transientStorage: transientStorage,
  44. persistStorage: persistStorage,
  45. systemListener: r.systemListener,
  46. systemDialer: r.systemDialer,
  47. filesystem: r.filesystem,
  48. ctx: r.ctx,
  49. }
  50. }
  51. func (r *rootEnvImpl) ProxyEnvironment(tag string) ProxyEnvironment {
  52. transientStorage, err := r.transientStorage.NarrowScope(r.ctx, tag)
  53. if err != nil {
  54. return nil
  55. }
  56. return &proxyEnvImpl{
  57. transientStorage: transientStorage,
  58. systemListener: r.systemListener,
  59. systemDialer: r.systemDialer,
  60. ctx: r.ctx,
  61. }
  62. }
  63. func (r *rootEnvImpl) DropProxyEnvironment(tag string) error {
  64. transientStorage, err := r.transientStorage.NarrowScope(r.ctx, tag)
  65. if err != nil {
  66. return err
  67. }
  68. return transientStorage.DropScope(r.ctx, tag)
  69. }
  70. type appEnvImpl struct {
  71. persistStorage storage.ScopedPersistentStorage
  72. transientStorage storage.ScopedTransientStorage
  73. systemDialer internet.SystemDialer
  74. systemListener internet.SystemListener
  75. filesystem FileSystemCapabilitySet
  76. ctx context.Context
  77. }
  78. func (a *appEnvImpl) RequireFeatures() interface{} {
  79. panic("implement me")
  80. }
  81. func (a *appEnvImpl) RecordLog() interface{} {
  82. panic("implement me")
  83. }
  84. func (a *appEnvImpl) Dialer() internet.SystemDialer {
  85. panic("implement me")
  86. }
  87. func (a *appEnvImpl) Listener() internet.SystemListener {
  88. panic("implement me")
  89. }
  90. func (a *appEnvImpl) OutboundDialer() tagged.DialFunc {
  91. return internet.DialTaggedOutbound
  92. }
  93. func (a *appEnvImpl) OpenFileForReadSeek() fsifce.FileSeekerFunc {
  94. return a.filesystem.OpenFileForReadSeek()
  95. }
  96. func (a *appEnvImpl) OpenFileForRead() fsifce.FileReaderFunc {
  97. return a.filesystem.OpenFileForRead()
  98. }
  99. func (a *appEnvImpl) OpenFileForWrite() fsifce.FileWriterFunc {
  100. return a.filesystem.OpenFileForWrite()
  101. }
  102. func (a *appEnvImpl) ReadDir() fsifce.FileReadDirFunc {
  103. return a.filesystem.ReadDir()
  104. }
  105. func (a *appEnvImpl) RemoveFile() fsifce.FileRemoveFunc {
  106. return a.filesystem.RemoveFile()
  107. }
  108. func (a *appEnvImpl) PersistentStorage() storage.ScopedPersistentStorage {
  109. return a.persistStorage
  110. }
  111. func (a *appEnvImpl) TransientStorage() storage.ScopedTransientStorage {
  112. return a.transientStorage
  113. }
  114. func (a *appEnvImpl) NarrowScope(key string) (AppEnvironment, error) {
  115. transientStorage, err := a.transientStorage.NarrowScope(a.ctx, key)
  116. if err != nil {
  117. return nil, err
  118. }
  119. return &appEnvImpl{
  120. transientStorage: transientStorage,
  121. systemDialer: a.systemDialer,
  122. systemListener: a.systemListener,
  123. ctx: a.ctx,
  124. }, nil
  125. }
  126. func (a *appEnvImpl) doNotImpl() {
  127. panic("placeholder doNotImpl")
  128. }
  129. type proxyEnvImpl struct {
  130. transientStorage storage.ScopedTransientStorage
  131. systemDialer internet.SystemDialer
  132. systemListener internet.SystemListener
  133. ctx context.Context
  134. }
  135. func (p *proxyEnvImpl) RequireFeatures() interface{} {
  136. panic("implement me")
  137. }
  138. func (p *proxyEnvImpl) RecordLog() interface{} {
  139. panic("implement me")
  140. }
  141. func (p *proxyEnvImpl) OutboundDialer() tagged.DialFunc {
  142. panic("implement me")
  143. }
  144. func (p *proxyEnvImpl) TransientStorage() storage.ScopedTransientStorage {
  145. return p.transientStorage
  146. }
  147. func (p *proxyEnvImpl) NarrowScope(key string) (ProxyEnvironment, error) {
  148. transientStorage, err := p.transientStorage.NarrowScope(p.ctx, key)
  149. if err != nil {
  150. return nil, err
  151. }
  152. return &proxyEnvImpl{
  153. transientStorage: transientStorage,
  154. ctx: p.ctx,
  155. }, nil
  156. }
  157. func (p *proxyEnvImpl) NarrowScopeToTransport(key string) (TransportEnvironment, error) {
  158. transientStorage, err := p.transientStorage.NarrowScope(p.ctx, key)
  159. if err != nil {
  160. return nil, err
  161. }
  162. return &transportEnvImpl{
  163. ctx: p.ctx,
  164. transientStorage: transientStorage,
  165. systemDialer: p.systemDialer,
  166. systemListener: p.systemListener,
  167. }, nil
  168. }
  169. func (p *proxyEnvImpl) doNotImpl() {
  170. panic("placeholder doNotImpl")
  171. }
  172. type transportEnvImpl struct {
  173. transientStorage storage.ScopedTransientStorage
  174. systemDialer internet.SystemDialer
  175. systemListener internet.SystemListener
  176. ctx context.Context
  177. }
  178. func (t *transportEnvImpl) RequireFeatures() interface{} {
  179. panic("implement me")
  180. }
  181. func (t *transportEnvImpl) RecordLog() interface{} {
  182. panic("implement me")
  183. }
  184. func (t *transportEnvImpl) Dialer() internet.SystemDialer {
  185. return t.systemDialer
  186. }
  187. func (t *transportEnvImpl) Listener() internet.SystemListener {
  188. return t.systemListener
  189. }
  190. func (t *transportEnvImpl) OutboundDialer() tagged.DialFunc {
  191. panic("implement me")
  192. }
  193. func (t *transportEnvImpl) TransientStorage() storage.ScopedTransientStorage {
  194. return t.transientStorage
  195. }
  196. func (t *transportEnvImpl) NarrowScope(key string) (TransportEnvironment, error) {
  197. transientStorage, err := t.transientStorage.NarrowScope(t.ctx, key)
  198. if err != nil {
  199. return nil, err
  200. }
  201. return &transportEnvImpl{
  202. ctx: t.ctx,
  203. transientStorage: transientStorage,
  204. }, nil
  205. }
  206. func (t *transportEnvImpl) doNotImpl() {
  207. panic("implement me")
  208. }