From 1cec5ac643d80f77205d9245ff83a689442ca3b0 Mon Sep 17 00:00:00 2001 From: V2Ray Date: Sun, 1 Nov 2015 23:01:15 +0100 Subject: [PATCH] Test case for inbound detour network --- app/point/config/json/inbound_detour.go | 6 ++-- app/point/config/json/json_test.go | 20 ++++++++++++ common/net/json/network.go | 26 ++++++++++++---- common/net/json/network_test.go | 28 +++++++++++++++++ release/config/vpoint_dns_detour.json | 41 +++++++++++++++++++++++++ 5 files changed, 112 insertions(+), 9 deletions(-) create mode 100644 common/net/json/network_test.go create mode 100644 release/config/vpoint_dns_detour.json diff --git a/app/point/config/json/inbound_detour.go b/app/point/config/json/inbound_detour.go index 7150d5e54..f4963e9b0 100644 --- a/app/point/config/json/inbound_detour.go +++ b/app/point/config/json/inbound_detour.go @@ -8,9 +8,9 @@ import ( ) type InboundDetourConfig struct { - ProtocolValue string - PortRangeValue *PortRange - SettingsValue json.RawMessage + ProtocolValue string `json:"protocol"` + PortRangeValue *PortRange `json:"port"` + SettingsValue json.RawMessage `json:"settings"` } func (this *InboundDetourConfig) Protocol() string { diff --git a/app/point/config/json/json_test.go b/app/point/config/json/json_test.go index a3ef827af..c5b915d1f 100644 --- a/app/point/config/json/json_test.go +++ b/app/point/config/json/json_test.go @@ -5,6 +5,7 @@ import ( "testing" "github.com/v2ray/v2ray-core/app/point/config/json" + _ "github.com/v2ray/v2ray-core/proxy/dokodemo/config/json" _ "github.com/v2ray/v2ray-core/proxy/freedom/config/json" _ "github.com/v2ray/v2ray-core/proxy/socks/config/json" _ "github.com/v2ray/v2ray-core/proxy/vmess/config/json" @@ -51,3 +52,22 @@ func TestServerSampleConfig(t *testing.T) { assert.String(pointConfig.OutboundConfig().Protocol()).Equals("freedom") assert.Pointer(pointConfig.OutboundConfig().Settings()).IsNotNil() } + +func TestDetourConfig(t *testing.T) { + assert := unit.Assert(t) + + // TODO: fix for Windows + baseDir := "$GOPATH/src/github.com/v2ray/v2ray-core/release/config" + + pointConfig, err := json.LoadConfig(filepath.Join(baseDir, "vpoint_dns_detour.json")) + assert.Error(err).IsNil() + + detours := pointConfig.InboundDetours() + assert.Int(len(detours)).Equals(1) + + detour := detours[0] + assert.String(detour.Protocol()).Equals("dokodemo-door") + assert.Uint16(detour.PortRange().From()).Equals(uint16(53)) + assert.Uint16(detour.PortRange().To()).Equals(uint16(53)) + assert.Pointer(detour.Settings()).IsNotNil() +} diff --git a/common/net/json/network.go b/common/net/json/network.go index ce8e5357a..e7abe4d47 100644 --- a/common/net/json/network.go +++ b/common/net/json/network.go @@ -2,6 +2,7 @@ package json import ( "encoding/json" + "errors" "strings" v2net "github.com/v2ray/v2ray-core/common/net" @@ -9,17 +10,30 @@ import ( type NetworkList []string +func NewNetworkList(networks []string) NetworkList { + list := NetworkList(make([]string, len(networks))) + for idx, network := range networks { + list[idx] = strings.ToLower(strings.TrimSpace(network)) + } + return list +} + func (this *NetworkList) UnmarshalJSON(data []byte) error { var strList []string err := json.Unmarshal(data, &strList) - if err != nil { - return err + if err == nil { + *this = NewNetworkList(strList) + return nil } - *this = make([]string, len(strList)) - for idx, str := range strList { - (*this)[idx] = strings.ToLower(str) + + var str string + err = json.Unmarshal(data, &str) + if err == nil { + strList := strings.Split(str, ",") + *this = NewNetworkList(strList) + return nil } - return nil + return errors.New("Unknown format of network list: " + string(data)) } func (this *NetworkList) HasNetwork(network v2net.Network) bool { diff --git a/common/net/json/network_test.go b/common/net/json/network_test.go new file mode 100644 index 000000000..32d77cebb --- /dev/null +++ b/common/net/json/network_test.go @@ -0,0 +1,28 @@ +package json + +import ( + "encoding/json" + "testing" + + "github.com/v2ray/v2ray-core/testing/unit" +) + +func TestArrayNetworkList(t *testing.T) { + assert := unit.Assert(t) + + var list NetworkList + err := json.Unmarshal([]byte("[\"Tcp\"]"), &list) + assert.Error(err).IsNil() + assert.Bool(list.HasNetwork("tcp")).IsTrue() + assert.Bool(list.HasNetwork("udp")).IsFalse() +} + +func TestStringNetworkList(t *testing.T) { + assert := unit.Assert(t) + + var list NetworkList + err := json.Unmarshal([]byte("\"TCP, ip\""), &list) + assert.Error(err).IsNil() + assert.Bool(list.HasNetwork("tcp")).IsTrue() + assert.Bool(list.HasNetwork("udp")).IsFalse() +} diff --git a/release/config/vpoint_dns_detour.json b/release/config/vpoint_dns_detour.json new file mode 100644 index 000000000..cdf16ec09 --- /dev/null +++ b/release/config/vpoint_dns_detour.json @@ -0,0 +1,41 @@ +{ + "port": 1080, + "log": { + "access": "" + }, + "inbound": { + "protocol": "socks", + "settings": { + "auth": "noauth", + "udp": false, + "ip": "127.0.0.1" + } + }, + "outbound": { + "protocol": "vmess", + "settings": { + "vnext": [ + { + "address": "127.0.0.1", + "port": 27183, + "users": [ + {"id": "ad937d9d-6e23-4a5a-ba23-bce5092a7c51"} + ], + "network": "tcp" + } + ] + } + }, + "inboundDetour": [ + { + "protocol": "dokodemo-door", + "port": 53, + "settings": { + "address": "8.8.8.8", + "port": 53, + "network": "tcp", + "timeout": 0 + } + } + ] +}