session.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  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. type ConfigureLoadingEnvironment interface {
  21. GetGeoLoader() geodata.Loader
  22. }
  23. func NewConfigureLoadingContext(ctx context.Context) context.Context {
  24. environment := &configureLoadingEnvironment{}
  25. return context.WithValue(ctx, confContextKey, environment)
  26. }
  27. func GetConfigureLoadingEnvironment(ctx context.Context) ConfigureLoadingEnvironment {
  28. return ctx.Value(confContextKey).(ConfigureLoadingEnvironment)
  29. }
  30. func SetGeoDataLoader(ctx context.Context, loader geodata.Loader) {
  31. GetConfigureLoadingEnvironment(ctx).(*configureLoadingEnvironment).geoLoader = loader
  32. }