2021-06-19 08:45:43 -04:00
|
|
|
package v4
|
2019-02-10 13:04:11 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2021-06-19 08:28:20 -04:00
|
|
|
"github.com/v2fly/v2ray-core/v4/infra/conf/cfgcommon/loader"
|
2021-09-04 08:19:43 -04:00
|
|
|
"github.com/v2fly/v2ray-core/v4/infra/conf/cfgcommon/muxcfg"
|
|
|
|
"github.com/v2fly/v2ray-core/v4/infra/conf/cfgcommon/proxycfg"
|
2021-09-04 07:37:53 -04:00
|
|
|
"github.com/v2fly/v2ray-core/v4/infra/conf/cfgcommon/sniffer"
|
2021-06-19 08:41:32 -04:00
|
|
|
"github.com/v2fly/v2ray-core/v4/infra/conf/synthetic/dns"
|
2021-06-19 08:30:46 -04:00
|
|
|
"github.com/v2fly/v2ray-core/v4/infra/conf/synthetic/log"
|
2021-06-19 08:28:20 -04:00
|
|
|
"github.com/v2fly/v2ray-core/v4/infra/conf/synthetic/router"
|
2021-06-19 09:36:54 -04:00
|
|
|
"google.golang.org/protobuf/types/known/anypb"
|
2019-02-10 13:04:11 -05:00
|
|
|
"strings"
|
|
|
|
|
2021-02-16 15:31:50 -05:00
|
|
|
core "github.com/v2fly/v2ray-core/v4"
|
|
|
|
"github.com/v2fly/v2ray-core/v4/app/dispatcher"
|
|
|
|
"github.com/v2fly/v2ray-core/v4/app/proxyman"
|
|
|
|
"github.com/v2fly/v2ray-core/v4/app/stats"
|
|
|
|
"github.com/v2fly/v2ray-core/v4/common/serial"
|
2021-05-04 09:52:35 -04:00
|
|
|
"github.com/v2fly/v2ray-core/v4/infra/conf/cfgcommon"
|
2019-02-10 13:04:11 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2021-06-19 08:28:20 -04:00
|
|
|
inboundConfigLoader = loader.NewJSONConfigLoader(loader.ConfigCreatorCache{
|
2019-02-10 13:04:11 -05:00
|
|
|
"dokodemo-door": func() interface{} { return new(DokodemoConfig) },
|
2020-10-11 07:22:46 -04:00
|
|
|
"http": func() interface{} { return new(HTTPServerConfig) },
|
2019-02-10 13:04:11 -05:00
|
|
|
"shadowsocks": func() interface{} { return new(ShadowsocksServerConfig) },
|
|
|
|
"socks": func() interface{} { return new(SocksServerConfig) },
|
2020-07-28 11:00:23 -04:00
|
|
|
"vless": func() interface{} { return new(VLessInboundConfig) },
|
2019-02-10 13:04:11 -05:00
|
|
|
"vmess": func() interface{} { return new(VMessInboundConfig) },
|
2020-09-26 11:31:24 -04:00
|
|
|
"trojan": func() interface{} { return new(TrojanServerConfig) },
|
2019-02-10 13:04:11 -05:00
|
|
|
}, "protocol", "settings")
|
|
|
|
|
2021-06-19 08:28:20 -04:00
|
|
|
outboundConfigLoader = loader.NewJSONConfigLoader(loader.ConfigCreatorCache{
|
2019-02-10 13:04:11 -05:00
|
|
|
"blackhole": func() interface{} { return new(BlackholeConfig) },
|
|
|
|
"freedom": func() interface{} { return new(FreedomConfig) },
|
2020-10-11 07:22:46 -04:00
|
|
|
"http": func() interface{} { return new(HTTPClientConfig) },
|
2019-02-10 13:04:11 -05:00
|
|
|
"shadowsocks": func() interface{} { return new(ShadowsocksClientConfig) },
|
|
|
|
"socks": func() interface{} { return new(SocksClientConfig) },
|
2020-07-28 11:00:23 -04:00
|
|
|
"vless": func() interface{} { return new(VLessOutboundConfig) },
|
|
|
|
"vmess": func() interface{} { return new(VMessOutboundConfig) },
|
2020-09-26 11:31:24 -04:00
|
|
|
"trojan": func() interface{} { return new(TrojanClientConfig) },
|
2020-10-11 07:22:46 -04:00
|
|
|
"dns": func() interface{} { return new(DNSOutboundConfig) },
|
2021-03-13 04:25:56 -05:00
|
|
|
"loopback": func() interface{} { return new(LoopbackConfig) },
|
2019-02-10 13:04:11 -05:00
|
|
|
}, "protocol", "settings")
|
|
|
|
)
|
|
|
|
|
|
|
|
func toProtocolList(s []string) ([]proxyman.KnownProtocols, error) {
|
|
|
|
kp := make([]proxyman.KnownProtocols, 0, 8)
|
|
|
|
for _, p := range s {
|
|
|
|
switch strings.ToLower(p) {
|
|
|
|
case "http":
|
|
|
|
kp = append(kp, proxyman.KnownProtocols_HTTP)
|
|
|
|
case "https", "tls", "ssl":
|
|
|
|
kp = append(kp, proxyman.KnownProtocols_TLS)
|
|
|
|
default:
|
|
|
|
return nil, newError("Unknown protocol: ", p)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return kp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type InboundDetourAllocationConfig struct {
|
|
|
|
Strategy string `json:"strategy"`
|
|
|
|
Concurrency *uint32 `json:"concurrency"`
|
|
|
|
RefreshMin *uint32 `json:"refresh"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build implements Buildable.
|
|
|
|
func (c *InboundDetourAllocationConfig) Build() (*proxyman.AllocationStrategy, error) {
|
|
|
|
config := new(proxyman.AllocationStrategy)
|
|
|
|
switch strings.ToLower(c.Strategy) {
|
|
|
|
case "always":
|
|
|
|
config.Type = proxyman.AllocationStrategy_Always
|
|
|
|
case "random":
|
|
|
|
config.Type = proxyman.AllocationStrategy_Random
|
|
|
|
case "external":
|
|
|
|
config.Type = proxyman.AllocationStrategy_External
|
|
|
|
default:
|
|
|
|
return nil, newError("unknown allocation strategy: ", c.Strategy)
|
|
|
|
}
|
|
|
|
if c.Concurrency != nil {
|
|
|
|
config.Concurrency = &proxyman.AllocationStrategy_AllocationStrategyConcurrency{
|
|
|
|
Value: *c.Concurrency,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.RefreshMin != nil {
|
|
|
|
config.Refresh = &proxyman.AllocationStrategy_AllocationStrategyRefresh{
|
|
|
|
Value: *c.RefreshMin,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return config, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type InboundDetourConfig struct {
|
|
|
|
Protocol string `json:"protocol"`
|
2021-05-03 22:15:11 -04:00
|
|
|
PortRange *cfgcommon.PortRange `json:"port"`
|
|
|
|
ListenOn *cfgcommon.Address `json:"listen"`
|
2019-02-10 13:04:11 -05:00
|
|
|
Settings *json.RawMessage `json:"settings"`
|
|
|
|
Tag string `json:"tag"`
|
|
|
|
Allocation *InboundDetourAllocationConfig `json:"allocate"`
|
|
|
|
StreamSetting *StreamConfig `json:"streamSettings"`
|
2021-05-03 22:15:11 -04:00
|
|
|
DomainOverride *cfgcommon.StringList `json:"domainOverride"`
|
2021-09-04 07:37:53 -04:00
|
|
|
SniffingConfig *sniffer.SniffingConfig `json:"sniffing"`
|
2019-02-10 13:04:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Build implements Buildable.
|
|
|
|
func (c *InboundDetourConfig) Build() (*core.InboundHandlerConfig, error) {
|
|
|
|
receiverSettings := &proxyman.ReceiverConfig{}
|
|
|
|
|
2020-10-29 03:30:38 -04:00
|
|
|
if c.ListenOn == nil {
|
|
|
|
// Listen on anyip, must set PortRange
|
|
|
|
if c.PortRange == nil {
|
|
|
|
return nil, newError("Listen on AnyIP but no Port(s) set in InboundDetour.")
|
2019-02-10 13:04:11 -05:00
|
|
|
}
|
2020-10-29 03:30:38 -04:00
|
|
|
receiverSettings.PortRange = c.PortRange.Build()
|
|
|
|
} else {
|
|
|
|
// Listen on specific IP or Unix Domain Socket
|
2019-02-10 13:04:11 -05:00
|
|
|
receiverSettings.Listen = c.ListenOn.Build()
|
2020-10-29 03:30:38 -04:00
|
|
|
listenDS := c.ListenOn.Family().IsDomain() && (c.ListenOn.Domain()[0] == '/' || c.ListenOn.Domain()[0] == '@')
|
|
|
|
listenIP := c.ListenOn.Family().IsIP() || (c.ListenOn.Family().IsDomain() && c.ListenOn.Domain() == "localhost")
|
2020-11-21 16:05:01 -05:00
|
|
|
switch {
|
|
|
|
case listenIP:
|
2020-10-29 03:30:38 -04:00
|
|
|
// Listen on specific IP, must set PortRange
|
|
|
|
if c.PortRange == nil {
|
|
|
|
return nil, newError("Listen on specific ip without port in InboundDetour.")
|
|
|
|
}
|
|
|
|
// Listen on IP:Port
|
|
|
|
receiverSettings.PortRange = c.PortRange.Build()
|
2020-11-21 16:05:01 -05:00
|
|
|
case listenDS:
|
2020-10-29 03:30:38 -04:00
|
|
|
if c.PortRange != nil {
|
|
|
|
// Listen on Unix Domain Socket, PortRange should be nil
|
|
|
|
receiverSettings.PortRange = nil
|
|
|
|
}
|
2020-11-21 16:05:01 -05:00
|
|
|
default:
|
2020-10-29 03:30:38 -04:00
|
|
|
return nil, newError("unable to listen on domain address: ", c.ListenOn.Domain())
|
|
|
|
}
|
2019-02-10 13:04:11 -05:00
|
|
|
}
|
2020-10-29 03:30:38 -04:00
|
|
|
|
2019-02-10 13:04:11 -05:00
|
|
|
if c.Allocation != nil {
|
|
|
|
concurrency := -1
|
|
|
|
if c.Allocation.Concurrency != nil && c.Allocation.Strategy == "random" {
|
|
|
|
concurrency = int(*c.Allocation.Concurrency)
|
|
|
|
}
|
|
|
|
portRange := int(c.PortRange.To - c.PortRange.From + 1)
|
|
|
|
if concurrency >= 0 && concurrency >= portRange {
|
|
|
|
return nil, newError("not enough ports. concurrency = ", concurrency, " ports: ", c.PortRange.From, " - ", c.PortRange.To)
|
|
|
|
}
|
|
|
|
|
|
|
|
as, err := c.Allocation.Build()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
receiverSettings.AllocationStrategy = as
|
|
|
|
}
|
|
|
|
if c.StreamSetting != nil {
|
|
|
|
ss, err := c.StreamSetting.Build()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
receiverSettings.StreamSettings = ss
|
|
|
|
}
|
|
|
|
if c.SniffingConfig != nil {
|
|
|
|
s, err := c.SniffingConfig.Build()
|
|
|
|
if err != nil {
|
|
|
|
return nil, newError("failed to build sniffing config").Base(err)
|
|
|
|
}
|
|
|
|
receiverSettings.SniffingSettings = s
|
|
|
|
}
|
|
|
|
if c.DomainOverride != nil {
|
|
|
|
kp, err := toProtocolList(*c.DomainOverride)
|
|
|
|
if err != nil {
|
|
|
|
return nil, newError("failed to parse inbound detour config").Base(err)
|
|
|
|
}
|
|
|
|
receiverSettings.DomainOverride = kp
|
|
|
|
}
|
|
|
|
|
|
|
|
settings := []byte("{}")
|
|
|
|
if c.Settings != nil {
|
|
|
|
settings = ([]byte)(*c.Settings)
|
|
|
|
}
|
|
|
|
rawConfig, err := inboundConfigLoader.LoadWithID(settings, c.Protocol)
|
|
|
|
if err != nil {
|
|
|
|
return nil, newError("failed to load inbound detour config.").Base(err)
|
|
|
|
}
|
|
|
|
if dokodemoConfig, ok := rawConfig.(*DokodemoConfig); ok {
|
|
|
|
receiverSettings.ReceiveOriginalDestination = dokodemoConfig.Redirect
|
|
|
|
}
|
2021-06-19 08:04:50 -04:00
|
|
|
ts, err := rawConfig.(cfgcommon.Buildable).Build()
|
2019-02-10 13:04:11 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &core.InboundHandlerConfig{
|
|
|
|
Tag: c.Tag,
|
|
|
|
ReceiverSettings: serial.ToTypedMessage(receiverSettings),
|
|
|
|
ProxySettings: serial.ToTypedMessage(ts),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type OutboundDetourConfig struct {
|
2021-09-04 08:19:43 -04:00
|
|
|
Protocol string `json:"protocol"`
|
|
|
|
SendThrough *cfgcommon.Address `json:"sendThrough"`
|
|
|
|
Tag string `json:"tag"`
|
|
|
|
Settings *json.RawMessage `json:"settings"`
|
|
|
|
StreamSetting *StreamConfig `json:"streamSettings"`
|
|
|
|
ProxySettings *proxycfg.ProxyConfig `json:"proxySettings"`
|
|
|
|
MuxSettings *muxcfg.MuxConfig `json:"mux"`
|
2019-02-10 13:04:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Build implements Buildable.
|
|
|
|
func (c *OutboundDetourConfig) Build() (*core.OutboundHandlerConfig, error) {
|
|
|
|
senderSettings := &proxyman.SenderConfig{}
|
|
|
|
|
|
|
|
if c.SendThrough != nil {
|
|
|
|
address := c.SendThrough
|
|
|
|
if address.Family().IsDomain() {
|
|
|
|
return nil, newError("unable to send through: " + address.String())
|
|
|
|
}
|
|
|
|
senderSettings.Via = address.Build()
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.StreamSetting != nil {
|
|
|
|
ss, err := c.StreamSetting.Build()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
senderSettings.StreamSettings = ss
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.ProxySettings != nil {
|
|
|
|
ps, err := c.ProxySettings.Build()
|
|
|
|
if err != nil {
|
|
|
|
return nil, newError("invalid outbound detour proxy settings.").Base(err)
|
|
|
|
}
|
|
|
|
senderSettings.ProxySettings = ps
|
|
|
|
}
|
|
|
|
|
2019-06-29 11:43:30 -04:00
|
|
|
if c.MuxSettings != nil {
|
2020-11-20 02:53:10 -05:00
|
|
|
senderSettings.MultiplexSettings = c.MuxSettings.Build()
|
2019-02-10 13:04:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
settings := []byte("{}")
|
|
|
|
if c.Settings != nil {
|
|
|
|
settings = ([]byte)(*c.Settings)
|
|
|
|
}
|
|
|
|
rawConfig, err := outboundConfigLoader.LoadWithID(settings, c.Protocol)
|
|
|
|
if err != nil {
|
|
|
|
return nil, newError("failed to parse to outbound detour config.").Base(err)
|
|
|
|
}
|
2021-06-19 08:04:50 -04:00
|
|
|
ts, err := rawConfig.(cfgcommon.Buildable).Build()
|
2019-02-10 13:04:11 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &core.OutboundHandlerConfig{
|
|
|
|
SenderSettings: serial.ToTypedMessage(senderSettings),
|
|
|
|
Tag: c.Tag,
|
|
|
|
ProxySettings: serial.ToTypedMessage(ts),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type StatsConfig struct{}
|
|
|
|
|
2020-09-21 10:56:43 -04:00
|
|
|
// Build implements Buildable.
|
2019-02-10 13:04:11 -05:00
|
|
|
func (c *StatsConfig) Build() (*stats.Config, error) {
|
|
|
|
return &stats.Config{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type Config struct {
|
2020-11-20 02:18:44 -05:00
|
|
|
// Port of this Point server.
|
|
|
|
// Deprecated: Port exists for historical compatibility
|
|
|
|
// and should not be used.
|
|
|
|
Port uint16 `json:"port"`
|
|
|
|
|
|
|
|
// Deprecated: InboundConfig exists for historical compatibility
|
|
|
|
// and should not be used.
|
|
|
|
InboundConfig *InboundDetourConfig `json:"inbound"`
|
|
|
|
|
|
|
|
// Deprecated: OutboundConfig exists for historical compatibility
|
|
|
|
// and should not be used.
|
|
|
|
OutboundConfig *OutboundDetourConfig `json:"outbound"`
|
|
|
|
|
|
|
|
// Deprecated: InboundDetours exists for historical compatibility
|
|
|
|
// and should not be used.
|
|
|
|
InboundDetours []InboundDetourConfig `json:"inboundDetour"`
|
|
|
|
|
|
|
|
// Deprecated: OutboundDetours exists for historical compatibility
|
|
|
|
// and should not be used.
|
|
|
|
OutboundDetours []OutboundDetourConfig `json:"outboundDetour"`
|
|
|
|
|
2021-06-19 08:30:46 -04:00
|
|
|
LogConfig *log.LogConfig `json:"log"`
|
2021-06-19 08:28:20 -04:00
|
|
|
RouterConfig *router.RouterConfig `json:"routing"`
|
2021-06-19 08:41:32 -04:00
|
|
|
DNSConfig *dns.DNSConfig `json:"dns"`
|
2021-03-20 11:10:17 -04:00
|
|
|
InboundConfigs []InboundDetourConfig `json:"inbounds"`
|
|
|
|
OutboundConfigs []OutboundDetourConfig `json:"outbounds"`
|
|
|
|
Transport *TransportConfig `json:"transport"`
|
|
|
|
Policy *PolicyConfig `json:"policy"`
|
|
|
|
API *APIConfig `json:"api"`
|
|
|
|
Stats *StatsConfig `json:"stats"`
|
|
|
|
Reverse *ReverseConfig `json:"reverse"`
|
|
|
|
FakeDNS *FakeDNSConfig `json:"fakeDns"`
|
|
|
|
BrowserForwarder *BrowserForwarderConfig `json:"browserForwarder"`
|
2021-04-08 17:20:30 -04:00
|
|
|
Observatory *ObservatoryConfig `json:"observatory"`
|
2021-06-18 19:29:50 -04:00
|
|
|
BurstObservatory *BurstObservatoryConfig `json:"burstObservatory"`
|
2021-06-19 06:15:48 -04:00
|
|
|
MultiObservatory *MultiObservatoryConfig `json:"multiObservatory"`
|
2021-03-06 12:14:01 -05:00
|
|
|
|
|
|
|
Services map[string]*json.RawMessage `json:"services"`
|
2019-02-10 13:04:11 -05:00
|
|
|
}
|
|
|
|
|
2020-01-02 20:26:48 -05:00
|
|
|
func (c *Config) findInboundTag(tag string) int {
|
|
|
|
found := -1
|
|
|
|
for idx, ib := range c.InboundConfigs {
|
|
|
|
if ib.Tag == tag {
|
|
|
|
found = idx
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return found
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Config) findOutboundTag(tag string) int {
|
|
|
|
found := -1
|
|
|
|
for idx, ob := range c.OutboundConfigs {
|
|
|
|
if ob.Tag == tag {
|
|
|
|
found = idx
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return found
|
|
|
|
}
|
|
|
|
|
2019-02-10 13:04:11 -05:00
|
|
|
func applyTransportConfig(s *StreamConfig, t *TransportConfig) {
|
|
|
|
if s.TCPSettings == nil {
|
|
|
|
s.TCPSettings = t.TCPConfig
|
|
|
|
}
|
|
|
|
if s.KCPSettings == nil {
|
|
|
|
s.KCPSettings = t.KCPConfig
|
|
|
|
}
|
|
|
|
if s.WSSettings == nil {
|
|
|
|
s.WSSettings = t.WSConfig
|
|
|
|
}
|
|
|
|
if s.HTTPSettings == nil {
|
|
|
|
s.HTTPSettings = t.HTTPConfig
|
|
|
|
}
|
|
|
|
if s.DSSettings == nil {
|
|
|
|
s.DSSettings = t.DSConfig
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build implements Buildable.
|
|
|
|
func (c *Config) Build() (*core.Config, error) {
|
2021-02-08 05:18:52 -05:00
|
|
|
if err := PostProcessConfigureFile(c); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-02-10 13:04:11 -05:00
|
|
|
config := &core.Config{
|
2021-06-19 09:36:54 -04:00
|
|
|
App: []*anypb.Any{
|
2019-02-10 13:04:11 -05:00
|
|
|
serial.ToTypedMessage(&dispatcher.Config{}),
|
|
|
|
serial.ToTypedMessage(&proxyman.InboundConfig{}),
|
|
|
|
serial.ToTypedMessage(&proxyman.OutboundConfig{}),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-10-11 07:22:46 -04:00
|
|
|
if c.API != nil {
|
|
|
|
apiConf, err := c.API.Build()
|
2019-02-10 13:04:11 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
config.App = append(config.App, serial.ToTypedMessage(apiConf))
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.Stats != nil {
|
|
|
|
statsConf, err := c.Stats.Build()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
config.App = append(config.App, serial.ToTypedMessage(statsConf))
|
|
|
|
}
|
|
|
|
|
2021-06-19 09:36:54 -04:00
|
|
|
var logConfMsg *anypb.Any
|
2019-02-10 13:04:11 -05:00
|
|
|
if c.LogConfig != nil {
|
2019-10-31 05:24:25 -04:00
|
|
|
logConfMsg = serial.ToTypedMessage(c.LogConfig.Build())
|
2019-02-10 13:04:11 -05:00
|
|
|
} else {
|
2021-06-19 08:30:46 -04:00
|
|
|
logConfMsg = serial.ToTypedMessage(log.DefaultLogConfig())
|
2019-02-10 13:04:11 -05:00
|
|
|
}
|
2019-10-31 05:24:25 -04:00
|
|
|
// let logger module be the first App to start,
|
|
|
|
// so that other modules could print log during initiating
|
2021-06-19 09:36:54 -04:00
|
|
|
config.App = append([]*anypb.Any{logConfMsg}, config.App...)
|
2019-02-10 13:04:11 -05:00
|
|
|
|
|
|
|
if c.RouterConfig != nil {
|
|
|
|
routerConfig, err := c.RouterConfig.Build()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
config.App = append(config.App, serial.ToTypedMessage(routerConfig))
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.DNSConfig != nil {
|
|
|
|
dnsApp, err := c.DNSConfig.Build()
|
|
|
|
if err != nil {
|
|
|
|
return nil, newError("failed to parse DNS config").Base(err)
|
|
|
|
}
|
|
|
|
config.App = append(config.App, serial.ToTypedMessage(dnsApp))
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.Policy != nil {
|
|
|
|
pc, err := c.Policy.Build()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
config.App = append(config.App, serial.ToTypedMessage(pc))
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.Reverse != nil {
|
|
|
|
r, err := c.Reverse.Build()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
config.App = append(config.App, serial.ToTypedMessage(r))
|
|
|
|
}
|
|
|
|
|
2021-02-08 05:18:52 -05:00
|
|
|
if c.FakeDNS != nil {
|
|
|
|
r, err := c.FakeDNS.Build()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
config.App = append(config.App, serial.ToTypedMessage(r))
|
|
|
|
}
|
|
|
|
|
2021-03-20 11:10:17 -04:00
|
|
|
if c.BrowserForwarder != nil {
|
|
|
|
r, err := c.BrowserForwarder.Build()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
config.App = append(config.App, serial.ToTypedMessage(r))
|
|
|
|
}
|
|
|
|
|
2021-04-08 17:20:30 -04:00
|
|
|
if c.Observatory != nil {
|
|
|
|
r, err := c.Observatory.Build()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
config.App = append(config.App, serial.ToTypedMessage(r))
|
|
|
|
}
|
|
|
|
|
2021-06-18 19:29:50 -04:00
|
|
|
if c.BurstObservatory != nil {
|
|
|
|
r, err := c.BurstObservatory.Build()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
config.App = append(config.App, serial.ToTypedMessage(r))
|
|
|
|
}
|
|
|
|
|
2021-06-19 06:15:48 -04:00
|
|
|
if c.MultiObservatory != nil {
|
|
|
|
r, err := c.MultiObservatory.Build()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
config.App = append(config.App, serial.ToTypedMessage(r))
|
|
|
|
}
|
|
|
|
|
2021-04-13 11:06:48 -04:00
|
|
|
// Load Additional Services that do not have a json translator
|
2021-03-06 12:14:01 -05:00
|
|
|
|
|
|
|
if msg, err := c.BuildServices(c.Services); err != nil {
|
|
|
|
developererr := newError("Loading a V2Ray Features as a service is intended for developers only. " +
|
|
|
|
"This is used for developers to prototype new features or for an advanced client to use special features in V2Ray," +
|
|
|
|
" instead of allowing end user to enable it without special tool and knowledge.")
|
2021-03-06 12:48:39 -05:00
|
|
|
sb := strings.Builder{}
|
|
|
|
return nil, newError("Cannot load service").Base(developererr).Base(err).Base(newError(sb.String()))
|
2021-04-13 10:50:29 -04:00
|
|
|
} else { // nolint: golint
|
2021-04-13 10:39:10 -04:00
|
|
|
// Using a else here is required to keep msg in scope
|
2021-03-06 12:14:01 -05:00
|
|
|
config.App = append(config.App, msg...)
|
|
|
|
}
|
|
|
|
|
2019-02-10 13:04:11 -05:00
|
|
|
var inbounds []InboundDetourConfig
|
|
|
|
|
|
|
|
if c.InboundConfig != nil {
|
|
|
|
inbounds = append(inbounds, *c.InboundConfig)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(c.InboundDetours) > 0 {
|
|
|
|
inbounds = append(inbounds, c.InboundDetours...)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(c.InboundConfigs) > 0 {
|
|
|
|
inbounds = append(inbounds, c.InboundConfigs...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Backward compatibility.
|
|
|
|
if len(inbounds) > 0 && inbounds[0].PortRange == nil && c.Port > 0 {
|
2021-05-03 22:15:11 -04:00
|
|
|
inbounds[0].PortRange = &cfgcommon.PortRange{
|
2019-02-10 13:04:11 -05:00
|
|
|
From: uint32(c.Port),
|
|
|
|
To: uint32(c.Port),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, rawInboundConfig := range inbounds {
|
|
|
|
if c.Transport != nil {
|
|
|
|
if rawInboundConfig.StreamSetting == nil {
|
|
|
|
rawInboundConfig.StreamSetting = &StreamConfig{}
|
|
|
|
}
|
|
|
|
applyTransportConfig(rawInboundConfig.StreamSetting, c.Transport)
|
|
|
|
}
|
|
|
|
ic, err := rawInboundConfig.Build()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
config.Inbound = append(config.Inbound, ic)
|
|
|
|
}
|
|
|
|
|
|
|
|
var outbounds []OutboundDetourConfig
|
|
|
|
|
|
|
|
if c.OutboundConfig != nil {
|
|
|
|
outbounds = append(outbounds, *c.OutboundConfig)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(c.OutboundDetours) > 0 {
|
|
|
|
outbounds = append(outbounds, c.OutboundDetours...)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(c.OutboundConfigs) > 0 {
|
|
|
|
outbounds = append(outbounds, c.OutboundConfigs...)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, rawOutboundConfig := range outbounds {
|
|
|
|
if c.Transport != nil {
|
|
|
|
if rawOutboundConfig.StreamSetting == nil {
|
|
|
|
rawOutboundConfig.StreamSetting = &StreamConfig{}
|
|
|
|
}
|
|
|
|
applyTransportConfig(rawOutboundConfig.StreamSetting, c.Transport)
|
|
|
|
}
|
|
|
|
oc, err := rawOutboundConfig.Build()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
config.Outbound = append(config.Outbound, oc)
|
|
|
|
}
|
|
|
|
|
|
|
|
return config, nil
|
|
|
|
}
|