2016-01-02 11:40:51 -05:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2016-01-25 11:19:09 -05:00
|
|
|
func RegisterInboundConfig(protocol string, creator ConfigObjectCreator) error {
|
2016-01-02 11:40:51 -05:00
|
|
|
return registerConfigType(protocol, "inbound", creator)
|
|
|
|
}
|
|
|
|
|
2016-01-25 11:19:09 -05:00
|
|
|
func RegisterOutboundConfig(protocol string, creator ConfigObjectCreator) error {
|
2016-01-02 11:40:51 -05:00
|
|
|
return registerConfigType(protocol, "outbound", creator)
|
|
|
|
}
|
|
|
|
|
2016-01-25 11:23:10 -05:00
|
|
|
func CreateInboundConfig(protocol string, data []byte) (interface{}, error) {
|
2016-01-02 11:40:51 -05:00
|
|
|
creator, found := configCache[getConfigKey(protocol, "inbound")]
|
|
|
|
if !found {
|
|
|
|
return nil, errors.New(protocol + " not found.")
|
|
|
|
}
|
|
|
|
return creator(data)
|
|
|
|
}
|
|
|
|
|
2016-01-25 11:23:10 -05:00
|
|
|
func CreateOutboundConfig(protocol string, data []byte) (interface{}, error) {
|
2016-01-02 11:40:51 -05:00
|
|
|
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()
|
|
|
|
}
|