mirror of https://github.com/v2ray/v2ray-core
dns config
parent
95c8910f87
commit
5b3e84ede6
|
@ -1,5 +1,5 @@
|
|||
package dns
|
||||
|
||||
type CacheConfig interface {
|
||||
TrustedSource() []string
|
||||
IsTrustedSource(tag string) bool
|
||||
}
|
||||
|
|
|
@ -31,10 +31,11 @@ func (this *entry) Extend() {
|
|||
|
||||
type DnsCache struct {
|
||||
sync.RWMutex
|
||||
cache map[string]*entry
|
||||
cache map[string]*entry
|
||||
config CacheConfig
|
||||
}
|
||||
|
||||
func NewCache() *DnsCache {
|
||||
func NewCache(config CacheConfig) *DnsCache {
|
||||
cache := &DnsCache{
|
||||
cache: make(map[string]*entry),
|
||||
}
|
||||
|
|
|
@ -13,7 +13,7 @@ func TestDnsAdd(t *testing.T) {
|
|||
v2testing.Current(t)
|
||||
|
||||
domain := "v2ray.com"
|
||||
cache := dns.NewCache()
|
||||
cache := dns.NewCache(nil)
|
||||
ip := cache.Get(domain)
|
||||
netassert.IP(ip).IsNil()
|
||||
|
||||
|
|
|
@ -0,0 +1,35 @@
|
|||
package json
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
serialjson "github.com/v2ray/v2ray-core/common/serial/json"
|
||||
)
|
||||
|
||||
type TagList map[string]bool
|
||||
|
||||
func NewTagList(tags []string) TagList {
|
||||
list := TagList(make(map[string]bool))
|
||||
for _, tag := range tags {
|
||||
list[strings.TrimSpace(tag)] = true
|
||||
}
|
||||
return list
|
||||
}
|
||||
|
||||
func (this *TagList) UnmarshalJSON(data []byte) error {
|
||||
tags, err := serialjson.UnmarshalStringList(data)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
*this = NewTagList(tags)
|
||||
return nil
|
||||
}
|
||||
|
||||
type CacheConfig struct {
|
||||
TrustedTags TagList `json:"trustedTags"`
|
||||
}
|
||||
|
||||
func (this *CacheConfig) IsTrustedSource(tag string) bool {
|
||||
_, found := this.TrustedTags[tag]
|
||||
return found
|
||||
}
|
|
@ -1,11 +1,10 @@
|
|||
package json
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"strings"
|
||||
|
||||
v2net "github.com/v2ray/v2ray-core/common/net"
|
||||
serialjson "github.com/v2ray/v2ray-core/common/serial/json"
|
||||
)
|
||||
|
||||
type NetworkList []string
|
||||
|
@ -19,21 +18,12 @@ func NewNetworkList(networks []string) NetworkList {
|
|||
}
|
||||
|
||||
func (this *NetworkList) UnmarshalJSON(data []byte) error {
|
||||
var strList []string
|
||||
err := json.Unmarshal(data, &strList)
|
||||
if err == nil {
|
||||
*this = NewNetworkList(strList)
|
||||
return nil
|
||||
strlist, err := serialjson.UnmarshalStringList(data)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var str string
|
||||
err = json.Unmarshal(data, &str)
|
||||
if err == nil {
|
||||
strList := strings.Split(str, ",")
|
||||
*this = NewNetworkList(strList)
|
||||
return nil
|
||||
}
|
||||
return errors.New("Unknown format of network list: " + string(data))
|
||||
*this = NewNetworkList(strlist)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (this *NetworkList) HasNetwork(network v2net.Network) bool {
|
||||
|
|
|
@ -0,0 +1,21 @@
|
|||
package json
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func UnmarshalStringList(data []byte) ([]string, error) {
|
||||
var strarray []string
|
||||
if err := json.Unmarshal(data, &strarray); err == nil {
|
||||
return strarray, nil
|
||||
}
|
||||
|
||||
var rawstr string
|
||||
if err := json.Unmarshal(data, &rawstr); err == nil {
|
||||
strlist := strings.Split(rawstr, ",")
|
||||
return strlist, nil
|
||||
}
|
||||
return nil, errors.New("Unknown format of a string list: " + string(data))
|
||||
}
|
Loading…
Reference in New Issue