1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-09-28 23:06:14 -04:00

completely remove json folder in v2ray

This commit is contained in:
v2ray 2016-01-17 21:43:10 +01:00
parent 9af80c834b
commit dde47290d7
19 changed files with 391 additions and 587 deletions

View File

@ -8,7 +8,7 @@ import (
. "github.com/v2ray/v2ray-core/app/router" . "github.com/v2ray/v2ray-core/app/router"
_ "github.com/v2ray/v2ray-core/app/router/rules" _ "github.com/v2ray/v2ray-core/app/router/rules"
v2net "github.com/v2ray/v2ray-core/common/net" v2net "github.com/v2ray/v2ray-core/common/net"
"github.com/v2ray/v2ray-core/shell/point/json" "github.com/v2ray/v2ray-core/shell/point"
v2testing "github.com/v2ray/v2ray-core/testing" v2testing "github.com/v2ray/v2ray-core/testing"
"github.com/v2ray/v2ray-core/testing/assert" "github.com/v2ray/v2ray-core/testing/assert"
) )
@ -18,10 +18,10 @@ func TestRouter(t *testing.T) {
baseDir := "$GOPATH/src/github.com/v2ray/v2ray-core/release/config" baseDir := "$GOPATH/src/github.com/v2ray/v2ray-core/release/config"
pointConfig, err := json.LoadConfig(filepath.Join(baseDir, "vpoint_socks_vmess.json")) pointConfig, err := point.LoadConfig(filepath.Join(baseDir, "vpoint_socks_vmess.json"))
assert.Error(err).IsNil() assert.Error(err).IsNil()
router, err := CreateRouter(pointConfig.RouterConfig().Strategy, pointConfig.RouterConfig().Settings) router, err := CreateRouter(pointConfig.RouterConfig.Strategy, pointConfig.RouterConfig.Settings)
assert.Error(err).IsNil() assert.Error(err).IsNil()
dest := v2net.TCPDestination(v2net.IPAddress(net.ParseIP("120.135.126.1")), 80) dest := v2net.TCPDestination(v2net.IPAddress(net.ParseIP("120.135.126.1")), 80)

View File

@ -7,7 +7,6 @@ import (
v2nettesting "github.com/v2ray/v2ray-core/common/net/testing" v2nettesting "github.com/v2ray/v2ray-core/common/net/testing"
_ "github.com/v2ray/v2ray-core/proxy/freedom" _ "github.com/v2ray/v2ray-core/proxy/freedom"
"github.com/v2ray/v2ray-core/shell/point" "github.com/v2ray/v2ray-core/shell/point"
"github.com/v2ray/v2ray-core/shell/point/testing/mocks"
v2testing "github.com/v2ray/v2ray-core/testing" v2testing "github.com/v2ray/v2ray-core/testing"
"github.com/v2ray/v2ray-core/testing/assert" "github.com/v2ray/v2ray-core/testing/assert"
"github.com/v2ray/v2ray-core/testing/servers/tcp" "github.com/v2ray/v2ray-core/testing/servers/tcp"
@ -34,24 +33,24 @@ func TestDokodemoTCP(t *testing.T) {
assert.Error(err).IsNil() assert.Error(err).IsNil()
pointPort := v2nettesting.PickPort() pointPort := v2nettesting.PickPort()
config := mocks.Config{ config := &point.Config{
PortValue: pointPort, Port: pointPort,
InboundConfigValue: &mocks.ConnectionConfig{ InboundConfig: &point.ConnectionConfig{
ProtocolValue: "dokodemo-door", Protocol: "dokodemo-door",
SettingsValue: []byte(`{ Settings: []byte(`{
"address": "127.0.0.1", "address": "127.0.0.1",
"port": ` + port.String() + `, "port": ` + port.String() + `,
"network": "tcp", "network": "tcp",
"timeout": 0 "timeout": 0
}`), }`),
}, },
OutboundConfigValue: &mocks.ConnectionConfig{ OutboundConfig: &point.ConnectionConfig{
ProtocolValue: "freedom", Protocol: "freedom",
SettingsValue: nil, Settings: nil,
}, },
} }
point, err := point.NewPoint(&config) point, err := point.NewPoint(config)
assert.Error(err).IsNil() assert.Error(err).IsNil()
err = point.Start() err = point.Start()
@ -95,24 +94,24 @@ func TestDokodemoUDP(t *testing.T) {
assert.Error(err).IsNil() assert.Error(err).IsNil()
pointPort := v2nettesting.PickPort() pointPort := v2nettesting.PickPort()
config := mocks.Config{ config := &point.Config{
PortValue: pointPort, Port: pointPort,
InboundConfigValue: &mocks.ConnectionConfig{ InboundConfig: &point.ConnectionConfig{
ProtocolValue: "dokodemo-door", Protocol: "dokodemo-door",
SettingsValue: []byte(`{ Settings: []byte(`{
"address": "127.0.0.1", "address": "127.0.0.1",
"port": ` + port.String() + `, "port": ` + port.String() + `,
"network": "udp", "network": "udp",
"timeout": 0 "timeout": 0
}`), }`),
}, },
OutboundConfigValue: &mocks.ConnectionConfig{ OutboundConfig: &point.ConnectionConfig{
ProtocolValue: "freedom", Protocol: "freedom",
SettingsValue: nil, Settings: nil,
}, },
} }
point, err := point.NewPoint(&config) point, err := point.NewPoint(config)
assert.Error(err).IsNil() assert.Error(err).IsNil()
err = point.Start() err = point.Start()

View File

@ -17,7 +17,6 @@ import (
proxytesting "github.com/v2ray/v2ray-core/proxy/testing" proxytesting "github.com/v2ray/v2ray-core/proxy/testing"
proxymocks "github.com/v2ray/v2ray-core/proxy/testing/mocks" proxymocks "github.com/v2ray/v2ray-core/proxy/testing/mocks"
"github.com/v2ray/v2ray-core/shell/point" "github.com/v2ray/v2ray-core/shell/point"
"github.com/v2ray/v2ray-core/shell/point/testing/mocks"
v2testing "github.com/v2ray/v2ray-core/testing" v2testing "github.com/v2ray/v2ray-core/testing"
"github.com/v2ray/v2ray-core/testing/assert" "github.com/v2ray/v2ray-core/testing/assert"
"github.com/v2ray/v2ray-core/testing/servers/tcp" "github.com/v2ray/v2ray-core/testing/servers/tcp"
@ -56,19 +55,19 @@ func TestUDPSend(t *testing.T) {
assert.Error(err).IsNil() assert.Error(err).IsNil()
pointPort := v2nettesting.PickPort() pointPort := v2nettesting.PickPort()
config := mocks.Config{ config := &point.Config{
PortValue: pointPort, Port: pointPort,
InboundConfigValue: &mocks.ConnectionConfig{ InboundConfig: &point.ConnectionConfig{
ProtocolValue: protocol, Protocol: protocol,
SettingsValue: nil, Settings: nil,
}, },
OutboundConfigValue: &mocks.ConnectionConfig{ OutboundConfig: &point.ConnectionConfig{
ProtocolValue: "freedom", Protocol: "freedom",
SettingsValue: nil, Settings: nil,
}, },
} }
point, err := point.NewPoint(&config) point, err := point.NewPoint(config)
assert.Error(err).IsNil() assert.Error(err).IsNil()
err = point.Start() err = point.Start()
@ -99,19 +98,19 @@ func TestSocksTcpConnect(t *testing.T) {
assert.Error(err).IsNil() assert.Error(err).IsNil()
pointPort := v2nettesting.PickPort() pointPort := v2nettesting.PickPort()
config := mocks.Config{ config := &point.Config{
PortValue: pointPort, Port: pointPort,
InboundConfigValue: &mocks.ConnectionConfig{ InboundConfig: &point.ConnectionConfig{
ProtocolValue: "socks", Protocol: "socks",
SettingsValue: []byte(`{"auth": "noauth"}`), Settings: []byte(`{"auth": "noauth"}`),
}, },
OutboundConfigValue: &mocks.ConnectionConfig{ OutboundConfig: &point.ConnectionConfig{
ProtocolValue: "freedom", Protocol: "freedom",
SettingsValue: nil, Settings: nil,
}, },
} }
point, err := point.NewPoint(&config) point, err := point.NewPoint(config)
assert.Error(err).IsNil() assert.Error(err).IsNil()
err = point.Start() err = point.Start()

View File

@ -15,7 +15,6 @@ import (
proxytesting "github.com/v2ray/v2ray-core/proxy/testing" proxytesting "github.com/v2ray/v2ray-core/proxy/testing"
proxymocks "github.com/v2ray/v2ray-core/proxy/testing/mocks" proxymocks "github.com/v2ray/v2ray-core/proxy/testing/mocks"
"github.com/v2ray/v2ray-core/shell/point" "github.com/v2ray/v2ray-core/shell/point"
"github.com/v2ray/v2ray-core/shell/point/testing/mocks"
v2testing "github.com/v2ray/v2ray-core/testing" v2testing "github.com/v2ray/v2ray-core/testing"
"github.com/v2ray/v2ray-core/testing/assert" "github.com/v2ray/v2ray-core/testing/assert"
) )
@ -36,22 +35,22 @@ func TestSocksTcpConnect(t *testing.T) {
}) })
assert.Error(err).IsNil() assert.Error(err).IsNil()
config := mocks.Config{ config := &point.Config{
PortValue: port, Port: port,
InboundConfigValue: &mocks.ConnectionConfig{ InboundConfig: &point.ConnectionConfig{
ProtocolValue: "socks", Protocol: "socks",
SettingsValue: []byte(` Settings: []byte(`
{ {
"auth": "noauth" "auth": "noauth"
}`), }`),
}, },
OutboundConfigValue: &mocks.ConnectionConfig{ OutboundConfig: &point.ConnectionConfig{
ProtocolValue: protocol, Protocol: protocol,
SettingsValue: nil, Settings: nil,
}, },
} }
point, err := point.NewPoint(&config) point, err := point.NewPoint(config)
assert.Error(err).IsNil() assert.Error(err).IsNil()
err = point.Start() err = point.Start()
@ -95,11 +94,11 @@ func TestSocksTcpConnectWithUserPass(t *testing.T) {
}) })
assert.Error(err).IsNil() assert.Error(err).IsNil()
config := mocks.Config{ config := &point.Config{
PortValue: port, Port: port,
InboundConfigValue: &mocks.ConnectionConfig{ InboundConfig: &point.ConnectionConfig{
ProtocolValue: "socks", Protocol: "socks",
SettingsValue: []byte(` Settings: []byte(`
{ {
"auth": "password", "auth": "password",
"accounts": [ "accounts": [
@ -107,13 +106,13 @@ func TestSocksTcpConnectWithUserPass(t *testing.T) {
] ]
}`), }`),
}, },
OutboundConfigValue: &mocks.ConnectionConfig{ OutboundConfig: &point.ConnectionConfig{
ProtocolValue: protocol, Protocol: protocol,
SettingsValue: nil, Settings: nil,
}, },
} }
point, err := point.NewPoint(&config) point, err := point.NewPoint(config)
assert.Error(err).IsNil() assert.Error(err).IsNil()
err = point.Start() err = point.Start()
@ -157,11 +156,11 @@ func TestSocksTcpConnectWithWrongUserPass(t *testing.T) {
}) })
assert.Error(err).IsNil() assert.Error(err).IsNil()
config := mocks.Config{ config := &point.Config{
PortValue: port, Port: port,
InboundConfigValue: &mocks.ConnectionConfig{ InboundConfig: &point.ConnectionConfig{
ProtocolValue: "socks", Protocol: "socks",
SettingsValue: []byte(` Settings: []byte(`
{ {
"auth": "password", "auth": "password",
"accounts": [ "accounts": [
@ -169,13 +168,13 @@ func TestSocksTcpConnectWithWrongUserPass(t *testing.T) {
] ]
}`), }`),
}, },
OutboundConfigValue: &mocks.ConnectionConfig{ OutboundConfig: &point.ConnectionConfig{
ProtocolValue: protocol, Protocol: protocol,
SettingsValue: nil, Settings: nil,
}, },
} }
point, err := point.NewPoint(&config) point, err := point.NewPoint(config)
assert.Error(err).IsNil() assert.Error(err).IsNil()
err = point.Start() err = point.Start()
@ -205,11 +204,11 @@ func TestSocksTcpConnectWithWrongAuthMethod(t *testing.T) {
}) })
assert.Error(err).IsNil() assert.Error(err).IsNil()
config := mocks.Config{ config := &point.Config{
PortValue: port, Port: port,
InboundConfigValue: &mocks.ConnectionConfig{ InboundConfig: &point.ConnectionConfig{
ProtocolValue: "socks", Protocol: "socks",
SettingsValue: []byte(` Settings: []byte(`
{ {
"auth": "password", "auth": "password",
"accounts": [ "accounts": [
@ -217,13 +216,13 @@ func TestSocksTcpConnectWithWrongAuthMethod(t *testing.T) {
] ]
}`), }`),
}, },
OutboundConfigValue: &mocks.ConnectionConfig{ OutboundConfig: &point.ConnectionConfig{
ProtocolValue: protocol, Protocol: protocol,
SettingsValue: nil, Settings: nil,
}, },
} }
point, err := point.NewPoint(&config) point, err := point.NewPoint(config)
assert.Error(err).IsNil() assert.Error(err).IsNil()
err = point.Start() err = point.Start()
@ -254,19 +253,19 @@ func TestSocksUdpSend(t *testing.T) {
}) })
assert.Error(err).IsNil() assert.Error(err).IsNil()
config := mocks.Config{ config := &point.Config{
PortValue: port, Port: port,
InboundConfigValue: &mocks.ConnectionConfig{ InboundConfig: &point.ConnectionConfig{
ProtocolValue: "socks", Protocol: "socks",
SettingsValue: []byte(`{"auth": "noauth", "udp": true}`), Settings: []byte(`{"auth": "noauth", "udp": true}`),
}, },
OutboundConfigValue: &mocks.ConnectionConfig{ OutboundConfig: &point.ConnectionConfig{
ProtocolValue: protocol, Protocol: protocol,
SettingsValue: nil, Settings: nil,
}, },
} }
point, err := point.NewPoint(&config) point, err := point.NewPoint(config)
assert.Error(err).IsNil() assert.Error(err).IsNil()
err = point.Start() err = point.Start()

