session.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package cfgcommon
  2. import (
  3. "context"
  4. "github.com/v2fly/v2ray-core/v4/common"
  5. "github.com/v2fly/v2ray-core/v4/infra/conf/geodata"
  6. )
  7. type configureLoadingContext int
  8. const confContextKey = configureLoadingContext(1)
  9. type configureLoadingEnvironment struct {
  10. geoLoader geodata.Loader
  11. }
  12. func (c *configureLoadingEnvironment) GetGeoLoader() geodata.Loader {
  13. if c.geoLoader == nil {
  14. var err error
  15. c.geoLoader, err = geodata.GetGeoDataLoader("standard")
  16. common.Must(err)
  17. }
  18. return c.geoLoader
  19. }
  20. func (c *configureLoadingEnvironment) doNotImpl() {}
  21. type ConfigureLoadingEnvironmentCapabilitySet interface {
  22. GetGeoLoader() geodata.Loader
  23. }
  24. type ConfigureLoadingEnvironment interface {
  25. ConfigureLoadingEnvironmentCapabilitySet
  26. doNotImpl()
  27. // TODO environment.BaseEnvironmentCapabilitySet
  28. // TODO environment.FileSystemCapabilitySet
  29. }
  30. func NewConfigureLoadingContext(ctx context.Context) context.Context {
  31. environment := &configureLoadingEnvironment{}
  32. return context.WithValue(ctx, confContextKey, environment)
  33. }
  34. func GetConfigureLoadingEnvironment(ctx context.Context) ConfigureLoadingEnvironment {
  35. return ctx.Value(confContextKey).(ConfigureLoadingEnvironment)
  36. }
  37. func SetGeoDataLoader(ctx context.Context, loader geodata.Loader) {
  38. GetConfigureLoadingEnvironment(ctx).(*configureLoadingEnvironment).geoLoader = loader
  39. }