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/tools/conf/v2ray.go

349 lines
9.6 KiB

package conf
import (
"encoding/json"
"io"
"strings"
"v2ray.com/core"
"v2ray.com/core/common/errors"
"v2ray.com/core/common/loader"
v2net "v2ray.com/core/common/net"
)
var (
inboundConfigLoader = NewJSONConfigLoader(ConfigCreatorCache{
"dokodemo-door": func() interface{} { return new(DokodemoConfig) },
"http": func() interface{} { return new(HttpServerConfig) },
"shadowsocks": func() interface{} { return new(ShadowsocksServerConfig) },
"socks": func() interface{} { return new(SocksServerConfig) },
"vmess": func() interface{} { return new(VMessInboundConfig) },
}, "protocol", "settings")
outboundConfigLoader = NewJSONConfigLoader(ConfigCreatorCache{
"blackhole": func() interface{} { return new(BlackholeConfig) },
"freedom": func() interface{} { return new(FreedomConfig) },
"shadowsocks": func() interface{} { return new(ShadowsocksClientConfig) },
"vmess": func() interface{} { return new(VMessOutboundConfig) },
}, "protocol", "settings")
)
type InboundConnectionConfig struct {
Port uint16 `json:"port"`
Listen *Address `json:"listen"`
Protocol string `json:"protocol"`
StreamSetting *StreamConfig `json:"streamSettings"`
Settings json.RawMessage `json:"settings"`
AllowPassive bool `json:"allowPassive"`
Tag string `json:"tag"`
}
8 years ago
func (v *InboundConnectionConfig) Build() (*core.InboundConnectionConfig, error) {
config := new(core.InboundConnectionConfig)
config.PortRange = &v2net.PortRange{
8 years ago
From: uint32(v.Port),
To: uint32(v.Port),
}
8 years ago
if v.Listen != nil {
if v.Listen.Family().IsDomain() {
return nil, errors.New("Point: Unable to listen on domain address: " + v.Listen.Domain())
}
8 years ago
config.ListenOn = v.Listen.Build()
}
8 years ago
if v.StreamSetting != nil {
ts, err := v.StreamSetting.Build()
if err != nil {
return nil, err
}
config.StreamSettings = ts
}
8 years ago
config.AllowPassiveConnection = v.AllowPassive
8 years ago
jsonConfig, err := inboundConfigLoader.LoadWithID(v.Settings, v.Protocol)
if err != nil {
8 years ago
return nil, errors.Base(err).Message("Failed to load inbound config.")
}
ts, err := jsonConfig.(Buildable).Build()
if err != nil {
return nil, err
}
config.Settings = ts
8 years ago
if len(v.Tag) > 0 {
config.Tag = v.Tag
}
return config, nil
}
type OutboundConnectionConfig struct {
Protocol string `json:"protocol"`
SendThrough *Address `json:"sendThrough"`
StreamSetting *StreamConfig `json:"streamSettings"`
ProxySettings *ProxyConfig `json:"proxySettings"`
Settings json.RawMessage `json:"settings"`
Tag string `json:"tag"`
}
8 years ago
func (v *OutboundConnectionConfig) Build() (*core.OutboundConnectionConfig, error) {
config := new(core.OutboundConnectionConfig)
8 years ago
rawConfig, err := outboundConfigLoader.LoadWithID(v.Settings, v.Protocol)
if err != nil {
8 years ago
return nil, errors.Base(err).Message("Failed to parse outbound config.")
}
ts, err := rawConfig.(Buildable).Build()
if err != nil {
return nil, err
}
config.Settings = ts
8 years ago
if v.SendThrough != nil {
address := v.SendThrough
if address.Family().IsDomain() {
8 years ago
return nil, errors.New("Invalid sendThrough address: " + address.String())
}
config.SendThrough = address.Build()
}
8 years ago
if v.StreamSetting != nil {
ss, err := v.StreamSetting.Build()
if err != nil {
return nil, err
}
config.StreamSettings = ss
}
8 years ago
if v.ProxySettings != nil {
ps, err := v.ProxySettings.Build()
if err != nil {
8 years ago
return nil, errors.Base(err).Message("Invalid outbound proxy settings.")
}
config.ProxySettings = ps
}
8 years ago
if len(v.Tag) > 0 {
config.Tag = v.Tag
}
return config, nil
}
type InboundDetourAllocationConfig struct {
Strategy string `json:"strategy"`
Concurrency *uint32 `json:"concurrency"`
RefreshMin *uint32 `json:"refresh"`
}
8 years ago
func (v *InboundDetourAllocationConfig) Build() (*core.AllocationStrategy, error) {
config := new(core.AllocationStrategy)
8 years ago
switch strings.ToLower(v.Strategy) {
case "always":
config.Type = core.AllocationStrategy_Always
case "random":
config.Type = core.AllocationStrategy_Random
case "external":
config.Type = core.AllocationStrategy_External
default:
8 years ago
return nil, errors.New("Unknown allocation strategy: ", v.Strategy)
}
8 years ago
if v.Concurrency != nil {
config.Concurrency = &core.AllocationStrategyConcurrency{
8 years ago
Value: *v.Concurrency,
}
}
8 years ago
if v.RefreshMin != nil {
config.Refresh = &core.AllocationStrategyRefresh{
8 years ago
Value: *v.RefreshMin,
}
}
return config, nil
}
type InboundDetourConfig struct {
Protocol string `json:"protocol"`
PortRange *PortRange `json:"port"`
ListenOn *Address `json:"listen"`
Settings json.RawMessage `json:"settings"`
Tag string `json:"tag"`
Allocation *InboundDetourAllocationConfig `json:"allocate"`
StreamSetting *StreamConfig `json:"streamSettings"`
AllowPassive bool `json:"allowPassive"`
}
8 years ago
func (v *InboundDetourConfig) Build() (*core.InboundConnectionConfig, error) {
config := new(core.InboundConnectionConfig)
8 years ago
if v.PortRange == nil {
8 years ago
return nil, errors.New("Port range not specified in InboundDetour.")
}
8 years ago
config.PortRange = v.PortRange.Build()
8 years ago
if v.ListenOn != nil {
if v.ListenOn.Family().IsDomain() {
8 years ago
return nil, errors.New("Unable to listen on domain address: ", v.ListenOn.Domain())
}
8 years ago
config.ListenOn = v.ListenOn.Build()
}
8 years ago
config.Tag = v.Tag
if v.Allocation != nil {
as, err := v.Allocation.Build()
if err != nil {
return nil, err
}
config.AllocationStrategy = as
}
8 years ago
if v.StreamSetting != nil {
ss, err := v.StreamSetting.Build()
if err != nil {
return nil, err
}
config.StreamSettings = ss
}
8 years ago
config.AllowPassiveConnection = v.AllowPassive
8 years ago
rawConfig, err := inboundConfigLoader.LoadWithID(v.Settings, v.Protocol)
if err != nil {
8 years ago
return nil, errors.Base(err).Message("Failed to load inbound detour config.")
}
ts, err := rawConfig.(Buildable).Build()
if err != nil {
return nil, err
}
config.Settings = ts
return config, nil
}
type OutboundDetourConfig struct {
Protocol string `json:"protocol"`
SendThrough *Address `json:"sendThrough"`
Tag string `json:"tag"`
Settings json.RawMessage `json:"settings"`
StreamSetting *StreamConfig `json:"streamSettings"`
ProxySettings *ProxyConfig `json:"proxySettings"`
}
8 years ago
func (v *OutboundDetourConfig) Build() (*core.OutboundConnectionConfig, error) {
config := new(core.OutboundConnectionConfig)
8 years ago
config.Tag = v.Tag
8 years ago
if v.SendThrough != nil {
address := v.SendThrough
if address.Family().IsDomain() {
return nil, errors.New("Point: Unable to send through: " + address.String())
}
config.SendThrough = address.Build()
}
8 years ago
if v.StreamSetting != nil {
ss, err := v.StreamSetting.Build()
if err != nil {
return nil, err
}
config.StreamSettings = ss
}
8 years ago
rawConfig, err := outboundConfigLoader.LoadWithID(v.Settings, v.Protocol)
if err != nil {
8 years ago
return nil, errors.Base(err).Message("Failed to parse to outbound detour config.")
}
ts, err := rawConfig.(Buildable).Build()
if err != nil {
return nil, err
}
8 years ago
if v.ProxySettings != nil {
ps, err := v.ProxySettings.Build()
if err != nil {
8 years ago
return nil, errors.Base(err).Message("Invalid outbound detour proxy settings.")
}
config.ProxySettings = ps
}
config.Settings = ts
return config, nil
}
type Config struct {
Port uint16 `json:"port"` // Port of this Point server.
LogConfig *LogConfig `json:"log"`
RouterConfig *RouterConfig `json:"routing"`
DNSConfig *DnsConfig `json:"dns"`
InboundConfig *InboundConnectionConfig `json:"inbound"`
OutboundConfig *OutboundConnectionConfig `json:"outbound"`
InboundDetours []InboundDetourConfig `json:"inboundDetour"`
OutboundDetours []OutboundDetourConfig `json:"outboundDetour"`
Transport *TransportConfig `json:"transport"`
}
8 years ago
func (v *Config) Build() (*core.Config, error) {
config := new(core.Config)
8 years ago
if v.LogConfig != nil {
config.Log = v.LogConfig.Build()
}
8 years ago
if v.Transport != nil {
ts, err := v.Transport.Build()
if err != nil {
return nil, err
}
config.Transport = ts
}
8 years ago
if v.RouterConfig != nil {
routerConfig, err := v.RouterConfig.Build()
if err != nil {
return nil, err
}
config.App = append(config.App, loader.NewTypedSettings(routerConfig))
}
8 years ago
if v.DNSConfig != nil {
config.App = append(config.App, loader.NewTypedSettings(v.DNSConfig.Build()))
}
8 years ago
if v.InboundConfig == nil {
return nil, errors.New("No inbound config specified.")
}
8 years ago
if v.InboundConfig.Port == 0 && v.Port > 0 {
v.InboundConfig.Port = v.Port
}
8 years ago
ic, err := v.InboundConfig.Build()
if err != nil {
return nil, err
}
config.Inbound = append(config.Inbound, ic)
8 years ago
for _, rawInboundConfig := range v.InboundDetours {
ic, err := rawInboundConfig.Build()
if err != nil {
return nil, err
}
config.Inbound = append(config.Inbound, ic)
}
8 years ago
oc, err := v.OutboundConfig.Build()
if err != nil {
return nil, err
}
config.Outbound = append(config.Outbound, oc)
8 years ago
for _, rawOutboundConfig := range v.OutboundDetours {
oc, err := rawOutboundConfig.Build()
if err != nil {
return nil, err
}
config.Outbound = append(config.Outbound, oc)
}
return config, nil
}
func init() {
core.RegisterConfigLoader(core.ConfigFormat_JSON, func(input io.Reader) (*core.Config, error) {
jsonConfig := &Config{}
decoder := json.NewDecoder(input)
err := decoder.Decode(jsonConfig)
if err != nil {
8 years ago
return nil, errors.Base(err).Message("Invalid V2Ray config.")
}
return jsonConfig.Build()
})
}