mirror of
https://github.com/v2fly/v2ray-core.git
synced 2024-11-09 03:37:37 -05:00
54 lines
1.3 KiB
Go
54 lines
1.3 KiB
Go
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()
|
|
}
|