mirror of
https://github.com/v2fly/v2ray-core.git
synced 2024-12-21 09:36:34 -05:00
Move config cache to proxy/common
This commit is contained in:
parent
c56e17fff9
commit
2b45e63607
@ -1,12 +1,5 @@
|
||||
package config
|
||||
|
||||
type Type string
|
||||
|
||||
const (
|
||||
TypeInbound = Type("inbound")
|
||||
TypeOutbound = Type("outbound")
|
||||
)
|
||||
|
||||
type RouterConfig interface {
|
||||
Strategy() string
|
||||
Settings() interface{}
|
||||
|
@ -1,20 +0,0 @@
|
||||
package json
|
||||
|
||||
import (
|
||||
"github.com/v2ray/v2ray-core/config"
|
||||
)
|
||||
|
||||
type ConfigObjectCreator func() interface{}
|
||||
|
||||
var (
|
||||
configCache = make(map[string]ConfigObjectCreator)
|
||||
)
|
||||
|
||||
func getConfigKey(protocol string, cType config.Type) string {
|
||||
return protocol + "_" + string(cType)
|
||||
}
|
||||
|
||||
func RegisterConfigType(protocol string, cType config.Type, creator ConfigObjectCreator) {
|
||||
// TODO: check name
|
||||
configCache[getConfigKey(protocol, cType)] = creator
|
||||
}
|
@ -4,13 +4,14 @@ import (
|
||||
"encoding/json"
|
||||
|
||||
"github.com/v2ray/v2ray-core/common/log"
|
||||
"github.com/v2ray/v2ray-core/config"
|
||||
proxyconfig "github.com/v2ray/v2ray-core/proxy/common/config"
|
||||
proxyjson "github.com/v2ray/v2ray-core/proxy/common/config/json"
|
||||
)
|
||||
|
||||
type ConnectionConfig struct {
|
||||
ProtocolString string `json:"protocol"`
|
||||
SettingsMessage json.RawMessage `json:"settings"`
|
||||
Type config.Type `json:"-"`
|
||||
ProtocolString string `json:"protocol"`
|
||||
SettingsMessage json.RawMessage `json:"settings"`
|
||||
Type proxyconfig.Type `json:"-"`
|
||||
}
|
||||
|
||||
func (c *ConnectionConfig) Protocol() string {
|
||||
@ -18,11 +19,10 @@ func (c *ConnectionConfig) Protocol() string {
|
||||
}
|
||||
|
||||
func (c *ConnectionConfig) Settings() interface{} {
|
||||
creator, found := configCache[getConfigKey(c.Protocol(), c.Type)]
|
||||
if !found {
|
||||
configObj := proxyjson.CreateConfig(c.Protocol(), c.Type)
|
||||
if configObj == nil {
|
||||
panic("Unknown protocol " + c.Protocol())
|
||||
}
|
||||
configObj := creator()
|
||||
err := json.Unmarshal(c.SettingsMessage, configObj)
|
||||
if err != nil {
|
||||
log.Error("Unable to parse connection config: %v", err)
|
||||
|
@ -7,6 +7,7 @@ import (
|
||||
|
||||
"github.com/v2ray/v2ray-core/common/log"
|
||||
"github.com/v2ray/v2ray-core/config"
|
||||
proxyconfig "github.com/v2ray/v2ray-core/proxy/common/config"
|
||||
)
|
||||
|
||||
// Config is the config for Point server.
|
||||
@ -57,8 +58,8 @@ func LoadConfig(file string) (*Config, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
jsonConfig.InboundConfigValue.Type = config.TypeInbound
|
||||
jsonConfig.OutboundConfigValue.Type = config.TypeOutbound
|
||||
jsonConfig.InboundConfigValue.Type = proxyconfig.TypeInbound
|
||||
jsonConfig.OutboundConfigValue.Type = proxyconfig.TypeOutbound
|
||||
|
||||
return jsonConfig, err
|
||||
}
|
||||
|
@ -1,15 +1,14 @@
|
||||
package json
|
||||
|
||||
import (
|
||||
"github.com/v2ray/v2ray-core/config"
|
||||
"github.com/v2ray/v2ray-core/config/json"
|
||||
"github.com/v2ray/v2ray-core/proxy/common/config/json"
|
||||
)
|
||||
|
||||
type BlackHoleConfig struct {
|
||||
}
|
||||
|
||||
func init() {
|
||||
json.RegisterConfigType("blackhole", config.TypeInbound, func() interface{} {
|
||||
json.RegisterOutboundConnectionConfig("blackhole", func() interface{} {
|
||||
return new(BlackHoleConfig)
|
||||
})
|
||||
}
|
||||
|
9
proxy/common/config/errors.go
Normal file
9
proxy/common/config/errors.go
Normal file
@ -0,0 +1,9 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"errors"
|
||||
)
|
||||
|
||||
var (
|
||||
BadConfiguration = errors.New("Bad proxy configuration.")
|
||||
)
|
37
proxy/common/config/json/config_cache.go
Normal file
37
proxy/common/config/json/config_cache.go
Normal file
@ -0,0 +1,37 @@
|
||||
package json
|
||||
|
||||
import (
|
||||
"github.com/v2ray/v2ray-core/proxy/common/config"
|
||||
)
|
||||
|
||||
type ConfigObjectCreator func() interface{}
|
||||
|
||||
var (
|
||||
configCache = make(map[string]ConfigObjectCreator)
|
||||
)
|
||||
|
||||
func getConfigKey(protocol string, cType config.Type) string {
|
||||
return protocol + "_" + string(cType)
|
||||
}
|
||||
|
||||
func registerConfigType(protocol string, cType config.Type, creator ConfigObjectCreator) error {
|
||||
// TODO: check name
|
||||
configCache[getConfigKey(protocol, cType)] = creator
|
||||
return nil
|
||||
}
|
||||
|
||||
func RegisterInboundConnectionConfig(protocol string, creator ConfigObjectCreator) error {
|
||||
return registerConfigType(protocol, config.TypeInbound, creator)
|
||||
}
|
||||
|
||||
func RegisterOutboundConnectionConfig(protocol string, creator ConfigObjectCreator) error {
|
||||
return registerConfigType(protocol, config.TypeOutbound, creator)
|
||||
}
|
||||
|
||||
func CreateConfig(protocol string, cType config.Type) interface{} {
|
||||
creator, found := configCache[getConfigKey(protocol, cType)]
|
||||
if !found {
|
||||
return nil
|
||||
}
|
||||
return creator()
|
||||
}
|
8
proxy/common/config/type.go
Normal file
8
proxy/common/config/type.go
Normal file
@ -0,0 +1,8 @@
|
||||
package config
|
||||
|
||||
type Type string
|
||||
|
||||
const (
|
||||
TypeInbound = Type("inbound")
|
||||
TypeOutbound = Type("outbound")
|
||||
)
|
@ -3,8 +3,7 @@ package json
|
||||
import (
|
||||
v2net "github.com/v2ray/v2ray-core/common/net"
|
||||
v2netjson "github.com/v2ray/v2ray-core/common/net/json"
|
||||
"github.com/v2ray/v2ray-core/config"
|
||||
"github.com/v2ray/v2ray-core/config/json"
|
||||
"github.com/v2ray/v2ray-core/proxy/common/config/json"
|
||||
)
|
||||
|
||||
type DokodemoConfig struct {
|
||||
@ -17,7 +16,7 @@ type DokodemoConfig struct {
|
||||
}
|
||||
|
||||
func init() {
|
||||
json.RegisterConfigType("dokodemo-door", config.TypeInbound, func() interface{} {
|
||||
json.RegisterInboundConnectionConfig("dokodemo-door", func() interface{} {
|
||||
return new(DokodemoConfig)
|
||||
})
|
||||
}
|
||||
|
@ -1,15 +1,14 @@
|
||||
package json
|
||||
|
||||
import (
|
||||
"github.com/v2ray/v2ray-core/config"
|
||||
"github.com/v2ray/v2ray-core/config/json"
|
||||
"github.com/v2ray/v2ray-core/proxy/common/config/json"
|
||||
)
|
||||
|
||||
type FreedomConfiguration struct {
|
||||
}
|
||||
|
||||
func init() {
|
||||
json.RegisterConfigType("freedom", config.TypeOutbound, func() interface{} {
|
||||
json.RegisterOutboundConnectionConfig("freedom", func() interface{} {
|
||||
return &FreedomConfiguration{}
|
||||
})
|
||||
}
|
||||
|
@ -1,15 +1,14 @@
|
||||
package json
|
||||
|
||||
import (
|
||||
"github.com/v2ray/v2ray-core/config"
|
||||
"github.com/v2ray/v2ray-core/config/json"
|
||||
"github.com/v2ray/v2ray-core/proxy/common/config/json"
|
||||
)
|
||||
|
||||
type HttpProxyConfig struct {
|
||||
}
|
||||
|
||||
func init() {
|
||||
json.RegisterConfigType("http", config.TypeInbound, func() interface{} {
|
||||
json.RegisterInboundConnectionConfig("http", func() interface{} {
|
||||
return new(HttpProxyConfig)
|
||||
})
|
||||
}
|
||||
|
@ -3,8 +3,7 @@ package json
|
||||
import (
|
||||
"net"
|
||||
|
||||
"github.com/v2ray/v2ray-core/config"
|
||||
"github.com/v2ray/v2ray-core/config/json"
|
||||
"github.com/v2ray/v2ray-core/proxy/common/config/json"
|
||||
)
|
||||
|
||||
const (
|
||||
@ -61,7 +60,7 @@ func (sc *SocksConfig) IP() net.IP {
|
||||
}
|
||||
|
||||
func init() {
|
||||
json.RegisterConfigType("socks", config.TypeInbound, func() interface{} {
|
||||
json.RegisterInboundConnectionConfig("socks", func() interface{} {
|
||||
return new(SocksConfig)
|
||||
})
|
||||
}
|
||||
|
@ -1,8 +1,7 @@
|
||||
package json
|
||||
|
||||
import (
|
||||
"github.com/v2ray/v2ray-core/config"
|
||||
"github.com/v2ray/v2ray-core/config/json"
|
||||
"github.com/v2ray/v2ray-core/proxy/common/config/json"
|
||||
vmessconfig "github.com/v2ray/v2ray-core/proxy/vmess/config"
|
||||
)
|
||||
|
||||
@ -24,7 +23,7 @@ func (c *Inbound) UDPEnabled() bool {
|
||||
}
|
||||
|
||||
func init() {
|
||||
json.RegisterConfigType("vmess", config.TypeInbound, func() interface{} {
|
||||
json.RegisterInboundConnectionConfig("vmess", func() interface{} {
|
||||
return new(Inbound)
|
||||
})
|
||||
}
|
||||
|
@ -7,8 +7,8 @@ import (
|
||||
|
||||
"github.com/v2ray/v2ray-core/common/log"
|
||||
v2net "github.com/v2ray/v2ray-core/common/net"
|
||||
"github.com/v2ray/v2ray-core/config"
|
||||
jsonconfig "github.com/v2ray/v2ray-core/config/json"
|
||||
proxyconfig "github.com/v2ray/v2ray-core/proxy/common/config"
|
||||
jsonconfig "github.com/v2ray/v2ray-core/proxy/common/config/json"
|
||||
vmessconfig "github.com/v2ray/v2ray-core/proxy/vmess/config"
|
||||
)
|
||||
|
||||
@ -39,7 +39,7 @@ func (t *ConfigTarget) UnmarshalJSON(data []byte) error {
|
||||
ip := net.ParseIP(rawConfig.Address)
|
||||
if ip == nil {
|
||||
log.Error("Unable to parse IP: %s", rawConfig.Address)
|
||||
return config.BadConfiguration
|
||||
return proxyconfig.BadConfiguration
|
||||
}
|
||||
t.Address = v2net.IPAddress(ip, rawConfig.Port)
|
||||
if rawConfig.HasNetwork("tcp") {
|
||||
@ -79,7 +79,7 @@ func (o *Outbound) Targets() []*vmessconfig.OutboundTarget {
|
||||
}
|
||||
|
||||
func init() {
|
||||
jsonconfig.RegisterConfigType("vmess", config.TypeOutbound, func() interface{} {
|
||||
jsonconfig.RegisterOutboundConnectionConfig("vmess", func() interface{} {
|
||||
return new(Outbound)
|
||||
})
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user