From 5ceac7a6e25960697fc04c921f61d824d523a2fb Mon Sep 17 00:00:00 2001 From: v2ray Date: Fri, 15 Jan 2016 13:17:04 +0100 Subject: [PATCH] move host from net/json to net --- common/net/address_json.go | 26 +++++++++++ common/net/address_json_test.go | 37 ++++++++++++++++ common/net/json/host.go | 63 --------------------------- common/net/json/host_test.go | 35 --------------- proxy/dokodemo/config_json.go | 4 +- proxy/socks/config_json.go | 11 +++-- proxy/vmess/outbound/receiver_json.go | 9 ++-- 7 files changed, 74 insertions(+), 111 deletions(-) create mode 100644 common/net/address_json.go create mode 100644 common/net/address_json_test.go delete mode 100644 common/net/json/host.go delete mode 100644 common/net/json/host_test.go diff --git a/common/net/address_json.go b/common/net/address_json.go new file mode 100644 index 000000000..60ba8e214 --- /dev/null +++ b/common/net/address_json.go @@ -0,0 +1,26 @@ +// +build json + +package net + +import ( + "encoding/json" + "net" +) + +type AddressJson struct { + Address Address +} + +func (this *AddressJson) UnmarshalJSON(data []byte) error { + var rawStr string + if err := json.Unmarshal(data, &rawStr); err != nil { + return err + } + ip := net.ParseIP(rawStr) + if ip != nil { + this.Address = IPAddress(ip) + } else { + this.Address = DomainAddress(rawStr) + } + return nil +} diff --git a/common/net/address_json_test.go b/common/net/address_json_test.go new file mode 100644 index 000000000..a3234ea50 --- /dev/null +++ b/common/net/address_json_test.go @@ -0,0 +1,37 @@ +// +build json + +package net_test + +import ( + "encoding/json" + "net" + "testing" + + . "github.com/v2ray/v2ray-core/common/net" + v2testing "github.com/v2ray/v2ray-core/testing" + "github.com/v2ray/v2ray-core/testing/assert" +) + +func TestIPParsing(t *testing.T) { + v2testing.Current(t) + + rawJson := "\"8.8.8.8\"" + var address AddressJson + err := json.Unmarshal([]byte(rawJson), &address) + assert.Error(err).IsNil() + assert.Bool(address.Address.IsIPv4()).IsTrue() + assert.Bool(address.Address.IsDomain()).IsFalse() + assert.Bool(address.Address.IP().Equal(net.ParseIP("8.8.8.8"))).IsTrue() +} + +func TestDomainParsing(t *testing.T) { + v2testing.Current(t) + + rawJson := "\"v2ray.com\"" + var address AddressJson + err := json.Unmarshal([]byte(rawJson), &address) + assert.Error(err).IsNil() + assert.Bool(address.Address.IsIPv4()).IsFalse() + assert.Bool(address.Address.IsDomain()).IsTrue() + assert.StringLiteral(address.Address.Domain()).Equals("v2ray.com") +} diff --git a/common/net/json/host.go b/common/net/json/host.go deleted file mode 100644 index 80088f58b..000000000 --- a/common/net/json/host.go +++ /dev/null @@ -1,63 +0,0 @@ -package json - -import ( - "encoding/json" - "net" - - v2net "github.com/v2ray/v2ray-core/common/net" -) - -type Host struct { - domain string - ip net.IP -} - -func NewIPHost(ip net.IP) *Host { - return &Host{ - ip: ip, - } -} - -func NewDomainHost(domain string) *Host { - return &Host{ - domain: domain, - } -} - -func (this *Host) UnmarshalJSON(data []byte) error { - var rawStr string - if err := json.Unmarshal(data, &rawStr); err != nil { - return err - } - ip := net.ParseIP(rawStr) - if ip != nil { - this.ip = ip - } else { - this.domain = rawStr - } - return nil -} - -func (this *Host) IsIP() bool { - return this.ip != nil -} - -func (this *Host) IsDomain() bool { - return !this.IsIP() -} - -func (this *Host) IP() net.IP { - return this.ip -} - -func (this *Host) Domain() string { - return this.domain -} - -func (this *Host) Address() v2net.Address { - if this.IsIP() { - return v2net.IPAddress(this.IP()) - } else { - return v2net.DomainAddress(this.Domain()) - } -} diff --git a/common/net/json/host_test.go b/common/net/json/host_test.go deleted file mode 100644 index 6eee6633f..000000000 --- a/common/net/json/host_test.go +++ /dev/null @@ -1,35 +0,0 @@ -package json_test - -import ( - "encoding/json" - "net" - "testing" - - . "github.com/v2ray/v2ray-core/common/net/json" - v2testing "github.com/v2ray/v2ray-core/testing" - "github.com/v2ray/v2ray-core/testing/assert" -) - -func TestIPParsing(t *testing.T) { - v2testing.Current(t) - - rawJson := "\"8.8.8.8\"" - host := &Host{} - err := json.Unmarshal([]byte(rawJson), host) - assert.Error(err).IsNil() - assert.Bool(host.IsIP()).IsTrue() - assert.Bool(host.IsDomain()).IsFalse() - assert.Bool(host.IP().Equal(net.ParseIP("8.8.8.8"))).IsTrue() -} - -func TestDomainParsing(t *testing.T) { - v2testing.Current(t) - - rawJson := "\"v2ray.com\"" - host := &Host{} - err := json.Unmarshal([]byte(rawJson), host) - assert.Error(err).IsNil() - assert.Bool(host.IsIP()).IsFalse() - assert.Bool(host.IsDomain()).IsTrue() - assert.StringLiteral(host.Domain()).Equals("v2ray.com") -} diff --git a/proxy/dokodemo/config_json.go b/proxy/dokodemo/config_json.go index 9c2d868d4..d5f6ada43 100644 --- a/proxy/dokodemo/config_json.go +++ b/proxy/dokodemo/config_json.go @@ -14,7 +14,7 @@ func init() { config.RegisterInboundConnectionConfig("dokodemo-door", func(data []byte) (interface{}, error) { type DokodemoConfig struct { - Host *v2netjson.Host `json:"address"` + Host *v2net.AddressJson `json:"address"` PortValue v2net.Port `json:"port"` NetworkList *v2netjson.NetworkList `json:"network"` TimeoutValue int `json:"timeout"` @@ -24,7 +24,7 @@ func init() { return nil, err } return &Config{ - Address: rawConfig.Host.Address(), + Address: rawConfig.Host.Address, Port: rawConfig.PortValue, Network: rawConfig.NetworkList, Timeout: rawConfig.TimeoutValue, diff --git a/proxy/socks/config_json.go b/proxy/socks/config_json.go index be23b5c97..a3a47e44b 100644 --- a/proxy/socks/config_json.go +++ b/proxy/socks/config_json.go @@ -7,7 +7,6 @@ import ( "github.com/v2ray/v2ray-core/common/log" v2net "github.com/v2ray/v2ray-core/common/net" - v2netjson "github.com/v2ray/v2ray-core/common/net/json" "github.com/v2ray/v2ray-core/proxy/internal" "github.com/v2ray/v2ray-core/proxy/internal/config" ) @@ -26,10 +25,10 @@ func init() { } type SocksConfig struct { - AuthMethod string `json:"auth"` - Accounts []*SocksAccount `json:"accounts"` - UDP bool `json:"udp"` - Host *v2netjson.Host `json:"ip"` + AuthMethod string `json:"auth"` + Accounts []*SocksAccount `json:"accounts"` + UDP bool `json:"udp"` + Host *v2net.AddressJson `json:"ip"` } rawConfig := new(SocksConfig) @@ -55,7 +54,7 @@ func init() { socksConfig.UDPEnabled = rawConfig.UDP if rawConfig.Host != nil { - socksConfig.Address = rawConfig.Host.Address() + socksConfig.Address = rawConfig.Host.Address } else { socksConfig.Address = v2net.IPAddress([]byte{127, 0, 0, 1}) } diff --git a/proxy/vmess/outbound/receiver_json.go b/proxy/vmess/outbound/receiver_json.go index 3a9ed58b2..a22861f7e 100644 --- a/proxy/vmess/outbound/receiver_json.go +++ b/proxy/vmess/outbound/receiver_json.go @@ -7,16 +7,15 @@ import ( "github.com/v2ray/v2ray-core/common/log" v2net "github.com/v2ray/v2ray-core/common/net" - v2netjson "github.com/v2ray/v2ray-core/common/net/json" "github.com/v2ray/v2ray-core/proxy/internal" "github.com/v2ray/v2ray-core/proxy/vmess" ) func (this *Receiver) UnmarshalJSON(data []byte) error { type RawConfigTarget struct { - Address *v2netjson.Host `json:"address"` - Port v2net.Port `json:"port"` - Users []*vmess.User `json:"users"` + Address *v2net.AddressJson `json:"address"` + Port v2net.Port `json:"port"` + Users []*vmess.User `json:"users"` } var rawConfig RawConfigTarget if err := json.Unmarshal(data, &rawConfig); err != nil { @@ -31,6 +30,6 @@ func (this *Receiver) UnmarshalJSON(data []byte) error { log.Error("VMess: Address is not set in VMess outbound config.") return internal.ErrorBadConfiguration } - this.Destination = v2net.TCPDestination(rawConfig.Address.Address(), rawConfig.Port) + this.Destination = v2net.TCPDestination(rawConfig.Address.Address, rawConfig.Port) return nil }