Jelajahi Sumber

allow dns modification only from trusted tags

Darien Raymond 10 tahun lalu
induk
melakukan
a540d7dc99
3 mengubah file dengan 26 tambahan dan 2 penghapusan
  1. 5 0
      app/dns/dns.go
  2. 11 2
      app/dns/dns_test.go
  3. 10 0
      app/dns/testing/config.go

+ 5 - 0
app/dns/dns.go

@@ -68,6 +68,11 @@ func (this *DnsCache) cleanup() {
 }
 
 func (this *DnsCache) Add(context app.Context, domain string, ip net.IP) {
+	callerTag := context.CallerTag()
+	if !this.config.IsTrustedSource(callerTag) {
+		return
+	}
+
 	this.RLock()
 	entry, found := this.cache[domain]
 	this.RUnlock()

+ 11 - 2
app/dns/dns_test.go

@@ -5,6 +5,7 @@ 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"
 	v2testing "github.com/v2ray/v2ray-core/testing"
@@ -14,11 +15,19 @@ func TestDnsAdd(t *testing.T) {
 	v2testing.Current(t)
 
 	domain := "v2ray.com"
-	cache := dns.NewCache(nil)
+	cache := dns.NewCache(&dnstesting.CacheConfig{
+		TrustedTags: map[string]bool{
+			"testtag": true,
+		},
+	})
 	ip := cache.Get(&apptesting.Context{}, domain)
 	netassert.IP(ip).IsNil()
 
-	cache.Add(&apptesting.Context{}, domain, []byte{1, 2, 3, 4})
+	cache.Add(&apptesting.Context{CallerTagValue: "notvalidtag"}, domain, []byte{1, 2, 3, 4})
+	ip = cache.Get(&apptesting.Context{}, domain)
+	netassert.IP(ip).IsNil()
+
+	cache.Add(&apptesting.Context{CallerTagValue: "testtag"}, domain, []byte{1, 2, 3, 4})
 	ip = cache.Get(&apptesting.Context{}, domain)
 	netassert.IP(ip).Equals(net.IP([]byte{1, 2, 3, 4}))
 }

+ 10 - 0
app/dns/testing/config.go

@@ -0,0 +1,10 @@
+package testing
+
+type CacheConfig struct {
+	TrustedTags map[string]bool
+}
+
+func (this *CacheConfig) IsTrustedSource(tag string) bool {
+	_, found := this.TrustedTags[tag]
+	return found
+}