simplify stringlist

This commit is contained in:
v2ray
2016-01-15 15:23:12 +01:00
parent a403859105
commit 2d233295e6
14 changed files with 120 additions and 143 deletions
+11 -2
View File
@@ -1,5 +1,14 @@
package dns
type CacheConfig interface {
IsTrustedSource(tag string) bool
import (
"github.com/v2ray/v2ray-core/common/serial"
)
type CacheConfig struct {
TrustedTags map[serial.StringLiteral]bool
}
func (this *CacheConfig) IsTrustedSource(tag serial.StringLiteral) bool {
_, found := this.TrustedTags[tag]
return found
}
+23
View File
@@ -0,0 +1,23 @@
// +build json
package dns
import (
"encoding/json"
"github.com/v2ray/v2ray-core/common/serial"
)
func (this *CacheConfig) UnmarshalJSON(data []byte) error {
var strlist serial.StringLiteralList
if err := json.Unmarshal(data, strlist); err != nil {
return err
}
config := &CacheConfig{
TrustedTags: make(map[serial.StringLiteral]bool, strlist.Len()),
}
for _, str := range strlist {
config.TrustedTags[str.TrimSpace()] = true
}
return nil
}
+3 -3
View File
@@ -34,10 +34,10 @@ func (this *entry) Extend() {
type DnsCache struct {
cache *collect.ValidityMap
config CacheConfig
config *CacheConfig
}
func NewCache(config CacheConfig) *DnsCache {
func NewCache(config *CacheConfig) *DnsCache {
cache := &DnsCache{
cache: collect.NewValidityMap(3600),
config: config,
@@ -47,7 +47,7 @@ func NewCache(config CacheConfig) *DnsCache {
func (this *DnsCache) Add(context app.Context, domain string, ip net.IP) {
callerTag := context.CallerTag()
if !this.config.IsTrustedSource(callerTag) {
if !this.config.IsTrustedSource(serial.StringLiteral(callerTag)) {
return
}
+4 -4
View File
@@ -5,9 +5,9 @@ import (
"testing"
"github.com/v2ray/v2ray-core/app/dns"
dnstesting "github.com/v2ray/v2ray-core/app/dns/testing"
apptesting "github.com/v2ray/v2ray-core/app/testing"
netassert "github.com/v2ray/v2ray-core/common/net/testing/assert"
"github.com/v2ray/v2ray-core/common/serial"
v2testing "github.com/v2ray/v2ray-core/testing"
)
@@ -15,9 +15,9 @@ func TestDnsAdd(t *testing.T) {
v2testing.Current(t)
domain := "v2ray.com"
cache := dns.NewCache(&dnstesting.CacheConfig{
TrustedTags: map[string]bool{
"testtag": true,
cache := dns.NewCache(&dns.CacheConfig{
TrustedTags: map[serial.StringLiteral]bool{
serial.StringLiteral("testtag"): true,
},
})
ip := cache.Get(&apptesting.Context{}, domain)
-35
View File
@@ -1,35 +0,0 @@
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
}
-10
View File
@@ -1,10 +0,0 @@
package testing
type CacheConfig struct {
TrustedTags map[string]bool
}
func (this *CacheConfig) IsTrustedSource(tag string) bool {
_, found := this.TrustedTags[tag]
return found
}
+9 -39
View File
@@ -8,39 +8,9 @@ import (
"strings"
v2net "github.com/v2ray/v2ray-core/common/net"
"github.com/v2ray/v2ray-core/common/serial"
)
type StringList []string
func NewStringList(str ...string) *StringList {
list := StringList(str)
return &list
}
func (this *StringList) UnmarshalJSON(data []byte) error {
var strList []string
err := json.Unmarshal(data, &strList)
if err == nil {
*this = make([]string, len(strList))
copy(*this, strList)
return nil
}
var str string
err = json.Unmarshal(data, &str)
if err == nil {
*this = make([]string, 0, 1)
*this = append(*this, str)
return nil
}
return errors.New("Failed to unmarshal string list: " + string(data))
}
func (this *StringList) Len() int {
return len([]string(*this))
}
type DomainMatcher interface {
Match(domain string) bool
}
@@ -138,10 +108,10 @@ func (this *FieldRule) Apply(dest v2net.Destination) bool {
func (this *FieldRule) UnmarshalJSON(data []byte) error {
type RawFieldRule struct {
Rule
Domain *StringList `json:"domain"`
IP *StringList `json:"ip"`
Port *v2net.PortRange `json:"port"`
Network *v2net.NetworkList `json:"network"`
Domain *serial.StringLiteralList `json:"domain"`
IP *serial.StringLiteralList `json:"ip"`
Port *v2net.PortRange `json:"port"`
Network *v2net.NetworkList `json:"network"`
}
rawFieldRule := RawFieldRule{}
err := json.Unmarshal(data, &rawFieldRule)
@@ -156,14 +126,14 @@ func (this *FieldRule) UnmarshalJSON(data []byte) error {
this.Domain = make([]DomainMatcher, rawFieldRule.Domain.Len())
for idx, rawDomain := range *(rawFieldRule.Domain) {
var matcher DomainMatcher
if strings.HasPrefix(rawDomain, "regexp:") {
rawMatcher, err := NewRegexpDomainMatcher(rawDomain[7:])
if strings.HasPrefix(rawDomain.String(), "regexp:") {
rawMatcher, err := NewRegexpDomainMatcher(rawDomain.String()[7:])
if err != nil {
return err
}
matcher = rawMatcher
} else {
matcher = NewPlainDomainMatcher(rawDomain)
matcher = NewPlainDomainMatcher(rawDomain.String())
}
this.Domain[idx] = matcher
}
@@ -173,7 +143,7 @@ func (this *FieldRule) UnmarshalJSON(data []byte) error {
if rawFieldRule.IP != nil && rawFieldRule.IP.Len() > 0 {
this.IP = make([]*net.IPNet, 0, rawFieldRule.IP.Len())
for _, ipStr := range *(rawFieldRule.IP) {
_, ipNet, err := net.ParseCIDR(ipStr)
_, ipNet, err := net.ParseCIDR(ipStr.String())
if err != nil {
return errors.New("Invalid IP range in router rule: " + err.Error())
}
-21
View File
@@ -1,7 +1,6 @@
package json
import (
"encoding/json"
"testing"
v2net "github.com/v2ray/v2ray-core/common/net"
@@ -9,26 +8,6 @@ import (
"github.com/v2ray/v2ray-core/testing/assert"
)
func TestStringListParsingList(t *testing.T) {
v2testing.Current(t)
rawJson := `["a", "b", "c", "d"]`
var strList StringList
err := json.Unmarshal([]byte(rawJson), &strList)
assert.Error(err).IsNil()
assert.Int(strList.Len()).Equals(4)
}
func TestStringListParsingString(t *testing.T) {
v2testing.Current(t)
rawJson := `"abcd"`
var strList StringList
err := json.Unmarshal([]byte(rawJson), &strList)
assert.Error(err).IsNil()
assert.Int(strList.Len()).Equals(1)
}
func TestDomainMatching(t *testing.T) {
v2testing.Current(t)