diff --git a/app/point/config/config.go b/app/point/config/config.go index 2daada22..99dabd21 100644 --- a/app/point/config/config.go +++ b/app/point/config/config.go @@ -16,9 +16,15 @@ type LogConfig interface { AccessLog() string } -type InboundDetour interface { +type PortRange interface { + From() uint16 + To() uint16 +} + +type InboundDetourConfig interface { Protocol() string - Port() + PortRange() PortRange + Settings() interface{} } type PointConfig interface { @@ -26,4 +32,5 @@ type PointConfig interface { LogConfig() LogConfig InboundConfig() ConnectionConfig OutboundConfig() ConnectionConfig + InboundDetours() []InboundDetourConfig } diff --git a/app/point/config/json/connection.go b/app/point/config/json/connection.go index fb0511ed..c19ca2b2 100644 --- a/app/point/config/json/connection.go +++ b/app/point/config/json/connection.go @@ -19,11 +19,15 @@ func (c *ConnectionConfig) Protocol() string { } func (c *ConnectionConfig) Settings() interface{} { - configObj := proxyjson.CreateConfig(c.Protocol(), c.Type) + return loadConnectionConfig(c.SettingsMessage, c.Protocol(), c.Type) +} + +func loadConnectionConfig(message json.RawMessage, protocol string, cType proxyconfig.Type) interface{} { + configObj := proxyjson.CreateConfig(protocol, cType) if configObj == nil { - panic("Unknown protocol " + c.Protocol()) + panic("Unknown protocol " + protocol) } - err := json.Unmarshal(c.SettingsMessage, configObj) + err := json.Unmarshal(message, configObj) if err != nil { log.Error("Unable to parse connection config: %v", err) panic("Failed to parse connection config.") diff --git a/app/point/config/json/inbound_detour.go b/app/point/config/json/inbound_detour.go new file mode 100644 index 00000000..7150d5e5 --- /dev/null +++ b/app/point/config/json/inbound_detour.go @@ -0,0 +1,26 @@ +package json + +import ( + "encoding/json" + + "github.com/v2ray/v2ray-core/app/point/config" + proxyconfig "github.com/v2ray/v2ray-core/proxy/common/config" +) + +type InboundDetourConfig struct { + ProtocolValue string + PortRangeValue *PortRange + SettingsValue json.RawMessage +} + +func (this *InboundDetourConfig) Protocol() string { + return this.ProtocolValue +} + +func (this *InboundDetourConfig) PortRange() config.PortRange { + return this.PortRangeValue +} + +func (this *InboundDetourConfig) Settings() interface{} { + return loadConnectionConfig(this.SettingsValue, this.ProtocolValue, proxyconfig.TypeInbound) +} diff --git a/app/point/config/json/json.go b/app/point/config/json/json.go index 579e1842..d7e7533d 100644 --- a/app/point/config/json/json.go +++ b/app/point/config/json/json.go @@ -12,10 +12,11 @@ import ( // Config is the config for Point server. type Config struct { - PortValue uint16 `json:"port"` // Port of this Point server. - LogConfigValue *LogConfig `json:"log"` - InboundConfigValue *ConnectionConfig `json:"inbound"` - OutboundConfigValue *ConnectionConfig `json:"outbound"` + PortValue uint16 `json:"port"` // Port of this Point server. + LogConfigValue *LogConfig `json:"log"` + InboundConfigValue *ConnectionConfig `json:"inbound"` + OutboundConfigValue *ConnectionConfig `json:"outbound"` + InboundDetoursValue []*InboundDetourConfig `json:"inboundDetour"` } func (config *Config) Port() uint16 { @@ -43,6 +44,14 @@ func (config *Config) OutboundConfig() config.ConnectionConfig { return config.OutboundConfigValue } +func (this *Config) InboundDetours() []config.InboundDetourConfig { + detours := make([]config.InboundDetourConfig, len(this.InboundDetoursValue)) + for idx, detour := range this.InboundDetoursValue { + detours[idx] = detour + } + return detours +} + func LoadConfig(file string) (*Config, error) { fixedFile := os.ExpandEnv(file) rawConfig, err := ioutil.ReadFile(fixedFile) diff --git a/testing/mocks/config.go b/testing/mocks/config.go index 1e23fa2f..ad70577c 100644 --- a/testing/mocks/config.go +++ b/testing/mocks/config.go @@ -21,6 +21,28 @@ type LogConfig struct { AccessLogValue string } +type PortRange struct { + FromValue uint16 + ToValue uint16 +} + +func (this *PortRange) From() uint16 { + return this.FromValue +} + +func (this *PortRange) To() uint16 { + return this.ToValue +} + +type InboundDetourConfig struct { + ConnectionConfig + PortRangeValue *PortRange +} + +func (this *InboundDetourConfig) PortRange() config.PortRange { + return this.PortRangeValue +} + func (config *LogConfig) AccessLog() string { return config.AccessLogValue } @@ -30,6 +52,7 @@ type Config struct { LogConfigValue *LogConfig InboundConfigValue *ConnectionConfig OutboundConfigValue *ConnectionConfig + InboundDetoursValue []*InboundDetourConfig } func (config *Config) Port() uint16 { @@ -47,3 +70,11 @@ func (config *Config) InboundConfig() config.ConnectionConfig { func (config *Config) OutboundConfig() config.ConnectionConfig { return config.OutboundConfigValue } + +func (this *Config) InboundDetours() []config.InboundDetourConfig { + detours := make([]config.InboundDetourConfig, len(this.InboundDetoursValue)) + for idx, detour := range this.InboundDetoursValue { + detours[idx] = detour + } + return detours +}