move host from net/json to net

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

No files matched your search

-63
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())
}
}
-35
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")
}