1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-21 09:36:34 -05:00

move host from net/json to net

This commit is contained in:
v2ray 2016-01-15 13:17:04 +01:00
parent 1f1f79b90a
commit 5ceac7a6e2
7 changed files with 74 additions and 111 deletions

View File

@ -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
}

View File

@ -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")
}

View File

@ -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())
}
}

View File

@ -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")
}

View File

@ -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,

View File

@ -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})
}

View File

@ -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
}