mirror of https://github.com/v2ray/v2ray-core
simplify stringlist
parent
a403859105
commit
2d233295e6
|
@ -1,5 +1,14 @@
|
||||||
package dns
|
package dns
|
||||||
|
|
||||||
type CacheConfig interface {
|
import (
|
||||||
IsTrustedSource(tag string) bool
|
"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
|
||||||
}
|
}
|
||||||
|
|
|
@ -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
|
||||||
|
}
|
|
@ -34,10 +34,10 @@ func (this *entry) Extend() {
|
||||||
|
|
||||||
type DnsCache struct {
|
type DnsCache struct {
|
||||||
cache *collect.ValidityMap
|
cache *collect.ValidityMap
|
||||||
config CacheConfig
|
config *CacheConfig
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewCache(config CacheConfig) *DnsCache {
|
func NewCache(config *CacheConfig) *DnsCache {
|
||||||
cache := &DnsCache{
|
cache := &DnsCache{
|
||||||
cache: collect.NewValidityMap(3600),
|
cache: collect.NewValidityMap(3600),
|
||||||
config: config,
|
config: config,
|
||||||
|
@ -47,7 +47,7 @@ func NewCache(config CacheConfig) *DnsCache {
|
||||||
|
|
||||||
func (this *DnsCache) Add(context app.Context, domain string, ip net.IP) {
|
func (this *DnsCache) Add(context app.Context, domain string, ip net.IP) {
|
||||||
callerTag := context.CallerTag()
|
callerTag := context.CallerTag()
|
||||||
if !this.config.IsTrustedSource(callerTag) {
|
if !this.config.IsTrustedSource(serial.StringLiteral(callerTag)) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -5,9 +5,9 @@ import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/v2ray/v2ray-core/app/dns"
|
"github.com/v2ray/v2ray-core/app/dns"
|
||||||
dnstesting "github.com/v2ray/v2ray-core/app/dns/testing"
|
|
||||||
apptesting "github.com/v2ray/v2ray-core/app/testing"
|
apptesting "github.com/v2ray/v2ray-core/app/testing"
|
||||||
netassert "github.com/v2ray/v2ray-core/common/net/testing/assert"
|
netassert "github.com/v2ray/v2ray-core/common/net/testing/assert"
|
||||||
|
"github.com/v2ray/v2ray-core/common/serial"
|
||||||
v2testing "github.com/v2ray/v2ray-core/testing"
|
v2testing "github.com/v2ray/v2ray-core/testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -15,9 +15,9 @@ func TestDnsAdd(t *testing.T) {
|
||||||
v2testing.Current(t)
|
v2testing.Current(t)
|
||||||
|
|
||||||
domain := "v2ray.com"
|
domain := "v2ray.com"
|
||||||
cache := dns.NewCache(&dnstesting.CacheConfig{
|
cache := dns.NewCache(&dns.CacheConfig{
|
||||||
TrustedTags: map[string]bool{
|
TrustedTags: map[serial.StringLiteral]bool{
|
||||||
"testtag": true,
|
serial.StringLiteral("testtag"): true,
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
ip := cache.Get(&apptesting.Context{}, domain)
|
ip := cache.Get(&apptesting.Context{}, domain)
|
||||||
|
|
|
@ -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
|
|
||||||
}
|
|
|
@ -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
|
|
||||||
}
|
|
|
@ -8,39 +8,9 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
v2net "github.com/v2ray/v2ray-core/common/net"
|
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 {
|
type DomainMatcher interface {
|
||||||
Match(domain string) bool
|
Match(domain string) bool
|
||||||
}
|
}
|
||||||
|
@ -138,8 +108,8 @@ func (this *FieldRule) Apply(dest v2net.Destination) bool {
|
||||||
func (this *FieldRule) UnmarshalJSON(data []byte) error {
|
func (this *FieldRule) UnmarshalJSON(data []byte) error {
|
||||||
type RawFieldRule struct {
|
type RawFieldRule struct {
|
||||||
Rule
|
Rule
|
||||||
Domain *StringList `json:"domain"`
|
Domain *serial.StringLiteralList `json:"domain"`
|
||||||
IP *StringList `json:"ip"`
|
IP *serial.StringLiteralList `json:"ip"`
|
||||||
Port *v2net.PortRange `json:"port"`
|
Port *v2net.PortRange `json:"port"`
|
||||||
Network *v2net.NetworkList `json:"network"`
|
Network *v2net.NetworkList `json:"network"`
|
||||||
}
|
}
|
||||||
|
@ -156,14 +126,14 @@ func (this *FieldRule) UnmarshalJSON(data []byte) error {
|
||||||
this.Domain = make([]DomainMatcher, rawFieldRule.Domain.Len())
|
this.Domain = make([]DomainMatcher, rawFieldRule.Domain.Len())
|
||||||
for idx, rawDomain := range *(rawFieldRule.Domain) {
|
for idx, rawDomain := range *(rawFieldRule.Domain) {
|
||||||
var matcher DomainMatcher
|
var matcher DomainMatcher
|
||||||
if strings.HasPrefix(rawDomain, "regexp:") {
|
if strings.HasPrefix(rawDomain.String(), "regexp:") {
|
||||||
rawMatcher, err := NewRegexpDomainMatcher(rawDomain[7:])
|
rawMatcher, err := NewRegexpDomainMatcher(rawDomain.String()[7:])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
matcher = rawMatcher
|
matcher = rawMatcher
|
||||||
} else {
|
} else {
|
||||||
matcher = NewPlainDomainMatcher(rawDomain)
|
matcher = NewPlainDomainMatcher(rawDomain.String())
|
||||||
}
|
}
|
||||||
this.Domain[idx] = matcher
|
this.Domain[idx] = matcher
|
||||||
}
|
}
|
||||||
|
@ -173,7 +143,7 @@ func (this *FieldRule) UnmarshalJSON(data []byte) error {
|
||||||
if rawFieldRule.IP != nil && rawFieldRule.IP.Len() > 0 {
|
if rawFieldRule.IP != nil && rawFieldRule.IP.Len() > 0 {
|
||||||
this.IP = make([]*net.IPNet, 0, rawFieldRule.IP.Len())
|
this.IP = make([]*net.IPNet, 0, rawFieldRule.IP.Len())
|
||||||
for _, ipStr := range *(rawFieldRule.IP) {
|
for _, ipStr := range *(rawFieldRule.IP) {
|
||||||
_, ipNet, err := net.ParseCIDR(ipStr)
|
_, ipNet, err := net.ParseCIDR(ipStr.String())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.New("Invalid IP range in router rule: " + err.Error())
|
return errors.New("Invalid IP range in router rule: " + err.Error())
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
package json
|
package json
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
v2net "github.com/v2ray/v2ray-core/common/net"
|
v2net "github.com/v2ray/v2ray-core/common/net"
|
||||||
|
@ -9,26 +8,6 @@ import (
|
||||||
"github.com/v2ray/v2ray-core/testing/assert"
|
"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) {
|
func TestDomainMatching(t *testing.T) {
|
||||||
v2testing.Current(t)
|
v2testing.Current(t)
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
package net
|
package net
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"strings"
|
"github.com/v2ray/v2ray-core/common/serial"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@ -9,14 +9,14 @@ const (
|
||||||
UDPNetwork = Network("udp")
|
UDPNetwork = Network("udp")
|
||||||
)
|
)
|
||||||
|
|
||||||
type Network string
|
type Network serial.StringLiteral
|
||||||
|
|
||||||
type NetworkList []Network
|
type NetworkList []Network
|
||||||
|
|
||||||
func NewNetworkList(networks []string) NetworkList {
|
func NewNetworkList(networks serial.StringLiteralList) NetworkList {
|
||||||
list := NetworkList(make([]Network, len(networks)))
|
list := NetworkList(make([]Network, networks.Len()))
|
||||||
for idx, network := range networks {
|
for idx, network := range networks {
|
||||||
list[idx] = Network(strings.ToLower(strings.TrimSpace(network)))
|
list[idx] = Network(network.TrimSpace().ToLower())
|
||||||
}
|
}
|
||||||
return list
|
return list
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,12 +3,14 @@
|
||||||
package net
|
package net
|
||||||
|
|
||||||
import (
|
import (
|
||||||
serialjson "github.com/v2ray/v2ray-core/common/serial/json"
|
"encoding/json"
|
||||||
|
|
||||||
|
"github.com/v2ray/v2ray-core/common/serial"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (this *NetworkList) UnmarshalJSON(data []byte) error {
|
func (this *NetworkList) UnmarshalJSON(data []byte) error {
|
||||||
strlist, err := serialjson.UnmarshalStringList(data)
|
var strlist serial.StringLiteralList
|
||||||
if err != nil {
|
if err := json.Unmarshal(data, &strlist); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
*this = NewNetworkList(strlist)
|
*this = NewNetworkList(strlist)
|
||||||
|
|
|
@ -1,21 +0,0 @@
|
||||||
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))
|
|
||||||
}
|
|
|
@ -1,5 +1,9 @@
|
||||||
package serial
|
package serial
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
// An interface for any objects that has string presentation.
|
// An interface for any objects that has string presentation.
|
||||||
type String interface {
|
type String interface {
|
||||||
String() string
|
String() string
|
||||||
|
@ -7,6 +11,22 @@ type String interface {
|
||||||
|
|
||||||
type StringLiteral string
|
type StringLiteral string
|
||||||
|
|
||||||
|
func NewStringLiteral(str String) StringLiteral {
|
||||||
|
return StringLiteral(str.String())
|
||||||
|
}
|
||||||
|
|
||||||
func (this StringLiteral) String() string {
|
func (this StringLiteral) String() string {
|
||||||
return string(this)
|
return string(this)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (this StringLiteral) ToLower() StringLiteral {
|
||||||
|
return StringLiteral(strings.ToLower(string(this)))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this StringLiteral) ToUpper() StringLiteral {
|
||||||
|
return StringLiteral(strings.ToUpper(string(this)))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this StringLiteral) TrimSpace() StringLiteral {
|
||||||
|
return StringLiteral(strings.TrimSpace(string(this)))
|
||||||
|
}
|
||||||
|
|
|
@ -0,0 +1,15 @@
|
||||||
|
package serial
|
||||||
|
|
||||||
|
type StringLiteralList []StringLiteral
|
||||||
|
|
||||||
|
func NewStringLiteralList(raw []string) *StringLiteralList {
|
||||||
|
list := StringLiteralList(make([]StringLiteral, len(raw)))
|
||||||
|
for idx, str := range raw {
|
||||||
|
list[idx] = StringLiteral(str)
|
||||||
|
}
|
||||||
|
return &list
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *StringLiteralList) Len() int {
|
||||||
|
return len(*this)
|
||||||
|
}
|
|
@ -0,0 +1,25 @@
|
||||||
|
// +build json
|
||||||
|
|
||||||
|
package serial
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (this *StringLiteralList) UnmarshalJSON(data []byte) error {
|
||||||
|
var strarray []string
|
||||||
|
if err := json.Unmarshal(data, &strarray); err == nil {
|
||||||
|
*this = *NewStringLiteralList(strarray)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var rawstr string
|
||||||
|
if err := json.Unmarshal(data, &rawstr); err == nil {
|
||||||
|
strlist := strings.Split(rawstr, ",")
|
||||||
|
*this = *NewStringLiteralList(strlist)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return errors.New("Unknown format of a string list: " + string(data))
|
||||||
|
}
|
Loading…
Reference in New Issue