You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
v2ray-core/proxy/internal/config/config_cache.go

54 lines
1.3 KiB

package config
import (
"errors"
)
type ConfigObjectCreator func(data []byte) (interface{}, error)
var (
configCache map[string]ConfigObjectCreator
)
func getConfigKey(protocol string, proxyType string) string {
return protocol + "_" + proxyType
}
func registerConfigType(protocol string, proxyType string, creator ConfigObjectCreator) error {
// TODO: check name
configCache[getConfigKey(protocol, proxyType)] = creator
return nil
}
func RegisterInboundConfig(protocol string, creator ConfigObjectCreator) error {
return registerConfigType(protocol, "inbound", creator)
}
func RegisterOutboundConfig(protocol string, creator ConfigObjectCreator) error {
return registerConfigType(protocol, "outbound", creator)
}
func CreateInboundConfig(protocol string, data []byte) (interface{}, error) {
creator, found := configCache[getConfigKey(protocol, "inbound")]
if !found {
return nil, errors.New(protocol + " not found.")
}
return creator(data)
}
func CreateOutboundConfig(protocol string, data []byte) (interface{}, error) {
creator, found := configCache[getConfigKey(protocol, "outbound")]
if !found {
return nil, errors.New(protocol + " not found.")
}
return creator(data)
}
func initializeConfigCache() {
configCache = make(map[string]ConfigObjectCreator)
}
func init() {
initializeConfigCache()
}