mirror of
https://github.com/v2fly/v2ray-core.git
synced 2024-11-04 17:27:23 -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 RegisterInboundConnectionConfig(protocol string, creator ConfigObjectCreator) error {
|
|
return registerConfigType(protocol, "inbound", creator)
|
|
}
|
|
|
|
func RegisterOutboundConnectionConfig(protocol string, creator ConfigObjectCreator) error {
|
|
return registerConfigType(protocol, "outbound", creator)
|
|
}
|
|
|
|
func CreateInboundConnectionConfig(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 CreateOutboundConnectionConfig(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()
|
|
}
|