github.com/v2fly/v2ray-core/v5@v5.16.2-0.20240507031116-8191faa6e095/infra/conf/cfgcommon/session.go (about)

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