forked from v2fly/v2ray-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
host.go
53 lines (44 loc) · 727 Bytes
/
host.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package json
import (
"encoding/json"
"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
}