View File

@ -15,7 +15,6 @@ import (
_ "github.com/v2ray/v2ray-core/proxy/vmess/inbound" _ "github.com/v2ray/v2ray-core/proxy/vmess/inbound"
_ "github.com/v2ray/v2ray-core/proxy/vmess/outbound" _ "github.com/v2ray/v2ray-core/proxy/vmess/outbound"
"github.com/v2ray/v2ray-core/shell/point" "github.com/v2ray/v2ray-core/shell/point"
"github.com/v2ray/v2ray-core/shell/point/testing/mocks"
v2testing "github.com/v2ray/v2ray-core/testing" v2testing "github.com/v2ray/v2ray-core/testing"
"github.com/v2ray/v2ray-core/testing/assert" "github.com/v2ray/v2ray-core/testing/assert"
) )
@ -44,15 +43,15 @@ func TestVMessInAndOut(t *testing.T) {
}) })
assert.Error(err).IsNil() assert.Error(err).IsNil()
configA := mocks.Config{ configA := &point.Config{
PortValue: portA, Port: portA,
InboundConfigValue: &mocks.ConnectionConfig{ InboundConfig: &point.ConnectionConfig{
ProtocolValue: protocol, Protocol: protocol,
SettingsValue: nil, Settings: nil,
}, },
OutboundConfigValue: &mocks.ConnectionConfig{ OutboundConfig: &point.ConnectionConfig{
ProtocolValue: "vmess", Protocol: "vmess",
SettingsValue: []byte(`{ Settings: []byte(`{
"vnext": [ "vnext": [
{ {
"address": "127.0.0.1", "address": "127.0.0.1",
@ -66,7 +65,7 @@ func TestVMessInAndOut(t *testing.T) {
}, },
} }
pointA, err := point.NewPoint(&configA) pointA, err := point.NewPoint(configA)
assert.Error(err).IsNil() assert.Error(err).IsNil()
err = pointA.Start() err = pointA.Start()
@ -84,23 +83,23 @@ func TestVMessInAndOut(t *testing.T) {
}) })
assert.Error(err).IsNil() assert.Error(err).IsNil()
configB := mocks.Config{ configB := &point.Config{
PortValue: portB, Port: portB,
InboundConfigValue: &mocks.ConnectionConfig{ InboundConfig: &point.ConnectionConfig{
ProtocolValue: "vmess", Protocol: "vmess",
SettingsValue: []byte(`{ Settings: []byte(`{
"clients": [ "clients": [
{"id": "` + testAccount.String() + `"} {"id": "` + testAccount.String() + `"}
] ]
}`), }`),
}, },
OutboundConfigValue: &mocks.ConnectionConfig{ OutboundConfig: &point.ConnectionConfig{
ProtocolValue: protocol, Protocol: protocol,
SettingsValue: nil, Settings: nil,
}, },
} }
pointB, err := point.NewPoint(&configB) pointB, err := point.NewPoint(configB)
assert.Error(err).IsNil() assert.Error(err).IsNil()
err = pointB.Start() err = pointB.Start()

View File

@ -10,7 +10,6 @@ import (
_ "github.com/v2ray/v2ray-core/app/router/rules" _ "github.com/v2ray/v2ray-core/app/router/rules"
"github.com/v2ray/v2ray-core/common/log" "github.com/v2ray/v2ray-core/common/log"
"github.com/v2ray/v2ray-core/shell/point" "github.com/v2ray/v2ray-core/shell/point"
pointjson "github.com/v2ray/v2ray-core/shell/point/json"
// The following are neccesary as they register handlers in their init functions. // The following are neccesary as they register handlers in their init functions.
_ "github.com/v2ray/v2ray-core/proxy/blackhole" _ "github.com/v2ray/v2ray-core/proxy/blackhole"
@ -64,14 +63,14 @@ func main() {
log.Error("Config file is not set.") log.Error("Config file is not set.")
return return
} }
config, err := pointjson.LoadConfig(configFile) config, err := point.LoadConfig(configFile)
if err != nil { if err != nil {
log.Error("Failed to read config file (%s): %v", configFile, err) log.Error("Failed to read config file (%s): %v", configFile, err)
return return
} }
if config.LogConfig() != nil && len(config.LogConfig().AccessLog()) > 0 { if config.LogConfig != nil && len(config.LogConfig.AccessLog) > 0 {
log.InitAccessLogger(config.LogConfig().AccessLog()) log.InitAccessLogger(config.LogConfig.AccessLog)
} }
vPoint, err := point.NewPoint(config) vPoint, err := point.NewPoint(config)

View File

@ -7,20 +7,20 @@ import (
v2net "github.com/v2ray/v2ray-core/common/net" v2net "github.com/v2ray/v2ray-core/common/net"
) )
type ConnectionConfig interface { type ConnectionConfig struct {
Protocol() string Protocol string
Settings() []byte Settings []byte
} }
type LogConfig interface { type LogConfig struct {
AccessLog() string AccessLog string
ErrorLog() string ErrorLog string
LogLevel() log.LogLevel LogLevel log.LogLevel
} }
type DnsConfig interface { type DnsConfig struct {
Enabled() bool Enabled bool
Settings() dns.CacheConfig Settings *dns.CacheConfig
} }
const ( const (
@ -29,32 +29,45 @@ const (
AllocationStrategyExternal = "external" AllocationStrategyExternal = "external"
) )
type InboundDetourAllocationConfig interface { type InboundDetourAllocationConfig struct {
Strategy() string // Allocation strategy of this inbound detour. Strategy string // Allocation strategy of this inbound detour.
Concurrency() int // Number of handlers (ports) running in parallel. Concurrency int // Number of handlers (ports) running in parallel.
Refresh() int // Number of seconds before a handler is regenerated. Refresh int // Number of seconds before a handler is regenerated.
} }
type InboundDetourConfig interface { type InboundDetourConfig struct {
Protocol() string Protocol string
PortRange() v2net.PortRange PortRange v2net.PortRange
Tag() string Tag string
Allocation() InboundDetourAllocationConfig Allocation *InboundDetourAllocationConfig
Settings() []byte Settings []byte
} }
type OutboundDetourConfig interface { type OutboundDetourConfig struct {
Protocol() string Protocol string
Tag() string Tag string
Settings() []byte Settings []byte
} }
type PointConfig interface { type Config struct {
Port() v2net.Port Port v2net.Port
LogConfig() LogConfig LogConfig *LogConfig
RouterConfig() *router.Config RouterConfig *router.Config
InboundConfig() ConnectionConfig InboundConfig *ConnectionConfig
OutboundConfig() ConnectionConfig OutboundConfig *ConnectionConfig
InboundDetours() []InboundDetourConfig InboundDetours []*InboundDetourConfig
OutboundDetours() []OutboundDetourConfig OutboundDetours []*OutboundDetourConfig
}
type ConfigLoader func(init string) (*Config, error)
var (
configLoader ConfigLoader
)
func LoadConfig(init string) (*Config, error) {
if configLoader == nil {
return nil, BadConfiguration
}
return configLoader(init)
} }

157
shell/point/config_json.go Normal file
View File

@ -0,0 +1,157 @@
// +build json
package point
import (
"encoding/json"
"io/ioutil"
"os"
"strings"
"github.com/v2ray/v2ray-core/app/router"
"github.com/v2ray/v2ray-core/common/log"
v2net "github.com/v2ray/v2ray-core/common/net"
)
func (this *Config) UnmarshalJSON(data []byte) error {
type JsonConfig struct {
Port v2net.Port `json:"port"` // Port of this Point server.
LogConfig *LogConfig `json:"log"`
RouterConfig *router.Config `json:"routing"`
InboundConfig *ConnectionConfig `json:"inbound"`
OutboundConfig *ConnectionConfig `json:"outbound"`
InboundDetours []*InboundDetourConfig `json:"inboundDetour"`
OutboundDetours []*OutboundDetourConfig `json:"outboundDetour"`
}
jsonConfig := new(JsonConfig)
if err := json.Unmarshal(data, jsonConfig); err != nil {
return err
}
this.Port = jsonConfig.Port
this.LogConfig = jsonConfig.LogConfig
this.RouterConfig = jsonConfig.RouterConfig
this.InboundConfig = jsonConfig.InboundConfig
this.OutboundConfig = jsonConfig.OutboundConfig
this.InboundDetours = jsonConfig.InboundDetours
this.OutboundDetours = jsonConfig.OutboundDetours
return nil
}
func (this *ConnectionConfig) UnmarshalJSON(data []byte) error {
type JsonConnectionConfig struct {
Protocol string `json:"protocol"`
Settings json.RawMessage `json:"settings"`
}
jsonConfig := new(JsonConnectionConfig)
if err := json.Unmarshal(data, jsonConfig); err != nil {
return err
}
this.Protocol = jsonConfig.Protocol
this.Settings = jsonConfig.Settings
return nil
}
func (this *LogConfig) UnmarshalJSON(data []byte) error {
type JsonLogConfig struct {
AccessLog string `json:"access"`
ErrorLog string `json:"error"`
LogLevel string `json:"loglevel"`
}
jsonConfig := new(JsonLogConfig)
if err := json.Unmarshal(data, jsonConfig); err != nil {
return err
}
this.AccessLog = jsonConfig.AccessLog
this.ErrorLog = jsonConfig.ErrorLog
level := strings.ToLower(jsonConfig.LogLevel)
switch level {
case "debug":
this.LogLevel = log.DebugLevel
case "info":
this.LogLevel = log.InfoLevel
case "error":
this.LogLevel = log.ErrorLevel
default:
this.LogLevel = log.WarningLevel
}
return nil
}
func (this *InboundDetourAllocationConfig) UnmarshalJSON(data []byte) error {
type JsonInboundDetourAllocationConfig struct {
Strategy string `json:"strategy"`
Concurrency int `json:"concurrency"`
RefreshSec int `json:"refresh"`
}
jsonConfig := new(JsonInboundDetourAllocationConfig)
if err := json.Unmarshal(data, jsonConfig); err != nil {
return err
}
this.Strategy = jsonConfig.Strategy
this.Concurrency = jsonConfig.Concurrency
this.Refresh = jsonConfig.RefreshSec
return nil
}
func (this *InboundDetourConfig) UnmarshalJSON(data []byte) error {
type JsonInboundDetourConfig struct {
Protocol string `json:"protocol"`
PortRange *v2net.PortRange `json:"port"`
Settings json.RawMessage `json:"settings"`
Tag string `json:"tag"`
Allocation *InboundDetourAllocationConfig `json:"allocate"`
}
jsonConfig := new(JsonInboundDetourConfig)
if err := json.Unmarshal(data, jsonConfig); err != nil {
return err
}
if jsonConfig.PortRange == nil {
log.Error("Point: Port range not specified in InboundDetour.")
return BadConfiguration
}
this.Protocol = jsonConfig.Protocol
this.PortRange = *jsonConfig.PortRange
this.Settings = jsonConfig.Settings
this.Tag = jsonConfig.Tag
this.Allocation = jsonConfig.Allocation
return nil
}
func (this *OutboundDetourConfig) UnmarshalJSON(data []byte) error {
type JsonOutboundDetourConfig struct {
Protocol string `json:"protocol"`
Tag string `json:"tag"`
Settings json.RawMessage `json:"settings"`
}
jsonConfig := new(JsonOutboundDetourConfig)
if err := json.Unmarshal(data, jsonConfig); err != nil {
return err
}
this.Protocol = jsonConfig.Protocol
this.Tag = jsonConfig.Tag
this.Settings = jsonConfig.Settings
return nil
}
func JsonLoadConfig(file string) (*Config, error) {
fixedFile := os.ExpandEnv(file)
rawConfig, err := ioutil.ReadFile(fixedFile)
if err != nil {
log.Error("Failed to read server config file (%s): %v", file, err)
return nil, err
}
jsonConfig := &Config{}
err = json.Unmarshal(rawConfig, jsonConfig)
if err != nil {
log.Error("Failed to load server config: %v", err)
return nil, err
}
return jsonConfig, err
}
func init() {
configLoader = JsonLoadConfig
}

View File

@ -0,0 +1,60 @@
// +build json
package point_test
import (
"path/filepath"
"testing"
_ "github.com/v2ray/v2ray-core/app/router/rules"
netassert "github.com/v2ray/v2ray-core/common/net/testing/assert"
_ "github.com/v2ray/v2ray-core/proxy/dokodemo"
_ "github.com/v2ray/v2ray-core/proxy/freedom"
_ "github.com/v2ray/v2ray-core/proxy/socks"
_ "github.com/v2ray/v2ray-core/proxy/vmess/inbound"
_ "github.com/v2ray/v2ray-core/proxy/vmess/outbound"
. "github.com/v2ray/v2ray-core/shell/point"
v2testing "github.com/v2ray/v2ray-core/testing"
"github.com/v2ray/v2ray-core/testing/assert"
)
func TestClientSampleConfig(t *testing.T) {
v2testing.Current(t)
// TODO: fix for Windows
baseDir := "$GOPATH/src/github.com/v2ray/v2ray-core/release/config"
pointConfig, err := LoadConfig(filepath.Join(baseDir, "vpoint_socks_vmess.json"))
assert.Error(err).IsNil()
netassert.Port(pointConfig.Port).IsValid()
assert.Pointer(pointConfig.InboundConfig).IsNotNil()
assert.Pointer(pointConfig.OutboundConfig).IsNotNil()
assert.StringLiteral(pointConfig.InboundConfig.Protocol).Equals("socks")
assert.Pointer(pointConfig.InboundConfig.Settings).IsNotNil()
assert.StringLiteral(pointConfig.OutboundConfig.Protocol).Equals("vmess")
assert.Pointer(pointConfig.OutboundConfig.Settings).IsNotNil()
}
func TestServerSampleConfig(t *testing.T) {
v2testing.Current(t)
// TODO: fix for Windows
baseDir := "$GOPATH/src/github.com/v2ray/v2ray-core/release/config"
pointConfig, err := LoadConfig(filepath.Join(baseDir, "vpoint_vmess_freedom.json"))
assert.Error(err).IsNil()
netassert.Port(pointConfig.Port).IsValid()
assert.Pointer(pointConfig.InboundConfig).IsNotNil()
assert.Pointer(pointConfig.OutboundConfig).IsNotNil()
assert.StringLiteral(pointConfig.InboundConfig.Protocol).Equals("vmess")
assert.Pointer(pointConfig.InboundConfig.Settings).IsNotNil()
assert.StringLiteral(pointConfig.OutboundConfig.Protocol).Equals("freedom")
assert.Pointer(pointConfig.OutboundConfig.Settings).IsNotNil()
}

View File

@ -17,16 +17,16 @@ type InboundConnectionHandlerWithPort struct {
// Handler for inbound detour connections. // Handler for inbound detour connections.
type InboundDetourHandler struct { type InboundDetourHandler struct {
space app.Space space app.Space
config InboundDetourConfig config *InboundDetourConfig
ich []*InboundConnectionHandlerWithPort ich []*InboundConnectionHandlerWithPort
} }
func (this *InboundDetourHandler) Initialize() error { func (this *InboundDetourHandler) Initialize() error {
ports := this.config.PortRange() ports := this.config.PortRange
this.ich = make([]*InboundConnectionHandlerWithPort, 0, ports.To-ports.From+1) this.ich = make([]*InboundConnectionHandlerWithPort, 0, ports.To-ports.From+1)
for i := ports.From; i <= ports.To; i++ { for i := ports.From; i <= ports.To; i++ {
ichConfig := this.config.Settings() ichConfig := this.config.Settings
ich, err := proxyrepo.CreateInboundConnectionHandler(this.config.Protocol(), this.space, ichConfig) ich, err := proxyrepo.CreateInboundConnectionHandler(this.config.Protocol, this.space, ichConfig)
if err != nil { if err != nil {
log.Error("Failed to create inbound connection handler: %v", err) log.Error("Failed to create inbound connection handler: %v", err)
return err return err

View File

@ -1,18 +0,0 @@
package json
import (
"encoding/json"
)
type ConnectionConfig struct {
ProtocolString string `json:"protocol"`
SettingsMessage json.RawMessage `json:"settings"`
}
func (c *ConnectionConfig) Protocol() string {
return c.ProtocolString
}
func (c *ConnectionConfig) Settings() []byte {
return []byte(c.SettingsMessage)
}

View File

@ -1,54 +0,0 @@
package json
import (
"encoding/json"
v2net "github.com/v2ray/v2ray-core/common/net"
"github.com/v2ray/v2ray-core/shell/point"
)
type InboundDetourAllocationConfig struct {
StrategyValue string `json:"strategy"`
ConcurrencyValue int `json:"concurrency"`
RefreshSec int `json:"refresh"`
}
func (this *InboundDetourAllocationConfig) Refresh() int {
return this.RefreshSec
}
func (this *InboundDetourAllocationConfig) Strategy() string {
return this.StrategyValue
}
func (this *InboundDetourAllocationConfig) Concurrency() int {
return this.ConcurrencyValue
}
type InboundDetourConfig struct {
ProtocolValue string `json:"protocol"`
PortRangeValue *v2net.PortRange `json:"port"`
SettingsValue json.RawMessage `json:"settings"`
TagValue string `json:"tag"`
AllocationValue *InboundDetourAllocationConfig `json:"allocate"`
}
func (this *InboundDetourConfig) Allocation() point.InboundDetourAllocationConfig {
return this.AllocationValue
}
func (this *InboundDetourConfig) Protocol() string {
return this.ProtocolValue
}
func (this *InboundDetourConfig) PortRange() v2net.PortRange {
return *this.PortRangeValue
}
func (this *InboundDetourConfig) Settings() []byte {
return []byte(this.SettingsValue)
}
func (this *InboundDetourConfig) Tag() string {
return this.TagValue
}

View File

@ -1,89 +0,0 @@
package json
import (
"encoding/json"
"io/ioutil"
"os"
"github.com/v2ray/v2ray-core/app/router"
"github.com/v2ray/v2ray-core/common/log"
v2net "github.com/v2ray/v2ray-core/common/net"
"github.com/v2ray/v2ray-core/shell/point"
)
// Config is the config for Point server.
type Config struct {
PortValue v2net.Port `json:"port"` // Port of this Point server.
LogConfigValue *LogConfig `json:"log"`
RouterConfigValue *router.Config `json:"routing"`
InboundConfigValue *ConnectionConfig `json:"inbound"`
OutboundConfigValue *ConnectionConfig `json:"outbound"`
InboundDetoursValue []*InboundDetourConfig `json:"inboundDetour"`
OutboundDetoursValue []*OutboundDetourConfig `json:"outboundDetour"`
}
func (config *Config) Port() v2net.Port {
return config.PortValue
}
func (config *Config) LogConfig() point.LogConfig {
if config.LogConfigValue == nil {
return nil
}
return config.LogConfigValue
}
func (this *Config) RouterConfig() *router.Config {
if this.RouterConfigValue == nil {
return nil
}
return this.RouterConfigValue
}
func (config *Config) InboundConfig() point.ConnectionConfig {
if config.InboundConfigValue == nil {
return nil
}
return config.InboundConfigValue
}
func (config *Config) OutboundConfig() point.ConnectionConfig {
if config.OutboundConfigValue == nil {
return nil
}
return config.OutboundConfigValue
}
func (this *Config) InboundDetours() []point.InboundDetourConfig {
detours := make([]point.InboundDetourConfig, len(this.InboundDetoursValue))
for idx, detour := range this.InboundDetoursValue {
detours[idx] = detour
}
return detours
}
func (this *Config) OutboundDetours() []point.OutboundDetourConfig {
detours := make([]point.OutboundDetourConfig, len(this.OutboundDetoursValue))
for idx, detour := range this.OutboundDetoursValue {
detours[idx] = detour
}
return detours
}
func LoadConfig(file string) (*Config, error) {
fixedFile := os.ExpandEnv(file)
rawConfig, err := ioutil.ReadFile(fixedFile)
if err != nil {
log.Error("Failed to read server config file (%s): %v", file, err)
return nil, err
}
jsonConfig := &Config{}
err = json.Unmarshal(rawConfig, jsonConfig)
if err != nil {
log.Error("Failed to load server config: %v", err)
return nil, err
}
return jsonConfig, err
}

View File

@ -1,58 +0,0 @@
package json_test
import (
"path/filepath"
"testing"
_ "github.com/v2ray/v2ray-core/app/router/rules"
netassert "github.com/v2ray/v2ray-core/common/net/testing/assert"
_ "github.com/v2ray/v2ray-core/proxy/dokodemo"
_ "github.com/v2ray/v2ray-core/proxy/freedom"
_ "github.com/v2ray/v2ray-core/proxy/socks"
_ "github.com/v2ray/v2ray-core/proxy/vmess/inbound"
_ "github.com/v2ray/v2ray-core/proxy/vmess/outbound"
"github.com/v2ray/v2ray-core/shell/point/json"
v2testing "github.com/v2ray/v2ray-core/testing"
"github.com/v2ray/v2ray-core/testing/assert"
)
func TestClientSampleConfig(t *testing.T) {
v2testing.Current(t)
// TODO: fix for Windows
baseDir := "$GOPATH/src/github.com/v2ray/v2ray-core/release/config"
pointConfig, err := json.LoadConfig(filepath.Join(baseDir, "vpoint_socks_vmess.json"))
assert.Error(err).IsNil()
netassert.Port(pointConfig.Port()).IsValid()
assert.Pointer(pointConfig.InboundConfig()).IsNotNil()
assert.Pointer(pointConfig.OutboundConfig()).IsNotNil()
assert.StringLiteral(pointConfig.InboundConfig().Protocol()).Equals("socks")
assert.Pointer(pointConfig.InboundConfig().Settings()).IsNotNil()
assert.StringLiteral(pointConfig.OutboundConfig().Protocol()).Equals("vmess")
assert.Pointer(pointConfig.OutboundConfig().Settings()).IsNotNil()
}
func TestServerSampleConfig(t *testing.T) {
v2testing.Current(t)
// TODO: fix for Windows
baseDir := "$GOPATH/src/github.com/v2ray/v2ray-core/release/config"
pointConfig, err := json.LoadConfig(filepath.Join(baseDir, "vpoint_vmess_freedom.json"))
assert.Error(err).IsNil()
assert.Uint16(pointConfig.Port().Value()).Positive()
assert.Pointer(pointConfig.InboundConfig()).IsNotNil()
assert.Pointer(pointConfig.OutboundConfig()).IsNotNil()
assert.StringLiteral(pointConfig.InboundConfig().Protocol()).Equals("vmess")
assert.Pointer(pointConfig.InboundConfig().Settings()).IsNotNil()
assert.StringLiteral(pointConfig.OutboundConfig().Protocol()).Equals("freedom")
assert.Pointer(pointConfig.OutboundConfig().Settings()).IsNotNil()
}

View File

@ -1,35 +0,0 @@
package json
import (
"strings"
"github.com/v2ray/v2ray-core/common/log"
)
type LogConfig struct {
AccessLogValue string `json:"access"`
ErrorLogValue string `json:"error"`
LogLevelValue string `json:"loglevel"`
}
func (this *LogConfig) AccessLog() string {
return this.AccessLogValue
}
func (this *LogConfig) ErrorLog() string {
return this.ErrorLogValue
}
func (this *LogConfig) LogLevel() log.LogLevel {
level := strings.ToLower(this.LogLevelValue)
switch level {
case "debug":
return log.DebugLevel
case "info":
return log.InfoLevel
case "error":
return log.ErrorLevel
default:
return log.WarningLevel
}
}

View File

@ -1,23 +0,0 @@
package json
import (
"encoding/json"
)
type OutboundDetourConfig struct {
ProtocolValue string `json:"protocol"`
TagValue string `json:"tag"`
SettingsValue json.RawMessage `json:"settings"`
}
func (this *OutboundDetourConfig) Protocol() string {
return this.ProtocolValue
}
func (this *OutboundDetourConfig) Tag() string {
return this.TagValue
}
func (this *OutboundDetourConfig) Settings() []byte {
return []byte(this.SettingsValue)
}

View File

@ -29,54 +29,54 @@ type Point struct {
// NewPoint returns a new Point server based on given configuration. // NewPoint returns a new Point server based on given configuration.
// The server is not started at this point. // The server is not started at this point.
func NewPoint(pConfig PointConfig) (*Point, error) { func NewPoint(pConfig *Config) (*Point, error) {
var vpoint = new(Point) var vpoint = new(Point)
vpoint.port = pConfig.Port() vpoint.port = pConfig.Port
if pConfig.LogConfig() != nil { if pConfig.LogConfig != nil {
logConfig := pConfig.LogConfig() logConfig := pConfig.LogConfig
if len(logConfig.AccessLog()) > 0 { if len(logConfig.AccessLog) > 0 {
err := log.InitAccessLogger(logConfig.AccessLog()) err := log.InitAccessLogger(logConfig.AccessLog)
if err != nil { if err != nil {
return nil, err return nil, err
} }
} }
if len(logConfig.ErrorLog()) > 0 { if len(logConfig.ErrorLog) > 0 {
err := log.InitErrorLogger(logConfig.ErrorLog()) err := log.InitErrorLogger(logConfig.ErrorLog)
if err != nil { if err != nil {
return nil, err return nil, err
} }
} }
log.SetLogLevel(logConfig.LogLevel()) log.SetLogLevel(logConfig.LogLevel)
} }
vpoint.space = controller.New() vpoint.space = controller.New()
vpoint.space.Bind(vpoint) vpoint.space.Bind(vpoint)
ichConfig := pConfig.InboundConfig().Settings() ichConfig := pConfig.InboundConfig.Settings
ich, err := proxyrepo.CreateInboundConnectionHandler(pConfig.InboundConfig().Protocol(), vpoint.space.ForContext("vpoint-default-inbound"), ichConfig) ich, err := proxyrepo.CreateInboundConnectionHandler(pConfig.InboundConfig.Protocol, vpoint.space.ForContext("vpoint-default-inbound"), ichConfig)
if err != nil { if err != nil {
log.Error("Failed to create inbound connection handler: %v", err) log.Error("Failed to create inbound connection handler: %v", err)
return nil, err return nil, err
} }
vpoint.ich = ich vpoint.ich = ich
ochConfig := pConfig.OutboundConfig().Settings() ochConfig := pConfig.OutboundConfig.Settings
och, err := proxyrepo.CreateOutboundConnectionHandler(pConfig.OutboundConfig().Protocol(), vpoint.space.ForContext("vpoint-default-outbound"), ochConfig) och, err := proxyrepo.CreateOutboundConnectionHandler(pConfig.OutboundConfig.Protocol, vpoint.space.ForContext("vpoint-default-outbound"), ochConfig)
if err != nil { if err != nil {
log.Error("Failed to create outbound connection handler: %v", err) log.Error("Failed to create outbound connection handler: %v", err)
return nil, err return nil, err
} }
vpoint.och = och vpoint.och = och
detours := pConfig.InboundDetours() detours := pConfig.InboundDetours
if len(detours) > 0 { if len(detours) > 0 {
vpoint.idh = make([]*InboundDetourHandler, len(detours)) vpoint.idh = make([]*InboundDetourHandler, len(detours))
for idx, detourConfig := range detours { for idx, detourConfig := range detours {
detourHandler := &InboundDetourHandler{ detourHandler := &InboundDetourHandler{
space: vpoint.space.ForContext(detourConfig.Tag()), space: vpoint.space.ForContext(detourConfig.Tag),
config: detourConfig, config: detourConfig,
} }
err := detourHandler.Initialize() err := detourHandler.Initialize()
@ -87,20 +87,20 @@ func NewPoint(pConfig PointConfig) (*Point, error) {
} }
} }
outboundDetours := pConfig.OutboundDetours() outboundDetours := pConfig.OutboundDetours
if len(outboundDetours) > 0 { if len(outboundDetours) > 0 {
vpoint.odh = make(map[string]proxy.OutboundConnectionHandler) vpoint.odh = make(map[string]proxy.OutboundConnectionHandler)
for _, detourConfig := range outboundDetours { for _, detourConfig := range outboundDetours {
detourHandler, err := proxyrepo.CreateOutboundConnectionHandler(detourConfig.Protocol(), vpoint.space.ForContext(detourConfig.Tag()), detourConfig.Settings()) detourHandler, err := proxyrepo.CreateOutboundConnectionHandler(detourConfig.Protocol, vpoint.space.ForContext(detourConfig.Tag), detourConfig.Settings)
if err != nil { if err != nil {
log.Error("Failed to create detour outbound connection handler: %v", err) log.Error("Failed to create detour outbound connection handler: %v", err)
return nil, err return nil, err
} }
vpoint.odh[detourConfig.Tag()] = detourHandler vpoint.odh[detourConfig.Tag] = detourHandler
} }
} }
routerConfig := pConfig.RouterConfig() routerConfig := pConfig.RouterConfig
if routerConfig != nil { if routerConfig != nil {
r, err := router.CreateRouter(routerConfig.Strategy, routerConfig.Settings) r, err := router.CreateRouter(routerConfig.Strategy, routerConfig.Settings)
if err != nil { if err != nil {

View File

@ -1,143 +0,0 @@
package mocks
import (
"github.com/v2ray/v2ray-core/app/router"
"github.com/v2ray/v2ray-core/common/log"
v2net "github.com/v2ray/v2ray-core/common/net"
"github.com/v2ray/v2ray-core/shell/point"
)
type ConnectionConfig struct {
ProtocolValue string
SettingsValue []byte
}
func (config *ConnectionConfig) Protocol() string {
return config.ProtocolValue
}
func (config *ConnectionConfig) Settings() []byte {
return config.SettingsValue
}
type LogConfig struct {
AccessLogValue string
ErrorLogValue string
LogLevelValue log.LogLevel
}
func (config *LogConfig) AccessLog() string {
return config.AccessLogValue
}
func (this *LogConfig) ErrorLog() string {
return this.ErrorLogValue
}
func (this *LogConfig) LogLevel() log.LogLevel {
return this.LogLevelValue
}
type InboundDetourAllocationConfig struct {
StrategyValue string
ConcurrencyValue int
RefreshSec int
}
func (this *InboundDetourAllocationConfig) Refresh() int {
return this.RefreshSec
}
func (this *InboundDetourAllocationConfig) Strategy() string {
return this.StrategyValue
}
func (this *InboundDetourAllocationConfig) Concurrency() int {
return this.ConcurrencyValue
}
type InboundDetourConfig struct {
*ConnectionConfig
PortRangeValue *v2net.PortRange
TagValue string
AllocationStrategy *InboundDetourAllocationConfig
}
func (this *InboundDetourConfig) Allocation() point.InboundDetourAllocationConfig {
return this.AllocationStrategy
}
func (this *InboundDetourConfig) Tag() string {
return this.TagValue
}
func (this *InboundDetourConfig) PortRange() v2net.PortRange {
return *this.PortRangeValue
}
type OutboundDetourConfig struct {
*ConnectionConfig
TagValue string
}
func (this *OutboundDetourConfig) Tag() string {
return this.TagValue
}
type Config struct {
PortValue v2net.Port
LogConfigValue *LogConfig
RouterConfigValue *router.Config
InboundConfigValue *ConnectionConfig
OutboundConfigValue *ConnectionConfig
InboundDetoursValue []*InboundDetourConfig
OutboundDetoursValue []*OutboundDetourConfig
}
func (config *Config) Port() v2net.Port {
return config.PortValue
}
func (config *Config) LogConfig() point.LogConfig {
if config.LogConfigValue == nil {
return nil
}
return config.LogConfigValue
}
func (this *Config) RouterConfig() *router.Config {
if this.RouterConfigValue == nil {
return nil
}
return this.RouterConfigValue
}
func (this *Config) InboundConfig() point.ConnectionConfig {
if this.InboundConfigValue == nil {
return nil
}
return this.InboundConfigValue
}
func (this *Config) OutboundConfig() point.ConnectionConfig {
if this.OutboundConfigValue == nil {
return nil
}
return this.OutboundConfigValue
}
func (this *Config) InboundDetours() []point.InboundDetourConfig {
detours := make([]point.InboundDetourConfig, len(this.InboundDetoursValue))
for idx, detour := range this.InboundDetoursValue {
detours[idx] = detour
}
return detours
}
func (this *Config) OutboundDetours() []point.OutboundDetourConfig {
detours := make([]point.OutboundDetourConfig, len(this.OutboundDetoursValue))
for idx, detour := range this.OutboundDetoursValue {
detours[idx] = detour
}
return detours
}

View File

@ -7,7 +7,6 @@ import (
_ "github.com/v2ray/v2ray-core/app/router/rules" _ "github.com/v2ray/v2ray-core/app/router/rules"
"github.com/v2ray/v2ray-core/common/log" "github.com/v2ray/v2ray-core/common/log"
"github.com/v2ray/v2ray-core/shell/point" "github.com/v2ray/v2ray-core/shell/point"
pointjson "github.com/v2ray/v2ray-core/shell/point/json"
// The following are neccesary as they register handlers in their init functions. // The following are neccesary as they register handlers in their init functions.
_ "github.com/v2ray/v2ray-core/proxy/blackhole" _ "github.com/v2ray/v2ray-core/proxy/blackhole"
@ -39,7 +38,7 @@ func InitializeServerSetOnce(testcase string) error {
} }
func InitializeServer(configFile string) error { func InitializeServer(configFile string) error {
config, err := pointjson.LoadConfig(configFile) config, err := point.LoadConfig(configFile)
if err != nil { if err != nil {
log.Error("Failed to read config file (%s): %v", configFile, err) log.Error("Failed to read config file (%s): %v", configFile, err)
return err return err