config_cache.go 976 B

12345678910111213141516171819202122232425262728293031323334353637
  1. package json
  2. import (
  3. "github.com/v2ray/v2ray-core/proxy/common/config"
  4. )
  5. type ConfigObjectCreator func() interface{}
  6. var (
  7. configCache = make(map[string]ConfigObjectCreator)
  8. )
  9. func getConfigKey(protocol string, cType config.Type) string {
  10. return protocol + "_" + string(cType)
  11. }
  12. func registerConfigType(protocol string, cType config.Type, creator ConfigObjectCreator) error {
  13. // TODO: check name
  14. configCache[getConfigKey(protocol, cType)] = creator
  15. return nil
  16. }
  17. func RegisterInboundConnectionConfig(protocol string, creator ConfigObjectCreator) error {
  18. return registerConfigType(protocol, config.TypeInbound, creator)
  19. }
  20. func RegisterOutboundConnectionConfig(protocol string, creator ConfigObjectCreator) error {
  21. return registerConfigType(protocol, config.TypeOutbound, creator)
  22. }
  23. func CreateConfig(protocol string, cType config.Type) interface{} {
  24. creator, found := configCache[getConfigKey(protocol, cType)]
  25. if !found {
  26. return nil
  27. }
  28. return creator()
  29. }