1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-09-16 00:48:14 -04:00
v2fly/tools/conf/v2ray.go

352 lines
9.7 KiB
Go
Raw Normal View History

2016-10-16 08:22:21 -04:00
package conf
import (
"encoding/json"
2016-10-17 08:35:13 -04:00
"io"
2016-12-04 03:10:47 -05:00
"strings"
2016-10-17 08:35:13 -04:00
"v2ray.com/core"
2016-12-04 03:10:47 -05:00
"v2ray.com/core/common/errors"
2016-10-17 08:35:13 -04:00
"v2ray.com/core/common/loader"
2016-10-16 08:22:21 -04:00
v2net "v2ray.com/core/common/net"
2016-12-14 17:51:03 -05:00
json_reader "v2ray.com/core/tools/conf/json"
2016-10-16 08:22:21 -04:00
)
2016-10-17 08:35:13 -04:00
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{
2016-11-04 20:01:30 -04:00
"blackhole": func() interface{} { return new(BlackholeConfig) },
"freedom": func() interface{} { return new(FreedomConfig) },
"shadowsocks": func() interface{} { return new(ShadowsocksClientConfig) },
"vmess": func() interface{} { return new(VMessOutboundConfig) },
2016-10-17 08:35:13 -04:00
}, "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"`
2016-11-17 17:23:46 -05:00
Tag string `json:"tag"`
2016-10-17 08:35:13 -04:00
}
2016-11-27 15:39:09 -05:00
func (v *InboundConnectionConfig) Build() (*core.InboundConnectionConfig, error) {
2016-10-17 08:35:13 -04:00
config := new(core.InboundConnectionConfig)
config.PortRange = &v2net.PortRange{
2016-11-27 15:39:09 -05:00
From: uint32(v.Port),
To: uint32(v.Port),
2016-10-17 08:35:13 -04:00
}
2016-11-27 15:39:09 -05:00
if v.Listen != nil {
if v.Listen.Family().IsDomain() {
return nil, errors.New("Point: Unable to listen on domain address: " + v.Listen.Domain())
2016-10-17 08:35:13 -04:00
}
2016-11-27 15:39:09 -05:00
config.ListenOn = v.Listen.Build()
2016-10-17 08:35:13 -04:00
}
2016-11-27 15:39:09 -05:00
if v.StreamSetting != nil {
ts, err := v.StreamSetting.Build()
2016-10-17 08:35:13 -04:00
if err != nil {
return nil, err
}
config.StreamSettings = ts
}
2016-11-27 15:39:09 -05:00
config.AllowPassiveConnection = v.AllowPassive
2016-10-17 08:35:13 -04:00
2016-11-27 15:39:09 -05:00
jsonConfig, err := inboundConfigLoader.LoadWithID(v.Settings, v.Protocol)
2016-10-17 08:35:13 -04:00
if err != nil {
2016-12-04 03:43:33 -05:00
return nil, errors.Base(err).Message("Failed to load inbound config.")
2016-10-17 08:35:13 -04:00
}
ts, err := jsonConfig.(Buildable).Build()
if err != nil {
return nil, err
}
config.Settings = ts
2016-11-27 15:39:09 -05:00
if len(v.Tag) > 0 {
config.Tag = v.Tag
2016-11-17 17:23:46 -05:00
}
2016-10-17 08:35:13 -04:00
return config, nil
}
type OutboundConnectionConfig struct {
Protocol string `json:"protocol"`
SendThrough *Address `json:"sendThrough"`
StreamSetting *StreamConfig `json:"streamSettings"`
2016-11-10 17:41:28 -05:00
ProxySettings *ProxyConfig `json:"proxySettings"`
2016-10-17 08:35:13 -04:00
Settings json.RawMessage `json:"settings"`
2016-11-17 17:23:46 -05:00
Tag string `json:"tag"`
2016-10-17 08:35:13 -04:00
}
2016-11-27 15:39:09 -05:00
func (v *OutboundConnectionConfig) Build() (*core.OutboundConnectionConfig, error) {
2016-10-17 08:35:13 -04:00
config := new(core.OutboundConnectionConfig)
2016-11-27 15:39:09 -05:00
rawConfig, err := outboundConfigLoader.LoadWithID(v.Settings, v.Protocol)
2016-10-17 08:35:13 -04:00
if err != nil {
2016-12-04 03:43:33 -05:00
return nil, errors.Base(err).Message("Failed to parse outbound config.")
2016-10-17 08:35:13 -04:00
}
ts, err := rawConfig.(Buildable).Build()
if err != nil {
return nil, err
}
config.Settings = ts
2016-11-27 15:39:09 -05:00
if v.SendThrough != nil {
address := v.SendThrough
2016-10-17 08:35:13 -04:00
if address.Family().IsDomain() {
2016-12-04 03:43:33 -05:00
return nil, errors.New("Invalid sendThrough address: " + address.String())
2016-10-17 08:35:13 -04:00
}
config.SendThrough = address.Build()
}
2016-11-27 15:39:09 -05:00
if v.StreamSetting != nil {
ss, err := v.StreamSetting.Build()
2016-10-17 08:35:13 -04:00
if err != nil {
return nil, err
}
config.StreamSettings = ss
}
2016-11-27 15:39:09 -05:00
if v.ProxySettings != nil {
ps, err := v.ProxySettings.Build()
2016-11-10 17:41:28 -05:00
if err != nil {
2016-12-04 03:43:33 -05:00
return nil, errors.Base(err).Message("Invalid outbound proxy settings.")
2016-11-10 17:41:28 -05:00
}
config.ProxySettings = ps
}
2016-11-27 15:39:09 -05:00
if len(v.Tag) > 0 {
config.Tag = v.Tag
2016-11-17 17:23:46 -05:00
}
2016-10-17 08:35:13 -04:00
return config, nil
}
type InboundDetourAllocationConfig struct {
Strategy string `json:"strategy"`
Concurrency *uint32 `json:"concurrency"`
RefreshMin *uint32 `json:"refresh"`
}
2016-11-27 15:39:09 -05:00
func (v *InboundDetourAllocationConfig) Build() (*core.AllocationStrategy, error) {
2016-10-17 08:35:13 -04:00
config := new(core.AllocationStrategy)
2016-11-27 15:39:09 -05:00
switch strings.ToLower(v.Strategy) {
2016-10-17 08:35:13 -04:00
case "always":
config.Type = core.AllocationStrategy_Always
case "random":
config.Type = core.AllocationStrategy_Random
case "external":
config.Type = core.AllocationStrategy_External
default:
2016-12-04 03:43:33 -05:00
return nil, errors.New("Unknown allocation strategy: ", v.Strategy)
2016-10-17 08:35:13 -04:00
}
2016-11-27 15:39:09 -05:00
if v.Concurrency != nil {
2016-10-17 08:35:13 -04:00
config.Concurrency = &core.AllocationStrategyConcurrency{
2016-11-27 15:39:09 -05:00
Value: *v.Concurrency,
2016-10-17 08:35:13 -04:00
}
}
2016-11-27 15:39:09 -05:00
if v.RefreshMin != nil {
2016-10-17 08:35:13 -04:00
config.Refresh = &core.AllocationStrategyRefresh{
2016-11-27 15:39:09 -05:00
Value: *v.RefreshMin,
2016-10-17 08:35:13 -04:00
}
}
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"`
}
2016-11-27 15:39:09 -05:00
func (v *InboundDetourConfig) Build() (*core.InboundConnectionConfig, error) {
2016-10-17 08:35:13 -04:00
config := new(core.InboundConnectionConfig)
2016-11-27 15:39:09 -05:00
if v.PortRange == nil {
2016-12-04 03:43:33 -05:00
return nil, errors.New("Port range not specified in InboundDetour.")
2016-10-17 08:35:13 -04:00
}
2016-11-27 15:39:09 -05:00
config.PortRange = v.PortRange.Build()
2016-10-17 08:35:13 -04:00
2016-11-27 15:39:09 -05:00
if v.ListenOn != nil {
if v.ListenOn.Family().IsDomain() {
2016-12-04 03:43:33 -05:00
return nil, errors.New("Unable to listen on domain address: ", v.ListenOn.Domain())
2016-10-17 08:35:13 -04:00
}
2016-11-27 15:39:09 -05:00
config.ListenOn = v.ListenOn.Build()
2016-10-17 08:35:13 -04:00
}
2016-11-27 15:39:09 -05:00
config.Tag = v.Tag
if v.Allocation != nil {
as, err := v.Allocation.Build()
2016-10-17 08:35:13 -04:00
if err != nil {
return nil, err
}
config.AllocationStrategy = as
}
2016-11-27 15:39:09 -05:00
if v.StreamSetting != nil {
ss, err := v.StreamSetting.Build()
2016-10-17 08:35:13 -04:00
if err != nil {
return nil, err
}
config.StreamSettings = ss
}
2016-11-27 15:39:09 -05:00
config.AllowPassiveConnection = v.AllowPassive
2016-10-17 08:35:13 -04:00
2016-11-27 15:39:09 -05:00
rawConfig, err := inboundConfigLoader.LoadWithID(v.Settings, v.Protocol)
2016-10-17 08:35:13 -04:00
if err != nil {
2016-12-04 03:43:33 -05:00
return nil, errors.Base(err).Message("Failed to load inbound detour config.")
2016-10-17 08:35:13 -04:00
}
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"`
2016-11-10 17:41:28 -05:00
ProxySettings *ProxyConfig `json:"proxySettings"`
2016-10-17 08:35:13 -04:00
}
2016-11-27 15:39:09 -05:00
func (v *OutboundDetourConfig) Build() (*core.OutboundConnectionConfig, error) {
2016-10-17 08:35:13 -04:00
config := new(core.OutboundConnectionConfig)
2016-11-27 15:39:09 -05:00
config.Tag = v.Tag
2016-10-17 08:35:13 -04:00
2016-11-27 15:39:09 -05:00
if v.SendThrough != nil {
address := v.SendThrough
2016-10-17 08:35:13 -04:00
if address.Family().IsDomain() {
return nil, errors.New("Point: Unable to send through: " + address.String())
}
config.SendThrough = address.Build()
}
2016-11-27 15:39:09 -05:00
if v.StreamSetting != nil {
ss, err := v.StreamSetting.Build()
2016-10-17 08:35:13 -04:00
if err != nil {
return nil, err
}
config.StreamSettings = ss
}
2016-11-27 15:39:09 -05:00
rawConfig, err := outboundConfigLoader.LoadWithID(v.Settings, v.Protocol)
2016-10-17 08:35:13 -04:00
if err != nil {
2016-12-04 03:43:33 -05:00
return nil, errors.Base(err).Message("Failed to parse to outbound detour config.")
2016-10-17 08:35:13 -04:00
}
ts, err := rawConfig.(Buildable).Build()
if err != nil {
return nil, err
}
2016-11-10 17:41:28 -05:00
2016-11-27 15:39:09 -05:00
if v.ProxySettings != nil {
ps, err := v.ProxySettings.Build()
2016-11-10 17:41:28 -05:00
if err != nil {
2016-12-04 03:43:33 -05:00
return nil, errors.Base(err).Message("Invalid outbound detour proxy settings.")
2016-11-10 17:41:28 -05:00
}
config.ProxySettings = ps
}
2016-10-17 08:35:13 -04:00
config.Settings = ts
return config, nil
}
2016-10-16 08:22:21 -04:00
type Config struct {
Port uint16 `json:"port"` // Port of this Point server.
LogConfig *LogConfig `json:"log"`
2016-10-17 08:35:13 -04:00
RouterConfig *RouterConfig `json:"routing"`
DNSConfig *DnsConfig `json:"dns"`
2016-10-16 08:22:21 -04:00
InboundConfig *InboundConnectionConfig `json:"inbound"`
OutboundConfig *OutboundConnectionConfig `json:"outbound"`
2016-10-17 08:35:13 -04:00
InboundDetours []InboundDetourConfig `json:"inboundDetour"`
OutboundDetours []OutboundDetourConfig `json:"outboundDetour"`
Transport *TransportConfig `json:"transport"`
}
2016-11-27 15:39:09 -05:00
func (v *Config) Build() (*core.Config, error) {
2016-10-17 08:35:13 -04:00
config := new(core.Config)
2016-11-27 15:39:09 -05:00
if v.LogConfig != nil {
config.Log = v.LogConfig.Build()
2016-10-17 08:35:13 -04:00
}
2016-11-27 15:39:09 -05:00
if v.Transport != nil {
ts, err := v.Transport.Build()
2016-10-17 08:35:13 -04:00
if err != nil {
return nil, err
}
config.Transport = ts
}
2016-11-27 15:39:09 -05:00
if v.RouterConfig != nil {
routerConfig, err := v.RouterConfig.Build()
2016-10-17 08:35:13 -04:00
if err != nil {
return nil, err
}
config.App = append(config.App, loader.NewTypedSettings(routerConfig))
}
2016-11-27 15:39:09 -05:00
if v.DNSConfig != nil {
config.App = append(config.App, loader.NewTypedSettings(v.DNSConfig.Build()))
2016-10-17 08:35:13 -04:00
}
2016-11-27 15:39:09 -05:00
if v.InboundConfig == nil {
2016-10-17 08:35:13 -04:00
return nil, errors.New("No inbound config specified.")
}
2016-11-27 15:39:09 -05:00
if v.InboundConfig.Port == 0 && v.Port > 0 {
v.InboundConfig.Port = v.Port
2016-10-17 08:35:13 -04:00
}
2016-11-27 15:39:09 -05:00
ic, err := v.InboundConfig.Build()
2016-10-17 08:35:13 -04:00
if err != nil {
return nil, err
}
config.Inbound = append(config.Inbound, ic)
2016-11-27 15:39:09 -05:00
for _, rawInboundConfig := range v.InboundDetours {
2016-10-17 08:35:13 -04:00
ic, err := rawInboundConfig.Build()
if err != nil {
return nil, err
}
config.Inbound = append(config.Inbound, ic)
}
2016-11-27 15:39:09 -05:00
oc, err := v.OutboundConfig.Build()
2016-10-17 08:35:13 -04:00
if err != nil {
return nil, err
}
config.Outbound = append(config.Outbound, oc)
2016-11-27 15:39:09 -05:00
for _, rawOutboundConfig := range v.OutboundDetours {
2016-10-17 08:35:13 -04:00
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{}
2016-12-14 17:51:03 -05:00
decoder := json.NewDecoder(&json_reader.Reader{
Reader: input,
})
2016-10-17 08:35:13 -04:00
err := decoder.Decode(jsonConfig)
if err != nil {
2016-12-04 03:43:33 -05:00
return nil, errors.Base(err).Message("Invalid V2Ray config.")
2016-10-17 08:35:13 -04:00
}
return jsonConfig.Build()
})
2016-10-16 08:22:21 -04:00
}